Getting started with your own model#
This guide will show you how to put your own machine learning model in a Docker image using Cog. If you haven't got a model to try out, you'll want to follow the main getting started guide.
Prerequisites#
- macOS or Linux. Cog works on macOS and Linux, but does not currently support Windows.
- Docker. Cog uses Docker to create a container for your model. You'll need to install Docker before you can run Cog.
Initialization#
First, install Cog if you haven't already:
macOS (recommended):
brew install replicate/tap/cog
Linux or macOS (manual):
sudo curl -o /usr/local/bin/cog -L https://github.com/replicate/cog/releases/latest/download/cog_`uname -s`_`uname -m`
sudo chmod +x /usr/local/bin/cog
To configure your project for use with Cog, you'll need to add two files:
cog.yamldefines system requirements, Python package dependencies, etcrun.pydescribes the run interface for your model
Use the cog init command to generate these files in your project:
$ cd path/to/your/model
$ cog init
Define the Docker environment#
The cog.yaml file defines all the different things that need to be installed for your model to run. You can think of it as a simple way of defining a Docker image.
For example:
build:
python_version: "3.13"
python_requirements: requirements.txt
With a requirements.txt containing your dependencies:
torch==2.6.0
This will generate a Docker image with Python 3.13 and PyTorch 2 installed, for both CPU and GPU, with the correct version of CUDA, and various other sensible best-practices.
To run a command inside this environment, prefix it with cog exec:
$ cog exec python
✓ Building Docker image from cog.yaml... Successfully built 8f54020c8981
Running 'python' in Docker with the current directory mounted as a volume...
────────────────────────────────────────────────────────────────────────────────────────
Python 3.13.x (main, ...)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is handy for ensuring a consistent environment for development or training.
With cog.yaml, you can also install system packages and other things. Take a look at the full reference to see what else you can do.
Define how to run your model#
The next step is to update run.py to define the interface for running your model. The run.py generated by cog init looks something like this:
from cog import BaseRunner, Path, Input
import torch
class Runner(BaseRunner):
def setup(self):
"""Load the model into memory to make running multiple inferences efficient"""
self.net = torch.load("weights.pth")
def run(self,
image: Path = Input(description="Image to enlarge"),
scale: float = Input(description="Factor to scale image by", default=1.5)
) -> Path:
"""Run the model"""
# ... pre-processing ...
output = self.net(input)
# ... post-processing ...
return output
Edit your run.py file and fill in the functions with your own model's setup and run code. You might need to import parts of your model from another file.
You also need to define the inputs to your model as arguments to the run() function, as demonstrated above. For each argument, you need to annotate with a type. The supported types are:
str: a stringint: an integerfloat: a floating point numberbool: a booleancog.File: a file-like object representing a file (deprecated — usecog.Pathinstead)cog.Path: a path to a file on disk
You can provide more information about the input with the Input() function, as shown above. It takes these basic arguments:
description: A description of what to pass to this input for users of the modeldefault: A default value to set the input to. If this argument is not passed, the input is required. If it is explicitly set toNone, the input is optional.ge: Forintorfloattypes, the value should be greater than or equal to this number.le: Forintorfloattypes, the value should be less than or equal to this number.min_length: Forstrtypes, the minimum length of the string.max_length: Forstrtypes, the maximum length of the string.regex: Forstrtypes, the string must match this regular expression.choices: Forstrorinttypes, a list of possible values for this input.deprecated: Mark this input as deprecated with a message explaining what to use instead.
There are some more advanced options you can pass, too. For more details, take a look at the run interface documentation.
Next, add the line run: "run.py:Runner" to your cog.yaml, so it looks something like this:
build:
python_version: "3.13"
python_requirements: requirements.txt
run: "run.py:Runner"
That's it! To test this works, try running the model:
$ cog run -i [email protected]
✓ Building Docker image from cog.yaml... Successfully built 664ef88bc1f4
✓ Model running in Docker image 664ef88bc1f4
Written output to output.png
To pass more inputs to the model, you can add more -i options:
$ cog run -i [email protected] -i scale=2.0
In this case it is just a number, not a file, so you don't need the @ prefix.
Using GPUs#
To use GPUs with Cog, add the gpu: true option to the build section of your cog.yaml:
build:
gpu: true
...
Cog will use the nvidia-docker base image and automatically figure out what versions of CUDA and cuDNN to use based on the version of Python, PyTorch, and Tensorflow that you are using.
For more details, see the gpu section of the cog.yaml reference.
Next steps#
Next, you might want to take a look at: