Machine Learning on Docker
Containerized ML
Before we begin lets answer the obvious: why are we doing this.
Today we see that all over the world institutes and universities are opening up undergraduate and postgraduate courses on Data Science, AI, ML, etc. But when companies try to apply these AI and ML projects, 85% of their projects fail. To solve it one of the steps can be containerization.
Suppose a developer has no knowledge of DevOps. Now if her/his model uses up too many resources in the bare metal server, it may slow it down making it pretty much unusable for some while and also waste time valuable to the business. Hence containerizing the model solves all of these as the container would take a second or lesser to restart which will decrease sown-time and also improve scalability.
Lets now begin containerizing a model. Follow along after you have installed Docker in your OS (preferably Linux).
yum install docker-ce --nobest -y
Containerizing ML model in 6 steps
Step 1: Pull the CentOS container image from Docker Hub and create a new container.
We are going to pull the CentOs image. To do that, type
docker pull centos:latest
Next, to create the container type in
docker run -dit --name ml_os centos:latest
Now run
docker ps
This will show the running container.
Step 2: Install the Python software on the top of docker container
Use this command to get inside the container’s terminal.
docker attach ml_os
Now when we are into the container install python using
yum install python3
Step 3: Create a model and save it
Now in your base OS create any type of Machine Learning Model of your choice. For example I have created a basic regression model.
Save it using joblib
Step 4: Start the container and copy the model inside it
To copy the model inside the container run
docker cp model.pkl ml_os:/root/model.pkl
Step 5: Install required libraries in the container
Install whatever libraries were used in creating your model. In our case they are numpy, pandas and sklearn.
pip3 install numpy pandas sklearn
Step 6: Create .py file to use the model
After installing the libraries create a program in python that uses this model and takes desired input and gives the output.
import joblib
mind = joblib.load(‘model.pkl’)
x = float(input(‘Per day average study hours: ’))
print(‘Marks obtained: {}’.format(round(mind.predict([[x]]))
Now that the model is set up on the container we can go about and create a DOCKERFILE and experiment other stuff.
Feel free to contact on my LinkedIn.