Madhi
Madhi

Reputation: 1236

google cloud engine: Input instances are not in JSON format

I am using Google Cloud ML Engine to do online prediction. I wrote Tensorflow Estimator API Code which I referred from the tf-estimator-tutorials repository. To do online prediction we need to export the model into proto buffer file (.pb) file. To serve the input function to the model I wrote the following code in serve_input_fn() function.

SERVING_HEADER = ['renancy','freq','monetary']
SERVING_HEADER_DEFAULTS = [[0.0],[0.0],[0.0]]

#shape=(?,), dtype=string
rows_string_tensor = tf.placeholder(dtype=tf.string,
                                    shape=[None],
                                    name="csv_rows")

#feeding rows_string_tensor value in the dictionary
receive_tensor = {'csv_rows':rows_string_tensor}

#shape=(?,1), dtype=string
row_columns = tf.expand_dims(rows_string_tensor, -1)

#<tf.Tensor 'DecodeCSV:0' shape=(?,1) dtype=float32>,<tf.Tensor 'DecodeCSV:1' shape=(?,1) dtype=float32>
#<tf.Tensor 'DecodeCSV:2' shape=(?,1) dtype=float32>
columns = tf.decode_csv(row_columns, record_defaults=SERVING_HEADER_DEFAULTS)

#<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32>
#<tf.Tensor 'Expand_dims_3:0' shape=(?,1,1) dtype=float32>
columns = [tf.expand_dims(tensor, -1) for tensor in columns]

#{"renancy":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>,
#"freq":<tf.Tensor 'Expand_dims_2:0' shape=(?,1,1) dtype=float32> 
#"monetary":<tf.Tensor 'Expand_dims_1:0' shape=(?,1,1) dtype=float32>}
features = dict(zip(SERVING_HEADER, columns))


#InputFnOps(features=None, labels=None, default_inputs={'csv_rows':<tf.Tensor 'csv_rows:0' shape=(?,) dtype=string>})
return tf.contrib.learn.InputFnOps(
    process_features(features),
    None,
    receive_tensor
)

I have deployed the model in the cloud ML. Now I have to do online prediction.To do so gcloud ml-engine predict --model-dir=<model_name> --version <version> --json-instances=test.json --project <project_name>

When I run the above command, It shows the following error

{ "error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"NodeDef mentions attr 'select_cols' not in Op output:; attr=OUT_TYPE:list(type),min=1,allowed=[DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64, DT_STRING]; attr=field_delim:string,default=\",\"; attr=use_quote_delim:bool,default=true; attr=na_value:string,default=\"\">; NodeDef: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,1], [?,1], [?,1]], field_delim=\",\", na_value=\"\", select_cols=[], use_quote_delim=true, _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"](ExpandDims, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_0). (Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary.).\n\t [[Node: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,1], [?,1], [?,1]], field_delim=\",\", na_value=\"\", select_cols=[], use_quote_delim=true, _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"](ExpandDims, DecodeCSV/record_de...TRUNCATED\")" }

I know tf.contrib.learn.InputFnOps has been deprecated but still out of curiosity I wonder if there is any way to do prediction. My test.json data look like this

       {"csv_rows":"7.0,8.0,7.0"}
       {"csv_rows":"5.0,6.0,4.0"}

I have trained the model using this data Train dataset.

Upvotes: 0

Views: 716

Answers (1)

Lak
Lak

Reputation: 4166

Your test.json has to have exactly one instance per line. In your code, you are reading csv_rows as a string and decoding it as a CSV, so this is what your code is expecting in test.json:

{"csv_rows":"7.0,8.0,7.0"}
{"csv_rows":"5.0,6.0,4.0"}

If you want to be able to provide:

{"renancy":"9.0","freq":"3.0","monetary":"5.0"}
{"renancy":"5.0","freq":"6.0","monetary":"4.0"}

Then, your serving code has to change to:

def serving_input_fn():
    feature_placeholders = {
        'renancy': tf.placeholder(tf.float32, [None]),
        'freq': tf.placeholder(tf.float32, [None]),
        'monetary': tf.placeholder(tf.float32, [None])
    }
    features = features_placeholders
    return tf.estimator.export.ServingInputReceiver(features, feature_placeholders)

Upvotes: 1

Related Questions