Keras model to Tensorflow to input b64 encoded dat

2020-04-06 01:36发布

问题:

I am trying to convert a keras model to use it for predictions on google cloud's ml-engine. I have a pre-trained classifier that takes in a numpy array as input. The normal working data I send to model.predict is named input_data.

I convert it to base 64 and dump it to a json file using the following few lines:

data = {}
data['image_bytes'] = [{'b64':base64.b64encode(input_data.tostring())}]

with open('weights/keras/example.json', 'w') as outfile:
    json.dump(data, outfile)

Now, I try to create the TF model from my existing model:

from keras.models import model_from_json
import tensorflow as tf
from keras import backend as K
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import build_signature_def, predict_signature_def
init = tf.global_variables_initializer()
with tf.Session() as sess:
    K.set_session(sess)
    sess.run(init)
    print("Keras model & weights loading...")
    K.set_learning_phase(0)
    with open(json_file_path, 'r') as file_handle:
        model = model_from_json(file_handle.read())

    model.load_weights(weight_file_path)

    builder = saved_model_builder.SavedModelBuilder(export_path)

    raw_byte_strings = tf.placeholder(dtype=tf.string, shape=[None], name='source')
    decode = lambda raw_byte_str: tf.decode_raw(raw_byte_str, tf.float32)
    input_images = tf.map_fn(decode, raw_byte_strings)
    print(input_images)
    signature = predict_signature_def(inputs={'image_bytes': input_images},
                                    outputs={'output': model.output})

    builder.add_meta_graph_and_variables(
        sess=sess,
        tags=[tag_constants.SERVING],
        signature_def_map={
            signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
        }
    )
    builder.save()

When I try to test this locally I get the following error:

ERROR:root:Exception during running the graph: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,473,473,3]
     [[Node: input_1 = Placeholder[dtype=DT_FLOAT, shape=[?,473,473,3], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Help?