2523浏览
查看: 2523|回复: 0

[平台测评] 【天天向上】OpenVINO学习笔记(四)目标检测模型

[复制链接]
【模型下载】首先,我们需要下载模型文件。进入 models , 选择下载 ssd_mobilenet_v2_coco 模型文件。
【天天向上】OpenVINO学习笔记(四)目标检测模型图1
tensorflow
一般我们恢复模型都是要用到tensorflow,大致代码如下所示:
  1. import numpy as np
  2. import tensorflow as tf
  3. import cv2 as cv
  4. # Read the graph.
  5. with tf.gfile.FastGFile('frozen_inference_graph.pb', 'rb') as f:
  6.     graph_def = tf.GraphDef()
  7.     graph_def.ParseFromString(f.read())
  8. with tf.Session() as sess:
  9.     # Restore session
  10.     sess.graph.as_default()
  11.     tf.import_graph_def(graph_def, name='')
  12.     # Read and preprocess an image.
  13.     img = cv.imread('example.jpg')
  14.     rows = img.shape[0]
  15.     cols = img.shape[1]
  16.     inp = cv.resize(img, (300, 300))
  17.     inp = inp[:, :, [2, 1, 0]]  # BGR2RGB
  18.     # Run the model
  19.     out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
  20.                     sess.graph.get_tensor_by_name('detection_scores:0'),
  21.                     sess.graph.get_tensor_by_name('detection_boxes:0'),
  22.                     sess.graph.get_tensor_by_name('detection_classes:0')],
  23.                    feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
  24.     # Visualize detected bounding boxes.
  25.     num_detections = int(out[0][0])
  26.     for i in range(num_detections):
  27.         classId = int(out[3][0])
  28.         score = float(out[1][0])
  29.         bbox = [float(v) for v in out[2][0]]
  30.         if score > 0.3:
  31.             x = bbox[1] * cols
  32.             y = bbox[0] * rows
  33.             right = bbox[3] * cols
  34.             bottom = bbox[2] * rows
  35.             cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
  36. cv.imshow('TensorFlow MobileNet-SSD', img)
  37. 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中找到
【天天向上】OpenVINO学习笔记(四)目标检测模型图3
在命令行执行

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)

大致代码如下:

  1. import cv2 as cv
  2. cvNet = cv.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'frozen_inference_graph.pbtxt')
  3. img = cv.imread('example.jpg')
  4. rows = img.shape[0]
  5. cols = img.shape[1]
  6. cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
  7. cvOut = cvNet.forward()
  8. for detection in cvOut[0,0,:,:]:
  9.     score = float(detection[2])
  10.     if score > 0.3:
  11.         left = detection[3] * cols
  12.         top = detection[4] * rows
  13.         right = detection[5] * cols
  14.         bottom = detection[6] * rows
  15.         cv.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)
  16. cv.imshow('img', img)
  17. cv.waitKey()
复制代码

【天天向上】OpenVINO学习笔记(四)目标检测模型图2



注:
Tensorflow模型的graph结构可以保存为.pb文件或者.pbtxt文件,或者.meta文件,其中只有.pbtxt文件是可读的。在OpenCV中,每个模型.pb文件,原则上应有一个对应的文本图形定义的.pbtxt文件,当然也可能没有,在opencv_extra\testdata\dnn有些.pbtxt文件是可以对应找到,这个要看opencv会不会提供。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail