readme: ---
  thumbnail: "https://repository-images.githubusercontent.com/523487884/fdb03a69-8353-4387-b5fc-0d85f888a63f"
  datasets:
  - ChristophSchuhmann/improved_aesthetics_6plus
  license: creativeml-openrail-m
  tags:
  - stable-diffusion
  - stable-diffusion-diffusers
  - image-to-image
  ---
  
  # Stable Diffusion Image Variations Model Card
  
  📣 V2 model released, and blurriness issues fixed! 📣
  
  🧨🎉 Image Variations is now natively supported in 🤗 Diffusers! 🎉🧨
  
  ![](https://raw.githubusercontent.com/justinpinkney/stable-diffusion/main/assets/im-vars-thin.jpg)
  
  ## Version 2
  
  This version of Stable Diffusion has been fine tuned from [CompVis/stable-diffusion-v1-4-original](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original) to accept CLIP image embedding rather than text embeddings. This allows the creation of "image variations" similar to DALLE-2 using Stable Diffusion. This version of the weights has been ported to huggingface Diffusers, to use this with the Diffusers library requires the [Lambda Diffusers repo](https://github.com/LambdaLabsML/lambda-diffusers).
  
  This model was trained in two stages and longer than the original variations model and gives better image quality and better CLIP rated similarity compared to the original version
  
  See training details and v1 vs v2 comparison below.
  
  
  ## Example
  
  Make sure you are using a version of Diffusers >=0.8.0 (for older version see the old instructions at the bottom of this model card)
  
  ```python
  from diffusers import StableDiffusionImageVariationPipeline
  from PIL import Image
  
  device = "cuda:0"
  sd_pipe = StableDiffusionImageVariationPipeline.from_pretrained(
    "lambdalabs/sd-image-variations-diffusers",
    revision="v2.0",
    )
  sd_pipe = sd_pipe.to(device)
  
  im = Image.open("path/to/image.jpg")
  tform = transforms.Compose([
      transforms.ToTensor(),
      transforms.Resize(
          (224, 224),
          interpolation=transforms.InterpolationMode.BICUBIC,
          antialias=False,
          ),
      transforms.Normalize(
        [0.48145466, 0.4578275, 0.40821073],
        [0.26862954, 0.26130258, 0.27577711]),
  ])
  inp = tform(im).to(device).unsqueeze(0)
  
  out = sd_pipe(inp, guidance_scale=3)
  out["images"][0].save("result.jpg")
  ```
  
  ### The importance of resizing correctly... (or not)
  
  Note that due a bit of an oversight during training, the model expects resized images without anti-aliasing. This turns out to make a big difference and is important to do the resizing the same way during inference. When passing a PIL image to the Diffusers pipeline antialiasing will be applied during resize, so it's better to input a tensor which you have prepared manually according to the transfrom in the example above!
  
  Here are examples of images generated without (top) and with (bottom) anti-aliasing during resize. (Input is [this image](https://github.com/SHI-Labs/Versatile-Diffusion/blob/master/assets/ghibli.jpg))
  
  ![](alias-montage.jpg)
  
  ![](default-montage.jpg)
  
  ### V1 vs V2
  
  Here's an example of V1 vs V2, version two was trained more carefully and for longer, see the details below. V2-top vs V1-bottom
  
  ![](v2-montage.jpg)
  
  ![](v1-montage.jpg)
  
  Input images:
  
  ![](inputs.jpg)
  
  One important thing to note is that due to the longer training V2 appears to have memorised some common images from the training data, e.g. now the previous example of the Girl with a Pearl Earring almosts perfectly reproduce the original rather than creating variations. You can always use v1 by specifiying `revision="v1.0"`.
  
  v2 output for girl with a pearl earing as input (guidance scale=3)
  
  ![](earring.jpg)
  
  # Training
  
  
  **Training Procedure**
  This model is fine tuned from Stable Diffusion v1-3 where the text encoder has been replaced with an image encoder. The training procedure is the same as for Stable Diffusion except for the fact that images are encoded through a ViT-L/14 image-encoder including the final projection layer to the CLIP shared embedding space. The model was trained on LAION improved aesthetics 6plus.
  
  - **Hardware:** 8 x A100-40GB GPUs (provided by [Lambda GPU Cloud](https://lambdalabs.com/service/gpu-cloud))
  - **Optimizer:** AdamW
  
  - **Stage 1** - Fine tune only CrossAttention layer weights from Stable Diffusion v1.4 model
    - **Steps**: 46,000
    - **Batch:** batch size=4, GPUs=8, Gradient Accumulations=4. Total batch size=128
    - **Learning rate:** warmup to 1e-5 for 10,000 steps and then kept constant
  
  - **Stage 2** - Resume from Stage 1 training the whole unet
    - **Steps**: 50,000
    - **Batch:** batch size=4, GPUs=8, Gradient Accumulations=5. Total batch size=160
    - **Learning rate:** warmup to 1e-5 for 5,000 steps and then kept constant
  
  
  Training was done using a [modified version of the original Stable Diffusion training code]((https://github.com/justinpinkney/stable-diffusion).
  
  
  # Uses
  _The following section is adapted from the [Stable Diffusion model card](https://huggingface.co/CompVis/stable-diffusion-v1-4)_
  
  ## Direct Use
  The model is intended for research purposes only. Possible research