{"id":13404248,"url":"https://github.com/CompVis/stable-diffusion","last_synced_at":"2025-03-14T09:30:55.102Z","repository":{"id":56061037,"uuid":"523379232","full_name":"CompVis/stable-diffusion","owner":"CompVis","description":"A latent text-to-image diffusion model","archived":false,"fork":false,"pushed_at":"2024-04-07T14:02:52.000Z","size":43676,"stargazers_count":65052,"open_issues_count":573,"forks_count":9805,"subscribers_count":551,"default_branch":"main","last_synced_at":"2024-04-10T07:08:52.574Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://ommer-lab.com/research/latent-diffusion-models/","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CompVis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-10T14:36:44.000Z","updated_at":"2024-07-20T07:58:23.717Z","dependencies_parsed_at":"2024-07-20T07:58:15.662Z","dependency_job_id":"029b7bdf-85df-4b53-9f0c-ec76ea15ea13","html_url":"https://github.com/CompVis/stable-diffusion","commit_stats":{"total_commits":27,"total_committers":10,"mean_commits":2.7,"dds":0.8148148148148149,"last_synced_commit":"21f890f9da3cfbeaba8e2ac3c425ee9e998d5229"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CompVis%2Fstable-diffusion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CompVis%2Fstable-diffusion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CompVis%2Fstable-diffusion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CompVis%2Fstable-diffusion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CompVis","download_url":"https://codeload.github.com/CompVis/stable-diffusion/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243553890,"owners_count":20309832,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:01:41.557Z","updated_at":"2025-03-14T09:30:55.073Z","avatar_url":"https://github.com/CompVis.png","language":"Jupyter Notebook","readme":"# Stable Diffusion\n*Stable Diffusion was made possible thanks to a collaboration with [Stability AI](https://stability.ai/) and [Runway](https://runwayml.com/) and builds upon our previous work:*\n\n[**High-Resolution Image Synthesis with Latent Diffusion Models**](https://ommer-lab.com/research/latent-diffusion-models/)\u003cbr/\u003e\n[Robin Rombach](https://github.com/rromb)\\*,\n[Andreas Blattmann](https://github.com/ablattmann)\\*,\n[Dominik Lorenz](https://github.com/qp-qp)\\,\n[Patrick Esser](https://github.com/pesser),\n[Björn Ommer](https://hci.iwr.uni-heidelberg.de/Staff/bommer)\u003cbr/\u003e\n_[CVPR '22 Oral](https://openaccess.thecvf.com/content/CVPR2022/html/Rombach_High-Resolution_Image_Synthesis_With_Latent_Diffusion_Models_CVPR_2022_paper.html) |\n[GitHub](https://github.com/CompVis/latent-diffusion) | [arXiv](https://arxiv.org/abs/2112.10752) | [Project page](https://ommer-lab.com/research/latent-diffusion-models/)_\n\n![txt2img-stable2](assets/stable-samples/txt2img/merged-0006.png)\n[Stable Diffusion](#stable-diffusion-v1) is a latent text-to-image diffusion\nmodel.\nThanks to a generous compute donation from [Stability AI](https://stability.ai/) and support from [LAION](https://laion.ai/), we were able to train a Latent Diffusion Model on 512x512 images from a subset of the [LAION-5B](https://laion.ai/blog/laion-5b/) database. \nSimilar to Google's [Imagen](https://arxiv.org/abs/2205.11487), \nthis model uses a frozen CLIP ViT-L/14 text encoder to condition the model on text prompts.\nWith its 860M UNet and 123M text encoder, the model is relatively lightweight and runs on a GPU with at least 10GB VRAM.\nSee [this section](#stable-diffusion-v1) below and the [model card](https://huggingface.co/CompVis/stable-diffusion).\n\n  \n## Requirements\nA suitable [conda](https://conda.io/) environment named `ldm` can be created\nand activated with:\n\n```\nconda env create -f environment.yaml\nconda activate ldm\n```\n\nYou can also update an existing [latent diffusion](https://github.com/CompVis/latent-diffusion) environment by running\n\n```\nconda install pytorch torchvision -c pytorch\npip install transformers==4.19.2 diffusers invisible-watermark\npip install -e .\n``` \n\n\n## Stable Diffusion v1\n\nStable Diffusion v1 refers to a specific configuration of the model\narchitecture that uses a downsampling-factor 8 autoencoder with an 860M UNet\nand CLIP ViT-L/14 text encoder for the diffusion model. The model was pretrained on 256x256 images and \nthen finetuned on 512x512 images.\n\n*Note: Stable Diffusion v1 is a general text-to-image diffusion model and therefore mirrors biases and (mis-)conceptions that are present\nin its training data. \nDetails on the training procedure and data, as well as the intended use of the model can be found in the corresponding [model card](Stable_Diffusion_v1_Model_Card.md).*\n\nThe weights are available via [the CompVis organization at Hugging Face](https://huggingface.co/CompVis) under [a license which contains specific use-based restrictions to prevent misuse and harm as informed by the model card, but otherwise remains permissive](LICENSE). While commercial use is permitted under the terms of the license, **we do not recommend using the provided weights for services or products without additional safety mechanisms and considerations**, since there are [known limitations and biases](Stable_Diffusion_v1_Model_Card.md#limitations-and-bias) of the weights, and research on safe and ethical deployment of general text-to-image models is an ongoing effort. **The weights are research artifacts and should be treated as such.**\n\n[The CreativeML OpenRAIL M license](LICENSE) is an [Open RAIL M license](https://www.licenses.ai/blog/2022/8/18/naming-convention-of-responsible-ai-licenses), adapted from the work that [BigScience](https://bigscience.huggingface.co/) and [the RAIL Initiative](https://www.licenses.ai/) are jointly carrying in the area of responsible AI licensing. See also [the article about the BLOOM Open RAIL license](https://bigscience.huggingface.co/blog/the-bigscience-rail-license) on which our license is based.\n\n### Weights\n\nWe currently provide the following checkpoints:\n\n- `sd-v1-1.ckpt`: 237k steps at resolution `256x256` on [laion2B-en](https://huggingface.co/datasets/laion/laion2B-en).\n  194k steps at resolution `512x512` on [laion-high-resolution](https://huggingface.co/datasets/laion/laion-high-resolution) (170M examples from LAION-5B with resolution `\u003e= 1024x1024`).\n- `sd-v1-2.ckpt`: Resumed from `sd-v1-1.ckpt`.\n  515k steps at resolution `512x512` on [laion-aesthetics v2 5+](https://laion.ai/blog/laion-aesthetics/) (a subset of laion2B-en with estimated aesthetics score `\u003e 5.0`, and additionally\nfiltered to images with an original size `\u003e= 512x512`, and an estimated watermark probability `\u003c 0.5`. The watermark estimate is from the [LAION-5B](https://laion.ai/blog/laion-5b/) metadata, the aesthetics score is estimated using the [LAION-Aesthetics Predictor V2](https://github.com/christophschuhmann/improved-aesthetic-predictor)).\n- `sd-v1-3.ckpt`: Resumed from `sd-v1-2.ckpt`. 195k steps at resolution `512x512` on \"laion-aesthetics v2 5+\" and 10\\% dropping of the text-conditioning to improve [classifier-free guidance sampling](https://arxiv.org/abs/2207.12598).\n- `sd-v1-4.ckpt`: Resumed from `sd-v1-2.ckpt`. 225k steps at resolution `512x512` on \"laion-aesthetics v2 5+\" and 10\\% dropping of the text-conditioning to improve [classifier-free guidance sampling](https://arxiv.org/abs/2207.12598).\n\nEvaluations with different classifier-free guidance scales (1.5, 2.0, 3.0, 4.0,\n5.0, 6.0, 7.0, 8.0) and 50 PLMS sampling\nsteps show the relative improvements of the checkpoints:\n![sd evaluation results](assets/v1-variants-scores.jpg)\n\n\n\n### Text-to-Image with Stable Diffusion\n![txt2img-stable2](assets/stable-samples/txt2img/merged-0005.png)\n![txt2img-stable2](assets/stable-samples/txt2img/merged-0007.png)\n\nStable Diffusion is a latent diffusion model conditioned on the (non-pooled) text embeddings of a CLIP ViT-L/14 text encoder.\nWe provide a [reference script for sampling](#reference-sampling-script), but\nthere also exists a [diffusers integration](#diffusers-integration), which we\nexpect to see more active community development.\n\n#### Reference Sampling Script\n\nWe provide a reference sampling script, which incorporates\n\n- a [Safety Checker Module](https://github.com/CompVis/stable-diffusion/pull/36),\n  to reduce the probability of explicit outputs,\n- an [invisible watermarking](https://github.com/ShieldMnt/invisible-watermark)\n  of the outputs, to help viewers [identify the images as machine-generated](scripts/tests/test_watermark.py).\n\nAfter [obtaining the `stable-diffusion-v1-*-original` weights](#weights), link them\n```\nmkdir -p models/ldm/stable-diffusion-v1/\nln -s \u003cpath/to/model.ckpt\u003e models/ldm/stable-diffusion-v1/model.ckpt \n```\nand sample with\n```\npython scripts/txt2img.py --prompt \"a photograph of an astronaut riding a horse\" --plms \n```\n\nBy default, this uses a guidance scale of `--scale 7.5`, [Katherine Crowson's implementation](https://github.com/CompVis/latent-diffusion/pull/51) of the [PLMS](https://arxiv.org/abs/2202.09778) sampler, \nand renders images of size 512x512 (which it was trained on) in 50 steps. All supported arguments are listed below (type `python scripts/txt2img.py --help`).\n\n\n```commandline\nusage: txt2img.py [-h] [--prompt [PROMPT]] [--outdir [OUTDIR]] [--skip_grid] [--skip_save] [--ddim_steps DDIM_STEPS] [--plms] [--laion400m] [--fixed_code] [--ddim_eta DDIM_ETA]\n                  [--n_iter N_ITER] [--H H] [--W W] [--C C] [--f F] [--n_samples N_SAMPLES] [--n_rows N_ROWS] [--scale SCALE] [--from-file FROM_FILE] [--config CONFIG] [--ckpt CKPT]\n                  [--seed SEED] [--precision {full,autocast}]\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --prompt [PROMPT]     the prompt to render\n  --outdir [OUTDIR]     dir to write results to\n  --skip_grid           do not save a grid, only individual samples. Helpful when evaluating lots of samples\n  --skip_save           do not save individual samples. For speed measurements.\n  --ddim_steps DDIM_STEPS\n                        number of ddim sampling steps\n  --plms                use plms sampling\n  --laion400m           uses the LAION400M model\n  --fixed_code          if enabled, uses the same starting code across samples\n  --ddim_eta DDIM_ETA   ddim eta (eta=0.0 corresponds to deterministic sampling\n  --n_iter N_ITER       sample this often\n  --H H                 image height, in pixel space\n  --W W                 image width, in pixel space\n  --C C                 latent channels\n  --f F                 downsampling factor\n  --n_samples N_SAMPLES\n                        how many samples to produce for each given prompt. A.k.a. batch size\n  --n_rows N_ROWS       rows in the grid (default: n_samples)\n  --scale SCALE         unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))\n  --from-file FROM_FILE\n                        if specified, load prompts from this file\n  --config CONFIG       path to config which constructs model\n  --ckpt CKPT           path to checkpoint of model\n  --seed SEED           the seed (for reproducible sampling)\n  --precision {full,autocast}\n                        evaluate at this precision\n```\nNote: The inference config for all v1 versions is designed to be used with EMA-only checkpoints. \nFor this reason `use_ema=False` is set in the configuration, otherwise the code will try to switch from\nnon-EMA to EMA weights. If you want to examine the effect of EMA vs no EMA, we provide \"full\" checkpoints\nwhich contain both types of weights. For these, `use_ema=False` will load and use the non-EMA weights.\n\n\n#### Diffusers Integration\n\nA simple way to download and sample Stable Diffusion is by using the [diffusers library](https://github.com/huggingface/diffusers/tree/main#new--stable-diffusion-is-now-fully-compatible-with-diffusers):\n```py\n# make sure you're logged in with `huggingface-cli login`\nfrom torch import autocast\nfrom diffusers import StableDiffusionPipeline\n\npipe = StableDiffusionPipeline.from_pretrained(\n\t\"CompVis/stable-diffusion-v1-4\", \n\tuse_auth_token=True\n).to(\"cuda\")\n\nprompt = \"a photo of an astronaut riding a horse on mars\"\nwith autocast(\"cuda\"):\n    image = pipe(prompt)[\"sample\"][0]  \n    \nimage.save(\"astronaut_rides_horse.png\")\n```\n\n\n### Image Modification with Stable Diffusion\n\nBy using a diffusion-denoising mechanism as first proposed by [SDEdit](https://arxiv.org/abs/2108.01073), the model can be used for different \ntasks such as text-guided image-to-image translation and upscaling. Similar to the txt2img sampling script, \nwe provide a script to perform image modification with Stable Diffusion.  \n\nThe following describes an example where a rough sketch made in [Pinta](https://www.pinta-project.com/) is converted into a detailed artwork.\n```\npython scripts/img2img.py --prompt \"A fantasy landscape, trending on artstation\" --init-img \u003cpath-to-img.jpg\u003e --strength 0.8\n```\nHere, strength is a value between 0.0 and 1.0, that controls the amount of noise that is added to the input image. \nValues that approach 1.0 allow for lots of variations but will also produce images that are not semantically consistent with the input. See the following example.\n\n**Input**\n\n![sketch-in](assets/stable-samples/img2img/sketch-mountains-input.jpg)\n\n**Outputs**\n\n![out3](assets/stable-samples/img2img/mountains-3.png)\n![out2](assets/stable-samples/img2img/mountains-2.png)\n\nThis procedure can, for example, also be used to upscale samples from the base model.\n\n\n## Comments \n\n- Our codebase for the diffusion models builds heavily on [OpenAI's ADM codebase](https://github.com/openai/guided-diffusion)\nand [https://github.com/lucidrains/denoising-diffusion-pytorch](https://github.com/lucidrains/denoising-diffusion-pytorch). \nThanks for open-sourcing!\n\n- The implementation of the transformer encoder is from [x-transformers](https://github.com/lucidrains/x-transformers) by [lucidrains](https://github.com/lucidrains?tab=repositories). \n\n\n## BibTeX\n\n```\n@misc{rombach2021highresolution,\n      title={High-Resolution Image Synthesis with Latent Diffusion Models}, \n      author={Robin Rombach and Andreas Blattmann and Dominik Lorenz and Patrick Esser and Björn Ommer},\n      year={2021},\n      eprint={2112.10752},\n      archivePrefix={arXiv},\n      primaryClass={cs.CV}\n}\n```\n\n\n","funding_links":[],"categories":["Jupyter Notebook","Uncategorized","Tools / Software","Diffusion Models","Model","👑Stable Diffusion","CV Foundation Model","Relevant Repos","\u003cspan id=\"image\"\u003eImage\u003c/span\u003e","Official Resources","其他_机器视觉","模型","Generative KI","Summary","Developer Resources","Acknowledgement","others","Tools","Text-to-Image Models","Libs With Online Books","💬 大语言模型（LLM）","Repos","Tools \u0026 Resources"],"sub_categories":["Uncategorized","Generative Art","CV Foundation Model","Jupyter Notebook","\u003cspan id=\"tool\"\u003eLLM (LLM \u0026 Tool)\u003c/span\u003e","网络服务_其他","图片生成","Image generation","Feel free to explore the repository and contribute!","Text-to-Image","Philosophy","多模态大模型"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCompVis%2Fstable-diffusion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCompVis%2Fstable-diffusion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCompVis%2Fstable-diffusion/lists"}