{ "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", "mbuehler@MIT.EDU\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 \"\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 }