Allen Wu
Allen Wu

Reputation: 325

Make API calls to model deployed on GCP

We have trained a Nasnet model on GCP, and deployed it so that API calls can be made.

The model takes an image as input (numpy array), and returns an array of predictions. However, when we try to make API calls to the model sending a numpy array, an error occurs (Request payload size exceeds the limit). Another commonly accepted format is base64, however our model is only configured to take in a numpy array as input.

Is there any way to fix this issue so that predictions can be made with API calls? Would creating a serverless function help? Thanks

EDIT: Here is the code I'm using the make a request to the GCP model:

import googleapiclient.discovery
import numpy as np
import requests

# x is an image encoded as a numpy array
def generate_custom_tags(x):

    project_id = "MY_PROJECT_ID"
    model_id = "MY_MODEL_NAME"
    model_path = "projects/{}/models/{}".format(project_id, model_id)
    ml_resource = googleapiclient.discovery.build("ml","v1").projects()
    instances = { 'input_2': x.tolist() }

    input_data_json = {"signature_name": "serving_default", "instances": instances}
    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["dense"] for pred in response["predictions"]])

Upvotes: 0

Views: 183

Answers (1)

Rajeevan
Rajeevan

Reputation: 150

The “Request payload size exceeds the limit” error is due to a hard limit for the Cloud Machine Learning Engine API. There's a feature request to increase this limit which you can follow for updates here [1]. In the meantime, try using the following solution as it is similar to your case [2].

[1] https://issuetracker.google.com/123314535

[2] gcloud ml-engine returns error on large files

Upvotes: 1

Related Questions