Prerequisites
Before you begin, make sure you have:- Cog installed - See the installation guide if you haven’t installed Cog yet
- Docker running - Cog uses Docker to create containers for your model
Create a project
First, let’s create a new directory for your Cog project:Define your model
Create requirements.txt
Create a
requirements.txt file with your Python dependencies:requirements.txt
Create predict.py
Create a This example creates a ResNet-50 image classifier that:
predict.py file that defines how predictions are run:predict.py
- Loads the model in
setup()(called once when the container starts) - Takes an image as input via the
predict()method - Returns the top 3 predicted categories with confidence scores
Run a prediction
Now let’s test the model by running a prediction:The first time you run
cog predict, it will build a Docker container with all your dependencies. This may take a few minutes. Subsequent predictions will be much faster.Build a Docker image
You can build your model into a standalone Docker image that serves predictions via HTTP:my-classifier:latest that includes:
- Your model code
- All dependencies
- An HTTP server for serving predictions
Serve predictions via HTTP
Run the Docker image to start an HTTP server:Push to a registry
To deploy your model, you can push it to a Docker registry. First, add the image name to yourcog.yaml:
cog.yaml
Next steps
Set up your own model
Learn how to adapt Cog for your own machine learning model
Deploy your model
Deploy your Cog model to production
YAML reference
Explore all configuration options in cog.yaml
Python API
Learn about the Predictor interface in detail