Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,176 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
base_model: black-forest-labs/FLUX.1-dev
|
3 |
+
library_name: diffusers
|
4 |
+
license: apache-2.0
|
5 |
+
tags:
|
6 |
+
- text-to-image
|
7 |
+
- diffusers-training
|
8 |
+
- diffusers
|
9 |
+
- lora
|
10 |
+
- FLUX.1-dev
|
11 |
+
- science
|
12 |
+
- materiomics
|
13 |
+
- bio-inspired
|
14 |
+
- materials science
|
15 |
+
- generative AI for science
|
16 |
+
datasets:
|
17 |
+
- lamm-mit/leaf-flux-images-and-captions
|
18 |
+
instance_prompt: <leaf microstructure>
|
19 |
+
widget: []
|
20 |
+
---
|
21 |
+
|
22 |
+
# FLUX.1 [dev] Fine-tuned with Leaf Images
|
23 |
+
|
24 |
+
FLUX.1 [dev] is a 12 billion parameter rectified flow transformer capable of generating images from text descriptions.
|
25 |
+
|
26 |
+
Install ```diffusers```
|
27 |
+
|
28 |
+
```raw
|
29 |
+
pip install -U diffusers
|
30 |
+
```
|
31 |
+
|
32 |
+
## Model description
|
33 |
+
|
34 |
+
These are LoRA adaption weights for the FLUX.1 [dev] model (```black-forest-labs/FLUX.1-dev```). The base model is, and you must first get access to it before loading this LoRA adapter.
|
35 |
+
|
36 |
+
This LoRA adapter has rank=64 and alpha=64, trained for 4,000 steps. Earlier checkpoints are available in this repository as well (you can load these via the ```adapter``` parameter, see example below).
|
37 |
+
|
38 |
+
## Trigger keywords
|
39 |
+
|
40 |
+
The model was fine-tuned with a set of ~1,600 images of biological materials, structures, shapes and other images of nature, using the keyword \bioinspired\>.
|
41 |
+
|
42 |
+
You should use \<bioinspired\> to trigger these features during image generation.
|
43 |
+
|
44 |
+
## How to use
|
45 |
+
|
46 |
+
Defining some helper functions:
|
47 |
+
|
48 |
+
```python
|
49 |
+
import os
|
50 |
+
from datetime import datetime
|
51 |
+
from PIL import Image
|
52 |
+
|
53 |
+
def generate_filename(base_name, extension=".png"):
|
54 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
55 |
+
return f"{base_name}_{timestamp}{extension}"
|
56 |
+
|
57 |
+
def save_image(image, directory, base_name="image_grid"):
|
58 |
+
filename = generate_filename(base_name)
|
59 |
+
file_path = os.path.join(directory, filename)
|
60 |
+
image.save(file_path)
|
61 |
+
print(f"Image saved as {file_path}")
|
62 |
+
|
63 |
+
def image_grid(imgs, rows, cols, save=True, save_dir='generated_images', base_name="image_grid",
|
64 |
+
save_individual_files=False):
|
65 |
+
|
66 |
+
if not os.path.exists(save_dir):
|
67 |
+
os.makedirs(save_dir)
|
68 |
+
|
69 |
+
assert len(imgs) == rows * cols
|
70 |
+
|
71 |
+
w, h = imgs[0].size
|
72 |
+
grid = Image.new('RGB', size=(cols * w, rows * h))
|
73 |
+
grid_w, grid_h = grid.size
|
74 |
+
|
75 |
+
for i, img in enumerate(imgs):
|
76 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
77 |
+
if save_individual_files:
|
78 |
+
save_image(img, save_dir, base_name=base_name+f'_{i}-of-{len(imgs)}_')
|
79 |
+
|
80 |
+
if save and save_dir:
|
81 |
+
save_image(grid, save_dir, base_name)
|
82 |
+
|
83 |
+
return grid
|
84 |
+
```
|
85 |
+
|
86 |
+
### Text-to-image
|
87 |
+
|
88 |
+
Model loading:
|
89 |
+
|
90 |
+
```python
|
91 |
+
from diffusers import FluxPipeline
|
92 |
+
import torch
|
93 |
+
|
94 |
+
repo_id = 'lamm-mit/bioinspired-L-FLUX.1-dev'
|
95 |
+
|
96 |
+
pipeline = FluxPipeline.from_pretrained(
|
97 |
+
"black-forest-labs/FLUX.1-dev",
|
98 |
+
torch_dtype=torch.bfloat16,
|
99 |
+
max_sequence_length=512,
|
100 |
+
)
|
101 |
+
|
102 |
+
#pipeline.enable_model_cpu_offload() #save some VRAM by offloading the model to CPU. Comment out if you have enough GPU VRAM
|
103 |
+
|
104 |
+
adapter='leaf-flux.safetensors' #Step 16000, final step
|
105 |
+
#adapter='leaf-flux-step-3000.safetensors' #Step 3000
|
106 |
+
#adapter='leaf-flux-step-3500.safetensors' #Step 3500
|
107 |
+
|
108 |
+
pipeline.load_lora_weights(repo_id, weight_name=adapter) #You need to use the weight_name parameter since the repo includes multiple checkpoints
|
109 |
+
|
110 |
+
pipeline=pipeline.to('cuda')
|
111 |
+
```
|
112 |
+
|
113 |
+
|
114 |
+
Image generation - Example #1:
|
115 |
+
|
116 |
+
```python
|
117 |
+
prompt="""Generate a futuristic, eco-friendly architectural concept utilizing a biomimetic composite material that integrates the structural efficiency of spider silk with the adaptive porosity of plant tissues. Utilize the following key features:
|
118 |
+
|
119 |
+
* Fibrous architecture inspired by spider silk, represented by sinuous lines and curved forms.
|
120 |
+
* Interconnected, spherical nodes reminiscent of plant cell walls, emphasizing growth and adaptation.
|
121 |
+
* Open cellular structures echoing the permeable nature of plant leaves, suggesting dynamic exchanges and self-regulation capabilities.
|
122 |
+
* Gradations of opacity and transparency inspired by the varying densities found in plant tissues, highlighting functional differentiation and multi-functionality.
|
123 |
+
"""
|
124 |
+
|
125 |
+
num_samples =2
|
126 |
+
num_rows = 2
|
127 |
+
n_steps=25
|
128 |
+
guidance_scale=3.5
|
129 |
+
all_images = []
|
130 |
+
for _ in range(num_rows):
|
131 |
+
|
132 |
+
|
133 |
+
image = pipeline(prompt,num_inference_steps=n_steps,num_images_per_prompt=num_samples,
|
134 |
+
guidance_scale=guidance_scale,).images
|
135 |
+
|
136 |
+
all_images.extend(image)
|
137 |
+
|
138 |
+
grid = image_grid(all_images, num_rows, num_samples, save_individual_files=True, )
|
139 |
+
grid
|
140 |
+
```
|
141 |
+
|
142 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/VJXJ3MguJHk32JARdU-wV.png)
|
143 |
+
|
144 |
+
|
145 |
+
Image generation - Example #2:
|
146 |
+
|
147 |
+
```python
|
148 |
+
prompt="""A jar of round <bioinspired> cookies with a piece of white tape that says "Materiomics Cookies". Looks tasty. Old fashioned."""
|
149 |
+
|
150 |
+
num_samples =2
|
151 |
+
num_rows = 2
|
152 |
+
n_steps=25
|
153 |
+
guidance_scale=15.
|
154 |
+
all_images = []
|
155 |
+
for _ in range(num_rows):
|
156 |
+
|
157 |
+
|
158 |
+
image = pipeline(prompt,num_inference_steps=n_steps,num_images_per_prompt=num_samples,
|
159 |
+
guidance_scale=guidance_scale,
|
160 |
+
height=1024, width=1024,).images
|
161 |
+
|
162 |
+
all_images.extend(image)
|
163 |
+
|
164 |
+
grid = image_grid(all_images, num_rows, num_samples, save_individual_files=True, )
|
165 |
+
grid
|
166 |
+
```
|
167 |
+
|
168 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/623ce1c6b66fedf374859fe7/VahIiPsIJSW0M1XmS08r-.png)
|
169 |
+
|
170 |
+
```bibtext
|
171 |
+
@article{BioinspiredFluxBuehler2024,
|
172 |
+
title={Fine-tuning image-generation models with biological patterns, shapes and topologies},
|
173 |
+
author={Markus J. Buehler},
|
174 |
+
journal={arXiv: XXXX.YYYYY},
|
175 |
+
year={2024},
|
176 |
+
}
|