Commit
•
6a4ba63
1
Parent(s):
fefe439
how to diffusers (#3)
Browse files- how to diffusers (7c5d0f9b82bd92221f1bc43608ea1208822023f7)
Co-authored-by: Radamés Ajna <[email protected]>
README.md
CHANGED
@@ -17,6 +17,70 @@ pipeline_tag: image-to-image
|
|
17 |
This repo holds the safetensors & diffusers versions of the QR code conditioned ControlNet for Stable Diffusion v2.1.
|
18 |
The Stable Diffusion 2.1 version is marginally more effective, as it was developed to address my specific needs. However, a 1.5 version model was also trained on the same dataset for those who are using the older version.
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
## Performance and Limitations
|
21 |
|
22 |
These models perform quite well in most cases, but please note that they are not 100% accurate. In some instances, the QR code shape might not come through as expected. You can increase the ControlNet weight to emphasize the QR code shape. However, be cautious as this might negatively impact the style of your output.**To optimize for scanning, please generate your QR codes with correction mode 'H' (30%).**
|
|
|
17 |
This repo holds the safetensors & diffusers versions of the QR code conditioned ControlNet for Stable Diffusion v2.1.
|
18 |
The Stable Diffusion 2.1 version is marginally more effective, as it was developed to address my specific needs. However, a 1.5 version model was also trained on the same dataset for those who are using the older version.
|
19 |
|
20 |
+
## How to use with diffusers
|
21 |
+
|
22 |
+
```bash
|
23 |
+
pip -q install diffusers transformers accelerate torch xformers
|
24 |
+
```
|
25 |
+
|
26 |
+
```python
|
27 |
+
import torch
|
28 |
+
from PIL import Image
|
29 |
+
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler
|
30 |
+
from diffusers.utils import load_image
|
31 |
+
|
32 |
+
controlnet = ControlNetModel.from_pretrained("DionTimmer/controlnet_qrcode-control_v11p_sd21",
|
33 |
+
torch_dtype=torch.float16)
|
34 |
+
|
35 |
+
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
36 |
+
"runwayml/stable-diffusion-v1-5",
|
37 |
+
controlnet=controlnet,
|
38 |
+
safety_checker=None,
|
39 |
+
torch_dtype=torch.float16
|
40 |
+
)
|
41 |
+
|
42 |
+
pipe.enable_xformers_memory_efficient_attention()
|
43 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
44 |
+
pipe.enable_model_cpu_offload()
|
45 |
+
|
46 |
+
def resize_for_condition_image(input_image: Image, resolution: int):
|
47 |
+
input_image = input_image.convert("RGB")
|
48 |
+
W, H = input_image.size
|
49 |
+
k = float(resolution) / min(H, W)
|
50 |
+
H *= k
|
51 |
+
W *= k
|
52 |
+
H = int(round(H / 64.0)) * 64
|
53 |
+
W = int(round(W / 64.0)) * 64
|
54 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
55 |
+
return img
|
56 |
+
|
57 |
+
|
58 |
+
# play with guidance_scale, controlnet_conditioning_scale and strength to make a valid QR Code Image
|
59 |
+
|
60 |
+
# qr code image
|
61 |
+
source_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/6064e095abd8d3692e3e2ed6/A_RqHaAM6YHBodPLwqtjn.png")
|
62 |
+
# initial image, anything
|
63 |
+
init_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/noauth/KfMBABpOwIuNolv1pe3qX.jpeg")
|
64 |
+
condition_image = resize_for_condition_image(source_image, 768)
|
65 |
+
init_image = resize_for_condition_image(init_image, 768)
|
66 |
+
generator = torch.manual_seed(123121231)
|
67 |
+
image = pipe(prompt="a bilboard in NYC with a qrcode",
|
68 |
+
negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
|
69 |
+
image=init_image,
|
70 |
+
control_image=condition_image,
|
71 |
+
width=768,
|
72 |
+
height=768,
|
73 |
+
guidance_scale=20,
|
74 |
+
controlnet_conditioning_scale=1.5,
|
75 |
+
generator=generator,
|
76 |
+
strength=0.9,
|
77 |
+
num_inference_steps=150,
|
78 |
+
)
|
79 |
+
|
80 |
+
image.images[0]
|
81 |
+
|
82 |
+
```
|
83 |
+
|
84 |
## Performance and Limitations
|
85 |
|
86 |
These models perform quite well in most cases, but please note that they are not 100% accurate. In some instances, the QR code shape might not come through as expected. You can increase the ControlNet weight to emphasize the QR code shape. However, be cautious as this might negatively impact the style of your output.**To optimize for scanning, please generate your QR codes with correction mode 'H' (30%).**
|