【发布时间】:2021-01-25 18:11:54
【问题描述】:
我的动机是构建一个自定义的异议检测 Web 应用程序。我从model zoo 下载了tf2 预训练的SSD Resnet1010 模型。我的想法是,如果这个实现有效,我将用我自己的数据训练模型。我运行$saved_model_cli show --dir saved_model --tag_set serve --signature_def serving_default 来计算输入和输出节点。
The given SavedModel SignatureDef contains the following input(s):
inputs['input_tensor'] tensor_info:
dtype: DT_UINT8
shape: (1, -1, -1, 3)
name: serving_default_input_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['detection_anchor_indices'] tensor_info:
dtype: DT_FLOAT
shape: (1, 100)
name: StatefulPartitionedCall:0
outputs['detection_boxes'] tensor_info:
dtype: DT_FLOAT
shape: (1, 100, 4)
name: StatefulPartitionedCall:1
outputs['detection_classes'] tensor_info:
dtype: DT_FLOAT
shape: (1, 100)
name: StatefulPartitionedCall:2
outputs['detection_multiclass_scores'] tensor_info:
dtype: DT_FLOAT
shape: (1, 100, 91)
name: StatefulPartitionedCall:3
outputs['detection_scores'] tensor_info:
dtype: DT_FLOAT
shape: (1, 100)
name: StatefulPartitionedCall:4
outputs['num_detections'] tensor_info:
dtype: DT_FLOAT
shape: (1)
name: StatefulPartitionedCall:5
outputs['raw_detection_boxes'] tensor_info:
dtype: DT_FLOAT
shape: (1, 51150, 4)
name: StatefulPartitionedCall:6
outputs['raw_detection_scores'] tensor_info:
dtype: DT_FLOAT
shape: (1, 51150, 91)
name: StatefulPartitionedCall:7
Method name is: tensorflow/serving/predict
然后我通过运行将模型转换为 tensorflowjs 模型
tensorflowjs_converter --input_format=tf_saved_model --output_node_names='detection_anchor_indices,detection_boxes,detection_classes,detection_multiclass_scores,detection_scores,num_detections,raw_detection_boxes,raw_detection_scores' --saved_model_tags=serve --output_format=tfjs_graph_model saved_model js_model
这是我的 javascript 代码(在 vue 方法中)
loadTfModel: async function(){
try {
this.model = await tf.loadGraphModel(this.MODEL_URL);
} catch(error) {
console.log(error);
}
},
predictImg: async function() {
const imgData = document.getElementById('img');
let tf_img = tf.browser.fromPixels(imgData);
tf_img = tf_img.expandDims(0);
const predictions = await this.model.executeAsync(tf_img);
const data = []
for (let i = 0; i < predictions.length; i++){
data.push(predictions[i].dataSync());
}
console.log(data);
}
我的问题是数组中的这八个项目是否对应八个定义的输出节点?如何理解这些数据?以及如何将其转换为像 python 那样的人类可读格式?
更新 1:
我试过这个answer 并编辑了我的预测方法:
predictImg: async function() {
const imgData = document.getElementById('img');
let tf_img = tf.browser.fromPixels(imgData);
tf_img = tf_img.expandDims(0);
const predictions = await this.model.executeAsync(tf_img, ['detection_classes']).then(predictions => {
const data = predictions.dataSync()
console.log('Predictions: ', data);
})
}
我最终得到了"Error: The output 'detection_classes' is not found in the graph"。我将不胜感激。
【问题讨论】:
标签: tensorflow object-detection object-detection-api tensorflow.js