Building Neural Networks

Dineshbathla
Google Cloud - Community
6 min readFeb 23, 2024

Image Classification using custom TensorFlow Keras using Vertex AI/ML Platform

Setting up the context

Suppose there are 10 thousand images of cats and dogs, and the task is to segregate and make a pile of cat’s images and dog’s images separately. Can a human being do that, technically yes, but is it going to be productive and efficient for a human to do that, the answer is no. Now, the same thing can be done efficiently and quickly if we train a machine or computer to do that. This whole method of teaching computers and processing data in Artificial intelligence is called neural networks.

Introduction to Neural Networks

A neural network is a method in artificial intelligence that teaches computers to process data in a way that is inspired by the human brain. It is a type of machine learning process, called deep learning, that uses interconnected nodes or neurons in a layered structure that resembles the human brain. It creates an adaptive system that computers use to learn from their mistakes and improve continuously.

​​ In our brain different neurons connect via Synapse and pass electrical signals to process the information, similarly, interconnected nodes in Neural networks pass information so that they can learn and model the relationships between input and output data that are nonlinear and complex in nature.

What’s coming next

Best way to learn about Neural Networks is by creating them. Lets build an Image classification Modal. I will walk you through using Vertex AI to train an image classification model and serve predictions using the model. In this Blog, we use Vertex AI’s custom training feature to run a TensorFlow Keras training application in one of Vertex AI’s prebuilt container environments. This custom training job trains a machine learning (ML) model to classify images of flowers by their type. After we train the ML model, I will show you how to create an endpoint and serve predictions from that endpoint to a web app. So let’s get started,

Step-1

Create a Cloud storage Bucket — Create a regional cloud storage bucket which will be used for rest of the steps like storing training code, model artifacts and Hosting the web app that gets predictions from your Vertex AI endpoint.

Creating Cloud Storage Bucket

Step-2

Download the sample code — use this command in the Cloud Shell to download the sample code

gsutil cp gs://cloud-samples-data/ai-platform/hello-custom/hello-custom-sample-v1.tar.gz — | tar -xzv

It will create a directory ‘hello-custom-sample” with four items in it and these are ,

  1. Trainer — A directory of TensorFlow Keras code for training the flower classification model.
  2. setup.py — A configuration file for packaging the trainer/ directory into a Python source distribution that Vertex AI can use.
  3. Function — A directory of Python code for a cloud function that can receive and preprocess prediction requests from a web browser, send them to Vertex AI, process the prediction responses, and send them back to the browser.
  4. Webapp — A directory with code and markup for a web app that gets flower classification predictions from Vertex AI.

Step 3

Create a custom training pipeline — Now, we will use the training package that we uploaded to Cloud Storage to run a Vertex AI custom training pipeline.

To create training pipeline, go to Vertex AI Training , and click on Train new Model

Select and fields as shown below and click continue,

Name the Model and click continue

Populate the fields as show below and click continue

Select the machine type and click continue

Populate the field as shown below, now click start training

Step 4

Create Endpoint — To get online predictions from the ML model that we trained when following the previous step 3, create a Vertex AI endpoint. Endpoints serve online predictions from models.

Go to Model Registry and click the name of the Model we just trained in last step. Go to “Deploy & Test” tab at the top. Click Deploy to Endpoint.

Name the Endpoint and enter other fields as shown below,

Click continue and enter the fields as show below

Now click done and continue and click Deploy as shown below

Wait for few minutes till deployment completes.

Step 5

Deploy Cloud Function — It serve tow purposes, it invokes the. Request from web app to vertex AI to get predictions. Also , performs useful preprocessing on requests. The Vertex AI endpoint expects prediction requests in the format of the trained TensorFlow Keras graph’s first layer: a tensor of normalized floats with fixed dimensions. The function takes the URL of an image as input and preprocesses the image into this format before requesting a prediction from the Vertex AI endpoint.

First, find the row of the endpoint that we just created in the previous step, named Test Endpoint. In this row, click Sample request to open the Sample request pane. Copy endpoint and past the command in the cloud shell. Make sure that you are in hello-custom-sample directory in cloud shell.

Now, run the following command in the cloud shell

gcloud functions deploy classify_flower \

— region=us-central1 \

— source=function \

— runtime=python37 \

— memory=2048MB \

— trigger-http \

— allow-unauthenticated \

— set-env-vars=ENDPOINT_ID=${ENDPOINT_ID}

Step 6

Deploy web application to get predictions — Finally we will host a static web app on Cloud Storage to get predictions from your trained ML model. The web app sends requests to your Cloud Function, which preprocesses them and gets predictions from the Vertex AI endpoint.

Run the following command in cloud shell,

PROJECT_ID=PROJECT_ID > Change your project id

BUCKET_NAME=BUCKET_NAME > change bucket name with the name of your bucket we create in step 1.

Now lets create a trigger URL using following command , run the cloud shell

echo “export const CLOUD_FUNCTION_URL = ‘https://us-central1-${PROJECT_ID}.cloudfunctions.net/classify_flower';" \> webapp/function-url.js

Now, Upload the webapp directory to your Cloud Storage bucket suing below command

gsutil -m cp -r webapp gs://${BUCKET_NAME}/

Lets make the web app files that you just uploaded publicly readable using below command

gsutil -m acl ch -u AllUsers:R gs://${BUCKET_NAME}/webapp/**

Finally you can open following URL to open web app and get predictions

https://storage.googleapis.com/your_bucket_name/webapp/index.html

Change the name of the bucket with the name of your bucket created in Step 1

Web page will look like below,

To get prediction on flower type, click any flower , below is the sample of prediction. You can get more images by clicking “Get Six new images” .

Open the web app and click an image of a flower to see your ML model’s classification of the flower type. The web app presents the prediction as a list of flower types and the probability that the image contains each type of flower.

So, this is the end of the blog, using step by step procedure given above, you can create your own Neural Network using TensorFlow keras. You can also change the images of flowers by animal images or even manufacturing parts etc.

Disclaimer: This is to inform readers that the views, thoughts, and opinions expressed in the text belong solely to the author, and not necessarily to the author’s employer, organization, committee or other group or individual.

--

--