简体   繁体   English

Tensorflow 预测错误,invalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP]

[英]Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]

I trained a Tensorflow Ssd object-detection model using Google object-detection Api and i exported the trained model using the provided "export_inference_graph.py" script as "Saved_model.pb" file with "encoded_image_string_tensor" as input type, however when i tried to make prediction to the model, i got the following error:我使用 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]

İ loaded the model into a graph as follow: © 将模型加载到图形中,如下所示:

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()

And made the prediction as follow:并做出如下预测:

# 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})

it seems in creating TFRecords, only jpeg images are supported and no where in the documentation this is indicated!似乎在创建 TFRecords 时,只支持 jpeg 图像,文档中没有指出这一点! also when you try to use other types, it does not issue any warnings or doesn't through any exceptions and therefore people like me lose an immense amount of time debugging something that could be easily spotted and fixed in first place.此外,当您尝试使用其他类型时,它不会发出任何警告或不会发出任何异常,因此像我这样的人会浪费大量时间调试一些很容易被发现并首先修复的东西。 Anyway, converting all images to jpg solved this weird hellbound issue.无论如何,将所有图像转换为 jpg 解决了这个奇怪的地狱问题。

you can also check this issue: https://github.com/tensorflow/tensorflow/issues/13044你也可以检查这个问题: https : //github.com/tensorflow/tensorflow/issues/13044

This program will pick out files that are not actually jpeg.该程序将挑选出实际上不是 jpeg 的文件。 Delete them then you are good to go.删除它们然后你就可以开始了。 """ import imghdr """ 导入 imghdr
import cv2导入 cv2
import os导入操作系统
import glob导入全局

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

cv2.imwrite(file, image) cv2.imwrite(文件,图像)

""" """

My issue was that I was saving the image as bytes, but it needed to be a string.我的问题是我将图像保存为字节,但它需要是一个字符串。

So instead of this:所以而不是这个:

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

You need to do this:你需要这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP] - InvalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] “预测失败:模型执行期间出错:无法将字节解码为JPEG,PNG,GIF或BMP” - “Prediction failed: Error during model execution: Unable to decode bytes as JPEG, PNG, GIF, or BMP” 使用 tf.image.decode_image 时出错“b'无法将字节解码为 JPEG、PNG、GIF 或 BMP'” - Error using tf.image.decode_image " b'Unable to decode bytes as JPEG, PNG, GIF, or BMP'" tensorflow.python.framework.errors_impl.InvalidArgumentError:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - tensorflow.python.framework.errors_impl.InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required Tensorflow:InvalidArgumentError:预期图像(JPEG、PNG 或 GIF),文件为空 - Tensorflow: InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got empty file Tensorflow Keras 错误:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - Tensorflow Keras error: Unknown image file format. One of JPEG, PNG, GIF, BMP required Tensorflow 无法解码 tfrecords 中的 jpeg 字节 - Tensorflow failed to decode jpeg bytes in tfrecords 我的训练数据集中的隐藏文件使 tensorflow 返回“未知图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一。” - Hidden file in my training dataset which makes tensorflow return “Unknown image file format. One of JPEG, PNG, GIF, BMP required.” Tensorflow InvalidArgumentError:断言失败:[标签必须是 &lt;= n_classes - 1] - Tensorflow InvalidArgumentError: assertion failed: [Labels must be <= n_classes - 1] tensorflow2.1 InvalidArgumentError:断言失败:[0] [Op:Assert] 名称:EagerVariableNameReuse - tensorflow2.1 InvalidArgumentError: assertion failed: [0] [Op:Assert] name: EagerVariableNameReuse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM