Tensorflow Serving: InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got unknown format starting with 'AAAAAAAAAAAAAAAA'

混江龙づ霸主 提交于 2019-12-11 17:43:13

问题


I'm trying to prepare my custom Keras model for deploy to be used with Tensorflow Serving, but I'm running into issues with preprocessing my images.

When i train my model i use the following functions to preprocess my images:

def process_image_from_tf_example(self, image_str_tensor, n_channels=3):
    image = tf.image.decode_image(image_str_tensor)
    image.set_shape([256, 256, n_channels])
    image = tf.cast(image, tf.float32) / 255.0
    return image

def read_and_decode(self, serialized):
    parsed_example = tf.parse_single_example(serialized=serialized, features=self.features)
    input_image = self.process_image_from_tf_example(parsed_example["image_raw"], 3)
    ground_truth_image = self.process_image_from_tf_example(parsed_example["gt_image_raw"], 1)
    return input_image, ground_truth_image

My images are PNGs saved locally, and when i write them on the .tfrecord files i use tf.gfile.GFile(str(image_path), 'rb').read()

This works, I'm able to train my model and use it for local predictions.

Now I want to deploy my model to be used with Tensorflow Serving. My serving_input_receiver_fn function looks like this:

def serving_input_receiver_fn(self):
    input_ph = tf.placeholder(dtype=tf.string, shape=[None], name='image_bytes')
    images_tensor = tf.map_fn(self.process_image_from_tf_example, input_ph, back_prop=False, dtype=tf.float32)
    return tf.estimator.export.ServingInputReceiver({'input_1': images_tensor}, {'image_bytes': input_ph})

where process_image_from_tf_example is the same function as above, but i get the following error:

InvalidArgumentError (see above for traceback): assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]

Reading here it looks like this error is due to the fact that i'm not using tf.gfile.GFile(str(image_path), 'rb').read()

as with my training/test files, but i can't use it because i need to send encoded bytes formatted as

{"image_bytes": {'b64': base64.b64encode(image).decode()}}

as requested by TF Serving.

Examples online send JPEG encoded bytes and preprocess the image starting with

tf.image.decode_jpeg(image_buffer, channels=3)

but if i use a different preprocessing function in my serving_input_receiver_fn (different than the one used for training) that starts with

tf.image.decode_png(image_buffer, channels=3)

i get the following error:

InvalidArgumentError (see above for traceback): Expected image (JPEG, PNG, or GIF), got unknown format starting with 'AAAAAAAAAAAAAAAA'

(the same happens with decode_jpeg, by the way)

What am i doing wrong? Do you need more code from me to answer? Thanks a lot!

Edit!! Changed the title because it was not clear enough


回答1:


OK I solved it.

image was a numpy array but i had to do the following:

buffer = cv2.imencode('.jpg', image)[1].tostring()
bytes_image = base64.b64encode(buffer).decode('ascii')
{"image_bytes": {"b64": bytes_image}}

Also, my preprocessing and serving_input_receiver_fn functions changed:

def process_image_from_buffer(self, image_buffer):
    image = tf.image.decode_jpeg(image_buffer, channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.expand_dims(image, 0)
    image = tf.image.resize_bilinear(image, [256, 256], align_corners=False)
    image = tf.squeeze(image, [0])
    image = tf.cast(image, tf.float32) / 255.0
    return image

def serving_input_receiver_fn(self):
    input_ph = tf.placeholder(dtype=tf.string, shape=[None])
    images_tensor = tf.map_fn(self.process_image_from_buffer, input_ph, back_prop=False, dtype=tf.float32)
    return tf.estimator.export.ServingInputReceiver({'input_1': images_tensor}, {'image_bytes': input_ph})

process_image_from_buffer is different than process_image_from_tf_example used above for training. I also removed name='image_bytes' from input_ph above.

Hope it's clear enough to help someone else.

Excellent guide partially used for solving it



来源:https://stackoverflow.com/questions/56275522/tensorflow-serving-invalidargumenterror-expected-image-jpeg-png-or-gif-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!