pythongoogle-cloud-platformgoogle-cloud-mlgoogle-prediction

Understanding inputs for google ai platform custom prediction routines


I am following this documentation on custom prediction routines and I am trying to understand how the inputs for custom prediction routine looks like. The code to send the input looks like this:

instances = [
        [6.7, 3.1, 4.7, 1.5],
        [4.6, 3.1, 1.5, 0.2],
    ]
service = discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)

if version is not None:
    name += '/versions/{}'.format(version)

response = service.projects().predict(
    name=name,
    body={'instances': instances}
).execute()

and the Predictor.py at the moment is very simple. I am just trying to understand how the input looks like...

class Predictor(object):
    """An example Predictor for an AI Platform custom prediction routine."""

    def __init__(self, model):
        self._model = model

    def predict(self, instances, **kwargs):

        inputs = np.asarray(instances)
        if kwargs.get('max'):
            return np.argmax(inputs, axis=1)

        return np.sum(inputs)


    @classmethod
    def from_path(cls, model_dir):
        return cls(None)

But when I try to get the response i get the following error:

{
  "error": "Prediction failed: unknown error."
}

Furthermore it is extremely difficult to debug the code, because there is no way to step into the code or print logs... I have no idea what's going on... How the input looks like? how should i access them? This is just a simple test, but eventually I want to send images, it will be even more difficult to debug then. How will I receive them? How will I preprocess them in the preprocessor? Let's assume that the proporcessing i have done at training time looks like this

data = cv2.imread(str(img_path))
data = cv2.resize(data, (224, 224))
data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
x = data.astype(np.float32) / 255.
return np.expand_dims(x, axis=0)

How the instances object looks like so i can construct the preprocessor accordingly? thank you in advance.


Solution

  • It looks like that using debug code (at the time of this post) without a model do not work. I used the following code to have everything worked for my image prediction use case:

    image_filename = 'your image path'
    PROJECT_ID = ''
    MODEL_NAME = ''
    VERSION_NAME = ''
    
    img = base64.b64encode(open(image_filename, "rb").read()).decode()
    image_bite_dict = {"key": "0", "image_bytes": {"b64": img}}
    
    instances = [
                image_bite_dict
            ]
    
    
    service = googleapiclient.discovery.build('ml', 'v1')
        name = 'projects/{}/models/{}/versions/{}'.format(PROJECT_ID, MODEL_NAME, VERSION_NAME)
    response = service.projects().predict(
            name=name,
            body={'instances': instances}
        ).execute()