使用SSD网络模型进行Tensorflow物体检测?(V1.0图像检测)
日期: 2020-02-24 分类: 个人收藏 373次阅读
使用SSD网络模型进行Tensorflow物体检测?(V1.0图像检测)
文章目录
1.模型的加载和utils库环境的配置?
1.准备好ssd_mobilenet_v1网络模型的frozen_inference_graph.pb和mscoco_label_map.pbtxt文件。(文中所涉及文件和代码均已上传,链接提取码:mztu)
2.配置好utils库文件以及pycache环境,将准备好的label_map_util.py和visualization_utils.py文件以及对应的label_map_util.cpython-36.pyc和visualization_utils.cpython-36.pyc复制粘贴到运行环境目录下。
2.模型的导入和使用?
1.import 使用的相关的库
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
关于库的安装和卸载更新问题?
提升下载速度的方法:
指定源:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio(指定的库)
指定版本的方法:
指定版本:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio==1.0.2(指定的源+库版本)
更新库时,卸载原来的库失败:
忽略installed的库:pip install --ignore-installed -i https://pypi.tuna.tsinghua.edu.cn/simple imageio(指定的库)
2.使用import_graph_def()和load_labelmap()对模型文件的导入
PATH_TO_CKPT = "E:/DeepLearn/github项目/PART1/11 TensorFlow物体检测/ssd_mobilenet_v1_coco_2017_11_17/" \
"frozen_inference_graph.pb"
PATH_TO_LABELS = "E:/DeepLearn/github项目/PART1/11 TensorFlow物体检测/ssd_mobilenet_v1_coco_2017_11_17/" \
"mscoco_label_map.pbtxt"
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
od_graph_def.ParseFromString(fid.read())
tf.import_graph_def(od_graph_def, name='')#这个地方容易报错,name不要填写名称,多调试几次即可运行。
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
3.对label_map进行转换得到category_index分类索引文件
NUM_CLASSES = 90#这里使用的ssd网络共90种分类
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
4.自定义将image图片转换为numpy数组的函数
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)#将图片的格式重组
5.定义测试图片路径,以及使用tf.Graph()默认图进行测试
TEST_IMAGE_PATHS = ['E:/DeepLearn/github项目/PART1/11 TensorFlow物体检测/test_images/image1.jpg']
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')#输入层,0是序号
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')#检测的候选区域层
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')#测试得分层
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')#分类类别
num_detections = detection_graph.get_tensor_by_name('num_detections:0')#总共的识别的类别总数
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)#将图片转换为numpy数组
image_np_expanded = np.expand_dims(image_np, axis=0)#将图片的三维数组变成四维,第一个维度是序号
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})#喂数据得到相应的结果(boxes, scores, classes, num)
6.在默认图tf.Graph()中添加测试图片的可视化效果
vis_util.visualize_boxes_and_labels_on_image_array(image_np, np.squeeze(boxes),
np.squeeze(classes).astype(np.int32), np.squeeze(scores),
category_index, use_normalized_coordinates=True,
line_thickness=8)#将候选区域和标签打印在图片上
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
cv2.imshow('object detection', image_np)
cv2.imwrite("E:\DeepLearn\dogggy.jpg", image_np)
cv2.waitKey(0)
需要注意的是,这一部分代码包含在for循环里,不要放在with detection_graph.as_default():的外面!
3.模型的测试与评估?
人:
狗:
对于距离适中的物体类别检测还是具有较高的识别精度,但是模型的细腻度还是有欠缺的,也存在错误识别,未识别的问题。SSD网络模型作为一个可以动态识别物体类别的模型,识别图片的内容类别整体效果还是不错的,SSD_V1模型也仅仅只有20多MB,可以供初学者上手学习作为计算机视觉入门的案例。
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
下一篇: HTML_first_复习
精华推荐