generative-models README

The project's README from github.com/Stability-AI/generative-models. As one markdown file: README.md.

Generative Models by Stability AI

sample1

News

May 20, 2025

QUICKSTART :

To run SV4D 2.0 on a single input video of 21 frames:

Notes:

python3.10 -m venv .generativemodels
source .generativemodels/bin/activate
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # check CUDA version
pip3 install -r requirements/pt2.txt
pip3 install .
pip3 install -e git+https://github.com/Stability-AI/datapipelines.git@main#egg=sdata

tile

July 24, 2024

QUICKSTART : python scripts/sampling/simple_video_sample_4d.py --input_path assets/sv4d_videos/test_video1.mp4 --output_folder outputs/sv4d (after downloading sv4d.safetensors and sv3d_u.safetensors from HuggingFace into checkpoints/)

To run SV4D on a single input video of 21 frames:

March 18, 2024

To run SV3D_u on a single image:

To run SV3D_p on a single image:

  1. Generate static orbit at a specified elevation eg. 10.0 : python scripts/sampling/simple_video_sample.py --input_path <path/to/image.png> --version sv3d_p --elevations_deg 10.0
  2. Generate dynamic orbit at a specified elevations and azimuths: specify sequences of 21 elevations (in degrees) to elevations_deg ([-90, 90]), and 21 azimuths (in degrees) to azimuths_deg [0, 360] in sorted order from 0 to 360. For example: python scripts/sampling/simple_video_sample.py --input_path <path/to/image.png> --version sv3d_p --elevations_deg [<list of 21 elevations in degrees>] --azimuths_deg [<list of 21 azimuths in degrees>]

To run SVD or SV3D on a streamlit server: streamlit run scripts/demo/video_sampling.py

tile

November 28, 2023

November 21, 2023

July 26, 2023

sample2

July 4, 2023

June 22, 2023

If you would like to access these models for your research, please apply using one of the following links: SDXL-0.9-Base model, and SDXL-0.9-Refiner. This means that you can apply for any of the two links - and if you are granted - you can access both. Please log in to your Hugging Face Account with your organization email to request access. We plan to do a full release soon (July).

The codebase

General Philosophy

Modularity is king. This repo implements a config-driven approach where we build and combine submodules by calling instantiate_from_config() on objects defined in yaml configs. See configs/ for many examples.

Changelog from the old ldm codebase

For training, we use PyTorch Lightning, but it should be easy to use other training wrappers around the base modules. The core diffusion model class (formerly LatentDiffusion, now DiffusionEngine) has been cleaned up:

Installation:

1. Clone the repo

git clone https://github.com/Stability-AI/generative-models.git
cd generative-models

2. Setting up the virtualenv

This is assuming you have navigated to the generative-models root after cloning it.

NOTE: This is tested under python3.10. For other python versions, you might encounter version conflicts.

PyTorch 2.0

# install required packages from pypi
python3 -m venv .pt2
source .pt2/bin/activate
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip3 install -r requirements/pt2.txt

3. Install sgm

pip3 install .

4. Install sdata for training

pip3 install -e git+https://github.com/Stability-AI/datapipelines.git@main#egg=sdata

Packaging

This repository uses PEP 517 compliant packaging using Hatch.

To build a distributable wheel, install hatch and run hatch build (specifying -t wheel will skip building a sdist, which is not necessary).

pip install hatch
hatch build -t wheel

