DR.MALEK
DR.MALEK

Reputation: 35

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:

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

Upvotes: 2

Views: 4110

Answers (2)

tsveti_iko
tsveti_iko

Reputation: 7992

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])),
    ...
}

Upvotes: 0

Parth chokhra
Parth chokhra

Reputation: 111

it seems in creating TFRecords, only jpeg images are supported and no where in the documentation this is indicated! 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.

you can also check this issue: https://github.com/tensorflow/tensorflow/issues/13044

This program will pick out files that are not actually jpeg. Delete them then you are good to go. """ import imghdr
import cv2
import os
import glob

for folder in ['train', 'test']:
image_path = os.path.join(os.getcwd(), ('images/' + folder))
print(image_path)
for file in glob.glob(image_path + '/*.jpg'):
image = cv2.imread(file)
file_type = imghdr.what(file)
if file_type != 'jpeg':
print(file + " - invalid - " + str(file_type))

cv2.imwrite(file, image)

"""

Upvotes: 1

Related Questions