【天天向上】OpenVINO学习笔记(四)目标检测模型
【模型下载】首先,我们需要下载模型文件。进入 models , 选择下载 ssd_mobilenet_v2_coco 模型文件。【tensorflow】
一般我们恢复模型都是要用到tensorflow,大致代码如下所示:
import numpy as np
import tensorflow as tf
import cv2 as cv
# Read the graph.
with tf.gfile.FastGFile('frozen_inference_graph.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Session() as sess:
# Restore session
sess.graph.as_default()
tf.import_graph_def(graph_def, name='')
# Read and preprocess an image.
img = cv.imread('example.jpg')
rows = img.shape
cols = img.shape
inp = cv.resize(img, (300, 300))
inp = inp[:, :, ]# BGR2RGB
# Run the model
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
sess.graph.get_tensor_by_name('detection_scores:0'),
sess.graph.get_tensor_by_name('detection_boxes:0'),
sess.graph.get_tensor_by_name('detection_classes:0')],
feed_dict={'image_tensor:0': inp.reshape(1, inp.shape, inp.shape, 3)})
# Visualize detected bounding boxes.
num_detections = int(out)
for i in range(num_detections):
classId = int(out)
score = float(out)
bbox = ]
if score > 0.3:
x = bbox * cols
y = bbox * rows
right = bbox * cols
bottom = bbox * rows
cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
cv.imshow('TensorFlow MobileNet-SSD', img)
cv.waitKey()
【opencv】
如果电脑没有tensorflow想要恢复模型怎么办?那就可以考虑下opencv了,opencv加载tensorflow模型需要pb文件和pbtxt文件,pbtxt是可以根据pb文件生成的,在opencv的源代码中进入sample/dnn 文件夹中,由于我们的模型是ssd因此找到以下两个文件
tf_text_graph_ssd.py
tf_text_graph_common.py
在https://github.com/opencv/opencv,中的 opencv-master\samples\dnn中找到
在命令行执行
python tf_text_graph_ssd.py --input C:\Users\zlzx\Downloads\ssd_mobilenet_v2_coco_2018_03_29\frozen_inference_graph.pb --config C:\Users\zlzx\Downloads\ssd_mobilenet_v2_coco_2018_03_29\pipeline.config --output frozen_inference_graph.pbtxt
frozen_inference_graph.pb是下载的训练pb模型,pipeline是训练时的配置文件,生成pb时会有,frozen_inference_graph.pbtxt就是生成的文件用于opencv恢复模型
然后用opencv恢复模型net = cv2.dnn.readNetFromTensorflow(weightsPath,configPath)大致代码如下:import cv2 as cv
cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'frozen_inference_graph.pbtxt')
img = cv.imread('example.jpg')
rows = img.shape
cols = img.shape
cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
cvOut = cvNet.forward()
for detection in cvOut:
score = float(detection)
if score > 0.3:
left = detection * cols
top = detection * rows
right = detection * cols
bottom = detection * rows
cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)
cv.imshow('img', img)
cv.waitKey()
注:Tensorflow模型的graph结构可以保存为.pb文件或者.pbtxt文件,或者.meta文件,其中只有.pbtxt文件是可读的。在OpenCV中,每个模型.pb文件,原则上应有一个对应的文本图形定义的.pbtxt文件,当然也可能没有,在opencv_extra\testdata\dnn有些.pbtxt文件是可以对应找到,这个要看opencv会不会提供。
页:
[1]