You will find the built package in dist/. You can install the wheel with pip install dist/*.whl.

Note that the package does not currently specify dependencies; you will need to install the required packages, depending on your use case and PyTorch version, manually.

Inference

We provide a streamlit demo for text-to-image and image-to-image sampling in scripts/demo/sampling.py. We provide file hashes for the complete file as well as for only the saved tensors in the file ( see Model Spec for a script to evaluate that). The following models are currently supported:

Weights for SDXL:

SDXL-1.0: The weights of SDXL-1.0 are available (subject to a CreativeML Open RAIL++-M license) here:

SDXL-0.9: The weights of SDXL-0.9 are available and subject to a research license. If you would like to access these models for your research, please apply using one of the following links: SDXL-base-0.9 model, and SDXL-refiner-0.9. This means that you can apply for any of the two links - and if you are granted - you can access both. Please log in to your Hugging Face Account with your organization email to request access.

After obtaining the weights, place them into checkpoints/. Next, start the demo using

streamlit run scripts/demo/sampling.py --server.port <your_port>

Invisible Watermark Detection

Images generated with our code use the invisible-watermark library to embed an invisible watermark into the model output. We also provide a script to easily detect that watermark. Please note that this watermark is not the same as in previous Stable Diffusion 1.x/2.x versions.

To run the script you need to either have a working installation as above or try an experimental import using only a minimal amount of packages:

python -m venv .detect
source .detect/bin/activate

pip install "numpy>=1.17" "PyWavelets>=1.1.1" "opencv-python>=4.1.0.25"
pip install --no-deps invisible-watermark

To run the script you need to have a working installation as above. The script is then useable in the following ways (don't forget to activate your virtual environment beforehand, e.g. source .pt1/bin/activate):

# test a single file
python scripts/demo/detect.py <your filename here>
# test multiple files at once
python scripts/demo/detect.py <filename 1> <filename 2> ... <filename n>
# test all files in a specific folder
python scripts/demo/detect.py <your folder name here>/*

Training:

We are providing example training configs in configs/example_training. To launch a training, run

python main.py --base configs/<config1.yaml> configs/<config2.yaml>

where configs are merged from left to right (later configs overwrite the same values). This can be used to combine model, training and data configs. However, all of them can also be defined in a single config. For example, to run a class-conditional pixel-based diffusion model training on MNIST, run

python main.py --base configs/example_training/toy/mnist_cond.yaml

NOTE 1: Using the non-toy-dataset configs configs/example_training/imagenet-f8_cond.yaml, configs/example_training/txt2img-clipl.yaml and configs/example_training/txt2img-clipl-legacy-ucg-training.yaml for training will require edits depending on the used dataset (which is expected to stored in tar-file in the webdataset-format). To find the parts which have to be adapted, search for comments containing USER: in the respective config.

NOTE 2: This repository supports both pytorch1.13 and pytorch2for training generative models. However for autoencoder training as e.g. in configs/example_training/autoencoder/kl-f4/imagenet-attnfree-logvar.yaml, only pytorch1.13 is supported.

NOTE 3: Training latent generative models (as e.g. in configs/example_training/imagenet-f8_cond.yaml) requires retrieving the checkpoint from Hugging Face and replacing the CKPT_PATH placeholder in this line. The same is to be done for the provided text-to-image configs.

Building New Diffusion Models

Conditioner

The GeneralConditioner is configured through the conditioner_config. Its only attribute is emb_models, a list of different embedders (all inherited from AbstractEmbModel) that are used to condition the generative model. All embedders should define whether or not they are trainable (is_trainable, default False), a classifier-free guidance dropout rate is used (ucg_rate, default 0), and an input key (input_key), for example, txt for text-conditioning or cls for class-conditioning. When computing conditionings, the embedder will get batch[input_key] as input. We currently support two to four dimensional conditionings and conditionings of different embedders are concatenated appropriately. Note that the order of the embedders in the conditioner_config is important.

Network

The neural network is set through the network_config. This used to be called unet_config, which is not general enough as we plan to experiment with transformer-based diffusion backbones.

Loss

The loss is configured through loss_config. For standard diffusion model training, you will have to set sigma_sampler_config.

Sampler config

As discussed above, the sampler is independent of the model. In the sampler_config, we set the type of numerical solver, number of steps, type of discretization, as well as, for example, guidance wrappers for classifier-free guidance.

Dataset Handling

For large scale training we recommend using the data pipelines from our data pipelines project. The project is contained in the requirement and automatically included when following the steps from the Installation section. Small map-style datasets should be defined here in the repository (e.g., MNIST, CIFAR-10, ...), and return a dict of data keys/values, e.g.,

example = {"jpg": x,  # this is a tensor -1...1 chw
           "txt": "a beautiful image"}

where we expect images in -1...1, channel-first format.

generative-models (Stability-AI/generative-models)

Searching Database…

这是什么?这是 Stability AI 发布的一套研究代码,用于一系列生成式扩散模型,涵盖文生图(SDXL、SDXL-Turbo)、图生视频(Stable Video Diffusion)、图生3D视频(SV3D),以及视频生成4D内容(SV4D、SV4D 2.0)。
这是可以直接使用的应用,还是我要用 Python 脚本运行的研究代码?它是一个基于配置的 Python 库/框架(包名为 'sgm'):你需要对 YAML 配置调用 instantiate_from_config(),来构建并组合模型的子模块,然后运行随附的采样脚本生成输出。
怎样才能把模型跑起来?克隆代码仓库,创建一个 Python 虚拟环境,安装 'sgm' 包(如需训练,还要安装 'sdata'),然后从 Hugging Face 下载具体模型的 .safetensors 权重,放入 checkpoints/ 目录,并运行对应的采样脚本,例如 scripts/sampling/simplevideosample4d2.py。
这套代码需要什么 Python 版本和框架?代码在 Python 3.10 下测试通过,需要 PyTorch 2.0;使用其他 Python 版本可能会引发依赖冲突。
我能直接下载并使用任何一个模型吗?不能。有几个模型(如 SDXL-0.9)需要先在 Hugging Face 上提交访问申请,并绑定所在机构的邮箱,而且这些模型仅供研究用途,不适用于生产环境。
运行这些模型一定需要高配显卡吗?是的,这些扩散模型需要在 GPU 上进行推理。如果显存较小,可以通过降低编码/解码批次大小(--encodingt=1、--decodingt=1)或降低图像分辨率来减少显存占用。
SV3D 和 SV4D 有什么区别?SV3D 从单张图像生成新视角的环绕视频,而 SV4D 以现有视频为输入,在时间维度上合成新的相机视角,最终得到完整的 4D(视频加多视角)素材;SV4D 2.0 提升了保真度,并且不再需要 SV3D 生成的参考视角。
什么是 SDXL-Turbo?SDXL-Turbo 是一个速度极快的文生图模型,与一份技术报告一同发布;下载权重后,可以通过 Streamlit 演示(scripts/demo/turbo.py)使用。
在运行 SV3D/SV4D 之前,我能去掉输入视频里的背景噪声吗?可以。背景单一的素材可以使用 rembg 工具(--removebg=True);对于背景更杂乱的真实场景素材,可以在运行模型前使用 Clipdrop 或 SAM2 等工具。
有图形界面吗,还是必须使用命令行?大多数模型通过 Python 命令行脚本运行(例如 simplevideosample4d2.py),但也有一些提供 Streamlit 演示(scripts/demo/videosampling.py),或是社区开发的、可在本地使用的 Gradio 演示。
Size
86.6 MB
Stars
27.3K
Age
3y
Commits · 90 Days