跳转到主要内容

机器学习容器

项目描述

Cog:机器学习容器

Cog是一个开源工具,允许您将机器学习模型打包成标准、生产就绪的容器。

您可以将打包的模型部署到自己的基础设施,或者部署到Replicate

亮点

  • 📦 无需痛苦的Docker容器。 编写自己的Dockerfile可能是一个令人困惑的过程。使用Cog,您只需用简单的配置文件定义环境,它就会为您生成一个包含所有最佳实践的Docker镜像:Nvidia基础镜像、依赖项的高效缓存、安装特定的Python版本、合理的环境变量默认值等等。

  • 🤬️ 不再有CUDA地狱。 Cog知道哪些CUDA/cuDNN/PyTorch/Tensorflow/Python组合是兼容的,并将为您正确设置一切。

  • 使用标准Python定义您模型的输入和输出。 然后,Cog会生成一个OpenAPI模式,并使用Pydantic验证输入和输出。

  • 🎁 自动HTTP预测服务器:使用FastAPI根据您模型的数据类型动态生成RESTful HTTP API。

  • 🥞 自动队列工作器:对于长时间运行的深度学习模型或批处理,最佳架构是使用队列。Cog模型可以轻松实现这一点。目前支持Redis,更多功能正在开发中。

  • ☁️ 云存储。 可以直接将文件读取和写入到Amazon S3和Google Cloud Storage。(即将推出。)

  • 🚀 随时可部署。 您可以将模型部署到任何运行Docker镜像的地方。您的自有基础设施或Replicate

工作原理

使用cog.yaml定义模型运行的Docker环境

build:
  gpu: true
  system_packages:
    - "libgl1-mesa-glx"
    - "libglib2.0-0"
  python_version: "3.12"
  python_packages:
    - "torch==2.3"
predict: "predict.py:Predictor"

使用predict.py定义如何对模型进行预测

from cog import BasePredictor, Input, Path
import torch

class Predictor(BasePredictor):
    def setup(self):
        """Load the model into memory to make running multiple predictions efficient"""
        self.model = torch.load("./weights.pth")

    # The arguments and types the model takes as input
    def predict(self,
          image: Path = Input(description="Grayscale input image")
    ) -> Path:
        """Run a single prediction on the model"""
        processed_image = preprocess(image)
        output = self.model(processed_image)
        return postprocess(output)

现在,您可以运行此模型的预测

$ cog predict -i image=@input.jpg
--> Building Docker image...
--> Running Prediction...
--> Output written to output.jpg

或,构建用于部署的Docker镜像

$ cog build -t my-colorization-model
--> Building Docker image...
--> Built my-colorization-model:latest

$ docker run -d -p 5000:5000 --gpus all my-colorization-model

$ curl http://localhost:5000/predictions -X POST \
    -H 'Content-Type: application/json' \
    -d '{"input": {"image": "https://.../input.jpg"}}'

我们为什么要构建这个?

研究人员要将机器学习模型推向生产非常困难。

解决方案的一部分是Docker,但让它正常工作非常复杂:Dockerfile、预处理/后处理、Flask服务器、CUDA版本。更多时候,研究人员必须与工程师坐下来才能部署这个该死的东西。

AndreasBen创建了Cog。Andreas曾在Spotify工作,在那里他使用Docker构建和部署机器学习模型的工具。Ben曾在Docker工作,创建了Docker Compose

我们意识到,除了Spotify之外,其他公司也使用Docker来构建和部署机器学习模型。例如Uber和其他公司已经建立了类似的系统。因此,我们正在创建一个开源版本,这样其他人也可以这样做。

如果您有兴趣使用它或想与我们合作,请联系我们。我们在Discord上或通过team@replicate.com给我们发邮件。

先决条件

  • macOS、Linux或Windows 11。Cog在macOS、Linux和Windows 11上工作,支持WSL 2
  • Docker。Cog使用Docker为您的模型创建一个容器。在您运行Cog之前,您需要安装Docker。如果您安装了Docker Engine而不是Docker Desktop,您还需要安装Buildx

安装

如果您使用macOS,您可以使用Homebrew安装Cog

brew install cog

您还可以使用我们的安装脚本下载并安装最新版本

# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)

# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)

# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh

您可以通过在终端中运行以下命令从GitHub直接手动安装Cog的最新版本

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

或者,您可以从源代码构建Cog并使用以下命令安装它

make
sudo make install

或者如果您在docker上

RUN sh -c "INSTALL_DIR=\"/usr/local/bin\" SUDO=\"\" $(curl -fsSL https://cog.run/install.sh)"

升级

如果您使用macOS并且之前使用Homebrew安装了Cog,请运行以下命令

brew upgrade cog

否则,您可以通过运行安装它时使用的相同命令来升级到最新版本。

下一步

需要帮助?

加入Discord上的#cog频道。

贡献者 ✨

感谢这些优秀的人们 (表情符号键)

Ben Firshman
Ben Firshman

💻 📖
Andreas Jansson
Andreas Jansson

💻 📖 🚧
Zeke Sikelianos
Zeke Sikelianos

💻 📖 🔧
Rory Byrne
Rory Byrne

💻 📖 ⚠️
Michael Floering
Michael Floering

💻 📖 🤔
Ben Evans
Ben Evans

📖
shashank agarwal
shashank agarwal

💻 📖
VictorXLR
VictorXLR

💻 📖 ⚠️
hung anna
hung anna

🐛
Brian Whitman
Brian Whitman

🐛
JimothyJohn
JimothyJohn

🐛
ericguizzo
ericguizzo

🐛
Dominic Baggott
Dominic Baggott

💻 ⚠️
Dashiell Stander
Dashiell Stander

🐛 💻 ⚠️
Shuwei Liang
Shuwei Liang

🐛 💬
Eric Allam
Eric Allam

🤔
Iván Perdomo
Iván Perdomo

🐛
Charles Frye
Charles Frye

📖
Luan Pham
Luan Pham

🐛 📖
TommyDew
TommyDew

💻
Jesse Andrews
Jesse Andrews

💻 📖 ⚠️
Nick Stenning
Nick Stenning

💻 📖 🎨 🚇 ⚠️
Justin Merrell
Justin Merrell

📖
Rurik Ylä-Onnenvuori
Rurik Ylä-Onnenvuori

🐛
Youka
Youka

🐛
Clay Mullis
Clay Mullis

📖
Mattt
Mattt

💻 📖 🚇
Eng Zer Jun
Eng Zer Jun

⚠️
BB
BB

💻
williamluer
williamluer

📖
Simon Eskildsen
Simon Eskildsen

💻
F
F

🐛 💻
Philip Potter
Philip Potter

🐛 💻
Joanne Chen
Joanne Chen

📖
technillogue
technillogue

💻
Aron Carroll
Aron Carroll

📖 💻 🤔
Bohdan Mykhailenko
Bohdan Mykhailenko

📖 🐛
Daniel Radu
Daniel Radu

📖 🐛
Itay Etelis
Itay Etelis

💻
Gennaro Schiano
Gennaro Schiano

📖

本项目遵循all-contributors规范。欢迎各种类型的贡献!

项目详情


发行历史 发行通知 | RSS源

下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

cog-0.9.24.tar.gz (919.9 kB 查看哈希值)

上传时间 源代码

构建分发

cog-0.9.24-py3-none-any.whl (57.7 kB 查看哈希值)

上传时间 Python 3

由以下机构支持