python - node.js 中的对象检测模型预测时间与 python 相差很大
问题描述
我使用 tfjs-converter从tensorflow 模型动物园转换了模型(ssd_mobilenet_v1_ppn_coco、ssd_mobilenet_v2_coco、ssd_mobilenet_v1_coco) 。但是,我在 node.js 中的预测时间(每帧 4 秒)似乎与我在 python 中使用相同模型时的预测时间(每帧 0.1 秒)大不相同。是什么导致了预测时间的这种差异?编辑:我将 freeze_model.pb 用于 Python,但将包含的 saved_model 转换为 tfjs 模型。
作为参考,我正在使用模型进行实时对象检测,使用 opencv 从网络摄像头读取输入。我直接使用了没有训练的模型,但是一旦我解决了性能问题,我打算在我自己的数据上训练它。
我还测试了 node.js 的 @tensorflow-models 中包含的 coco_ssd 实现,它引用了此处找到的模型:ssdlite_mobilenet_v2。比较时间:ssd_mobilenet_v2_coco(每帧 3-4 秒)和这个模型(每帧 0.3-0.7 秒)。我相信这表明我可以进一步优化动物园模型?
这是我用来在 Node.js 中对模型进行基准测试的代码:
async function load() {
model = await tf.loadGraphModel('file://./javascript_models/ssdlite_mobilenet_v2_js/model.json')
return model
}
const model = await load()
let t = Date.now()
outputs = await model.executeAsync({image_tensor: input},
['detection_boxes:0',
'num_detections:0']
)
t = Date.now() - t
console.log('execute_time: ' + t)
这是我用来在 python 中对代码进行基准测试的代码:
#Load graph
def load():
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile('./mobilenet_ppn/frozen_inference_graph.pb', 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
return detection_graph
detection_graph = load()
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
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.
detection_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.
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')
t = time.time()
#the actual prediction
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores,
detection_classes, num_detections],
feed_dict={image_tensor: frame_expanded})
#print the time taken to execute
print('execute time: ', time.time()- t)
我预计模型预测所花费的时间是相同的数量级,相反,与 node.js 相比,python 上的预测运行速度要快 100 倍。
解决方案
推荐阅读
- postgresql - 将varbit转换为int的其他方法?还有大神?
- windows - 在 Windows 上安装 Ghost CMS
- python-3.x - 尝试发送数据 Python CAN
- php - 如何从文件中删除包含某些文本的所有行以及紧随其后的行
- typescript - 使用 TS 条件类型缩小到数值属性
- html - 表格内的自动宽度按钮组?
- swift - CryptoSwift AES 加密和解密
- java - 为什么我的地图没有正确排序?
- android - 在 Kotlin 中使用按钮展开和折叠折叠工具栏
- react-native - 我怎么知道我的 react-native 应用程序运行顺利并且没有风险?