【问题标题】:Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]Tensorflow 预测错误,invalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP]
【发布时间】: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


【解决方案1】:

似乎在创建 TFRecords 时,只支持 jpeg 图像,并且在文档中没有指出这一点!同样,当您尝试使用其他类型时,它不会发出任何警告或不会出现任何异常,因此像我这样的人会浪费大量时间来调试可以很容易发现并首先修复的东西。 无论如何,将所有图像转换为 jpg 解决了这个奇怪的地狱问题。

你也可以检查这个问题: https://github.com/tensorflow/tensorflow/issues/13044

这个程序会挑选出实际上不是 jpeg 的文件。删除它们然后你就可以走了。 """ 导入 imghdr
导入 cv2
导入操作系统
导入全局

对于 ['train', 'test'] 中的文件夹:
image_path = os.path.join(os.getcwd(), ('images/' + 文件夹))
打印(图像路径)
对于 glob.glob 中的文件(image_path + '/*.jpg'):
图像 = cv2.imread(文件)
file_type = imghdr.what(文件)
如果 file_type != 'jpeg':
print(file + " - 无效 - " + str(file_type))

cv2.imwrite(文件,图像)

"""

【讨论】:

    【解决方案2】:

    我的问题是我将图像保存为字节,但它必须是字符串。

    所以不要这样:

    encoded = image.tobytes()
    features = {
        'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[encoded])),
        ...
    }
    

    你需要这样做:

    encoded = cv2.imencode('.jpg', image)[1].tostring()
    features = {
        'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[encoded])),
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 2021-04-09
      • 2022-12-03
      • 2020-01-03
      相关资源
      最近更新 更多