ageron / handson-ml2

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Apache License 2.0
28k stars 12.8k forks source link

Chapter 19 - KeyError in GCP Prediction Service Model Version Mismatch #174

Open mshearer0 opened 4 years ago

mshearer0 commented 4 years ago

If you deploy Model v2 to GCP (as per page 680 .../my_mnist_model/0002/) then parsing of the prediction results fails due to a Key Error, eg:

KeyError: 'dense_2'

This occurs because the output_name is still assigned to the value from v1, eg 'dense_1'. Resetting the output_name to the v2 value prior to calling Predict allows the results to be parsed correctly

output_name = model.output_names[0]

STRZGR commented 4 years ago

I had the same issue. but instead of reassigning the variable output_name, I used a line comprehension to get the values from the dictionary, since that's really the only thing we need here:

def predict(X):
    input_data_json = {"signature_name": "serving_default",
                       "instances": X.tolist()}
    request = ml_resource.predict(name=model_path, body=input_data_json)
    response = request.execute()
    if "error" in response:
        raise RuntimeError(response["error"])
    # return np.array([pred[output_name] for pred in response["predictions"]])
    return np.array([v for pred in response["predictions"] 
                       for k, v in pred.items()])

I had originally taken the same approach as the OP (reassigning output_name to model.output_names[0]), but this will only work if the current model is the same model that is being queried via GCP.