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