2

I am trying to export my TensorFlow image-classifying model such that it accepts base64 strings as input.

I have tried to implement the solution that is provided on this question, however I am getting the following error:

"InvalidArgumentError: Shape must be rank 0 but is rank 1 for 'DecodeJpeg_1' (op: 'DecodeJpeg') with input shapes: [?]."

The error appears as a result of the code on line 4.

export_dir = '~/models/1'
builder = saved_model_builder.SavedModelBuilder(export_dir)
image = tf.placeholder(dtype=tf.string, shape=[None], name='source')
decoded = tf.image.decode_jpeg(image)
scores = build_model(decoded)
signature = predict_signature_def(inputs={'image_bytes': image},
                                 outputs={'output': scores})

with K.get_session() as sess:
    builder.add_meta_graph_and_variables(sess=sess,
                                         tags=[tag_constants.SERVING],
                                         signature_def_map={'predict': signature})
    builder.save()
sess.close()

Also,

I see that on line 5, "scores" provides the output of the model based on the build_model function. However, I can't find in the original question's answers or in the TensorFlow documentation where this function comes from.

Theo
  • 57,719
  • 8
  • 24
  • 41
sjamil
  • 51
  • 3
  • Can you try with `shape=[1]` on line 4? – sdcbr Apr 26 '19 at 13:29
  • Doesn't work unfortunately. Raises a similar error, namely: ```InvalidArgumentError: Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [1].``` – sjamil Apr 26 '19 at 14:08
  • Can you try with `shape=[]`? – sdcbr Apr 26 '19 at 14:09
  • shape[] works, thank you, the error isn't appearing now. However, now I get an error saying that "build_model" (line 5) isn't defined, which I was afraid of. Do you have any idea where this function comes from? – sjamil Apr 26 '19 at 14:33
  • No, probably from some example code? – sdcbr Apr 26 '19 at 14:40
  • It most likely is. I understand that it is needed in order to generate the outputs of the served model, as per the code on line 7. However, since I am still relatively new to serving TensorFlow models, I find it hard to visualize what this function precisely does. I couldn't find any resources online since it's a fairly new and niche subject, do you have any pointers or resources that I can use to generate these outputs for my model? – sjamil Apr 29 '19 at 07:32
  • So this function will 'apply' your model on the `decoded` image tensor. The output is used to define the prediction signature definition, which defines the path through the model graph that needs to be exported. The output graph is completely defined by the input and output tensors, and `build_model` will give you the output tensor as a function of the input tensor. There are many blogposts online on how to export models for TF Serving, one of which I wrote myself: https://medium.com/tensorflow/training-and-serving-ml-models-with-tf-keras-fd975cc0fa27. – sdcbr Apr 29 '19 at 07:49
  • Finally, note that these API's are constantly being updated and this way of exporting a model will probably be deprecated in TF 2.0. – sdcbr Apr 29 '19 at 07:49

1 Answers1

1

As per the Tutorials mentioned in the tf.decode_jpeg, we should use image = tf.io.read_file(path) before using image = tf.image.decode_jpeg(image).

Working code example is shown below:

import tensorflow as tf

path = '/home/mothukuru/Jupyter_Notebooks/Stack_Overflow/cat.jpeg'

z = tf.placeholder(tf.string, shape=())

image = tf.io.read_file(path)

image = tf.image.decode_jpeg(image)

with tf.Session() as sess:
   v = sess.run([image], feed_dict={z:path})
   print(v)

Output of the above code is an array as shown below:

[array([[[216, 216, 216],
        [218, 218, 218],
        [220, 220, 220],
        ...,
        [226, 228, 217],
        [221, 223, 212],
        [221, 223, 212]],

       [[216, 216, 216],
        [217, 217, 217],
        [219, 219, 219],
        ...,
        [225, 227, 216],
        [222, 224, 213],
        [222, 224, 213]],

       [[216, 216, 216],
        [216, 216, 216],
        [218, 218, 218],
        ...,
        [223, 225, 214],
        [222, 224, 213],
        [222, 224, 213]],

       ...,

       [[240,  20,  64],
        [240,  20,  64],
        [243,  23,  67],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]],

       [[241,  21,  65],
        [240,  20,  64],
        [245,  25,  69],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]],

       [[242,  22,  66],
        [239,  19,  63],
        [246,  26,  70],
        ...,
        [  7,   3,   0],
        [  8,   4,   1],
        [  8,   4,   1]]], dtype=uint8)]