Hello custom training: Serve predictions from a custom image classification model Stay organized with collections Save and categorize content based on your preferences.
This page walks through serving predictions from your image classification modeland viewing these predictions in a web app.
This tutorial has several pages:Serving predictions from a custom imageclassification model.
Each page assumes that you have already performed the instructions from theprevious pages of the tutorial.
The rest of this document assumes that you are using the same Cloud Shellenvironment that you created when following thefirst page of thistutorial. If your original Cloud Shell session is nolonger open, you can return to the environment by doing the following:In the Google Cloud console, activate Cloud Shell.
In the Cloud Shell session, run the following command:
cdhello-custom-sample
Create an endpoint
To get online predictions from the ML model that you trained when followingthe previous page of this tutorial, create a Vertex AIendpoint.Endpoints serve online predictions from one or more models.
In the Google Cloud console, in the Vertex AI section, go totheModels page.
Find the row of the model that you trained in theprevious step of thistutorial,
hello_custom, and click the model'sname to open the model detail page.On theDeploy & test tab, clickDeploy to endpoint to open theDeploy to endpoint pane.
On theDefine your endpoint step, add some basic information for yourendpoint:
SelectCreate new endpoint.
In theEndpoint name field, enter
hello_custom.In theModel settings section, ensure that you see the name of yourmodel, which is also called
hello_custom. Specify the following modelsettings:In theTraffic split field, enter
100. Vertex AIsupports splitting traffic for an endpoint to multiple models, butthis tutorial doesn't use that feature.In theMinimum number of compute nodes field, enter
1.In theMachine type drop-down list, selectn1-standard-2 fromtheStandard section.
ClickDone.
In theLogging section, ensure that both types of prediction loggingare enabled.
ClickContinue.
On theEndpoint details step, confirm that your endpoint will be deployedto
us-central1 (Iowa).Do not select theUse a customer-managed encryption key (CMEK) checkbox.This tutorial does not useCMEK.
ClickDeploy to create the endpoint and deploy your model to theendpoint.
After a few minutes,
Deploy a Cloud Run function
You can get predictions from the Vertex AI endpoint that you justcreated by sending requests to the Vertex AI API's REST interface. However, onlyprincipals with theaiplatform.endpoints.predictpermission can send online prediction requests. Youcannot make the endpoint public for anybody to send requests to, for example viaa web app.
In this section, deploy code toCloud Run functions to handleunauthenticated requests. The sample code that you downloaded when you read thefirst page of this tutorial contains code for thisCloud Run function in thefunction/ directory. Optionally, run thefollowing command to explore the Cloud Run function code:
lessfunction/main.pyDeploying the function serves the following purposes:
Youcan configure a Cloud Run function to receive unauthenticatedrequests. Additionally, functions run usinga service account with the Editorrole by default, which includesthe
aiplatform.endpoints.predictpermission necessary to get predictionsfrom your Vertex AI endpoint.This function also performs useful preprocessing on requests. TheVertex AI endpoint expects prediction requests in the formatof the trained TensorFlow Keras graph's first layer: a tensor of normalizedfloats with fixed dimensions. The function takes the URL of an image as inputand preprocesses the image into this format before requesting a predictionfrom the Vertex AI endpoint.
To deploy the Cloud Run function, do the following:
In the Google Cloud console, in the Vertex AI section, go totheEndpoints page.
Find the row of the endpoint that you created in the previous section, named
hello_custom. In this row, clickSample request to open theSample request pane.In theSample request pane, find the line of shell code that matches thefollowing pattern:
ENDPOINT_ID="ENDPOINT_ID"
ENDPOINT_ID is a number that identifies this particular endpoint.
Copy this line of code, and run it in your Cloud Shell session toset the
ENDPOINT_IDvariable.Run the following command in your Cloud Shell session to deploy theCloud Run function:
gcloudfunctionsdeployclassify_flower\--region=us-central1\--source=function\--runtime=python37\--memory=2048MB\--trigger-http\--allow-unauthenticated\--set-env-vars=ENDPOINT_ID=${ENDPOINT_ID}
Deploy a web app to send prediction requests
Finally, host a static web app on Cloud Storage to get predictionsfrom your trained ML model. The web app sends requests to yourCloud Run function, which preprocesses them and gets predictions from theVertex AI endpoint.
Thewebapp directory of the sample code that you downloaded contains a sampleweb app. In your Cloud Shell session, run the following commandsto prepare and deploy the web app:
Set a couple of shell variables for commands in following steps to use:
PROJECT_ID=PROJECT_IDBUCKET_NAME=BUCKET_NAMEReplace the following:
- PROJECT_ID: Your Google CloudprojectID.
- BUCKET_NAME: The name of the Cloud Storage bucket thatyou created when following thefirst page of this tutorial.
Edit the app to provide it with the trigger URL of yourCloud Run function:
echo"export const CLOUD_FUNCTION_URL = 'https://us-central1-${PROJECT_ID}.cloudfunctions.net/classify_flower';"\ >webapp/function-url.jsUpload the
webappdirectory to your Cloud Storage bucket:gcloudstoragecpwebappgs://${BUCKET_NAME}/--recursiveMake the web app files that you just uploadedpubliclyreadable:
Note: Shells (like bash, zsh) sometimes attempt to expand wildcards in waysthat can be surprising. For more details, seeURI wildcards.gcloudstorageobjectsupdategs://${BUCKET_NAME}/webapp/**--add-acl-grant=entity=allUsers,role=READERYou can now navigate to the following URL to open web app and getpredictions:
https://storage.googleapis.com/BUCKET_NAME/webapp/index.html
Open the web app and click an image of a flower to see your ML model'sclassification of the flower type. The web app presents the prediction as alist of flower types and the probability that the image contains each type offlower.
Note: This web app gets predictions for images that were also included in thetraining dataset for the model. Therefore the model might appear moreaccurate than it actually is due tooverfitting.
In the following screenshot, the web app has already gotten one prediction and is in the process of sending another prediction request.

What's next
Follow thelast page of the tutorial to clean upresources that you have created.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.