PyTorch is a freely available machine learning library that can be imported and used inside the code for performing machine learning operations based on requirements. The front-end api is written in Python and the tensor operations are implemented using C++. It is developed by Facebook's AI Research Lab (FAIR). It is easy to use, adaptive (provides flexibility) and most importantly it poses dynamic computation graph capability (providing graphs based on the input in run time at that instant).
- Overview of Vertex AI
- Installation of libraries
- Implement Pytorch Model
- Flask Application and Pytorch Model
- Dockerizing the Flask Application
- Setting up Google Cloud Environment
- Push the Dockerized Flask Image to GCP Container Registry
- Deploy the GCP Container to GCP Vertex AI
- Test and Monitor the Deployed Container
Overview of Vertex AI
Vertes AI is a service which is provided by Google Cloud Platform (GCP) which allow developers to deploy the machine learning model , to build the machine learning model and most importantly to scale it up very conveniently. It comprises of various tools and services that a developer can access and can efficiently manage the the machine learning model from building the model to deployment of the model to scaling of the model everything can be done inside the service provided by Vertex AI.
Terminologies related to Vertex AI
Model Artifact :- when a machine learning model produce files and data in the training process it is known as Model Artifact . It is required because without this Artifact the model after the training phase cannot be deployed in the production or it cannot be used.
Vertex AI Model Registry :- It basically acts as a overall container (repository) for storing and managing the various type of machine learning model and developers can access it throughout the development phase.
Google Cloud Storage (GCS) :- This is also a service provided by the google in which it offers a scalable storage option to store data on demand basis and charge accordingly. It can handle a huge volume of data also as it is scalable and efficient.
Containerization :- It means that the dependencies and the application are packed into a container and this container performs in all possible computing environment which assure that it should perform same regardless of the its deployment environment.
Model Endpoint :- It is basically a dedicated URL or network location which can be accessed and can be use for making prediction for the deployed machine learning model. It plays a important role as it help in sending data to the model and receiving result from it which a client can do by accessing the endpoints.
Installation of the required library
Let's add the required libraries to the requirements.txt file.
Flask==3.0.3
Flask-Cors==4.0.1
numpy==2.0.0
torch==2.3.1
gunicorn==22.0.0
By using the pip install command one can install the libraries mentioned in the requirements.tx file. The command is as follows:
pip install -r requirements.txt
Since we are dockerizing the application, we will mention the above installation command in the Dockerfile.
Implementation of the PyTorch model
Let's implement a Pytorch model by applying a linear transformation to the incoming data. We can make use of one nn.Lienar model, one of the fundamental component of PyTorch. The code is as follows:
Python
import torch
import torch.nn as nun
class SimpleModel(nun.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.linear = nun.Linear(10, 1)
def forward(self, x):
return self.linear(x)
The nn.Linear applies a linear transformation to input data using weight and biases. The module takes two parameters called in_features
and out_features. These parameters
represent the number of input and output features. Upon object creation, it randomly initializes a weight matrix and a bias vector.
Let's try a sample prediction before we save the model.
Python
model= SimpleModel()
model.linear
Output
Linear(in_features=10, out_features=1, bias=True)
Here we have iniatialzed a linear Pytorch model. Now let's create a random input data of same matrix and make the prediction
Python
x = torch.randn(1, 10)
t1 = x.to(torch.float)
with torch.no_grad():
prediction = model(t1).tolist()
prediction
Output
[[-0.26785045862197876]]
So our model works fine. Next we can save our model so that our flask application can load it and make prediction.
Saving the PyTorch model
The model can be saved by using the following code
Python
model= SimpleModel()
torch.save(model.state_dict(),'model.pth')
Flask Application and Pytorch Model
As a next step, we need to create a flask application and load the Pytorch model. Finally, one can make predictions using the model by invoking a REST API.
Create a Flask Application
Let's create a directory called 'app'. Inside 'app' folder we can create a main.py file. The file contains the code to create flask application. The main.py file is as follows:
Python
from flask import (
Flask, request, jsonify)
from flask_cors import CORS
@app.route('/health', methods=['GET'])
def health():
return jsonify(status='healthy'), 200
@app.route('/predict', methods=['POST'])
def predict():
return None
app = Flask(__name__)
CORS(app)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Here we created a basic structure needed for a FLASK application. Now the predict() method does nothing. To make it functional we need to load our Pytorch model and make predictions when the user invokes the REST API ('/predict'). Also, we created a health monitoring API, which is used to check the health of the deployed model. The /health route is mentioned while creating the endpoint in the GCP Vertex AI.
Loading the Pytorch model in Flask
To load the Pytorch model, it is necessary to mention the linear module in our Flask application. The code is as follows:
Python
import torch
import torch.nn as nun
# linear module
class SimpleModel (nun.Module):
def __init__(self):
super(SimpleModel, self). __init__()
self.linear = nun.Linear(10, 1)
def forward(self,x):
return self.linear(x)
# initialize the module
model = SimpleModel()
# load the module
model.load_state_dict(torch.load('model.pth'))
Here we created a linear Pytorch model and loaded the saved Pytorch model. Now we can construct the predict() method.
Python
@app.route('/predict', methods=['POST'])
def predict():
data = request.json['inputs']
data = torch.tensor(data)
with torch.no_grad():
prediction = model(data).tolist()
return jsonify(prediction=prediction)
The complete code is as follows:
Python
from flask import (
Flask, request, jsonify)
from flask_cors import CORS
import torch
import torch.nn as nun
# linear module
class SimpleModel (nun.Module):
def __init__(self):
super(SimpleModel, self). __init__()
self.linear = nun.Linear(10, 1)
def forward(self,x):
return self.linear(x)
# initialize the module
model = SimpleModel()
# load the module
model.load_state_dict(torch.load('model.pth'))
@app.route('/health', methods=['GET'])
def health():
return jsonify(status='healthy'), 200
@app.route('/predict', methods=['POST'])
def predict():
data = request.json['inputs']
data = torch.tensor(data)
with torch.no_grad():
prediction = model(data).tolist()
return jsonify(prediction=prediction)
app = Flask(__name__)
CORS(app)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Dockerizing the Flask Application
To dockerize the flask application, it is necessary to create a docker file with necessary installation and running commands.
Creation of a Dockerfile
You need to create a Dockerfile in the same folder where 'app' directory is mentioned. The commands in the Dockerfile is as follows.
FROM python:3.9-slim
# Install libraries
COPY ./requirements.txt ./
RUN pip install -r requirements.txt && \
rm ./requirements.txt
# container directories
RUN mkdir /app
# Copy app directory (code and Pytorch model) to the container
COPY ./app /app
# run server with gunicorn
WORKDIR /app
EXPOSE 8080
CMD ["gunicorn", "main:app", "--timeout=0", "--preload", \
"--workers=1", "--threads=4", "--bind=0.0.0.0:8080"]
Now we need to build a docker conatiner based on the above docker file. Before that lets check the directory structure. The directory structure is as follows:
app
|-- main.py
|-- model.pth
Dockerfile
requirements.txt
The app directory contains our flask based python code (main.py) and the Pytorch model (model.pth).
Build a Docker Container
To build a docker container you need to execute the command below:
docker build -t flask-torch-docker .
The above command will execute the Dockerfile and build a docker named 'flask-torch-docker'
Run a Docker Container
Let's run the 'flask-torch-docker' docker using below command
docker run -it -p 8080:8080 flask-torch-docker
Testing a Docker Container Locally
It can be tested by using the curl command, below is the mentioned code.
curl -X POST http://localhost:8080/predict \
-H "Content-Type: application/json" \
-d '{"inputs": [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]]}'
Output
{ "prediction": 0.785}
The command prompt screenshot is as follows:
Push the dockerized flask image (Pytorch model) to GCP
In the above steps we created a Pytorch model using Flask, dockerized it and ensured that the dockerized application is working locally. Now it's time to move to the crucial step, which is deploying the Pytorch model on Vertex AI. In this article we will deploy our dockerized image to the Google Cloud Registry (container) and then later deploy the container to VertexAI. As a first step we need to set up Google Cloud Environment
Setting up Google Cloud Environment
To set up google cloud environment, the user needs to create an account or sign in through the google account and add the payment details and after that user have the access to Google Cloud CLI (for managing the resources and services ). You can create a Google cloud project and install the gcloud CLI. Now we can focus on how to push our dockerised image to Google Cloud Registry (GCR).
Steps to push dockerized image to GCR
Let's look at the steps to push the dockerized image to GCR. The steps are as follows:
Step 1: Initializing the google cloud SDK (software development kit )
gcloud init
Step 2: Setting up Docker to authenticate requests to GCR (Google Cloud Registry) using the gcloud command-line tool
gcloud auth configure-docker
Step 3: Building the Docker image
docker build -t flask-torch-docker:latest .
Step 4: Add your GCP project ID and the preferred region to the Docker image
docker tag flask-torch-docker:latest gcr.io/your-project-id/flask-torch-docker:latest
In the above command replace the your-project-id with the project ID of your system. You can use the below command to get all the project IDs.
gcloud projects list
The above command will list the project IDs from where you can choose and replace it there and run the command.
Step 5: Pushing the Docker image to GCR (Google Cloud Registry )
docker push gcr.io/your-project-id/flask-torch-docker:latest
Same in the above command replace the project ID of your system in place of your-project-id.
The pushed image can be checked under the Artifact RegistryDeploying the GCP container to Vertex AI
Now we have deployed our dockerized Py torch model to the Google Cloud Registry, which acts as a container. Next step is to deploy the container to Vertex AI. You can login in to your google cloud account and search for Vertex AI. The page is as shown below:
This is the home page of Vertex AI , after this you can click on enable all APIs below.Import the Model Using Model Registry
To import the model, you can choose the model registry functionality from Vertex AI. The model registry page is as follows:
After clicking on the model registryHere user can create a new model for registry or can import the model model from container registry or artifact registry. In this article we will deploy the model by importing the container from artifact registry and provide necessary model setting details. The steps are as follows:
Step 1: Create new model and provide appropriate name and region
Here we create a new model (for existing model, you can update the version) and provide a name and appropriate region.
Step 2: Import an existing custom container
import custom container from artifact registryHere we choose the option to import the existing custom conatiner from artifact registry and browse the container from artifact registry, which has the dockerized Pytorch model (flask application).
Step 3: Provide Model Setting details
setting detailsYou can set the prediction route as /predict and port as 8080 (as mentioned in dockerized flask app). For Health route you can mention it as "/health".
Step 4: Click the 'Import model' to create the model in the Model Registry
Finally, one can click the Import model to create the model in the Model Registry.
Define the endpoint and Deploy the Model
In the above steps, we created a model in the model registry. Now we need to define the endpoint and deploy the model. In the Model Registry we have the option to Deploy the endpoint. Select the Endpoints from navigation menu and the click on create and then configure it.
Step 1: Enter the model name and select the region
After clicking on Deploy End-Points.Step 2: Mention details in Model Settings
In the Model Setting first set the Traffic split.
Then set the number of computing node and click on Done at the bottom sideHere we set the traffic split and also set the number of computing node.
Step 3: Deploy the model
After configuring the necessary endpoint details you can deploy the model by clicking on 'DEPLOY TO ENDPOINT'.
After deploying the model it will be displayed After creating the endpoints click on deploy then select the model name and configure the rest setting according to the requirement and click on deploy.
Testing the Endpoints and Monitoring the Model
For testing the Endpoints you can use the following curl command.
curl -X POST https://<your-endpoint-url>/predict \
-H "Content-Type: application/json" \
-d '{"inputs": [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]]}'
Here replace the "your-endpoint-URL" with your system endpoint URL and then run the command it will return a JSON output.
{
"prediction": 0.785
}
For monitoring the Deployed model user can navigate to deploy and use and there choose monitoring and the following page will appear where user can monitor the deployed model and also can configure the monitoring according to the user convenience .
After clicking on monitoring
Monitoring the Deployed Model.Additional features of Vertex AI
- User can customize the labels of the dataset and set other parameter and after that deploying the model as per the prizing of google cloud vertex AI service and it will be successfully deployed.
- Apart from this there are other services which are provided like workbench , colab Enterprise , etc. to customize and to Collaboratory work together.
Applications
- It can be used in classification of any skin related diseases and other different variety of diseases and correctly identifying it.
- It can be used in prediction overall weather forecast for the upcoming years and the expected values ranges.
- It can be used in self driven cars by providing quality data set which will help in learning the trajectory of paths and smooth operations can be performed.
Similar Reads
Data Science Tutorial Data Science is a field that combines statistics, machine learning and data visualization to extract meaningful insights from vast amounts of raw data and make informed decisions, helping businesses and industries to optimize their operations and predict future trends.This Data Science tutorial offe
3 min read
Introduction to Machine Learning
What is Data Science?Data science is the study of data that helps us derive useful insight for business decision making. Data Science is all about using tools, techniques, and creativity to uncover insights hidden within data. It combines math, computer science, and domain expertise to tackle real-world challenges in a
8 min read
Top 25 Python Libraries for Data Science in 2025Data Science continues to evolve with new challenges and innovations. In 2025, the role of Python has only grown stronger as it powers data science workflows. It will remain the dominant programming language in the field of data science. Its extensive ecosystem of libraries makes data manipulation,
10 min read
Difference between Structured, Semi-structured and Unstructured dataBig Data includes huge volume, high velocity, and extensible variety of data. There are 3 types: Structured data, Semi-structured data, and Unstructured data. Structured data - Structured data is data whose elements are addressable for effective analysis. It has been organized into a formatted repos
2 min read
Types of Machine LearningMachine learning is the branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data and improve from previous experience without being explicitly programmed for every task.In simple words, ML teaches the systems to think and understand like h
13 min read
What's Data Science Pipeline?Data Science is a field that focuses on extracting knowledge from data sets that are huge in amount. It includes preparing data, doing analysis and presenting findings to make informed decisions in an organization. A pipeline in data science is a set of actions which changes the raw data from variou
3 min read
Applications of Data ScienceData Science is the deep study of a large quantity of data, which involves extracting some meaning from the raw, structured, and unstructured data. Extracting meaningful data from large amounts usesalgorithms processing of data and this processing can be done using statistical techniques and algorit
6 min read
Python for Machine Learning
Learn Data Science Tutorial With PythonData Science has become one of the fastest-growing fields in recent years, helping organizations to make informed decisions, solve problems and understand human behavior. As the volume of data grows so does the demand for skilled data scientists. The most common languages used for data science are P
3 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Introduction to Statistics
Statistics For Data ScienceStatistics is like a toolkit we use to understand and make sense of information. It helps us collect, organize, analyze and interpret data to find patterns, trends and relationships in the world around us.From analyzing scientific experiments to making informed business decisions, statistics plays a
12 min read
Descriptive StatisticStatistics is the foundation of data science. Descriptive statistics are simple tools that help us understand and summarize data. They show the basic features of a dataset, like the average, highest and lowest values and how spread out the numbers are. It's the first step in making sense of informat
5 min read
What is Inferential Statistics?Inferential statistics is an important tool that allows us to make predictions and conclusions about a population based on sample data. Unlike descriptive statistics, which only summarize data, inferential statistics let us test hypotheses, make estimates, and measure the uncertainty about our predi
7 min read
Bayes' TheoremBayes' Theorem is a mathematical formula used to determine the conditional probability of an event based on prior knowledge and new evidence. It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities ba
13 min read
Probability Data Distributions in Data ScienceUnderstanding how data behaves is one of the first steps in data science. Before we dive into building models or running analysis, we need to understand how the values in our dataset are spread out and thatâs where probability distributions come in.Let us start with a simple example: If you roll a f
8 min read
Parametric Methods in StatisticsParametric statistical methods are those that make assumptions regarding the distribution of the population. These methods presume that the data have a known distribution (e.g., normal, binomial, Poisson) and rely on parameters (e.g., mean and variance) to define the data.Key AssumptionsParametric t
6 min read
Non-Parametric TestsNon-parametric tests are applied in hypothesis testing when the data does not satisfy the assumptions necessary for parametric tests, such as normality or equal variances. These tests are especially helpful for analyzing ordinal data, small sample sizes, or data with outliers.Common Non-Parametric T
5 min read
Hypothesis TestingHypothesis testing compares two opposite ideas about a group of people or things and uses data from a small part of that group (a sample) to decide which idea is more likely true. We collect and study the sample data to check if the claim is correct.Hypothesis TestingFor example, if a company says i
9 min read
ANOVA for Data Science and Data AnalyticsANOVA is useful when we need to compare more than two groups and determine whether their means are significantly different. Suppose you're trying to understand which ingredients in a recipe affect its taste. Some ingredients, like spices might have a strong influence while others like a pinch of sal
9 min read
Bayesian Statistics & ProbabilityBayesian statistics sees unknown values as things that can change and updates what we believe about them whenever we get new information. It uses Bayesâ Theorem to combine what we already know with new data to get better estimates. In simple words, it means changing our initial guesses based on the
6 min read
Feature Engineering
Model Evaluation and Tuning
Data Science Practice