【发布时间】:2019-03-13 18:45:51
【问题描述】:
我使用 Google 对象检测 Api 训练了一个 Tensorflow Ssd 对象检测模型,并使用提供的“export_inference_graph.py”脚本将训练后的模型导出为“Saved_model.pb”文件,其中“encoded_image_string_tensor”作为输入类型,但是当我试图对模型进行预测,我得到了以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]
İ 将模型加载到图表中,如下所示:
with tf.Session() as sess:
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], saved_model_file)
graph = tf.get_default_graph()
并做出如下预测:
# Convert the image into base64 encoded string
img = Image.open(IMAGE_PATH)
resized_img = img.resize((300, 300), Image.ANTIALIAS)
binary_io = io.BytesIO()
resized_img.save(binary_io, "JPEG")
bytes_string_image = base64.b64encode(binary_io.getvalue()).decode("utf-8")
# Define the input and output placeholder tensors
input_tensor = graph.get_tensor_by_name('encoded_image_string_tensor:0')
tensor_dict = {}
for key in ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes']:
tensor_name = key + ':0'
tensor_dict[key] = graph.get_tensor_by_name(tensor_name)
# Finally, do the prediciton
output_dict = sess.run(tensor_dict, feed_dict={
input_tensor: bytes_string_image})
【问题讨论】:
-
您转换为 base64 编码字符串的原因是什么?
-
因为,我将训练好的模型导出为具有base64编码字符串的输入类型。
-
尽管如此,似乎希望 jpg 在 TF 中自行决定
-
İ 试图将 jpg 图像作为 np 数组发送,但我得到:“ValueError:无法为具有形状的 Tensor 'encoded_image_string_tensor:0' 提供形状 (1, 853, 1280, 3) 的值'(?,)'"
-
是的。我现在重新训练模型。我会选择 image_tensor 作为输入类型。
标签: python tensorflow base64 prediction