Getting batch predictions for TFrecords via CloudM

2020-02-13 05:23发布

问题:

I followed this great tutorial and successfully trained a model (on CloudML). My code also makes predictions offline, but now I am trying to use Cloud ML to make predictions and have some problems.

To deploy my model I followed this tutorial. Now I have a code that generates TFRecords via apache_beam.io.WriteToTFRecord and I want to make predictions for those TFRecords. To do so I am following this article, my command looks like this:

gcloud ml-engine jobs submit prediction $JOB_ID --model $MODEL --input-paths gs://"$FILE_INPUT".gz --output-path gs://"$OUTPUT"/predictions --region us-west1 --data-format TF_RECORD_GZIP

But I get only errors: 'Exception during running the graph: Expected serialized to be a scalar, got shape: [64]

It seems like it expect data in a different format. I found the format specs for JSON here, but couldn't find how to do it with TFrecords.

UPDATE: here is the output of saved_model_cli show --all --dir

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['prediction']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['example_proto'] tensor_info:
    dtype: DT_STRING
    shape: unknown_rank
    name: input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['probability'] tensor_info:
    dtype: DT_FLOAT
    shape: (1, 1)
    name: probability:0
  Method name is: tensorflow/serving/predict

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['example_proto'] tensor_info:
    dtype: DT_STRING
    shape: unknown_rank
    name: input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['probability'] tensor_info:
    dtype: DT_FLOAT
    shape: (1, 1)
    name: probability:0
  Method name is: tensorflow/serving/predict

回答1:

When you export your model, you need to make sure that it is "batchable", i.e., the outer dimension of the input Placeholder has shape=[None], e.g.,

input = tf.Placeholder(dtype=tf.string, shape=[None])
...

That may require reworking the graph a bit. For instance, I see that the shape of your output is hard-coded to [1,1]. The outermost dimension should be None, this may happen automatically when you fix the placeholder, or it may require other changes.

Given that the name of the output is probabilities, I would also expect the innermost dimension to be >1, i.e. the number of classes being predicted, so something like [None, NUM_CLASSES].