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
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "HHxHa_96z0gM"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Stable Diffusion 3 medium fine-tuned for leaf-inspired generation\n",
|
10 |
+
"\n",
|
11 |
+
"Markus J. Buehler, MIT\n",
|
12 |
+
"[email protected]\n",
|
13 |
+
"\n",
|
14 |
+
"https://huggingface.co/lamm-mit/stable-diffusion-3-medium-leaf-inspired/"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 5,
|
20 |
+
"metadata": {
|
21 |
+
"colab": {
|
22 |
+
"base_uri": "https://localhost:8080/",
|
23 |
+
"height": 531
|
24 |
+
},
|
25 |
+
"id": "vQzwg68ByEiC",
|
26 |
+
"outputId": "895980fe-52df-49f5-fd02-bf0b2846f12f"
|
27 |
+
},
|
28 |
+
"outputs": [
|
29 |
+
{
|
30 |
+
"output_type": "stream",
|
31 |
+
"name": "stdout",
|
32 |
+
"text": [
|
33 |
+
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
|
34 |
+
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
|
35 |
+
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
|
36 |
+
"Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/dist-packages (0.23.5)\n",
|
37 |
+
"Collecting huggingface_hub\n",
|
38 |
+
" Downloading huggingface_hub-0.24.0-py3-none-any.whl (419 kB)\n",
|
39 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m419.0/419.0 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
40 |
+
"\u001b[?25hRequirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (3.15.4)\n",
|
41 |
+
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (2023.6.0)\n",
|
42 |
+
"Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (24.1)\n",
|
43 |
+
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (6.0.1)\n",
|
44 |
+
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (2.31.0)\n",
|
45 |
+
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (4.66.4)\n",
|
46 |
+
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (4.12.2)\n",
|
47 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (3.3.2)\n",
|
48 |
+
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (3.7)\n",
|
49 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (2.0.7)\n",
|
50 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (2024.7.4)\n",
|
51 |
+
"Installing collected packages: huggingface_hub\n",
|
52 |
+
" Attempting uninstall: huggingface_hub\n",
|
53 |
+
" Found existing installation: huggingface-hub 0.23.5\n",
|
54 |
+
" Uninstalling huggingface-hub-0.23.5:\n",
|
55 |
+
" Successfully uninstalled huggingface-hub-0.23.5\n",
|
56 |
+
"Successfully installed huggingface_hub-0.24.0\n"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"output_type": "display_data",
|
61 |
+
"data": {
|
62 |
+
"application/vnd.colab-display-data+json": {
|
63 |
+
"pip_warning": {
|
64 |
+
"packages": [
|
65 |
+
"huggingface_hub"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
"id": "991053a6adb54643aed1b64c87e5d2b8"
|
69 |
+
}
|
70 |
+
},
|
71 |
+
"metadata": {}
|
72 |
+
}
|
73 |
+
],
|
74 |
+
"source": [
|
75 |
+
"!pip install -q git+https://github.com/huggingface/diffusers.git\n",
|
76 |
+
"!pip install --upgrade huggingface_hub"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "markdown",
|
81 |
+
"source": [
|
82 |
+
"#### The Stable Diffusion 3 medium model is gated. Get access at:\n",
|
83 |
+
"\n",
|
84 |
+
"https://huggingface.co/stabilityai/stable-diffusion-3-medium\n",
|
85 |
+
"\n",
|
86 |
+
"Make sure you add your Hugging Face token and sign in."
|
87 |
+
],
|
88 |
+
"metadata": {
|
89 |
+
"id": "HaYOIlR25nb2"
|
90 |
+
}
|
91 |
+
},
|
92 |
+
{
|
93 |
+
"cell_type": "code",
|
94 |
+
"source": [
|
95 |
+
"hf_token = \"hf_----\"\n",
|
96 |
+
"from huggingface_hub import login\n",
|
97 |
+
"login(token=hf_token_write)"
|
98 |
+
],
|
99 |
+
"metadata": {
|
100 |
+
"id": "hlVgYHNH5hgw"
|
101 |
+
},
|
102 |
+
"execution_count": null,
|
103 |
+
"outputs": []
|
104 |
+
},
|
105 |
+
{
|
106 |
+
"cell_type": "code",
|
107 |
+
"execution_count": null,
|
108 |
+
"metadata": {
|
109 |
+
"id": "GSfz5tNZyC7V"
|
110 |
+
},
|
111 |
+
"outputs": [],
|
112 |
+
"source": [
|
113 |
+
"from diffusers import DiffusionPipeline\n",
|
114 |
+
"import torch\n",
|
115 |
+
"import os\n",
|
116 |
+
"from datetime import datetime\n",
|
117 |
+
"from PIL import Image\n",
|
118 |
+
"\n",
|
119 |
+
"def generate_filename(base_name, extension=\".png\"):\n",
|
120 |
+
" timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
|
121 |
+
" return f\"{base_name}_{timestamp}{extension}\"\n",
|
122 |
+
"\n",
|
123 |
+
"def save_image(image, directory, base_name=\"image_grid\"):\n",
|
124 |
+
"\n",
|
125 |
+
" filename = generate_filename(base_name)\n",
|
126 |
+
" file_path = os.path.join(directory, filename)\n",
|
127 |
+
" image.save(file_path)\n",
|
128 |
+
" print(f\"Image saved as {file_path}\")\n",
|
129 |
+
"\n",
|
130 |
+
"def image_grid(imgs, rows, cols, save=True, save_dir='generated_images', base_name=\"image_grid\",\n",
|
131 |
+
" save_individual_files=False):\n",
|
132 |
+
"\n",
|
133 |
+
" if not os.path.exists(save_dir):\n",
|
134 |
+
" os.makedirs(save_dir)\n",
|
135 |
+
"\n",
|
136 |
+
" assert len(imgs) == rows * cols\n",
|
137 |
+
"\n",
|
138 |
+
" w, h = imgs[0].size\n",
|
139 |
+
" grid = Image.new('RGB', size=(cols * w, rows * h))\n",
|
140 |
+
" grid_w, grid_h = grid.size\n",
|
141 |
+
"\n",
|
142 |
+
" for i, img in enumerate(imgs):\n",
|
143 |
+
" grid.paste(img, box=(i % cols * w, i // cols * h))\n",
|
144 |
+
" if save_individual_files:\n",
|
145 |
+
" save_image(img, save_dir, base_name=base_name+f'_{i}-of-{len(imgs)}_')\n",
|
146 |
+
"\n",
|
147 |
+
" if save and save_dir:\n",
|
148 |
+
" save_image(grid, save_dir, base_name)\n",
|
149 |
+
"\n",
|
150 |
+
" return grid"
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "code",
|
155 |
+
"execution_count": null,
|
156 |
+
"metadata": {
|
157 |
+
"id": "xaeDDt0IyYvh"
|
158 |
+
},
|
159 |
+
"outputs": [],
|
160 |
+
"source": [
|
161 |
+
"repo_id_load='lamm-mit/stable-diffusion-3-medium-leaf-inspired'\n",
|
162 |
+
"\n",
|
163 |
+
"pipeline = DiffusionPipeline.from_pretrained (\"stabilityai/stable-diffusion-3-medium-diffusers\",\n",
|
164 |
+
" torch_dtype=torch.float16\n",
|
165 |
+
" )\n",
|
166 |
+
"\n",
|
167 |
+
"pipeline.load_lora_weights(repo_id_load)\n",
|
168 |
+
"pipeline=pipeline.to('cuda')"
|
169 |
+
]
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"cell_type": "code",
|
173 |
+
"execution_count": null,
|
174 |
+
"metadata": {
|
175 |
+
"id": "vqdIKEdy0BrU"
|
176 |
+
},
|
177 |
+
"outputs": [],
|
178 |
+
"source": [
|
179 |
+
"prompt = \"a cube in the shape of a <leaf microstructure>\"\n",
|
180 |
+
"negative_prompt = \"\"\n",
|
181 |
+
"\n",
|
182 |
+
"num_samples = 2\n",
|
183 |
+
"num_rows = 2\n",
|
184 |
+
"n_steps=75\n",
|
185 |
+
"guidance_scale=15\n",
|
186 |
+
"all_images = []\n",
|
187 |
+
"\n",
|
188 |
+
"for _ in range(num_rows):\n",
|
189 |
+
" image = pipeline(prompt,num_inference_steps=n_steps,num_images_per_prompt=num_samples,\n",
|
190 |
+
" guidance_scale=guidance_scale,negative_prompt=negative_prompt).images\n",
|
191 |
+
"\n",
|
192 |
+
" all_images.extend(image)\n",
|
193 |
+
"\n",
|
194 |
+
"grid = image_grid(all_images, num_rows, num_samples,\n",
|
195 |
+
" save_individual_files=True,\n",
|
196 |
+
" save_dir='generated_images',\n",
|
197 |
+
" base_name=\"image_grid\",\n",
|
198 |
+
" )\n",
|
199 |
+
"grid"
|
200 |
+
]
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"cell_type": "code",
|
204 |
+
"source": [],
|
205 |
+
"metadata": {
|
206 |
+
"id": "2yGuDJTH2zvx"
|
207 |
+
},
|
208 |
+
"execution_count": null,
|
209 |
+
"outputs": []
|
210 |
+
}
|
211 |
+
],
|
212 |
+
"metadata": {
|
213 |
+
"accelerator": "GPU",
|
214 |
+
"colab": {
|
215 |
+
"gpuType": "A100",
|
216 |
+
"machine_shape": "hm",
|
217 |
+
"provenance": []
|
218 |
+
},
|
219 |
+
"kernelspec": {
|
220 |
+
"display_name": "Python 3",
|
221 |
+
"name": "python3"
|
222 |
+
},
|
223 |
+
"language_info": {
|
224 |
+
"name": "python"
|
225 |
+
}
|
226 |
+
},
|
227 |
+
"nbformat": 4,
|
228 |
+
"nbformat_minor": 0
|
229 |
+
}
|