系统信息
在我的自定义数据集上训练了一个ssd_inception_v2模型之后,我想使用它进行推理。因为推理以后应该在没有GPU的设备上运行,所以I只提供给CPU进行推理。我对opject_detection_tutorial.ipynb进行了调整,以度量推理的时间,并让下面的代码在一系列视频图像上运行。
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while success:
#print(str(datetime.datetime.now().time()) + " " + str(count))
#read image
success,image = vidcap.read()
#resize image
image = cv2.resize(image , (711, 400))
# crop image to fit 690 x 400
image = image[ : , 11:691]
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image, axis=0)
#print(image_np_expanded.shape)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
before = datetime.datetime.now()
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
print("This took : " + str(datetime.datetime.now() - before))
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
#cv2.imwrite("converted/frame%d.jpg" % count, image) # save frame as JPEG file
count += 1具有以下输出:
这个花了: 0:00:04.289925
这个花了: 0:00:00.909071
这个花了: 0:00:00.917636
这个花了: 0:00:00.908391
这个花了: 0:00:00.896601
这个花了: 0:00:00.908698
这个花了: 0:00:00.890018
这个花了: 0:00:00.896373
.
当然,每幅图像900毫秒是不够快的视频处理。在阅读了许多线程之后,我发现了两种可能的改进方法:
因此,我的问题是,上述两个改进是否有可能提高对实时使用(10-20 fps)的推断,还是我在这里走错了路,应该尝试其他什么?欢迎任何建议。
发布于 2022-05-26 16:13:28
另一种选择是使用不同的工具箱进行推理,例如OpenVINO。OpenVINO是为英特尔硬件设计的,尽管它应该与任何CPU一起工作。它通过将模型转换为中间表示(中间表示,IR),进行图形剪枝,并将某些操作合并到其他操作,从而提高模型的准确性。然后,在运行时,它使用矢量化。
将Tensorflow模型转换为OpenVINO非常简单,除非您有漂亮的自定义层。关于如何做到这一点的完整教程可以找到这里。下面是一些片段。
安装OpenVINO
最简单的方法是使用PIP。或者,您可以使用这个工具在您的情况下找到最佳方法。
pip install openvino-dev[tensorflow2]使用模型优化器转换SavedModel模型
模型优化器是来自OpenVINO开发包的命令行工具。它将Tensorflow模型转换为IR,这是OpenVINO的默认格式。您还可以尝试FP16的精度,这将使您在不降低精度的情况下获得更好的性能(只需更改data_type)。在命令行中运行:
mo --saved_model_dir "model" --input_shape "[1, 3, 224, 224]" --data_type FP32 --output_dir "model_ir"运行推理
转换后的模型可以由运行时加载,并为特定的设备进行编译,例如CPU或GPU (集成到CPU中,比如Intel HD Graphics)。如果你不知道什么是你最好的选择,只需使用汽车。
# Load the network
ie = Core()
model_ir = ie.read_model(model="model_ir/model.xml")
compiled_model_ir = ie.compile_model(model=model_ir, device_name="CPU")
# Get output layer
output_layer_ir = compiled_model_ir.output(0)
# Run inference on the input image
result = compiled_model_ir([input_image])[output_layer_ir]免责声明:我在OpenVINO工作。
https://stackoverflow.com/questions/46301822
复制相似问题