How to predict using keras and google cloud ml engine

321 views Asked by At

I need to understand how to get predictions from a keras model with tensorflow backend deployed on google cloud ml engine.

I have done following steps: 1) Trained a CNN sentence classification model developed using Keras

2) Saved the model using tensorflow saved model

3) Deployed to google cloud ml engine.

My problem is prediction is giving only probabilities but not the labels for those probabilities. How do I get both labels and probabilities at the time of prediction.

My code is as follows:

def get_cnn_model_v1():
    model = Sequential()
    model.add(Embedding(VOCAB_SIZE,
                        50,
                        input_length=max_document_length))
    model.add(Dropout(0.2))
    model.add(Conv1D(64,
                     3,
                     padding='valid',
                     activation='relu',
                     strides=1))
    model.add(GlobalMaxPooling1D())
    model.add(Dense(256))
    model.add(Dropout(0.2))
    model.add(Activation('relu'))
    model.add(Dense(number_of_labels))
    model.add(Activation('softmax'))
    model.summary()
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=[metrics.mae,metrics.categorical_accuracy])

    return model

model.fit(x,y,batch_size=32,epochs=50,verbose=1, validation_data=(testX, testY),shuffle=True)


builder = saved_model_builder.SavedModelBuilder("<path>")
signature = predict_signature_def(
inputs={'input': m.inputs[0]},
outputs={'output': m.outputs[0]})


with K.get_session() as sess:
    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()

I uploaded this model on google cloud storage and deployed it successfully. When I run predictions using python client I get an array of probabilities . I also want the class name against each probability . How do I modify my model or signature to achieve this ?

1

There are 1 answers

0
Lak On

Sorry, but you do need to know what class number corresponds to each label. Once you know that, you can add that to your dictionary of outputs.

You might want to ask keras folks how to access the vocabulary of labels. I assume it is available from the layer where you do the mapping.