In machine learning projects, deploying models is a key link, and it is important to ensure that the model can run stably in different environments. As a containerization platform, Docker can help us package the machine learning model and all its dependencies into containers, ensuring that the model can run in a consistent manner in various environments.
Install Docker.
Build and train machine learning models.
Create a requirements.txt
file listing the dependencies.
Write Dockerfile
and build the image.
Use docker build
to build the image.
Use docker run
to run the container.
Push the image to Docker Hub for sharing.
First, you need to install Docker on your local machine. You can download and install the Docker version suitable for your operating system from the official Docker website . After the installation is complete, you can check whether the installation was successful by running the following command:
docker --version
We will use scikit-learn
and Python as an example to train a simple machine learning model and save it. Here, we train a random forest classifier using the Iris dataset.
model.py
:
from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier import pickle # Train and save the model def train_model(): #Load the data set data = load_iris() X, y = data.data, data.target #Training model model = RandomForestClassifier() model.fit(X, y) # Save model with open('model.pkl', 'wb') as f: pickle.dump(model, f) print("The model has been trained and saved as model.pkl") # Load the model and make predictions def predict(): # Load the trained model with open('model.pkl', 'rb') as f: model = pickle.load(f) # Predicted test data test_data = [5.1, 3.5, 1.4, 0.2] # Example features prediction = model.predict([test_data]) print(f"Prediction result: {test_data} => {int(prediction[0])}") if __name__ == '__main__': train_model() predict()
In this code:
train_model()
function loads the Iris data set, trains a random forest classifier, and saves the trained model as model.pkl
.
The predict()
function loads the saved model and uses a test data to make predictions.
requirements.txt
file In order for the Docker container to install all the dependencies required for the project, we need to create a requirements.txt
file listing the Python library dependencies. In this example, we only need scikit-learn
.
requirements.txt
:
scikit-learn
Dockerfile
is a script for building a Docker image, which defines how to configure and build the image. Here is an example Dockerfile
from our project:
Dockerfile
:
# Use Python base image FROM python:3.11-slim # Set the working directory WORKDIR /app in the container # Copy requirements.txt and model.py files to the container COPY requirements.txt requirements.txt COPY model.py model.py # Install dependencies RUN pip install -r requirements.txt # Command CMD executed when the container starts ["python", "model.py"]
FROM
: Specify the base image. We used python:3.11-slim
, which is a lightweight Python environment.
WORKDIR
: Set the working directory to /app
, and subsequent commands will be executed in this directory.
COPY
: Copy the requirements.txt
and model.py
files into the container.
RUN
: Install the dependencies listed in requirements.txt
.
CMD
: Specify the command to be executed when the container starts, here it is to run model.py
.
Enter the directory where Dockerfile
is located in the terminal, and then run the following command to build the Docker image:
docker build -t ml-model .
-t ml-model
: Specify a label (name) for the image, here we name it ml-model
.
.
: Indicates the current directory where the Dockerfile is located.
Once the build process is complete, you will see output similar to the following:
Successfully built <image ID> Successfully tagged ml-model:latest
Now, you can run the built Docker image and view the output of the model. Start the container with the following command:
docker run ml-model
This will start the container and execute the model.py
script. You should see output similar to the following:
The model is trained and saved as model.pkl Prediction result: [5.1, 3.5, 1.4, 0.2] => 0
If you want to share the built Docker image with others or team members, you can push it to Docker Hub.
First, you need to log in to Docker Hub:
docker login
After logging in, use the following command to label the image (the label is yourdockerhubusername/ml-model
):
docker tag ml-model yourdockerhubusername/ml-model
Then, push the image to Docker Hub:
docker push yourdockerhubusername/ml-model
Replace yourdockerhubusername
with your Docker Hub username.
After the push is completed, others can pull and run your image with the following command:
docker pull yourdockerhubusername/ml-model docker run yourdockerhubusername/ml-model
By using Docker, we can package the machine learning model with all dependencies into a container, ensuring that it can run consistently in any environment. Docker not only simplifies the model deployment process, but also ensures consistency between different development, testing and production environments, thereby improving the reliability and maintainability of model deployment.
AI courses are suitable for people who are interested in artificial intelligence technology, including but not limited to students, engineers, data scientists, developers, and professionals in AI technology.
The course content ranges from basic to advanced. Beginners can choose basic courses and gradually go into more complex algorithms and applications.
Learning AI requires a certain mathematical foundation (such as linear algebra, probability theory, calculus, etc.), as well as programming knowledge (Python is the most commonly used programming language).
You will learn the core concepts and technologies in the fields of natural language processing, computer vision, data analysis, and master the use of AI tools and frameworks for practical development.
You can work as a data scientist, machine learning engineer, AI researcher, or apply AI technology to innovate in all walks of life.