0

Right now, I am using the default retrain.py from tensorflow to train an image classification model. But when I serve the model on google ai platform and try to call the api, I get an error saying that the image is too large since it is a float32 array. I’m thinking the best thing would be is to change retrain.py to take in a b64 image instead of a float32 array, but I have no idea how to do that. Any suggestions?

Any help is appreciated! Thanks!

UPDATE

def export_model(module_spec, class_count, saved_model_dir):
 sess, in_image, _, _, _, _ = build_eval_session(module_spec, class_count)

 image = tf.placeholder(shape=[None], dtype=tf.string)
 export_dir = "/tmp/save/"

 inputs = {'image_bytes': image}
 with sess.graph.as_default() as graph:
    tf.saved_model.simple_save(sess, export_dir, inputs, {'prediction': graph.get_tensor_by_name('final_result:0')})

This is what I have updated my code to be, but it still doesn't work

Psonthalia
  • 179
  • 1
  • 12

1 Answers1

0

take a look at this post, it contains the info you need. If not, then reply and I'll help you prepare some code, you probbably need the url safe b64 variant though.

EDIT

your code is a bit confusing, I don't think the input is already connected to your graph, have you looked at the graph with tf.summary.FileWriter('output folder', sess.graph)?

I'm gonna gradually try to explain how you build some layers in front of your model, with some examples, this code should not be in retrain.py and can be ran after you trained the model.

1) Load your tensorflow model in if it is build with savedModelBuilder or the simple save thing you can do it like this:

def loader(path):
    with tf.Session(graph=tf.Graph()) as sess:
        tf.saved_model.loader.load(sess, [tag_constants.TRAINING], path)
        return tf.get_default_graph().as_graph_def()

The tagconstants can be checked with the saved_model_cli tool, it is possible that this has to be empty [] in your case.

2) add the layers/tensors you need, you need something that accept a byte string, or base 64 in this case, that decodes it and transforms it into a 3D image:

image_str_tensor = tf.placeholder(dtype=tf.string, shape=(None,), name='input_image_bytes')
input_image = tf.decode_base64(image_str_tensor)          
decoder = tf.image.decode_jpeg(input_image[0], channels=3)

The other tensors like converting to float, dim_expanding and reshaping should already be in the graph if you got it from retrain.py.

3) implement them into your graph by feeding them into it.

graph_def_inception = loader('path to your saved model')
output_prediction, = tf.import_graph_def(graph_def_inception, input_map={"DecodeJpeg:0": decoder}, return_elements=['final_result:0'], name="")

4) create a saved model and check if everything is like you want it to be!

builder = tf.saved_model.builder.SavedModelBuilder('output/model/path')

with tf.Session() as sess:
    tf.summary.FileWriter('output/graph_log/files', sess.graph)

    input_tensor_info = tf.saved_model.utils.build_tensor_info(input_image)
    output_tensor_info = tf.saved_model.utils.build_tensor_info(output_prediction)
    signature = tf.saved_model.signature_def_utils.build_signature_def(
        inputs={'input_image': input_tensor_info},
        outputs={'output_prediction': output_tensor_info},
        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)

    # save as SavedModel
    builder.add_meta_graph_and_variables(sess,
                                         [tf.saved_model.tag_constants.SERVING],
                                         signature_def_map={'serving_default': signature})
    builder.save()

5) if you get errors try to debug them with tensorboard

tensorboard --logdir=output/graph_log/files

I hoped I helped a bit, this code will not work from the first try, you need to puzzle with some parts. If you truly cannot succeed, then you should share the model, maybe I can do it then and share the code with you, if I have time.

T. Kelher
  • 1,188
  • 10
  • 9
  • Thanks for your response! I took a look at the post and updated my code as shown in the update above, but when I try to run the saved_mode, it gives me this error: Cannot interpret feed_dict key as Tensor: The name 'Placeholder_1:0' refers to a Tensor which does not exist. The operation, 'Placeholder_1', does not exist in the graph. – Psonthalia Apr 15 '19 at 00:14