0

I made a Keras model

model = Sequential() 
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

Trained it locally

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fit the model
model.fit(X_train, Y_train, epochs=150, batch_size=10)

Tested that it works

example = np.array([X_test.iloc[0]])    
model.predict(example)

saved it using this function

def to_savedmodel(model, export_path):
"""Convert the Keras HDF5 model into TensorFlow SavedModel."""
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'input': model.inputs[0]},
                                outputs={'income': model.outputs[0]})
K.clear_session()
sess = K.get_session()
builder.add_meta_graph_and_variables(
        sess=sess,
        tags=[tag_constants.SERVING],
        signature_def_map={
            signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
    )
sess.close()
K.clear_session()
builder.save()

The model is now in GC Storage in .pb format.

I made a new model in ML Engine and deploy this first version. When I try to use it via HTTP POST request using this json body

{
  "instances": [{
    "input": [1, 2, 3, 4, 5, 6, 7 ]
  }]
}

I get this Error:

{
    "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.NOT_FOUND, details=\"FeedInputs: unable to find feed output dense_34_input:0\")"
}

Any idea how can I send the correct Body or save the model correctly?

Dudley Díaz
  • 65
  • 2
  • 11
  • [This](https://github.com/Fematich/mlengine-boilerplate/blob/master/predictions/predict.py) repo contains good and up-to-date boilerplate code for ML engine, it might be helpful. It is tensorflow-based though. – sdcbr Jul 28 '18 at 10:29
  • However, aren't you throwing away your trained graph when clearing the Keras session before saving your model? – sdcbr Jul 28 '18 at 10:31
  • Does this post help: https://stackoverflow.com/questions/51432589/how-do-i-get-a-tensorflow-keras-model-that-takes-images-as-input-to-serve-predic – rhaertel80 Jul 30 '18 at 15:05
  • Yes! That was it! your second suggestion was correct. I was throwing away the trained graph. I made a new function that works: – Dudley Díaz Jul 31 '18 at 00:03

1 Answers1

2

Thanks sdcbr. You pointed me in the right direction. The function that saved the model was throwing away my trained model. I changed it and works well now:

def to_savedmodel(fname, export_path):
    with tf.Session() as sess:
        K.set_session(sess)
    model = load_model(fname)
    sess.run(tf.initialize_all_variables())
    K.set_learning_phase(0)
    builder = SavedModelBuilder(export_path)
    signature = predict_signature_def(
        inputs={"inputs": model.input},
        outputs={"outputs": model.output}) 
builder.add_meta_graph_and_variables(
        sess=sess,
        tags=[tag_constants.SERVING],
        signature_def_map={
            'predict': signature})
    builder.save()
Dudley Díaz
  • 65
  • 2
  • 11