Spaces:
Runtime error
Runtime error
Commit
Β·
4176850
0
Parent(s):
Duplicate from lambdalabs/stable-diffusion-image-variations
Browse filesCo-authored-by: Justin Pinkney <[email protected]>
- .gitattributes +31 -0
- README.md +14 -0
- app.py +105 -0
- examples/matisse.jpg +0 -0
- examples/vermeer.jpg +0 -0
- requirements.txt +5 -0
- unsafe.png +0 -0
.gitattributes
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
23 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Stable Diffusion Image Variations
|
3 |
+
emoji: πΌοΈ
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.3
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
duplicated_from: lambdalabs/stable-diffusion-image-variations
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from torchvision import transforms
|
5 |
+
|
6 |
+
from diffusers import StableDiffusionImageVariationPipeline
|
7 |
+
|
8 |
+
def main(
|
9 |
+
input_im,
|
10 |
+
scale=3.0,
|
11 |
+
n_samples=4,
|
12 |
+
steps=25,
|
13 |
+
seed=0,
|
14 |
+
):
|
15 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
16 |
+
|
17 |
+
tform = transforms.Compose([
|
18 |
+
transforms.ToTensor(),
|
19 |
+
transforms.Resize(
|
20 |
+
(224, 224),
|
21 |
+
interpolation=transforms.InterpolationMode.BICUBIC,
|
22 |
+
antialias=False,
|
23 |
+
),
|
24 |
+
transforms.Normalize(
|
25 |
+
[0.48145466, 0.4578275, 0.40821073],
|
26 |
+
[0.26862954, 0.26130258, 0.27577711]),
|
27 |
+
])
|
28 |
+
inp = tform(input_im).to(device)
|
29 |
+
|
30 |
+
images_list = pipe(
|
31 |
+
inp.tile(n_samples, 1, 1, 1),
|
32 |
+
guidance_scale=scale,
|
33 |
+
num_inference_steps=steps,
|
34 |
+
generator=generator,
|
35 |
+
)
|
36 |
+
|
37 |
+
images = []
|
38 |
+
for i, image in enumerate(images_list["images"]):
|
39 |
+
if(images_list["nsfw_content_detected"][i]):
|
40 |
+
safe_image = Image.open(r"unsafe.png")
|
41 |
+
images.append(safe_image)
|
42 |
+
else:
|
43 |
+
images.append(image)
|
44 |
+
return images
|
45 |
+
|
46 |
+
|
47 |
+
description = \
|
48 |
+
"""
|
49 |
+
__Now using Image Variations v2!__
|
50 |
+
|
51 |
+
Generate variations on an input image using a fine-tuned version of Stable Diffision.
|
52 |
+
Trained by [Justin Pinkney](https://www.justinpinkney.com) ([@Buntworthy](https://twitter.com/Buntworthy)) at [Lambda](https://lambdalabs.com/)
|
53 |
+
|
54 |
+
This version has been ported to π€ Diffusers library, see more details on how to use this version in the [Lambda Diffusers repo](https://github.com/LambdaLabsML/lambda-diffusers).
|
55 |
+
For the original training code see [this repo](https://github.com/justinpinkney/stable-diffusion).
|
56 |
+
|
57 |
+

|
58 |
+
|
59 |
+
"""
|
60 |
+
|
61 |
+
article = \
|
62 |
+
"""
|
63 |
+
## How does this work?
|
64 |
+
|
65 |
+
The normal Stable Diffusion model is trained to be conditioned on text input. This version has had the original text encoder (from CLIP) removed, and replaced with
|
66 |
+
the CLIP _image_ encoder instead. So instead of generating images based a text input, images are generated to match CLIP's embedding of the image.
|
67 |
+
This creates images which have the same rough style and content, but different details, in particular the composition is generally quite different.
|
68 |
+
This is a totally different approach to the img2img script of the original Stable Diffusion and gives very different results.
|
69 |
+
|
70 |
+
The model was fine tuned on the [LAION aethetics v2 6+ dataset](https://laion.ai/blog/laion-aesthetics/) to accept the new conditioning.
|
71 |
+
Training was done on 8xA100 GPUs on [Lambda GPU Cloud](https://lambdalabs.com/service/gpu-cloud).
|
72 |
+
More details are on the [model card](https://huggingface.co/lambdalabs/sd-image-variations-diffusers).
|
73 |
+
"""
|
74 |
+
|
75 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
76 |
+
pipe = StableDiffusionImageVariationPipeline.from_pretrained(
|
77 |
+
"lambdalabs/sd-image-variations-diffusers",
|
78 |
+
)
|
79 |
+
pipe = pipe.to(device)
|
80 |
+
|
81 |
+
inputs = [
|
82 |
+
gr.Image(),
|
83 |
+
gr.Slider(0, 25, value=3, step=1, label="Guidance scale"),
|
84 |
+
gr.Slider(1, 4, value=1, step=1, label="Number images"),
|
85 |
+
gr.Slider(5, 50, value=25, step=5, label="Steps"),
|
86 |
+
gr.Number(0, label="Seed", precision=0)
|
87 |
+
]
|
88 |
+
output = gr.Gallery(label="Generated variations")
|
89 |
+
output.style(grid=2)
|
90 |
+
|
91 |
+
examples = [
|
92 |
+
["examples/vermeer.jpg", 3, 1, 25, 0],
|
93 |
+
["examples/matisse.jpg", 3, 1, 25, 0],
|
94 |
+
]
|
95 |
+
|
96 |
+
demo = gr.Interface(
|
97 |
+
fn=main,
|
98 |
+
title="Stable Diffusion Image Variations",
|
99 |
+
description=description,
|
100 |
+
article=article,
|
101 |
+
inputs=inputs,
|
102 |
+
outputs=output,
|
103 |
+
examples=examples,
|
104 |
+
)
|
105 |
+
demo.launch()
|
examples/matisse.jpg
ADDED
![]() |
examples/vermeer.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
4 |
+
torch
|
5 |
+
torchvision
|
unsafe.png
ADDED
![]() |