Build and Deploy Machine Learning Model In Docker
In this article, we will deploy and build a simple linear regression Machine learning model in docker container and further build a docker image that will predict values using the model that we created.
Why Containers for Machine Learning?
Docker allows to easily reproduce the working environment that is used to train and run the machine learning model anywhere. Docker allows packaging the code and dependencies into containers that can be ported to different servers even if it’s a different hardware or operating system. A training model can be developed on a local machine and be easily ported to external clusters with additional resources such as GPUs, more memory or powerful CPUs. It’s easy to deploy and make your model available to the globe by wrapping it into an API in a container.
Now, lets start..
Here, I’m using my RHEL-8 operating system.
As on my system Docker(community edition) already installed so I'll start docker service using following command.
systemctl start docker
To check the status of docker use given command:
systemctl status docker
Here, I’m going to create a simple python code for predicting the salary based on years of experience using simple linear regression algorithm in machine learning. I will be using a salary dataset (csv file), which you can get it from my GitHub repository mentioned in bottom:
https://github.com/lalita-rajpoot/Python-ML-Model-in-Docker.git
Here we use some python dependencies to run our code. To install it we use
pip3 install pandas
pip3 install sklearn
salary_pred.py
import pandas#extract content of dataset in an array
ds=pandas.read_csv('salaryData.csv')#separate feature and target
x=ds['YearsExperience'].values.reshape(30,1)
y=ds['Salary']from sklearn.linear_model import LinearRegression#train the model
model=LinearRegression()
model.fit(x,y)while True:exp=float(input( "Enter your years of experience ="))#exception handling
if(exp==0):
print("Better earn some experience first!!!")
else:
#predicting the value
predicted_value=model.predict([[exp]])
print("Predicted Salary of person with {} yrs of experience is".format(exp)+'\x1b[4;60;60m'+ " {}".format(predicted_value[0]) + " INR\n"+'\x1b[0m')
term=input("Type exit to terminate and press enter to continue: ")
if(term=="exit"):
print("Container exited..")
break
Now, we have to make this python file executable by running using following command:
chmod +x pythonfile.py
chmod +x salary_pred.py
Now we are ready to deploy our code to docker container to train and build linear regression ML model Using Dockerfile to build our own custom image that will predict the salary just by running the image. Dockerfile automates the process of docker image creation.
FROM centos
RUN yum install python3 -y
RUN pip3 install sklearn
RUN pip3 install pandas
COPY salary_pred.py /salary_pred.py
COPY SalaryData.csv /SalaryData.csv
RUN chmod +x /salary_pred.py
ENTRYPOINT ["python3", "salary_pred.py"]
This Dockerfile will build the image using centos image as its base image that i have already pulled. It will build image by copying all for required files and install python with all required dependencies.
To build the image use following command:
docker run -t imagename:version .
docker run -t model_prediction .
Finally, at the end to run our container having trained model inside it, we use following command:
docker run -it imagename:version
dokcer run -it model_prediction
Here, it’ll automatically take latest version of the image
So we have successfully predicted our model …..
I hope this article will help you ….
Thanks for your time to reading this article :)