From 314678ed11a57be17ca85c606e3736423b3fb059 Mon Sep 17 00:00:00 2001 From: Parag Ekbote Date: Wed, 19 Mar 2025 18:01:30 +0000 Subject: [PATCH 1/3] Add 4 Notebooks for diffusers community pipelines. --- diffusers/composable_stable_diffusion.ipynb | 155 ++++++++++++ ...to_image_inpainting_stable_diffusion.ipynb | 116 +++++++++ diffusers/prompt_2_prompt_pipeline.ipynb | 161 ++++++++++++ ...text2image_stable_diffusion_pipeline.ipynb | 230 ++++++++++++++++++ 4 files changed, 662 insertions(+) create mode 100644 diffusers/composable_stable_diffusion.ipynb create mode 100644 diffusers/image_to_image_inpainting_stable_diffusion.ipynb create mode 100644 diffusers/prompt_2_prompt_pipeline.ipynb create mode 100644 diffusers/tensorrt_text2image_stable_diffusion_pipeline.ipynb diff --git a/diffusers/composable_stable_diffusion.ipynb b/diffusers/composable_stable_diffusion.ipynb new file mode 100644 index 00000000..b33abef0 --- /dev/null +++ b/diffusers/composable_stable_diffusion.ipynb @@ -0,0 +1,155 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Composable Stable diffusion\n", + "\n", + "[Composable Stable Diffusion](https://energy-based-model.github.io/Compositional-Visual-Generation-with-Composable-Diffusion-Models/) proposes conjunction and negation (negative prompts) operators for compositional generation with conditional diffusion models. This script was contributed by [MarkRich](https://github.com/MarkRich) and the notebook by [Parag Ekbote](https://github.com/ParagEkbote)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pip install torch numpy torchvision diffusers" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "3dadcf1262e0492cafe9556f62ba3a9f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "composable_stable_diffusion.py: 0%| | 0.00/27.6k [00:00.\n", + "cannot get type annotation for Parameter text_encoder of .\n", + "cannot get type annotation for Parameter tokenizer of .\n", + "cannot get type annotation for Parameter unet of .\n", + "cannot get type annotation for Parameter scheduler of .\n", + "cannot get type annotation for Parameter safety_checker of .\n", + "cannot get type annotation for Parameter feature_extractor of .\n", + "cannot get type annotation for Parameter image_encoder of .\n", + "cannot get type annotation for Parameter requires_safety_checker of .\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "3e47bb32a31d4d6fb2e0fc2a197ac074", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Loading pipeline components...: 0%| | 0/7 [00:00=1.20 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnx) (1.26.4)\n", + "Requirement already satisfied: protobuf>=3.20.2 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnx) (6.30.1)\n", + "Requirement already satisfied: cuda-bindings~=12.8.0 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from cuda-python) (12.8.0)\n", + "Requirement already satisfied: tensorrt_cu12==10.9.0.34 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from tensorrt) (10.9.0.34)\n", + "Requirement already satisfied: tensorrt_cu12_libs==10.9.0.34 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from tensorrt_cu12==10.9.0.34->tensorrt) (10.9.0.34)\n", + "Requirement already satisfied: tensorrt_cu12_bindings==10.9.0.34 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from tensorrt_cu12==10.9.0.34->tensorrt) (10.9.0.34)\n", + "Requirement already satisfied: nvidia-cuda-runtime-cu12 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from tensorrt_cu12_libs==10.9.0.34->tensorrt_cu12==10.9.0.34->tensorrt) (12.4.127)\n", + "Requirement already satisfied: coloredlogs in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnxruntime-gpu) (15.0.1)\n", + "Requirement already satisfied: flatbuffers in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnxruntime-gpu) (25.2.10)\n", + "Requirement already satisfied: packaging in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnxruntime-gpu) (24.2)\n", + "Requirement already satisfied: sympy in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from onnxruntime-gpu) (1.13.1)\n", + "Requirement already satisfied: humanfriendly>=9.1 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from coloredlogs->onnxruntime-gpu) (10.0)\n", + "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /system/conda/miniconda3/envs/cloudspace/lib/python3.10/site-packages (from sympy->onnxruntime-gpu) (1.3.0)\n", + "Downloading colored-2.3.0-py3-none-any.whl (18 kB)\n", + "Installing collected packages: colored\n", + "Successfully installed colored-2.3.0\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "pip install polygraphy onnx cuda-python onnx-graphsurgeon tensorrt onnxruntime-gpu colored" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e388ba8b82364f27be70166a7d74439c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Loading pipeline components...: 0%| | 0/6 [00:00 Date: Thu, 20 Mar 2025 04:00:24 +0000 Subject: [PATCH 2/3] update as per code review. --- ...to_image_inpainting_stable_diffusion.ipynb | 23 ++++++++++++------- diffusers/prompt_2_prompt_pipeline.ipynb | 10 +++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/diffusers/image_to_image_inpainting_stable_diffusion.ipynb b/diffusers/image_to_image_inpainting_stable_diffusion.ipynb index dbfa8315..f2326966 100644 --- a/diffusers/image_to_image_inpainting_stable_diffusion.ipynb +++ b/diffusers/image_to_image_inpainting_stable_diffusion.ipynb @@ -24,13 +24,13 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "3988967ef7f8488abaf43c4a712e27da", + "model_id": "0b56a945eb5145598c4fd153bc658786", "version_major": 2, "version_minor": 0 }, @@ -45,16 +45,22 @@ "name": "stderr", "output_type": "stream", "text": [ - "An error occurred while trying to fetch /home/zeus/.cache/huggingface/hub/models--runwayml--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/vae: Error no file named diffusion_pytorch_model.safetensors found in directory /home/zeus/.cache/huggingface/hub/models--runwayml--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/vae.\n", - "Defaulting to unsafe serialization. Pass `allow_pickle=False` to raise an error instead.\n", - "An error occurred while trying to fetch /home/zeus/.cache/huggingface/hub/models--runwayml--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/unet: Error no file named diffusion_pytorch_model.safetensors found in directory /home/zeus/.cache/huggingface/hub/models--runwayml--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/unet.\n", + "An error occurred while trying to fetch /home/zeus/.cache/huggingface/hub/models--stable-diffusion-v1-5--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/unet: Error no file named diffusion_pytorch_model.safetensors found in directory /home/zeus/.cache/huggingface/hub/models--stable-diffusion-v1-5--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/unet.\n", + "Defaulting to unsafe serialization. Pass `allow_pickle=False` to raise an error instead.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "An error occurred while trying to fetch /home/zeus/.cache/huggingface/hub/models--stable-diffusion-v1-5--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/vae: Error no file named diffusion_pytorch_model.safetensors found in directory /home/zeus/.cache/huggingface/hub/models--stable-diffusion-v1-5--stable-diffusion-inpainting/snapshots/8a4288a76071f7280aedbdb3253bdb9e9d5d84bb/vae.\n", "Defaulting to unsafe serialization. Pass `allow_pickle=False` to raise an error instead.\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "b393bb433c3f46ba9a9ed392f534742d", + "model_id": "cfa34d5822784b449f9014f3f1b0e4ef", "version_major": 2, "version_minor": 0 }, @@ -93,14 +99,15 @@ "\n", "# Load the pipeline\n", "pipe = DiffusionPipeline.from_pretrained(\n", - " \"runwayml/stable-diffusion-inpainting\",\n", + " \"stable-diffusion-v1-5/stable-diffusion-inpainting\",\n", + " custom_pipeline=\"img2img_inpainting\",\n", " torch_dtype=torch.float16\n", ")\n", "pipe = pipe.to(\"cuda\")\n", "\n", "# Inpainting\n", "prompt = \"a mecha robot sitting on a bench\"\n", - "image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]\n", + "image = pipe(prompt=prompt, image=init_image, inner_image=inner_image, mask_image=mask_image).images[0]\n", "\n", "image.save(\"output.png\")\n" ] diff --git a/diffusers/prompt_2_prompt_pipeline.ipynb b/diffusers/prompt_2_prompt_pipeline.ipynb index 1756f53d..2e892235 100644 --- a/diffusers/prompt_2_prompt_pipeline.ipynb +++ b/diffusers/prompt_2_prompt_pipeline.ipynb @@ -18,6 +18,7 @@ "\n", "ReplaceEdit with local blend\n", "\n", + "```python\n", "prompts = [\"A turtle playing with a ball\",\n", " \"A monkey playing with a ball\"]\n", "\n", @@ -27,8 +28,10 @@ " \"self_replace_steps\": 0.4,\n", " \"local_blend_words\": [\"turtle\", \"monkey\"]\n", "}\n", + "```\n", "RefineEdit\n", "\n", + "```python\n", "prompts = [\"A turtle\",\n", " \"A turtle in a forest\"]\n", "\n", @@ -37,8 +40,10 @@ " \"cross_replace_steps\": 0.4,\n", " \"self_replace_steps\": 0.4,\n", "}\n", - "RefineEdit with local blend\n", + "```\n", "\n", + "RefineEdit with local blend\n", + "```python\n", "prompts = [\"A turtle\",\n", " \"A turtle in a forest\"]\n", "\n", @@ -48,8 +53,10 @@ " \"self_replace_steps\": 0.4,\n", " \"local_blend_words\": [\"in\", \"a\" , \"forest\"]\n", "}\n", + "```\n", "ReweightEdit\n", "\n", + "```python\n", "prompts = [\"A smiling turtle\"] * 2\n", "\n", "edit_kcross_attention_kwargswargs = {\n", @@ -59,6 +66,7 @@ " \"equalizer_words\": [\"smiling\"],\n", " \"equalizer_strengths\": [5]\n", "}\n", + "```\n", "\n", "Side note: See this [GitHub gist](https://gist.github.com/UmerHA/b65bb5fb9626c9c73f3ade2869e36164) if you want to visualize the attention maps. This script was contributed by [Umer Adil](https://github.com/UmerHA) and the notebook by [Parag Ekbote](https://github.com/ParagEkbote)." ] From 18168244d8f6fb57c3f7241155645e7fd7237061 Mon Sep 17 00:00:00 2001 From: Parag Ekbote Date: Thu, 20 Mar 2025 13:11:11 +0000 Subject: [PATCH 3/3] Update prompt_2_prompt_pipeline with proper subheading. --- diffusers/prompt_2_prompt_pipeline.ipynb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/diffusers/prompt_2_prompt_pipeline.ipynb b/diffusers/prompt_2_prompt_pipeline.ipynb index 2e892235..ed59af12 100644 --- a/diffusers/prompt_2_prompt_pipeline.ipynb +++ b/diffusers/prompt_2_prompt_pipeline.ipynb @@ -29,6 +29,7 @@ " \"local_blend_words\": [\"turtle\", \"monkey\"]\n", "}\n", "```\n", + "\n", "RefineEdit\n", "\n", "```python\n", @@ -43,6 +44,7 @@ "```\n", "\n", "RefineEdit with local blend\n", + "\n", "```python\n", "prompts = [\"A turtle\",\n", " \"A turtle in a forest\"]\n", @@ -54,6 +56,7 @@ " \"local_blend_words\": [\"in\", \"a\" , \"forest\"]\n", "}\n", "```\n", + "\n", "ReweightEdit\n", "\n", "```python\n",