[TOC]

1. 开发环境推荐

  1. GPU
FROM nvidia/cuda:11.7.1-cudnn8-runtime-ubuntu20.04
LABEL maintainer="Hugging Face"
LABEL repository="diffusers"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && \\\\
    apt install -y bash \\\\
                   build-essential \\\\
                   git \\\\
                   git-lfs \\\\
                   curl \\\\
                   ca-certificates \\\\
                   libsndfile1-dev \\\\
                   python3.8 \\\\
                   python3-pip \\\\
                   python3.8-venv && \\\\
    rm -rf /var/lib/apt/lists

# make sure to use venv
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# pre-install the heavy dependencies (these can later be overridden by the deps from setup.py)
RUN python3 -m pip install --no-cache-dir --upgrade pip && \\\\
    python3 -m pip install --no-cache-dir \\\\
        torch \\\\
        torchvision \\\\
        torchaudio && \\\\
    python3 -m pip install --no-cache-dir \\\\
        accelerate \\\\
        datasets \\\\
        hf-doc-builder \\\\
        huggingface-hub \\\\
        Jinja2 \\\\
        librosa \\\\
        numpy \\\\
        scipy \\\\
        tensorboard \\\\
        transformers \\\\
        omegaconf

CMD ["/bin/bash"]

  1. cpu
FROM ubuntu:20.04
LABEL maintainer="Hugging Face"
LABEL repository="diffusers"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && \\\\
    apt install -y bash \\\\
                   build-essential \\\\
                   git \\\\
                   git-lfs \\\\
                   curl \\\\
                   ca-certificates \\\\
                   libsndfile1-dev \\\\
                   python3.8 \\\\
                   python3-pip \\\\
                   python3.8-venv && \\\\
    rm -rf /var/lib/apt/lists

# make sure to use venv
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# pre-install the heavy dependencies (these can later be overridden by the deps from setup.py)
RUN python3 -m pip install --no-cache-dir --upgrade pip && \\\\
    python3 -m pip install --no-cache-dir \\\\
        torch \\\\
        torchvision \\\\
        torchaudio \\\\
        --extra-index-url <https://download.pytorch.org/whl/cpu> && \\\\
    python3 -m pip install --no-cache-dir \\\\
        accelerate \\\\
        datasets \\\\
        hf-doc-builder \\\\
        huggingface-hub \\\\
        Jinja2 \\\\
        librosa \\\\
        numpy \\\\
        scipy \\\\
        tensorboard \\\\
        transformers

CMD ["/bin/bash"]

2. 下载模型

$ git lfs install
$ git clone <https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main>

3.运行

from diffusers import StableDiffusionPipeline
import torch

model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

image.save("astronaut_rides_horse.png")

8W1mKC

kwBzFe

参考

  1. https://huggingface.co/runwayml/stable-diffusion-v1-5