Update README.md
Browse files
README.md
CHANGED
@@ -15,48 +15,135 @@ instance_prompt: <leaf microstructure>
|
|
15 |
widget: []
|
16 |
---
|
17 |
|
18 |
-
|
19 |
-
should probably proofread and complete it, then remove this comment. -->
|
20 |
-
|
21 |
-
|
22 |
-
# SDXL LoRA DreamBooth - lamm-mit/leaf_LoRA_SDXL_V10
|
23 |
-
|
24 |
-
<Gallery />
|
25 |
|
26 |
## Model description
|
27 |
|
28 |
-
These are
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
## Trigger words
|
37 |
|
38 |
You should use <leaf microstructure> to trigger the image generation.
|
39 |
|
40 |
-
##
|
41 |
-
|
42 |
-
Weights for this model are available in Safetensors format.
|
43 |
-
|
44 |
-
[Download](lamm-mit/leaf_LoRA_SDXL_V10/tree/main) them in the Files & versions tab.
|
45 |
|
|
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
|
50 |
-
|
51 |
|
52 |
```python
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
```
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
|
62 |
-
[TODO: describe the data used to train the model]
|
|
|
15 |
widget: []
|
16 |
---
|
17 |
|
18 |
+
# SDXL Fine-tuned with Leaf Images
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
## Model description
|
21 |
|
22 |
+
These are LoRA adaption weights for the SDXL-base-1.0 model.
|
23 |
|
24 |
+
## Trigger keywords
|
25 |
|
26 |
+
The following image were used during fine-tuning using the keyword \<leaf microstructure\>:
|
27 |
|
28 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/sI_exTnLy6AtOFDX1-7eq.png)
|
|
|
|
|
29 |
|
30 |
You should use <leaf microstructure> to trigger the image generation.
|
31 |
|
32 |
+
## How to use
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
Defining some helper functions:
|
35 |
|
36 |
+
```python
|
37 |
+
from diffusers import DiffusionPipeline
|
38 |
+
import torch
|
39 |
+
import os
|
40 |
+
from datetime import datetime
|
41 |
+
from PIL import Image
|
42 |
+
|
43 |
+
def generate_filename(base_name, extension=".png"):
|
44 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
45 |
+
return f"{base_name}_{timestamp}{extension}"
|
46 |
+
|
47 |
+
def save_image(image, directory, base_name="image_grid"):
|
48 |
+
|
49 |
+
filename = generate_filename(base_name)
|
50 |
+
file_path = os.path.join(directory, filename)
|
51 |
+
image.save(file_path)
|
52 |
+
print(f"Image saved as {file_path}")
|
53 |
+
|
54 |
+
def image_grid(imgs, rows, cols, save=True, save_dir='generated_images', base_name="image_grid",
|
55 |
+
save_individual_files=False):
|
56 |
+
|
57 |
+
if not os.path.exists(save_dir):
|
58 |
+
os.makedirs(save_dir)
|
59 |
+
|
60 |
+
assert len(imgs) == rows * cols
|
61 |
+
|
62 |
+
w, h = imgs[0].size
|
63 |
+
grid = Image.new('RGB', size=(cols * w, rows * h))
|
64 |
+
grid_w, grid_h = grid.size
|
65 |
+
|
66 |
+
for i, img in enumerate(imgs):
|
67 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
68 |
+
if save_individual_files:
|
69 |
+
save_image(img, save_dir, base_name=base_name+f'_{i}-of-{len(imgs)}_')
|
70 |
+
|
71 |
+
if save and save_dir:
|
72 |
+
save_image(grid, save_dir, base_name)
|
73 |
+
|
74 |
+
return grid
|
75 |
+
```
|
76 |
|
77 |
+
### Text-to-image
|
78 |
|
79 |
+
Model loading:
|
80 |
|
81 |
```python
|
82 |
+
|
83 |
+
import torch
|
84 |
+
from diffusers import DiffusionPipeline, AutoencoderKL
|
85 |
+
|
86 |
+
repo_id='lamm-mit/SDXL-leaf-inspired'
|
87 |
+
|
88 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
89 |
+
base = DiffusionPipeline.from_pretrained(
|
90 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
91 |
+
vae=vae,
|
92 |
+
torch_dtype=torch.float16,
|
93 |
+
variant="fp16",
|
94 |
+
use_safetensors=True
|
95 |
+
)
|
96 |
+
base.load_lora_weights(repo_id)
|
97 |
+
_ = base.to("cuda")
|
98 |
+
|
99 |
+
refiner = DiffusionPipeline.from_pretrained(
|
100 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
101 |
+
text_encoder_2=base.text_encoder_2,
|
102 |
+
vae=base.vae,
|
103 |
+
torch_dtype=torch.float16,
|
104 |
+
use_safetensors=True,
|
105 |
+
variant="fp16",
|
106 |
+
)
|
107 |
+
refiner.to("cuda")
|
108 |
```
|
109 |
|
110 |
+
Image generation:
|
111 |
|
112 |
+
```python
|
113 |
+
prompt = "a vase that resembles a <leaf microstructure>, high quality"
|
114 |
+
|
115 |
+
num_samples = 4
|
116 |
+
num_rows = 4
|
117 |
+
guidance_scale = 15
|
118 |
+
|
119 |
+
all_images = []
|
120 |
+
|
121 |
+
for _ in range(num_rows):
|
122 |
+
# Define how many steps and what % of steps to be run on each experts (80/20)
|
123 |
+
n_steps = 25
|
124 |
+
high_noise_frac = 0.8
|
125 |
+
|
126 |
+
# run both experts
|
127 |
+
image = base(
|
128 |
+
prompt=prompt,
|
129 |
+
num_inference_steps=n_steps, guidance_scale=guidance_scale,
|
130 |
+
denoising_end=high_noise_frac,num_images_per_prompt=num_samples,
|
131 |
+
output_type="latent",
|
132 |
+
).images
|
133 |
+
image = refiner(
|
134 |
+
prompt=prompt,
|
135 |
+
num_inference_steps=n_steps, guidance_scale=guidance_scale,
|
136 |
+
denoising_start=high_noise_frac,num_images_per_prompt=num_samples,
|
137 |
+
image=image,
|
138 |
+
).images
|
139 |
+
|
140 |
+
all_images.extend(image)
|
141 |
+
|
142 |
+
grid = image_grid(all_images, num_rows, num_samples,
|
143 |
+
save_individual_files=True,
|
144 |
+
)
|
145 |
+
grid
|
146 |
+
```
|
147 |
|
148 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/E8fADoQEgZiMkWzrXPl7q.png)
|
149 |
|
|