Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]
Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]
我使用 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})
似乎在创建 TFRecords 时,只支持 jpeg 图像,文档中没有指出这一点!同样,当您尝试使用其他类型时,它不会发出任何警告或不会出现任何异常,因此像我这样的人会花费大量时间调试可以很容易地首先发现和修复的东西地方。
无论如何,将所有图像转换为 jpg 解决了这个奇怪的地狱问题。
你也可以查看这个问题:
https://github.com/tensorflow/tensorflow/issues/13044
此程序将挑选出实际上不是 jpeg 的文件。删除它们就可以了。
"""
导入 imghdr
导入 cv2
导入 os
导入 glob
对于 ['train'、'test'] 中的文件夹:
image_path = os.path.join(os.getcwd(), ('images/' + 文件夹))
打印(image_path)
对于 glob.glob(image_path + '/*.jpg') 中的文件:
图片 = cv2.imread(文件)
file_type = imghdr.what(文件)
如果 file_type != 'jpeg':
print(file + " - invalid - " + str(file_type))
cv2.imwrite(文件,图片)
"""
我的问题是我将图像保存为字节,但它必须是字符串。
所以不是这个:
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])),
...
}
我使用 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})
似乎在创建 TFRecords 时,只支持 jpeg 图像,文档中没有指出这一点!同样,当您尝试使用其他类型时,它不会发出任何警告或不会出现任何异常,因此像我这样的人会花费大量时间调试可以很容易地首先发现和修复的东西地方。 无论如何,将所有图像转换为 jpg 解决了这个奇怪的地狱问题。
你也可以查看这个问题: https://github.com/tensorflow/tensorflow/issues/13044
此程序将挑选出实际上不是 jpeg 的文件。删除它们就可以了。
"""
导入 imghdr
导入 cv2
导入 os
导入 glob
对于 ['train'、'test'] 中的文件夹:
image_path = os.path.join(os.getcwd(), ('images/' + 文件夹))
打印(image_path)
对于 glob.glob(image_path + '/*.jpg') 中的文件:
图片 = cv2.imread(文件)
file_type = imghdr.what(文件)
如果 file_type != 'jpeg':
print(file + " - invalid - " + str(file_type))
cv2.imwrite(文件,图片)
"""
我的问题是我将图像保存为字节,但它必须是字符串。
所以不是这个:
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])),
...
}