使用anaconda3, 先安装
换源,若不然会非常慢 方式参考 https://www.cnblogs.com/Dean0731/p/11560371.html
export PATH="/usr/local/anaconda3/bin:$PATH"
新建虚拟环境 本例python=3.5
进入环境本例安装的tensorflow=1.14.0
安装其他依赖包,pillow,lxml 等,亦可以等待报错时安装相应模块
本例环境 windwos:python3.6+tensorflow-1.12
windwos:python3.6+tensorflow-gpu-1.12+cuda8.0
# 注意选择cuda版本 有最小计算能力 ,需要小于你显卡的计算能力,若不匹配的话 可能会忽略显卡的加速作用,而且很卡
# 本例使用GT820m 2.1计算能力 cuda9.0 计算能力最小为3.7
# 显卡计算能力查询 https://developer.nvidia.com/cuda-gpus
# tensorflow---》cuda--》cudnn https://tensorflow.google.cn/install/source
# python----决定tensorflow的版本
# 先根据显卡选cuda
linux:python3.5+tensorflow-1.14
注:train 训练集,trainval 训练集中的测试集,val 测试集
# 生成objection——detection/protos 下的py文件 models/research 目录下
linux protoc object_detection/protos/*.proto --python_out=.
windows Get-ChildItem object_detection/protos/*.proto | Resolve-Path -Relative | %{ protoc $_ --python_out=. }
# 运行完成后,可以检查object_detection/protos/文件夹,如果每个proto文件都成了对应的以py为后缀的python源码,就说明编译成功了
# 添加环境
# 需要当前虚拟环境中进入在这2目录中(research,research/slim)python setup.py install 把object_detection等模块装入
# 若还出现错误变量(报当前线程错误等)可试试添加环境变量:
# export PYTHONPATH=$PYTHONPATH:/home/dzf/models/research:/home/dzf/models/research/slim
# PYTHONPATH:上边两个目录
python export_inference_graph.py
--input_type=image_tensor
--pipeline_config_path=/...../models/faster_rcnn_inception_resnet_v2_atrous_coco.config
--trained_checkpoint_prefix=/...../train/model.ckpt-10
--output_directory=/..../train
注:上编的路径尽量使用绝对路径,不要使用相对路径和~符号 可能报错 生成frozen_inference_graph.pb文件 及其他文件
import cv2
import os
import pathlib
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1
# Patch the location of gfile
tf.gfile = tf.io.gfile
def load_model(model_name):
# here
# base_url = 'http://download.tensorflow.org/models/object_detection/'
# model_file = model_name + '.tar.gz'
# model_dir = tf.keras.utils.get_file(
# fname=model_name,
# origin=base_url + model_file,
# untar=True)
#model_dir = pathlib.Path("ssd_mobilenet_v1_coco_2017_11_17")/"saved_model"
#model_dir = pathlib.Path("legacy/pb/pb56339")/"saved_model"
model_dir = pathlib.Path("pb/pb1218")/"saved_model"
model = tf.saved_model.load(str(model_dir))
model = model.signatures['serving_default']
return model
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'data/pascal_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('test_images2')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS
model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)
def run_inference_for_single_image(model, image):
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(output_dict.pop('num_detections'))
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
# Handle models with masks:
if 'detection_masks' in output_dict:
# Reframe the the bbox mask to the image size.
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
output_dict['detection_masks'], output_dict['detection_boxes'],
image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
print(output_dict['detection_boxes'])
print(output_dict['detection_classes'])
print(output_dict['detection_scores'])
return output_dict
def show_inference(model, image_path):
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = np.array(Image.open(image_path))
# Actual detection.
output_dict = run_inference_for_single_image(model, image_np)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
#display(Image.fromarray(image_np))
cv2.namedWindow("detection", cv2.WINDOW_NORMAL)
cv2.imshow("detection", image_np)
cv2.waitKey(0)
for image_path in TEST_IMAGE_PATHS:
show_inference(detection_model, image_path)
# 本例中在原始模型训练的基础上的训练一定次数 生成model.ckpt 之后转为pb文件 进行目标检测 没有检测框
# 若使用原始模型的pb文件 faster_rcnn_inception_resnet_v2_atrous_coco_2018_1_29/frozen_interence_inception.pb 可以显示检测框,至于什么原因还没有找到
# 对于上面所述的现象,我重新搭建了一次环境,上面的train,export 等过程,我都是将py文件复制都单独文件夹 进行操作,
# 本次搜有的操作都位于models中,将数据放入object_detection中等,可解决上述问题。但还是会出现某些图片不能检测的问题,也可能是由于训练测试过少的原因。
# 使用model_main.py 预测时可能效果较好