Build and run the ML Models on Docker Container

MLOps series

In this article, We are going to see how to build the linear ML models and made them to run on top of a Docker container.

Contents:

  • Introduction to Containers
  • Introduction to Machine Learning
  • Docker installation on RHEL8
  • Build the models
  • Run the model on Container
  • Save models

Containers:

image.png

Whenever you heard about container, this picture come into your mind. Container concept is similar like this. In this picture, Containers are your running OS and the ship that carrying containers are your Docker Engine which launches, ships and manages the running OS.

In other words, Containers are the by-product of the Containerization concept and it comes into the market in the year 2013 for the replacement of existing technology ie. Virtualization.

image.png

Both Virtual Machines and Containers provide PaaS (Platform-As-A-Service). Platform as a Service allows users to focus on running applications instead of managing their infrastructure. With PaaS, the virtual infrastructure is provided and users can deploy any application on it, including web applications such as Nginx and ghost; custom services and more.

Benefits of Containers over Virtual Machines

  • Lightweight
  • Requires less memory space
  • Startup time in milliseconds
  • OS virtualization
  • All containers share the host OS

Use cases of Containers

  • Micro services
  • Internet Of Things
  • DevOps
  • Kubernetes
  • And much more

Introduction to Machine Learning:

image.png

Machine learning (ML) is the study of computer algorithms that improve automatically through experience and by the use of data. It is seen as a part of artificial intelligence.

Machine learning algorithms build a model based on sample data, known as "training data", in order to make predictions or decisions without being explicitly programmed to do so.

In this article, we are going to use existing datasets for the prediction of values and will find the weights/co-efficient.

Ok, Let's come to our practical part.

Here, We are going to use the data set which gives an average masses for women as a function of their height in a sample of American women of age 30–39 for building Machine Learning Models inside the Docker containers.

System Requirements:

  • RHEL8 Virtual Machine ( Here it is hosted on Oracle VirtualBox )

Docker Installation

In my Virtual Machine, Docker is already configured. If you want to configure it, follow these three steps namely

  • Yum repo configuration for Docker
  • Docker installation using Yum
  • Start the service/Daemon

Yum configuration:

First step is to configure the YUM for installing the Docker. In RHEL8, YUM is used to install the programs and its dependencies.

Yum repo location in RHEL8 is /etc/yum.repos.d. You have to go to this location, create one file with the extension of .repo and copy the contents as shown in below picture.

image.png

Docker installation:

After configured the Yum repo, you can install docker using below command.

yum install docker-ce --nobest

Start the service/Daemon:

Then you have to start the service/Daemon of Docker and enable it permanently after every reloads by running the below command.

systemctl enable docker --now

Let's come to our ML part.

The dataset contains the following variables. Namely,

  • Height (m)
  • Weight (kg)

To make it simpler and easier, this article is divided into three parts

  • Building ML model
  • Run the model in RHEL8
  • Saving the models
  • Prediction

Building ML model

Since the dataset has only one field, so we are going to use Simple Linear Regression model to predict the values and will find their respective weights/co-efficient.

For better understanding of code, I divided the code into three parts.

Dataset Upload

1.PNG Here, we used pandas library/module for uploading csv dataset.

Field assignment as features (X) and targets (y)

2.PNG

Feature must be in 2D array form since In real life, there will be multiple features used to get an output. Since we used pandas to upload the dataset, its property is pandas wireframe, so we for converting to 2D array, we have to convert wireframe to array by x.values code.

Model fit:

Next step is to create the mind/ model namely LinearRegression and we have to fit the values in it.

3.PNG

Save the model:

Final step is to save the model and its weights in order to save time by not running again and again after every reboot of OS/program by using joblib module.

4.PNG

Run the model in RHEL8:

First step is to pull the container container by running the following command. Here, we are going to use Centos image.

docker pull centos

5 docker pull.PNG

Then we have to create the container by running the following command.

docker run -it --name c1 -v /root/ML-SLR:/root centos

Here we are attaching Host's folder where the dataset and code is located to the docker container's root folder.

7.PNG

So, we can get the exact data which we stored in host's folder.

After creating container, we have to install python package and its libraries required to train our models.

yum install python3 -y

pip3 install pandas

pip3 install scikit-learn

9 module.PNG

Then we can run our model by entering the below code.

python3 ML.py

The weights will be saved in one file which mentioned in code with an extension of .pk1 Then we can run the below code in Python interpreter in order to predict the values or finding respective weights.

12 final.PNG

Finally we predict the values.

Thats it. Thank you all for your reads. Stay tuned to my article!!!