justinpinkney commited on
Commit
a2a1398
•
1 Parent(s): ca6f97f

update model and readme for v2

Browse files
README.md CHANGED
@@ -11,9 +11,14 @@ tags:
11
 
12
  # Stable Diffusion Image Variations Model Card
13
 
 
14
  🧨🎉 Image Variations is now natively supported in 🤗 Diffusers! 🎉🧨
15
 
16
- This version of Stable Diffusion has been fine tuned from [CompVis/stable-diffusion-v1-3-original](https://huggingface.co/CompVis/stable-diffusion-v-1-3-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).
 
 
 
 
17
 
18
  ![](https://raw.githubusercontent.com/justinpinkney/stable-diffusion/main/assets/im-vars-thin.jpg)
19
 
@@ -26,36 +31,85 @@ from diffusers import StableDiffusionImageVariationPipeline
26
  from PIL import Image
27
 
28
  device = "cuda:0"
29
- sd_pipe = StableDiffusionImageVariationPipeline.from_pretrained("lambdalabs/sd-image-variations-diffusers")
 
 
 
30
  sd_pipe = sd_pipe.to(device)
31
- out = sd_pipe(image=Image.open("path/to/image.jpg"), guidance_scale=3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  out["images"][0].save("result.jpg")
33
  ```
34
 
35
- # Training
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- **Training Data**
38
- The model developers used the following dataset for training the model:
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- - LAION-2B (en) and subsets thereof (see next section)
41
 
42
  **Training Procedure**
43
- 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.
44
 
45
- - **Hardware:** 4 x A6000 GPUs (provided by [Lambda GPU Cloud](https://lambdalabs.com/service/gpu-cloud))
46
  - **Optimizer:** AdamW
47
- - **Gradient Accumulations**: 1
48
- - **Steps**: 87,000
49
- - **Batch:** 6 x 4 = 24
50
- - **Learning rate:** warmup to 0.0001 for 1,000 steps and then kept constant
51
 
52
- Training was done using a [modified version of the original Stable Diffusion training code]((https://github.com/justinpinkney/stable-diffusion), the original version of the weights is [here](https://huggingface.co/lambdalabs/stable-diffusion-image-conditioned).
 
 
 
 
 
 
 
 
 
 
 
53
 
54
 
55
  # Uses
56
  _The following section is adapted from the [Stable Diffusion model card](https://huggingface.co/CompVis/stable-diffusion-v1-4)_
57
 
58
- ## Direct Use
59
  The model is intended for research purposes only. Possible research areas and
60
  tasks include
61
 
@@ -105,25 +159,25 @@ Using the model to generate content that is cruel to individuals is a misuse of
105
 
106
  ### Bias
107
 
108
- While the capabilities of image generation models are impressive, they can also reinforce or exacerbate social biases.
109
- Stable Diffusion v1 was trained on subsets of [LAION-2B(en)](https://laion.ai/blog/laion-5b/),
110
- which consists of images that are primarily limited to English descriptions.
111
- Texts and images from communities and cultures that use other languages are likely to be insufficiently accounted for.
112
- This affects the overall output of the model, as white and western cultures are often set as the default. Further, the
113
  ability of the model to generate content with non-English prompts is significantly worse than with English-language prompts.
114
 
115
  ### Safety Module
116
 
117
- The intended use of this model is with the [Safety Checker](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/safety_checker.py) in Diffusers.
118
  This checker works by checking model outputs against known hard-coded NSFW concepts.
119
  The concepts are intentionally hidden to reduce the likelihood of reverse-engineering this filter.
120
- Specifically, the checker compares the class probability of harmful concepts in the embedding space of the `CLIPModel` *after generation* of the images.
121
  The concepts are passed into the model with the generated image and compared to a hand-engineered weight for each NSFW concept.
122
 
123
 
124
  ## Old instructions
125
 
126
- If you are using a diffusers version <0.8.0 there is no `StableDiffusionImageVariationPipeline`,
127
  in this case you need to use an older revision (`2ddbd90b14bc5892c19925b15185e561bc8e5d0a`) in conjunction with the lambda-diffusers repo:
128
 
129
 
11
 
12
  # Stable Diffusion Image Variations Model Card
13
 
14
+ 📣 V2 model released, and blurriness issues fixed! 📣
15
  🧨🎉 Image Variations is now natively supported in 🤗 Diffusers! 🎉🧨
16
 
17
+ ## Version 2
18
+
19
+ 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).
20
+
21
+ 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
22
 
23
  ![](https://raw.githubusercontent.com/justinpinkney/stable-diffusion/main/assets/im-vars-thin.jpg)
24
 
31
  from PIL import Image
32
 
33
  device = "cuda:0"
34
+ sd_pipe = StableDiffusionImageVariationPipeline.from_pretrained(
35
+ "lambdalabs/sd-image-variations-diffusers",
36
+ revision="v2.0",
37
+ )
38
  sd_pipe = sd_pipe.to(device)
39
+
40
+ im = Image.open("path/to/image.jpg")
41
+ tform = transforms.Compose([
42
+ transforms.ToTensor(),
43
+ transforms.Resize(
44
+ (224, 224),
45
+ interpolation=transforms.InterpolationMode.BICUBIC,
46
+ antialias=False,
47
+ ),
48
+ transforms.Normalize(
49
+ [0.48145466, 0.4578275, 0.40821073],
50
+ [0.26862954, 0.26130258, 0.27577711]),
51
+ ])
52
+ inp = tform(im).to(device)
53
+
54
+ out = sd_pipe(inp, guidance_scale=3)
55
  out["images"][0].save("result.jpg")
56
  ```
57
 
58
+ ### The importance of resizing correctly... (or not)
59
+
60
+ 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!
61
+
62
+ 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))
63
+
64
+ ![](alias-montage.jpg)
65
+
66
+ ![](default-montage.jpg)
67
+
68
+ ### V1 vs V2
69
+
70
+ 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
71
+
72
+ ![](v2-montage.jpg)
73
 
74
+ ![](v1-montage.jpg)
75
+
76
+ Input images:
77
+
78
+ ![](inputs.jpg)
79
+
80
+ 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"`.
81
+
82
+ v2 output for girl with a pearl earing as input (guidance scale=3)
83
+
84
+ ![](earring.jpg)
85
+
86
+ # Training
87
 
 
88
 
89
  **Training Procedure**
90
+ 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.
91
 
92
+ - **Hardware:** 8 x A100-40GB GPUs (provided by [Lambda GPU Cloud](https://lambdalabs.com/service/gpu-cloud))
93
  - **Optimizer:** AdamW
 
 
 
 
94
 
95
+ - **Stage 1** - Fine tune only CrossAttention layer weights from Stable Diffusion v1.4 model
96
+ - **Steps**: 46,000
97
+ - **Batch:** batch size=4, GPUs=8, Gradient Accumulations=4. Total batch size=128
98
+ - **Learning rate:** warmup to 1e-5 for 10,000 steps and then kept constant
99
+
100
+ - **Stage 2** - Resume from Stage 1 training the whole unet
101
+ - **Steps**: 50,000
102
+ - **Batch:** batch size=4, GPUs=8, Gradient Accumulations=5. Total batch size=160
103
+ - **Learning rate:** warmup to 1e-5 for 5,000 steps and then kept constant
104
+
105
+
106
+ Training was done using a [modified version of the original Stable Diffusion training code]((https://github.com/justinpinkney/stable-diffusion).
107
 
108
 
109
  # Uses
110
  _The following section is adapted from the [Stable Diffusion model card](https://huggingface.co/CompVis/stable-diffusion-v1-4)_
111
 
112
+ ## Direct Use
113
  The model is intended for research purposes only. Possible research areas and
114
  tasks include
115
 
159
 
160
  ### Bias
161
 
162
+ While the capabilities of image generation models are impressive, they can also reinforce or exacerbate social biases.
163
+ Stable Diffusion v1 was trained on subsets of [LAION-2B(en)](https://laion.ai/blog/laion-5b/),
164
+ which consists of images that are primarily limited to English descriptions.
165
+ Texts and images from communities and cultures that use other languages are likely to be insufficiently accounted for.
166
+ This affects the overall output of the model, as white and western cultures are often set as the default. Further, the
167
  ability of the model to generate content with non-English prompts is significantly worse than with English-language prompts.
168
 
169
  ### Safety Module
170
 
171
+ The intended use of this model is with the [Safety Checker](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/safety_checker.py) in Diffusers.
172
  This checker works by checking model outputs against known hard-coded NSFW concepts.
173
  The concepts are intentionally hidden to reduce the likelihood of reverse-engineering this filter.
174
+ Specifically, the checker compares the class probability of harmful concepts in the embedding space of the `CLIPModel` *after generation* of the images.
175
  The concepts are passed into the model with the generated image and compared to a hand-engineered weight for each NSFW concept.
176
 
177
 
178
  ## Old instructions
179
 
180
+ If you are using a diffusers version <0.8.0 there is no `StableDiffusionImageVariationPipeline`,
181
  in this case you need to use an older revision (`2ddbd90b14bc5892c19925b15185e561bc8e5d0a`) in conjunction with the lambda-diffusers repo:
182
 
183
 
alias-montage.jpg ADDED
default-montage.jpg ADDED
earring.jpg ADDED
image_encoder/config.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "_name_or_path": "./lambdalabs/sd-image-variations-diffusers/image_encoder",
3
  "architectures": [
4
  "CLIPVisionModelWithProjection"
5
  ],
@@ -19,5 +19,5 @@
19
  "patch_size": 14,
20
  "projection_dim": 768,
21
  "torch_dtype": "float32",
22
- "transformers_version": "4.25.0.dev0"
23
  }
1
  {
2
+ "_name_or_path": "/home/jpinkney/.cache/huggingface/diffusers/models--lambdalabs--sd-image-variations-diffusers/snapshots/ca6f97f838ae1b5bf764f31363a21f388f4d8f3e/image_encoder",
3
  "architectures": [
4
  "CLIPVisionModelWithProjection"
5
  ],
19
  "patch_size": 14,
20
  "projection_dim": 768,
21
  "torch_dtype": "float32",
22
+ "transformers_version": "4.25.1"
23
  }
inputs.jpg ADDED
model_index.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "_class_name": "StableDiffusionImageVariationPipeline",
3
- "_diffusers_version": "0.8.0.dev0",
4
  "feature_extractor": [
5
  "transformers",
6
  "CLIPImageProcessor"
@@ -9,6 +9,7 @@
9
  "transformers",
10
  "CLIPVisionModelWithProjection"
11
  ],
 
12
  "safety_checker": [
13
  "stable_diffusion",
14
  "StableDiffusionSafetyChecker"
1
  {
2
  "_class_name": "StableDiffusionImageVariationPipeline",
3
+ "_diffusers_version": "0.9.0",
4
  "feature_extractor": [
5
  "transformers",
6
  "CLIPImageProcessor"
9
  "transformers",
10
  "CLIPVisionModelWithProjection"
11
  ],
12
+ "requires_safety_checker": true,
13
  "safety_checker": [
14
  "stable_diffusion",
15
  "StableDiffusionSafetyChecker"
safety_checker/config.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
- "_commit_hash": null,
3
- "_name_or_path": "./lambdalabs/sd-image-variations-diffusers/safety_checker",
4
  "architectures": [
5
  "StableDiffusionSafetyChecker"
6
  ],
@@ -80,7 +80,7 @@
80
  "top_p": 1.0,
81
  "torch_dtype": null,
82
  "torchscript": false,
83
- "transformers_version": "4.25.0.dev0",
84
  "typical_p": 1.0,
85
  "use_bfloat16": false,
86
  "vocab_size": 49408
@@ -167,7 +167,7 @@
167
  "top_p": 1.0,
168
  "torch_dtype": null,
169
  "torchscript": false,
170
- "transformers_version": "4.25.0.dev0",
171
  "typical_p": 1.0,
172
  "use_bfloat16": false
173
  },
1
  {
2
+ "_commit_hash": "ca6f97f838ae1b5bf764f31363a21f388f4d8f3e",
3
+ "_name_or_path": "/home/jpinkney/.cache/huggingface/diffusers/models--lambdalabs--sd-image-variations-diffusers/snapshots/ca6f97f838ae1b5bf764f31363a21f388f4d8f3e/safety_checker",
4
  "architectures": [
5
  "StableDiffusionSafetyChecker"
6
  ],
80
  "top_p": 1.0,
81
  "torch_dtype": null,
82
  "torchscript": false,
83
+ "transformers_version": "4.25.1",
84
  "typical_p": 1.0,
85
  "use_bfloat16": false,
86
  "vocab_size": 49408
167
  "top_p": 1.0,
168
  "torch_dtype": null,
169
  "torchscript": false,
170
+ "transformers_version": "4.25.1",
171
  "typical_p": 1.0,
172
  "use_bfloat16": false
173
  },
scheduler/scheduler_config.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "_class_name": "PNDMScheduler",
3
- "_diffusers_version": "0.8.0.dev0",
4
  "beta_end": 0.012,
5
  "beta_schedule": "scaled_linear",
6
  "beta_start": 0.00085,
1
  {
2
  "_class_name": "PNDMScheduler",
3
+ "_diffusers_version": "0.9.0",
4
  "beta_end": 0.012,
5
  "beta_schedule": "scaled_linear",
6
  "beta_start": 0.00085,
unet/config.json CHANGED
@@ -1,7 +1,6 @@
1
  {
2
  "_class_name": "UNet2DConditionModel",
3
- "_diffusers_version": "0.8.0.dev0",
4
- "_name_or_path": "./lambdalabs/sd-image-variations-diffusers/unet",
5
  "act_fn": "silu",
6
  "attention_head_dim": 8,
7
  "block_out_channels": [
@@ -19,6 +18,7 @@
19
  "DownBlock2D"
20
  ],
21
  "downsample_padding": 1,
 
22
  "flip_sin_to_cos": true,
23
  "freq_shift": 0,
24
  "in_channels": 4,
@@ -26,6 +26,8 @@
26
  "mid_block_scale_factor": 1,
27
  "norm_eps": 1e-05,
28
  "norm_num_groups": 32,
 
 
29
  "out_channels": 4,
30
  "sample_size": 64,
31
  "up_block_types": [
@@ -33,5 +35,6 @@
33
  "CrossAttnUpBlock2D",
34
  "CrossAttnUpBlock2D",
35
  "CrossAttnUpBlock2D"
36
- ]
 
37
  }
1
  {
2
  "_class_name": "UNet2DConditionModel",
3
+ "_diffusers_version": "0.9.0",
 
4
  "act_fn": "silu",
5
  "attention_head_dim": 8,
6
  "block_out_channels": [
18
  "DownBlock2D"
19
  ],
20
  "downsample_padding": 1,
21
+ "dual_cross_attention": false,
22
  "flip_sin_to_cos": true,
23
  "freq_shift": 0,
24
  "in_channels": 4,
26
  "mid_block_scale_factor": 1,
27
  "norm_eps": 1e-05,
28
  "norm_num_groups": 32,
29
+ "num_class_embeds": null,
30
+ "only_cross_attention": false,
31
  "out_channels": 4,
32
  "sample_size": 64,
33
  "up_block_types": [
35
  "CrossAttnUpBlock2D",
36
  "CrossAttnUpBlock2D",
37
  "CrossAttnUpBlock2D"
38
+ ],
39
+ "use_linear_projection": false
40
  }
unet/diffusion_pytorch_model.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:e281b502e677c99977043e8cf576f88a86534eeefbc39bf1049bb6984d524ab2
3
  size 3438354725
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ee23e3368e4e7c0e4ef636ed61923609c97fcaa583f8bb416e3e0986d4a0cfc6
3
  size 3438354725
v1-montage.jpg ADDED
v2-montage.jpg ADDED
vae/config.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "_class_name": "AutoencoderKL",
3
- "_diffusers_version": "0.8.0.dev0",
4
- "_name_or_path": "./lambdalabs/sd-image-variations-diffusers/vae",
5
  "act_fn": "silu",
6
  "block_out_channels": [
7
  128,
@@ -20,7 +20,7 @@
20
  "layers_per_block": 2,
21
  "norm_num_groups": 32,
22
  "out_channels": 3,
23
- "sample_size": 512,
24
  "up_block_types": [
25
  "UpDecoderBlock2D",
26
  "UpDecoderBlock2D",
1
  {
2
  "_class_name": "AutoencoderKL",
3
+ "_diffusers_version": "0.9.0",
4
+ "_name_or_path": "stabilityai/sd-vae-ft-mse",
5
  "act_fn": "silu",
6
  "block_out_channels": [
7
  128,
20
  "layers_per_block": 2,
21
  "norm_num_groups": 32,
22
  "out_channels": 3,
23
+ "sample_size": 256,
24
  "up_block_types": [
25
  "UpDecoderBlock2D",
26
  "UpDecoderBlock2D",
vae/diffusion_pytorch_model.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:1b134cded8eb78b184aefb8805b6b572f36fa77b255c483665dda931fa0130c5
3
  size 334707217
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b4889b6b1d4ce7ae320a02dedaeff1780ad77d415ea0d744b476155c6377ddc
3
  size 334707217