Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,120 @@
|
|
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
|
11 |
+
- science
|
12 |
+
- materiomics
|
13 |
+
- bio-inspired
|
14 |
+
- materials science
|
15 |
+
instance_prompt: <leaf microstructure>
|
16 |
+
widget: []
|
17 |
+
---
|
18 |
+
|
19 |
+
# FLUX.1 [dev] Fine-tuned with Leaf Images
|
20 |
+
|
21 |
+
FLUX.1 [dev] is a 12 billion parameter rectified flow transformer capable of generating images from text descriptions.
|
22 |
+
|
23 |
+
## Model description
|
24 |
+
|
25 |
+
These are LoRA adaption weights for the FLUX.1 [dev] model (```black-forest-labs/FLUX.1-dev```).
|
26 |
+
|
27 |
+
## Trigger keywords
|
28 |
+
|
29 |
+
The following images were used during fine-tuning using the keyword \<leaf microstructure\>:
|
30 |
+
|
31 |
+

|
32 |
+
|
33 |
+
Full dataset used for training: (lamm-mit/leaf-flux-images-and-captions)
|
34 |
+
|
35 |
+
You should use \<leaf microstructure\> to trigger this feature during image generation.
|
36 |
+
|
37 |
+
## How to use
|
38 |
+
|
39 |
+
Defining some helper functions:
|
40 |
+
|
41 |
+
```python
|
42 |
+
import os
|
43 |
+
from datetime import datetime
|
44 |
+
from PIL import Image
|
45 |
+
|
46 |
+
def generate_filename(base_name, extension=".png"):
|
47 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
48 |
+
return f"{base_name}_{timestamp}{extension}"
|
49 |
+
|
50 |
+
def save_image(image, directory, base_name="image_grid"):
|
51 |
+
filename = generate_filename(base_name)
|
52 |
+
file_path = os.path.join(directory, filename)
|
53 |
+
image.save(file_path)
|
54 |
+
print(f"Image saved as {file_path}")
|
55 |
+
|
56 |
+
def image_grid(imgs, rows, cols, save=True, save_dir='generated_images', base_name="image_grid",
|
57 |
+
save_individual_files=False):
|
58 |
+
|
59 |
+
if not os.path.exists(save_dir):
|
60 |
+
os.makedirs(save_dir)
|
61 |
+
|
62 |
+
assert len(imgs) == rows * cols
|
63 |
+
|
64 |
+
w, h = imgs[0].size
|
65 |
+
grid = Image.new('RGB', size=(cols * w, rows * h))
|
66 |
+
grid_w, grid_h = grid.size
|
67 |
+
|
68 |
+
for i, img in enumerate(imgs):
|
69 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
70 |
+
if save_individual_files:
|
71 |
+
save_image(img, save_dir, base_name=base_name+f'_{i}-of-{len(imgs)}_')
|
72 |
+
|
73 |
+
if save and save_dir:
|
74 |
+
save_image(grid, save_dir, base_name)
|
75 |
+
|
76 |
+
return grid
|
77 |
+
```
|
78 |
+
|
79 |
+
### Text-to-image
|
80 |
+
|
81 |
+
Model loading:
|
82 |
+
|
83 |
+
```python
|
84 |
+
from diffusers import FluxPipeline
|
85 |
+
import torch
|
86 |
+
|
87 |
+
repo_id = 'lamm-mit/leaf-FLUX'
|
88 |
+
|
89 |
+
pipeline = FluxPipeline.from_pretrained(
|
90 |
+
"black-forest-labs/FLUX.1-dev",
|
91 |
+
torch_dtype=torch.bfloat16,
|
92 |
+
)
|
93 |
+
|
94 |
+
pipeline.load_lora_weights(repo_id, )
|
95 |
+
pipeline=pipeline.to('cuda')
|
96 |
+
```
|
97 |
+
|
98 |
+
Image generation:
|
99 |
+
|
100 |
+
```python
|
101 |
+
prompt=('Generate an image of a golden spider web network intertwined with collagen veins, '
|
102 |
+
'forming a dynamic, leaf-inspired microstructure amidst a lush green background.' )
|
103 |
+
|
104 |
+
num_samples =2
|
105 |
+
num_rows = 2
|
106 |
+
n_steps=25
|
107 |
+
guidance_scale=3.5
|
108 |
+
all_images = []
|
109 |
+
for _ in range(num_rows):
|
110 |
+
|
111 |
+
|
112 |
+
image = pipeline(prompt,num_inference_steps=n_steps,num_images_per_prompt=num_samples,
|
113 |
+
guidance_scale=guidance_scale,).images
|
114 |
+
|
115 |
+
all_images.extend(image)
|
116 |
+
|
117 |
+
grid = image_grid(all_images, num_rows, num_samples, ave_individual_files=True, )
|
118 |
+
grid
|
119 |
+
```
|
120 |
+
|