Deploying Machine Learning Models With Docker
Why and how to use Docker images and containers to deploy ML models
Photo by Rahul Chakraborty on Unsplash
I wo n’t be talking about how to create machine learning or deep learning models here, there are plenty of articles, blog post, and tutorials on that subject and I would recommend checking out Machine Learning Mastery if that is what you ‘re looking for or if you ‘re looking to improve In this article, I will talk about preparing models so they can be deployed on any device and through any online deployment method.
Why do you use it ?
Data science models can be deployed anywhere and run on a few lines of code with the help of the containerization service, which allows for websites, APIs, databases, and, in our case, data science models to be deployed anywhere and run on a few lines of code.
The method has a faster startup time and does n’t take up as much memory as other methods. You can easily update your models and test the changes.
There is a machine learning model.
While each of your models will have their own quirks, the process usually follows these steps.
- Save your model by train.
- Data can be sent and predictions made with your model.
- There is an optional Docker-Compose file specified to your model.
- You can create and test a container of your model.
- You can deploy to application hosting service.
Saving and training your model.
Depending on what library/framework you are using, there are many ways to save your model.
- You will be using their built-in save model methods if you are using Tensorflow.
- Pytorch has its own save model method.
- If you ‘re creating a model from scratch, I recommend saving it in a pickle file and turning it into an object. This is how to do that in python.
- If your framework is not mentioned, I recommend searching for Saving models and selecting the first result.
You can create an application for your model.
The API can be created using many different services. I will be using Flask because it is easy to read and interpret into other languages and because it is written in Python, the primary language for creating models in the first place.
There is a template for creating an interface for your model.
The file needs to be created.
The model itself is not the most important part of this process. The container that hosts the model and API is created with the help of the Dockerfile. It may be tempting to add the code that creates/trains the model, but it is best practice for an image to have one purpose and that is to host the model.
This is a template for your model.
We are going to look at this step by step.
The first step is to add our base image, it ‘s pulled from a site called DockerHub. The image pulled can have its own pre-downloaded dependencies, just like the one you ‘re making here.
In the second step, we add the requirements text file to the base image. You need to add a requirements file if you do n’t know what it is. This file tells you what dependencies you have installed and how to load the model. This includes data processing libraries and your framework.
We install the new requirements ‘ dependencies. The text should be in the image.
We add the files to the image. You can add the API if you created it in the same folder as the model. It ‘s called dockerignore. The ignore file is the same as the ignore file. You should use gitignore to prevent files that are not being used in the model from being loaded.
The port will be exposed in step six.
The command is run to start the python.
The YML file can beComposed using the Docker.
The docker-compose file can make building the image and starting the container easier.
To give a quick explanation of this file, the version is the docker version, the services are the different applications for the app ( could be a website and database or just the API or website ), the build is where docker needs the build the service from, the ports are where the API will
The container and image are being built.
The image is a read-only file and is used to make the container that hosts the API. If you created a docker-compose, this step is easy. You can do both in one command.
You need to build the image and then start the container if you did n’t create a file.
build -t The name of the run is docker run — rm.
Make sure that everything runs correctly and that you can make a query.
The model container is being deployed.
There are a lot of options when it comes to deployment. There is a great overview of how Caprover works.