Predict value of single image after training model on TPU

Viewed 728

I still want to know how I can predict the value of an image after training the network, but it seems like it is not supported yet. Any idea for a workaround (taken from the mnist_tpu.py)?

  if mode == tf.estimator.ModeKeys.PREDICT:
    raise RuntimeError("mode {} is not supported yet".format(mode))

Besides Stackoverflow - anywhere else I can get support for the implementing my models using TPU?

3 Answers

Here is a Python program that sends an image to a TPU-trained model (ResNet in this case) and gets back a classification:

with tf.gfile.FastGFile('/some/path.jpg', 'r') as ifp:
    credentials = GoogleCredentials.get_application_default()
    api = discovery.build('ml', 'v1', credentials=credentials,
               discoveryServiceUrl='https://storage.googleapis.com/cloud-ml/discovery/ml_v1_discovery.json')

    request_data = {'instances':
      [
         {"image_bytes": {"b64": base64.b64encode(ifp.read())}}
      ]
    }
    parent = 'projects/%s/models/%s/versions/%s' % (PROJECT, MODEL, VERSION)
    response = api.projects().predict(body=request_data, name=parent).execute()
    print("response={0}".format(response))

Full code is here: https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/quests/tpu/flowers_resnet.ipynb

This article documents the process of writing a model for the Cloud TPU: https://medium.com/tensorflow/how-to-write-a-custom-estimator-model-for-the-cloud-tpu-7d8bd9068c26

According to the documentation, you can choose online or batch modes for prediction, but you can't select the target device. As stated, "the prediction service allocates resources to run your job."

The documentation says that prediction is performed by nodes. I thought I'd read somewhere that prediction nodes are always CPUs in the Google Compute Engine, but I can't find a clear reference.

Related