tensorflow: InvalidArgumentError : Expected image (JPEG, PNG, or GIF), got unknown format starting with '255'
System information
- OS Platform and Distribution :Linux Ubuntu 16.04
- TensorFlow installed from :binary
- TensorFlow version :1.1.0
- Python version : 2.7.12
Describe the problem
trying to feed a model an image encoded in string as the model require that as an input string
signature_def {
key: "serving_default"
value {
inputs {
key: "image_bytes"
value {
name: "Placeholder:0"
dtype: DT_STRING
tensor_shape {
dim {
size: -1
}
}
}
}
inputs {
key: "key"
value {
name: "Placeholder_1:0"
dtype: DT_STRING
tensor_shape {
dim {
size: -1
}
}
}
}
outputs {
key: "key"
value {
name: "Identity:0"
dtype: DT_STRING
tensor_shape {
dim {
size: -1
}
}
}
}
outputs {
key: "prediction"
value {
name: "ArgMax:0"
dtype: DT_INT64
tensor_shape {
dim {
size: -1
}
}
}
}
outputs {
key: "scores"
value {
name: "final_ops/softmax:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: -1
}
dim {
size: 3
}
}
}
}
method_name: "tensorflow/serving/predict"
}
}
Here is the code :-
def load_image( infilename ) :
img = Image.open( infilename )
img.load()
data = np.asarray( img, dtype="string" )
return data
export_dir = '.'
with tf.Session(graph=tf.Graph()) as sess:
model = tf.saved_model.loader.load(sess, ['serve'], export_dir)
input_dict, output_dict =_signature_def_to_tensors(model.signature_def['serving_default'])
out = sess.run(output_dict, feed_dict={input_dict['image_bytes']: load_image("fullsize.jpeg").flatten()})
print(input_dict)
Error returns is :
InvalidArgumentError (see above for traceback): Expected image (JPEG, PNG, or GIF), got unknown format starting with '255'
[[Node: map/while/DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](map/while/TensorArrayReadV3)]]
how i can solve that , any help
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (2 by maintainers)
This question is better asked on StackOverflow since it is not a bug or feature request. There is also a larger community that reads questions there. Thanks!
That said, a few things to note:
tf.image.decode_jpegexpects the encoded-JPEG bytes (i.e., the contents of the file). The use ofPILand in particularPIL.Image.load()suggests that the numpy array you’re creating consists of the decoded contents (decoded byPIL). Which is why thesess.run()call is failing as it expects an encoded JPEG but you’re providing it a decoded one.To provide the raw contents of the file as a feed, you’d want to do something like so:
Which will create the numpy array of the encoded JPEG bytes that you can feed to your session.
Hope that helps.
@rohitkumar9989 you can solve this problem with the below code:
For execution of the above code:
Awesome, thanks! Thanks for your handy help…
On Thu, 10 Jun 2021, 16:07 Deep Modi, @.***> wrote:
@keyankarthisdk , maybe the reason is some picture is not the JPEG format. You can use tf.image.decode_jpeg() to test which picture is not the right format for tensorflow and then discard. I use this way solve the problem.