├── .github └── FUNDING.yml ├── LICENSE ├── README.md ├── flax_stable_diffusion.ipynb ├── flax_stable_diffusion_2.ipynb ├── flax_stable_diffusion_3.ipynb ├── pytorch_freeu_stable_diffusion.ipynb ├── pytorch_riffusion_stable_diffusion.ipynb ├── pytorch_stable_diffusion.ipynb ├── pytorch_stable_diffusion_2.ipynb └── pytorch_stable_diffusion_3.ipynb /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: camenduru 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: camenduru 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🐣 Please follow me for new updates https://twitter.com/camenduru
2 | 🔥 Please join our discord server https://discord.gg/k5BwmmvJJU
3 | 🥳 Please join my patreon community https://patreon.com/camenduru
4 | 5 | ## 🦒 Colab TPU 6 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/flax_stable_diffusion.ipynb) flax_stable_diffusion 7 | 8 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/flax_stable_diffusion_2.ipynb) flax_stable_diffusion_2 (🏗WIP) 9 | 10 | ## 🦆 Kaggle TPU 11 | https://www.kaggle.com/code/camenduru/flax-stable-diffusion 12 | 13 | | 🤗 HuggingFace Path | Model Page | 14 | | --- | --- | 15 | | flax/anything-v3.0 | https://huggingface.co/Linaqruf/anything-v3.0 16 | | flax/samdoesart `samdoesarts style` | https://huggingface.co/lilpotat/sa 17 | | flax/Stable_Diffusion_PaperCut_Model `PaperCut style` | https://huggingface.co/Fictiverse/Stable_Diffusion_PaperCut_Model 18 | | flax/redshift-diffusion `redshift style` | https://huggingface.co/nitrosocke/redshift-diffusion 19 | | flax/sd15 | https://huggingface.co/runwayml/stable-diffusion-v1-5 20 | | flax/sd15-non-ema | https://huggingface.co/runwayml/stable-diffusion-v1-5 21 | | flax/midjourney-v4-diffusion `mdjrny-v4 style` | https://huggingface.co/prompthero/midjourney-v4-diffusion 22 | | flax/StudioGhibli `studio_ghibli_anime_style style` | https://huggingface.co/IShallRiseAgain/StudioGhibli 23 | | flax/Tron-Legacy-diffusion `trnlgcy style` | https://huggingface.co/dallinmackay/Tron-Legacy-diffusion 24 | | flax/Cyberpunk-Anime-Diffusion `dgs illustration style` | https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion 25 | | flax/waifu-diffusion | https://huggingface.co/hakurei/waifu-diffusion-v1-3 26 | | flax/Arcane-Diffusion `arcane style` | https://huggingface.co/nitrosocke/Arcane-Diffusion 27 | | flax/classic-anim-diffusion `classic disney style` | https://huggingface.co/nitrosocke/classic-anim-diffusion 28 | | flax/mo-di-diffusion `modern disney style` | https://huggingface.co/nitrosocke/mo-di-diffusion 29 | | camenduru/plushies | https://huggingface.co/camenduru/plushies 30 | | flax/stable-diffusion-2 | https://huggingface.co/stabilityai/stable-diffusion-2 31 | | flax/stable-diffusion-2-base | https://huggingface.co/stabilityai/stable-diffusion-2-base 32 | 33 | ## 🦒 Colab GPU 34 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_stable_diffusion.ipynb) pytorch_stable_diffusion 35 | 36 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_stable_diffusion_2.ipynb) pytorch_stable_diffusion_2 (🏗WIP) 37 | 38 | ## 🦆 Kaggle GPU 39 | https://www.kaggle.com/code/camenduru/pytorch-stable-diffusion 40 | 41 | | 🤗 HuggingFace Path | Model Page | 42 | | --- | --- | 43 | | Linaqruf/anything-v3.0 | https://huggingface.co/Linaqruf/anything-v3.0 44 | | lilpotat/sa `samdoesarts style` | https://huggingface.co/lilpotat/sa 45 | | Fictiverse/Stable_Diffusion_PaperCut_Model `PaperCut style` | https://huggingface.co/Fictiverse/Stable_Diffusion_PaperCut_Model 46 | | nitrosocke/redshift-diffusion `redshift style` | https://huggingface.co/nitrosocke/redshift-diffusion 47 | | camenduru/sd15 | https://huggingface.co/runwayml/stable-diffusion-v1-5 48 | | prompthero/midjourney-v4-diffusion `mdjrny-v4 style` | https://huggingface.co/prompthero/midjourney-v4-diffusion 49 | | dallinmackay/Tron-Legacy-diffusion `trnlgcy style` | https://huggingface.co/dallinmackay/Tron-Legacy-diffusion 50 | | DGSpitzer/Cyberpunk-Anime-Diffusion `dgs illustration style` | https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion 51 | | hakurei/waifu-diffusion-v1-3 | https://huggingface.co/hakurei/waifu-diffusion-v1-3 52 | | nitrosocke/Arcane-Diffusion `arcane style` | https://huggingface.co/nitrosocke/Arcane-Diffusion 53 | | nitrosocke/classic-anim-diffusion `classic disney style` | https://huggingface.co/nitrosocke/classic-anim-diffusion 54 | | nitrosocke/mo-di-diffusion `modern disney style` | https://huggingface.co/nitrosocke/mo-di-diffusion 55 | 56 | ## Models License 57 | https://huggingface.co/spaces/CompVis/stable-diffusion-license 58 | -------------------------------------------------------------------------------- /flax_stable_diffusion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/flax_stable_diffusion.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "cellView": "form", 17 | "id": "ymcDoPQn41U4" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "save_to = \"here\" #@param [\"here\", \"discord\", \"gdrive\"]\n", 22 | "if save_to == \"gdrive\":\n", 23 | " from google.colab import drive\n", 24 | " drive.mount('/content/gdrive')\n", 25 | "\n", 26 | "!pip install -U jax==0.3.25 jaxlib==0.3.25 flax==0.6.2 transformers piexif fold_to_ascii ftfy\n", 27 | "\n", 28 | "is_tile = False #@param {type: 'boolean'}\n", 29 | "if(is_tile):\n", 30 | " !pip install -U git+https://github.com/camenduru/diffusers@padding\n", 31 | "else:\n", 32 | " !pip install -U git+https://github.com/camenduru/diffusers@v0.9.1\n", 33 | "\n", 34 | "import jax.tools.colab_tpu\n", 35 | "jax.tools.colab_tpu.setup_tpu('tpu_driver_nightly')\n", 36 | "\n", 37 | "import jax, random, os, gc, requests, json, piexif\n", 38 | "import numpy as np\n", 39 | "from flax.jax_utils import replicate\n", 40 | "from flax.training.common_utils import shard\n", 41 | "from diffusers import FlaxStableDiffusionPipeline\n", 42 | "from PIL import Image\n", 43 | "from PIL.PngImagePlugin import PngInfo\n", 44 | "from fold_to_ascii import fold\n", 45 | "metadata = PngInfo()\n", 46 | "\n", 47 | "def closestNumber(n, m):\n", 48 | " q = int(n / m)\n", 49 | " n1 = m * q\n", 50 | " if (n * m) > 0:\n", 51 | " n2 = m * (q + 1)\n", 52 | " else:\n", 53 | " n2 = m * (q - 1)\n", 54 | " if abs(n - n1) < abs(n - n2):\n", 55 | " return n1\n", 56 | " return n2\n", 57 | "\n", 58 | "def image_grid(imgs, rows, cols):\n", 59 | " w,h = imgs[0].size\n", 60 | " grid = Image.new('RGB', size=(cols*w, rows*h))\n", 61 | " for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h))\n", 62 | " return grid\n", 63 | "\n", 64 | "folder_max_files = 500 #@param {type: 'integer'}\n", 65 | "root_folder = 'ai_images' #@param {type: 'string'}\n", 66 | "if save_to == \"gdrive\":\n", 67 | " root_folder = f\"/content/gdrive/MyDrive/{root_folder}\"\n", 68 | "\n", 69 | "if os.path.exists(f\"{root_folder}\") == False:\n", 70 | " os.mkdir(f\"{root_folder}\")\n", 71 | "image_folder = max([int(f) for f in os.listdir(f\"{root_folder}\")], default=0)\n", 72 | "if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 73 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 74 | "name = max([int(f[: f.index(\".\")]) for f in os.listdir(f\"{root_folder}/{image_folder:04}\")],default=0,)\n", 75 | "\n", 76 | "model_folder = \"flax/sd15\" #@param [\"flax/anything-v3.0\", \"flax/samdoesart\", \"flax/Stable_Diffusion_PaperCut_Model\", \"flax/redshift-diffusion\", \"flax/sd15\", \"flax/sd15-non-ema\", \"flax/midjourney-v4-diffusion\", \"flax/StudioGhibli\", \"flax/Tron-Legacy-diffusion\", \"flax/Cyberpunk-Anime-Diffusion\", \"flax/waifu-diffusion\", \"flax/Arcane-Diffusion\", \"flax/classic-anim-diffusion\", \"flax/mo-di-diffusion\", \"camenduru/plushies\", \"lilpotat/f2\", \"lilpotat/rbm\"] {allow-input: true}\n", 77 | "if model_folder == \"lilpotat/f2\" or model_folder == \"lilpotat/rbm\":\n", 78 | " revision = \"flax\"\n", 79 | "else:\n", 80 | " revision = \"main\"\n", 81 | "pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_folder, revision=revision, from_pt=False, dtype=jax.numpy.bfloat16, safety_checker=None)\n", 82 | "params = replicate(params)\n", 83 | "\n", 84 | "def generate(discord_token, discord_channel_id, discord_user, by, num_inference_steps, guidance_scale, sampler, width, height, prompt, negative_prompt, suffix, image_folder, name):\n", 85 | " width = closestNumber(width, 8)\n", 86 | " height = closestNumber(height, 8)\n", 87 | " metadata.add_text(\"Prompt\", f\"{prompt}\")\n", 88 | " metadata.add_text(\"by\", f\"{by}\")\n", 89 | " gc.collect()\n", 90 | " real_seed = random.randint(0, 2147483647)\n", 91 | " prng_seed = jax.random.PRNGKey(real_seed)\n", 92 | " prng_seed = jax.random.split(prng_seed, jax.device_count())\n", 93 | " num_samples = jax.device_count()\n", 94 | " prompt_n = num_samples * [prompt]\n", 95 | " prompt_ids = pipe.prepare_inputs(prompt_n)\n", 96 | " prompt_ids = shard(prompt_ids)\n", 97 | " negative_prompt_n = num_samples * [negative_prompt]\n", 98 | " negative_prompt_ids = pipe.prepare_inputs(negative_prompt_n)\n", 99 | " negative_prompt_ids = shard(negative_prompt_ids)\n", 100 | " images = pipe(prompt_ids, params, prng_seed, neg_prompt_ids=negative_prompt_ids, num_inference_steps=num_inference_steps, height=height, width=width, guidance_scale=guidance_scale, jit=True).images\n", 101 | " images = pipe.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))\n", 102 | " image = image_grid(images, 2, 4)\n", 103 | " if(suffix == 'png'):\n", 104 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", pnginfo=metadata)\n", 105 | " else:\n", 106 | " zeroth_ifd = {piexif.ImageIFD.ImageDescription: f\"{fold(prompt)}\", piexif.ImageIFD.Make: f\"{fold(by)}\", piexif.ImageIFD.Model: f\"{model_folder}\"}\n", 107 | " exif_dict = {\"0th\": zeroth_ifd}\n", 108 | " exif_bytes = piexif.dump(exif_dict)\n", 109 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"JPEG\", quality=70, exif=exif_bytes)\n", 110 | " files = {f\"{image_folder:04}_{name:04}.{suffix}\": open(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"rb\").read()}\n", 111 | " if save_to == \"discord\":\n", 112 | " payload = {\"content\": f\"{prompt}\\nNegative prompt: {negative_prompt}\\nSteps: {num_inference_steps}, Sampler: {sampler}, CFG scale: {guidance_scale}, Seed: {real_seed}, Size: {width}x{height}, Model folder: {model_folder} - {discord_user}\"}\n", 113 | " requests.post(f\"https://discord.com/api/v9/channels/{discord_channel_id}/messages\", data=payload, headers={\"authorization\": f\"Bot {discord_token}\"}, files=files)\n", 114 | " os.remove(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": { 121 | "cellView": "form", 122 | "id": "DDUev8TOMxJ4" 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "discord_token = \"token\" #@param {type: 'string'}\n", 127 | "discord_channel_id = 0 #@param {type: 'integer'}\n", 128 | "prompt = \"duck\" #@param {type: 'string'}\n", 129 | "negative_prompt = \"\" #@param {type: 'string'}\n", 130 | "width = 512 #@param {type: 'integer'}\n", 131 | "height = 512 #@param {type: 'integer'}\n", 132 | "guidance_scale = 7.5 #@param {type: 'number'}\n", 133 | "num_inference_steps = 50 #@param {type: 'integer'}\n", 134 | "suffix = \"jpg\" #@param [\"jpg\", \"png\"]\n", 135 | "by = \"camenduru\" #@param {type: 'string'}\n", 136 | "template = {\n", 137 | " \"discord_token\": discord_token,\n", 138 | " \"discord_channel_id\": discord_channel_id,\n", 139 | " \"by\": by,\n", 140 | " \"num_inference_steps\": num_inference_steps,\n", 141 | " \"guidance_scale\": guidance_scale,\n", 142 | " \"sampler\": \"PLMS\",\n", 143 | " \"width\": width,\n", 144 | " \"height\": height,\n", 145 | " \"prompt\": prompt,\n", 146 | " \"negative_prompt\": negative_prompt,\n", 147 | " \"suffix\": suffix\n", 148 | " }\n", 149 | "with open(\"template.json\", \"w\") as outfile:\n", 150 | " outfile.write(json.dumps(template))\n", 151 | "\n", 152 | "is_loop = False #@param {type:\"boolean\"}\n", 153 | "\n", 154 | "if is_loop:\n", 155 | " while True:\n", 156 | " if name < folder_max_files:\n", 157 | " with open(\"template.json\", \"r\") as file:\n", 158 | " prompts = file.readlines()\n", 159 | " for prompt in prompts:\n", 160 | " d = json.loads(prompt)\n", 161 | " name += 1\n", 162 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 163 | " else:\n", 164 | " image_folder += 1\n", 165 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 166 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 167 | " name = 0\n", 168 | "else:\n", 169 | " if name < folder_max_files:\n", 170 | " with open(\"template.json\", \"r\") as file:\n", 171 | " prompts = file.readlines()\n", 172 | " for prompt in prompts:\n", 173 | " d = json.loads(prompt)\n", 174 | " name += 1\n", 175 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 176 | " else:\n", 177 | " image_folder += 1\n", 178 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 179 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 180 | " name = 0" 181 | ] 182 | } 183 | ], 184 | "metadata": { 185 | "accelerator": "TPU", 186 | "colab": { 187 | "provenance": [] 188 | }, 189 | "gpuClass": "standard", 190 | "kernelspec": { 191 | "display_name": "Python 3", 192 | "name": "python3" 193 | }, 194 | "language_info": { 195 | "name": "python" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 0 200 | } 201 | -------------------------------------------------------------------------------- /flax_stable_diffusion_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/flax_stable_diffusion_2.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "cellView": "form", 17 | "id": "ymcDoPQn41U4" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "save_to = \"here\" #@param [\"here\", \"discord\", \"gdrive\"]\n", 22 | "if save_to == \"gdrive\":\n", 23 | " from google.colab import drive\n", 24 | " drive.mount('/content/gdrive')\n", 25 | "\n", 26 | "!pip install -U jax==0.3.25 jaxlib==0.3.25 flax==0.6.2 transformers piexif fold_to_ascii ftfy\n", 27 | "!pip install -U diffusers==0.10.0\n", 28 | "\n", 29 | "import jax.tools.colab_tpu\n", 30 | "jax.tools.colab_tpu.setup_tpu('tpu_driver_nightly')\n", 31 | "\n", 32 | "import jax, random, os, gc, requests, json, piexif\n", 33 | "import numpy as np\n", 34 | "from flax.jax_utils import replicate\n", 35 | "from flax.training.common_utils import shard\n", 36 | "from diffusers import FlaxStableDiffusionPipeline\n", 37 | "from PIL import Image\n", 38 | "from PIL.PngImagePlugin import PngInfo\n", 39 | "from fold_to_ascii import fold\n", 40 | "metadata = PngInfo()\n", 41 | "\n", 42 | "def closestNumber(n, m):\n", 43 | " q = int(n / m)\n", 44 | " n1 = m * q\n", 45 | " if (n * m) > 0:\n", 46 | " n2 = m * (q + 1)\n", 47 | " else:\n", 48 | " n2 = m * (q - 1)\n", 49 | " if abs(n - n1) < abs(n - n2):\n", 50 | " return n1\n", 51 | " return n2\n", 52 | "\n", 53 | "def image_grid(imgs, rows, cols):\n", 54 | " w,h = imgs[0].size\n", 55 | " grid = Image.new('RGB', size=(cols*w, rows*h))\n", 56 | " for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h))\n", 57 | " return grid\n", 58 | "\n", 59 | "folder_max_files = 500 #@param {type: 'integer'}\n", 60 | "root_folder = 'ai_images' #@param {type: 'string'}\n", 61 | "if save_to == \"gdrive\":\n", 62 | " root_folder = f\"/content/gdrive/MyDrive/{root_folder}\"\n", 63 | "\n", 64 | "if os.path.exists(f\"{root_folder}\") == False:\n", 65 | " os.mkdir(f\"{root_folder}\")\n", 66 | "image_folder = max([int(f) for f in os.listdir(f\"{root_folder}\")], default=0)\n", 67 | "if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 68 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 69 | "name = max([int(f[: f.index(\".\")]) for f in os.listdir(f\"{root_folder}/{image_folder:04}\")],default=0,)\n", 70 | "\n", 71 | "model_folder = \"flax/stable-diffusion-2\" #@param [\"flax/stable-diffusion-2\", \"flax/stable-diffusion-2-base\"] {allow-input: true}\n", 72 | "revision = \"main\"\n", 73 | "pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_folder, revision=revision, from_pt=False, dtype=jax.numpy.bfloat16, safety_checker=None)\n", 74 | "params = replicate(params)\n", 75 | "\n", 76 | "def generate(discord_token, discord_channel_id, discord_user, by, num_inference_steps, guidance_scale, sampler, width, height, prompt, negative_prompt, suffix, image_folder, name):\n", 77 | " width = closestNumber(width, 8)\n", 78 | " height = closestNumber(height, 8)\n", 79 | " metadata.add_text(\"Prompt\", f\"{prompt}\")\n", 80 | " metadata.add_text(\"by\", f\"{by}\")\n", 81 | " gc.collect()\n", 82 | " real_seed = random.randint(0, 2147483647)\n", 83 | " prng_seed = jax.random.PRNGKey(real_seed)\n", 84 | " prng_seed = jax.random.split(prng_seed, jax.device_count())\n", 85 | " num_samples = jax.device_count()\n", 86 | " prompt_n = num_samples * [prompt]\n", 87 | " prompt_ids = pipe.prepare_inputs(prompt_n)\n", 88 | " prompt_ids = shard(prompt_ids)\n", 89 | " negative_prompt_n = num_samples * [negative_prompt]\n", 90 | " negative_prompt_ids = pipe.prepare_inputs(negative_prompt_n)\n", 91 | " negative_prompt_ids = shard(negative_prompt_ids)\n", 92 | " images = pipe(prompt_ids, params, prng_seed, neg_prompt_ids=negative_prompt_ids, num_inference_steps=num_inference_steps, height=height, width=width, guidance_scale=guidance_scale, jit=True).images\n", 93 | " images = pipe.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))\n", 94 | " image = image_grid(images, 2, 4)\n", 95 | " if(suffix == 'png'):\n", 96 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", pnginfo=metadata)\n", 97 | " else:\n", 98 | " zeroth_ifd = {piexif.ImageIFD.ImageDescription: f\"{fold(prompt)}\", piexif.ImageIFD.Make: f\"{fold(by)}\", piexif.ImageIFD.Model: f\"{model_folder}\"}\n", 99 | " exif_dict = {\"0th\": zeroth_ifd}\n", 100 | " exif_bytes = piexif.dump(exif_dict)\n", 101 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"JPEG\", quality=70, exif=exif_bytes)\n", 102 | " files = {f\"{image_folder:04}_{name:04}.{suffix}\": open(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"rb\").read()}\n", 103 | " if save_to == \"discord\":\n", 104 | " payload = {\"content\": f\"{prompt}\\nNegative prompt: {negative_prompt}\\nSteps: {num_inference_steps}, Sampler: {sampler}, CFG scale: {guidance_scale}, Seed: {real_seed}, Size: {width}x{height}, Model folder: {model_folder} - {discord_user}\"}\n", 105 | " requests.post(f\"https://discord.com/api/v9/channels/{discord_channel_id}/messages\", data=payload, headers={\"authorization\": f\"Bot {discord_token}\"}, files=files)\n", 106 | " os.remove(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\")" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": { 113 | "cellView": "form", 114 | "id": "DDUev8TOMxJ4" 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "discord_token = \"token\" #@param {type: 'string'}\n", 119 | "discord_channel_id = 0 #@param {type: 'integer'}\n", 120 | "prompt = \"duck\" #@param {type: 'string'}\n", 121 | "negative_prompt = \"\" #@param {type: 'string'}\n", 122 | "width = 512 #@param {type: 'integer'}\n", 123 | "height = 512 #@param {type: 'integer'}\n", 124 | "guidance_scale = 7.5 #@param {type: 'number'}\n", 125 | "num_inference_steps = 50 #@param {type: 'integer'}\n", 126 | "suffix = \"jpg\" #@param [\"jpg\", \"png\"]\n", 127 | "by = \"camenduru\" #@param {type: 'string'}\n", 128 | "template = {\n", 129 | " \"discord_token\": discord_token,\n", 130 | " \"discord_channel_id\": discord_channel_id,\n", 131 | " \"by\": by,\n", 132 | " \"num_inference_steps\": num_inference_steps,\n", 133 | " \"guidance_scale\": guidance_scale,\n", 134 | " \"sampler\": \"PLMS\",\n", 135 | " \"width\": width,\n", 136 | " \"height\": height,\n", 137 | " \"prompt\": prompt,\n", 138 | " \"negative_prompt\": negative_prompt,\n", 139 | " \"suffix\": suffix\n", 140 | " }\n", 141 | "with open(\"template.json\", \"w\") as outfile:\n", 142 | " outfile.write(json.dumps(template))\n", 143 | "\n", 144 | "is_loop = False #@param {type:\"boolean\"}\n", 145 | "\n", 146 | "if is_loop:\n", 147 | " while True:\n", 148 | " if name < folder_max_files:\n", 149 | " with open(\"template.json\", \"r\") as file:\n", 150 | " prompts = file.readlines()\n", 151 | " for prompt in prompts:\n", 152 | " d = json.loads(prompt)\n", 153 | " name += 1\n", 154 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 155 | " else:\n", 156 | " image_folder += 1\n", 157 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 158 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 159 | " name = 0\n", 160 | "else:\n", 161 | " if name < folder_max_files:\n", 162 | " with open(\"template.json\", \"r\") as file:\n", 163 | " prompts = file.readlines()\n", 164 | " for prompt in prompts:\n", 165 | " d = json.loads(prompt)\n", 166 | " name += 1\n", 167 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 168 | " else:\n", 169 | " image_folder += 1\n", 170 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 171 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 172 | " name = 0" 173 | ] 174 | } 175 | ], 176 | "metadata": { 177 | "accelerator": "TPU", 178 | "colab": { 179 | "provenance": [] 180 | }, 181 | "gpuClass": "standard", 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "name": "python3" 185 | }, 186 | "language_info": { 187 | "name": "python" 188 | } 189 | }, 190 | "nbformat": 4, 191 | "nbformat_minor": 0 192 | } 193 | -------------------------------------------------------------------------------- /flax_stable_diffusion_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/flax_stable_diffusion_3.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "#@title Install - Step 1\n", 19 | "!pip install -q jax==0.3.25 jaxlib==0.3.25 flax==0.6.2 transformers accelerate omegaconf\n", 20 | "!pip install -q git+https://github.com/huggingface/diffusers\n", 21 | "\n", 22 | "!apt -y install -qq aria2\n", 23 | "!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M 'https://civitai.com/api/download/models/138176?type=Model&format=SafeTensor&size=pruned&fp=fp16' -d /content/models -o cyberrealistic_v33.safetensors\n", 24 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M 'https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth' -d /content/models/controlnet -o control_v11p_sd15_canny.pth\n", 25 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M 'https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny_fp16.safetensors' -d /content/models/controlnet -o control_v11p_sd15_canny_fp16.safetensors" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "#@title Install - Step 2\n", 35 | "import jax, random, gc, torch\n", 36 | "from flax.jax_utils import replicate\n", 37 | "from flax.training.common_utils import shard\n", 38 | "from diffusers import StableDiffusionPipeline\n", 39 | "\n", 40 | "import numpy as np\n", 41 | "from PIL import Image\n", 42 | "\n", 43 | "import jax.tools.colab_tpu\n", 44 | "jax.tools.colab_tpu.setup_tpu('tpu_driver_nightly')\n", 45 | "jax.device_count()\n", 46 | "\n", 47 | "def image_grid(imgs, rows, cols):\n", 48 | " w,h = imgs[0].size\n", 49 | " grid = Image.new('RGB', size=(cols*w, rows*h))\n", 50 | " for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h))\n", 51 | " return grid" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "#@title Vanilla - Step 1\n", 61 | "pipeline = StableDiffusionPipeline.from_single_file('/content/models/cyberrealistic_v33.safetensors', torch_dtype=torch.float16, safety_checker=None)\n", 62 | "pipeline.save_pretrained('/content/models/torch/cyberrealistic', safe_serialization=False)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "#@title Vanilla - Step 2\n", 72 | "from diffusers import FlaxStableDiffusionPipeline\n", 73 | "\n", 74 | "pipeline, params = FlaxStableDiffusionPipeline.from_pretrained('/content/models/torch/cyberrealistic', from_pt=True, dtype=jax.numpy.bfloat16, safety_checker=None)\n", 75 | "params = replicate(params)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "#@title Vanilla - Step 3\n", 85 | "gc.collect()\n", 86 | "\n", 87 | "prompt = \"cute duck\"\n", 88 | "negative_prompt = \"blurry\"\n", 89 | "\n", 90 | "num_samples = jax.device_count()\n", 91 | "\n", 92 | "prompt_n = num_samples * [prompt]\n", 93 | "prompt_ids = pipeline.prepare_inputs(prompt_n)\n", 94 | "prompt_ids = shard(prompt_ids)\n", 95 | "\n", 96 | "negative_prompt_n = num_samples * [negative_prompt]\n", 97 | "negative_prompt_ids = pipeline.prepare_inputs(negative_prompt_n)\n", 98 | "negative_prompt_ids = shard(negative_prompt_ids)\n", 99 | "\n", 100 | "real_seed = random.randint(0, 2147483647)\n", 101 | "prng_seed = jax.random.PRNGKey(real_seed)\n", 102 | "prng_seed = jax.random.split(prng_seed, jax.device_count())\n", 103 | "\n", 104 | "images = pipeline(prompt_ids, params, prng_seed, neg_prompt_ids=negative_prompt_ids, num_inference_steps=50, height=512, width=512, guidance_scale=7, jit=True).images\n", 105 | "images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))\n", 106 | "\n", 107 | "image = image_grid(images, 2, 4)\n", 108 | "image" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "#@title ControlNet #Canny - Step 1\n", 118 | "import jax, torch\n", 119 | "from diffusers import FlaxStableDiffusionControlNetPipeline, ControlNetModel\n", 120 | "# controlnet = ControlNetModel.from_single_file('/content/models/controlnet/control_v11p_sd15_canny.pth', dtype=torch.float16) # invalid load key, '<'.\n", 121 | "controlnet = ControlNetModel.from_single_file('https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_canny.pth', dtype=torch.float16)\n", 122 | "pipeline, params = FlaxStableDiffusionControlNetPipeline.from_pretrained('/content/models/torch/cyberrealistic', controlnet=controlnet, dtype=jax.numpy.bfloat16, from_pt=True, safety_checker=None)\n", 123 | "params = replicate(params)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "#@title ControlNet #Canny - Step 2\n", 133 | "from diffusers.utils import load_image\n", 134 | "image = load_image(\"https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png\")\n", 135 | "image_raw = image\n", 136 | "\n", 137 | "import cv2\n", 138 | "from PIL import Image\n", 139 | "import numpy as np\n", 140 | "image = np.array(image)\n", 141 | "low_threshold = 100\n", 142 | "high_threshold = 200\n", 143 | "image = cv2.Canny(image, low_threshold, high_threshold)\n", 144 | "image = image[:, :, None]\n", 145 | "image = np.concatenate([image, image, image], axis=2)\n", 146 | "canny_image = Image.fromarray(image)\n", 147 | "image = image_grid([image_raw, canny_image], 1, 2)\n", 148 | "image" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "#@title ControlNet #Canny - Step 3\n", 158 | "gc.collect()\n", 159 | "\n", 160 | "prompt = \"cute duck\"\n", 161 | "negative_prompt = \"blurry, ugly\"\n", 162 | "\n", 163 | "num_samples = jax.device_count()\n", 164 | "\n", 165 | "prompt_n = num_samples * [prompt]\n", 166 | "prompt_ids = pipeline.prepare_inputs(prompt_n)\n", 167 | "prompt_ids = shard(prompt_ids)\n", 168 | "\n", 169 | "negative_prompt_n = num_samples * [negative_prompt]\n", 170 | "negative_prompt_ids = pipeline.prepare_inputs(negative_prompt_n)\n", 171 | "negative_prompt_ids = shard(negative_prompt_ids)\n", 172 | "\n", 173 | "real_seed = random.randint(0, 2147483647)\n", 174 | "prng_seed = jax.random.PRNGKey(real_seed)\n", 175 | "prng_seed = jax.random.split(prng_seed, jax.device_count())\n", 176 | "\n", 177 | "images = pipeline(prompt_ids, canny_image, params, prng_seed, neg_prompt_ids=negative_prompt_ids, num_inference_steps=50, height=512, width=512, guidance_scale=7, jit=True).images\n", 178 | "images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))\n", 179 | "\n", 180 | "image = image_grid(images, 2, 4)\n", 181 | "image" 182 | ] 183 | } 184 | ], 185 | "metadata": { 186 | "accelerator": "TPU", 187 | "colab": { 188 | "provenance": [] 189 | }, 190 | "gpuClass": "standard", 191 | "kernelspec": { 192 | "display_name": "Python 3", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "name": "python" 197 | } 198 | }, 199 | "nbformat": 4, 200 | "nbformat_minor": 0 201 | } 202 | -------------------------------------------------------------------------------- /pytorch_freeu_stable_diffusion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_freeu_stable_diffusion.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "vscode": { 17 | "languageId": "plaintext" 18 | } 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "# Main Repo: https://github.com/lyn-rgb/FreeU_Diffusers\n", 23 | "\n", 24 | "%cd /content\n", 25 | "!pip install -q transformers accelerate\n", 26 | "!pip install -q diffusers==0.21.2\n", 27 | "# !pip install -q diffusers==0.19.3\n", 28 | "!rm -rf /content/FreeU_Diffusers\n", 29 | "!git clone -b diffusers-v0.21.2 https://github.com/camenduru/FreeU_Diffusers\n", 30 | "%cd /content/FreeU_Diffusers" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "vscode": { 38 | "languageId": "plaintext" 39 | } 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "from diffusers import StableDiffusionPipeline\n", 44 | "import torch, sys\n", 45 | "sys.path.append('/content/FreeU_Diffusers')\n", 46 | "from free_lunch_utils import register_free_upblock2d, register_free_crossattn_upblock2d\n", 47 | "\n", 48 | "model_id = \"runwayml/stable-diffusion-v1-5\"\n", 49 | "pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(\"cuda\")\n", 50 | "\n", 51 | "# -------- freeu block registration\n", 52 | "register_free_upblock2d(pipe, b1=1.2, b2=1.4, s1=0.9, s2=0.2)\n", 53 | "register_free_crossattn_upblock2d(pipe, b1=1.2, b2=1.4, s1=0.9, s2=0.2)\n", 54 | "# -------- freeu block registration" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": { 61 | "vscode": { 62 | "languageId": "plaintext" 63 | } 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "prompt = \"a photo of an astronaut riding a horse on mars\"\n", 68 | "image = pipe(prompt).images[0] \n", 69 | "image.save(\"astronaut_rides_horse.png\")" 70 | ] 71 | } 72 | ], 73 | "metadata": { 74 | "accelerator": "TPU", 75 | "colab": { 76 | "provenance": [] 77 | }, 78 | "gpuClass": "standard", 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "name": "python" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 0 89 | } 90 | -------------------------------------------------------------------------------- /pytorch_riffusion_stable_diffusion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_riffusion_stable_diffusion.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "id": "ymcDoPQn41U4" 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "!pip install -q torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 torchtext==0.14.1 torchdata==0.5.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U\n", 21 | "!pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+189828c.d20221207-cp38-cp38-linux_x86_64.whl\n", 22 | "!wget -q https://raw.githubusercontent.com/hmartiro/riffusion-inference/main/riffusion/audio.py\n", 23 | "!pip install -q -U diffusers transformers ftfy pydub\n", 24 | "\n", 25 | "import torch, IPython\n", 26 | "from diffusers import StableDiffusionPipeline\n", 27 | "from audio import wav_bytes_from_spectrogram_image\n", 28 | "\n", 29 | "pipe = StableDiffusionPipeline.from_pretrained(\"riffusion/riffusion-model-v1\", torch_dtype=torch.float16).to(\"cuda\")\n", 30 | "pipe.enable_xformers_memory_efficient_attention()" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "id": "BKK30_wrQpww" 38 | }, 39 | "outputs": [], 40 | "source": [ 41 | "prompt = \"bongos on a havana street\" #@param {type: 'string'}\n", 42 | "\n", 43 | "with torch.autocast(\"cuda\"):\n", 44 | " image = pipe(prompt, height=512, width=512).images[0];\n", 45 | "wav = wav_bytes_from_spectrogram_image(image)\n", 46 | "with open(\"output.wav\", \"wb\") as f:\n", 47 | " f.write(wav[0].getbuffer())\n", 48 | "IPython.display.Audio(\"/content/output.wav\", rate=44100)" 49 | ] 50 | } 51 | ], 52 | "metadata": { 53 | "accelerator": "GPU", 54 | "colab": { 55 | "provenance": [] 56 | }, 57 | "gpuClass": "standard", 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "name": "python" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 0 68 | } 69 | -------------------------------------------------------------------------------- /pytorch_stable_diffusion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_stable_diffusion.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "cellView": "form", 17 | "id": "ymcDoPQn41U4" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "save_to = \"here\" #@param [\"here\", \"discord\", \"gdrive\"]\n", 22 | "if save_to == \"gdrive\":\n", 23 | " from google.colab import drive\n", 24 | " drive.mount('/content/gdrive')\n", 25 | "\n", 26 | "!pip install -q torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 torchtext==0.14.1 torchdata==0.5.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U\n", 27 | "!pip install -U diffusers transformers piexif fold_to_ascii ftfy\n", 28 | "!pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+189828c.d20221207-cp38-cp38-linux_x86_64.whl\n", 29 | "\n", 30 | "import torch, os, gc, requests, json, piexif\n", 31 | "from diffusers import StableDiffusionPipeline\n", 32 | "from PIL import Image\n", 33 | "from PIL.PngImagePlugin import PngInfo\n", 34 | "from fold_to_ascii import fold\n", 35 | "metadata = PngInfo()\n", 36 | "\n", 37 | "def closestNumber(n, m):\n", 38 | " q = int(n / m)\n", 39 | " n1 = m * q\n", 40 | " if (n * m) > 0:\n", 41 | " n2 = m * (q + 1)\n", 42 | " else:\n", 43 | " n2 = m * (q - 1)\n", 44 | " if abs(n - n1) < abs(n - n2):\n", 45 | " return n1\n", 46 | " return n2\n", 47 | "\n", 48 | "folder_max_files = 500 #@param {type: 'integer'}\n", 49 | "root_folder = \"ai_images\" #@param {type: 'string'}\n", 50 | "if save_to == \"gdrive\":\n", 51 | " root_folder = f\"/content/gdrive/MyDrive/{root_folder}\"\n", 52 | "\n", 53 | "if os.path.exists(f\"{root_folder}\") == False:\n", 54 | " os.mkdir(f\"{root_folder}\")\n", 55 | "image_folder = max([int(f) for f in os.listdir(f\"{root_folder}\")], default=0)\n", 56 | "if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 57 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 58 | "name = max([int(f[: f.index(\".\")]) for f in os.listdir(f\"{root_folder}/{image_folder:04}\")],default=0,)\n", 59 | "\n", 60 | "model_folder = \"camenduru/sd15\" #@param [\"camenduru/sd15\", \"Linaqruf/anything-v3.0\", \"lilpotat/sa\", \"Fictiverse/Stable_Diffusion_PaperCut_Model\", \"nitrosocke/redshift-diffusion\", \"prompthero/midjourney-v4-diffusion\", \"dallinmackay/Tron-Legacy-diffusion\", \"DGSpitzer/Cyberpunk-Anime-Diffusion\", \"hakurei/waifu-diffusion\", \"nitrosocke/Arcane-Diffusion\", \"nitrosocke/classic-anim-diffusion\", \"nitrosocke/mo-di-diffusion\"] {allow-input: true}\n", 61 | "\n", 62 | "is_tile = False #@param {type: 'boolean'}\n", 63 | "if(is_tile):\n", 64 | " def patch_conv(cls):\n", 65 | " init = cls.__init__\n", 66 | " def __init__(self, *args, **kwargs):\n", 67 | " return init(self, *args, **kwargs, padding_mode='circular')\n", 68 | " cls.__init__ = __init__\n", 69 | " patch_conv(torch.nn.Conv2d)\n", 70 | "\n", 71 | "pipe = StableDiffusionPipeline.from_pretrained(model_folder, safety_checker=None).to(\"cuda\")\n", 72 | "pipe.enable_xformers_memory_efficient_attention()\n", 73 | "\n", 74 | "def generate(discord_token, discord_channel_id, discord_user, by, num_inference_steps, guidance_scale, sampler, width, height, prompt, negative_prompt, suffix, image_folder, name):\n", 75 | " width = closestNumber(width, 8)\n", 76 | " height = closestNumber(height, 8)\n", 77 | " metadata.add_text(\"Prompt\", f\"{prompt}\")\n", 78 | " metadata.add_text(\"by\", f\"{by}\")\n", 79 | " gc.collect()\n", 80 | " with torch.autocast(\"cuda\"):\n", 81 | " images = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, height=height, width=width, guidance_scale=guidance_scale).images\n", 82 | " image = images[0]\n", 83 | " real_seed = torch.cuda.initial_seed()\n", 84 | " if(suffix == 'png'):\n", 85 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", pnginfo=metadata)\n", 86 | " else:\n", 87 | " zeroth_ifd = {piexif.ImageIFD.ImageDescription: f\"{fold(prompt)}\", piexif.ImageIFD.Make: f\"{fold(by)}\", piexif.ImageIFD.Model: f\"{model_folder}\"}\n", 88 | " exif_dict = {\"0th\": zeroth_ifd}\n", 89 | " exif_bytes = piexif.dump(exif_dict)\n", 90 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"JPEG\", quality=70, exif=exif_bytes)\n", 91 | " files = {f\"{image_folder:04}_{name:04}.{suffix}\": open(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"rb\").read()}\n", 92 | " if save_to == \"discord\":\n", 93 | " payload = {\"content\": f\"{prompt}\\nNegative prompt: {negative_prompt}\\nSteps: {num_inference_steps}, Sampler: {sampler}, CFG scale: {guidance_scale}, Seed: {real_seed}, Size: {width}x{height}, Model folder: {model_folder} - {discord_user}\"}\n", 94 | " requests.post(f\"https://discord.com/api/v9/channels/{discord_channel_id}/messages\", data=payload, headers={\"authorization\": f\"Bot {discord_token}\"}, files=files)\n", 95 | " os.remove(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\")" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": { 102 | "cellView": "form", 103 | "id": "DDUev8TOMxJ4" 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "discord_token = \"token\" #@param {type: 'string'}\n", 108 | "discord_channel_id = 0 #@param {type: 'integer'}\n", 109 | "prompt = \"duck\" #@param {type: 'string'}\n", 110 | "negative_prompt = \"\" #@param {type: 'string'}\n", 111 | "width = 512 #@param {type: 'integer'}\n", 112 | "height = 512 #@param {type: 'integer'}\n", 113 | "guidance_scale = 7.5 #@param {type: 'number'}\n", 114 | "num_inference_steps = 50 #@param {type: 'integer'}\n", 115 | "suffix = \"jpg\" #@param [\"jpg\", \"png\"]\n", 116 | "by = \"camenduru\" #@param {type: 'string'}\n", 117 | "template = {\n", 118 | " \"discord_token\": discord_token,\n", 119 | " \"discord_channel_id\": discord_channel_id,\n", 120 | " \"by\": by,\n", 121 | " \"num_inference_steps\": num_inference_steps,\n", 122 | " \"guidance_scale\": guidance_scale,\n", 123 | " \"sampler\": \"PLMS\",\n", 124 | " \"width\": width,\n", 125 | " \"height\": height,\n", 126 | " \"prompt\": prompt,\n", 127 | " \"negative_prompt\": negative_prompt,\n", 128 | " \"suffix\": suffix\n", 129 | " }\n", 130 | "with open(\"template.json\", \"w\") as outfile:\n", 131 | " outfile.write(json.dumps(template))\n", 132 | "\n", 133 | "is_loop = False #@param {type:\"boolean\"}\n", 134 | "\n", 135 | "if is_loop:\n", 136 | " while True:\n", 137 | " if name < folder_max_files:\n", 138 | " with open(\"template.json\", \"r\") as file:\n", 139 | " prompts = file.readlines()\n", 140 | " for prompt in prompts:\n", 141 | " d = json.loads(prompt)\n", 142 | " name += 1\n", 143 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 144 | " else:\n", 145 | " image_folder += 1\n", 146 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 147 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 148 | " name = 0\n", 149 | "else:\n", 150 | " if name < folder_max_files:\n", 151 | " with open(\"template.json\", \"r\") as file:\n", 152 | " prompts = file.readlines()\n", 153 | " for prompt in prompts:\n", 154 | " d = json.loads(prompt)\n", 155 | " name += 1\n", 156 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 157 | " else:\n", 158 | " image_folder += 1\n", 159 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 160 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 161 | " name = 0" 162 | ] 163 | } 164 | ], 165 | "metadata": { 166 | "accelerator": "GPU", 167 | "colab": { 168 | "provenance": [] 169 | }, 170 | "gpuClass": "standard", 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "name": "python3" 174 | }, 175 | "language_info": { 176 | "name": "python" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 0 181 | } 182 | -------------------------------------------------------------------------------- /pytorch_stable_diffusion_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_stable_diffusion_2.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "cellView": "form", 17 | "id": "ymcDoPQn41U4" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "save_to = \"here\" #@param [\"here\", \"discord\", \"gdrive\"]\n", 22 | "if save_to == \"gdrive\":\n", 23 | " from google.colab import drive\n", 24 | " drive.mount('/content/gdrive')\n", 25 | "\n", 26 | "!pip install -q torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 torchtext==0.14.1 torchdata==0.5.1 --extra-index-url https://download.pytorch.org/whl/cu116 -U\n", 27 | "!pip install -U git+https://github.com/huggingface/diffusers\n", 28 | "!pip install -U transformers piexif fold_to_ascii ftfy\n", 29 | "!pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+189828c.d20221207-cp38-cp38-linux_x86_64.whl\n", 30 | "\n", 31 | "import torch, os, gc, requests, json, piexif\n", 32 | "from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler\n", 33 | "from PIL import Image\n", 34 | "from PIL.PngImagePlugin import PngInfo\n", 35 | "from fold_to_ascii import fold\n", 36 | "metadata = PngInfo()\n", 37 | "\n", 38 | "def closestNumber(n, m):\n", 39 | " q = int(n / m)\n", 40 | " n1 = m * q\n", 41 | " if (n * m) > 0:\n", 42 | " n2 = m * (q + 1)\n", 43 | " else:\n", 44 | " n2 = m * (q - 1)\n", 45 | " if abs(n - n1) < abs(n - n2):\n", 46 | " return n1\n", 47 | " return n2\n", 48 | "\n", 49 | "folder_max_files = 500 #@param {type: 'integer'}\n", 50 | "root_folder = \"ai_images\" #@param {type: 'string'}\n", 51 | "if save_to == \"gdrive\":\n", 52 | " root_folder = f\"/content/gdrive/MyDrive/{root_folder}\"\n", 53 | "\n", 54 | "if os.path.exists(f\"{root_folder}\") == False:\n", 55 | " os.mkdir(f\"{root_folder}\")\n", 56 | "image_folder = max([int(f) for f in os.listdir(f\"{root_folder}\")], default=0)\n", 57 | "if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 58 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 59 | "name = max([int(f[: f.index(\".\")]) for f in os.listdir(f\"{root_folder}/{image_folder:04}\")],default=0,)\n", 60 | "\n", 61 | "model_folder = \"stabilityai/stable-diffusion-2\" #@param [\"stabilityai/stable-diffusion-2\", \"stabilityai/stable-diffusion-2-base\", \"stabilityai/stable-diffusion-2-inpainting\", \"stabilityai/stable-diffusion-2-depth\"] {allow-input: true}\n", 62 | "\n", 63 | "is_tile = False #@param {type: 'boolean'}\n", 64 | "if(is_tile):\n", 65 | " def patch_conv(cls):\n", 66 | " init = cls.__init__\n", 67 | " def __init__(self, *args, **kwargs):\n", 68 | " return init(self, *args, **kwargs, padding_mode='circular')\n", 69 | " cls.__init__ = __init__\n", 70 | " patch_conv(torch.nn.Conv2d)\n", 71 | "\n", 72 | "scheduler = EulerDiscreteScheduler.from_pretrained(model_folder, subfolder=\"scheduler\")\n", 73 | "pipe = StableDiffusionPipeline.from_pretrained(model_folder, scheduler=scheduler).to(\"cuda\")\n", 74 | "pipe.safety_checker = lambda images, clip_input: (images, False)\n", 75 | "pipe.enable_xformers_memory_efficient_attention()\n", 76 | "\n", 77 | "def generate(discord_token, discord_channel_id, discord_user, by, num_inference_steps, guidance_scale, sampler, width, height, prompt, negative_prompt, suffix, image_folder, name):\n", 78 | " width = closestNumber(width, 8)\n", 79 | " height = closestNumber(height, 8)\n", 80 | " metadata.add_text(\"Prompt\", f\"{prompt}\")\n", 81 | " metadata.add_text(\"by\", f\"{by}\")\n", 82 | " gc.collect()\n", 83 | " with torch.autocast(\"cuda\"):\n", 84 | " images = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=num_inference_steps, height=height, width=width, guidance_scale=guidance_scale).images\n", 85 | " image = images[0]\n", 86 | " real_seed = torch.cuda.initial_seed()\n", 87 | " if(suffix == 'png'):\n", 88 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", pnginfo=metadata)\n", 89 | " else:\n", 90 | " zeroth_ifd = {piexif.ImageIFD.ImageDescription: f\"{fold(prompt)}\", piexif.ImageIFD.Make: f\"{fold(by)}\", piexif.ImageIFD.Model: f\"{model_folder}\"}\n", 91 | " exif_dict = {\"0th\": zeroth_ifd}\n", 92 | " exif_bytes = piexif.dump(exif_dict)\n", 93 | " image.save(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"JPEG\", quality=70, exif=exif_bytes)\n", 94 | " files = {f\"{image_folder:04}_{name:04}.{suffix}\": open(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\", \"rb\").read()}\n", 95 | " if save_to == \"discord\":\n", 96 | " payload = {\"content\": f\"{prompt}\\nNegative prompt: {negative_prompt}\\nSteps: {num_inference_steps}, Sampler: {sampler}, CFG scale: {guidance_scale}, Seed: {real_seed}, Size: {width}x{height}, Model folder: {model_folder} - {discord_user}\"}\n", 97 | " requests.post(f\"https://discord.com/api/v9/channels/{discord_channel_id}/messages\", data=payload, headers={\"authorization\": f\"Bot {discord_token}\"}, files=files)\n", 98 | " os.remove(f\"{root_folder}/{image_folder:04}/{name:04}.{suffix}\")" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": { 105 | "cellView": "form", 106 | "id": "DDUev8TOMxJ4" 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "discord_token = \"\" #@param {type: 'string'}\n", 111 | "discord_channel_id = 0 #@param {type: 'integer'}\n", 112 | "prompt = \"duck\" #@param {type: 'string'}\n", 113 | "negative_prompt = \"\" #@param {type: 'string'}\n", 114 | "width = 768 #@param {type: 'integer'}\n", 115 | "height = 768 #@param {type: 'integer'}\n", 116 | "guidance_scale = 7.5 #@param {type: 'number'}\n", 117 | "num_inference_steps = 50 #@param {type: 'integer'}\n", 118 | "suffix = \"jpg\" #@param [\"jpg\", \"png\"]\n", 119 | "by = \"camenduru\" #@param {type: 'string'}\n", 120 | "template = {\n", 121 | " \"discord_token\": discord_token,\n", 122 | " \"discord_channel_id\": discord_channel_id,\n", 123 | " \"by\": by,\n", 124 | " \"num_inference_steps\": num_inference_steps,\n", 125 | " \"guidance_scale\": guidance_scale,\n", 126 | " \"sampler\": \"PLMS\",\n", 127 | " \"width\": width,\n", 128 | " \"height\": height,\n", 129 | " \"prompt\": prompt,\n", 130 | " \"negative_prompt\": negative_prompt,\n", 131 | " \"suffix\": suffix\n", 132 | " }\n", 133 | "with open(\"template.json\", \"w\") as outfile:\n", 134 | " outfile.write(json.dumps(template))\n", 135 | "\n", 136 | "is_loop = False #@param {type:\"boolean\"}\n", 137 | "\n", 138 | "if is_loop:\n", 139 | " while True:\n", 140 | " if name < folder_max_files:\n", 141 | " with open(\"template.json\", \"r\") as file:\n", 142 | " prompts = file.readlines()\n", 143 | " for prompt in prompts:\n", 144 | " d = json.loads(prompt)\n", 145 | " name += 1\n", 146 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 147 | " else:\n", 148 | " image_folder += 1\n", 149 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 150 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 151 | " name = 0\n", 152 | "else:\n", 153 | " if name < folder_max_files:\n", 154 | " with open(\"template.json\", \"r\") as file:\n", 155 | " prompts = file.readlines()\n", 156 | " for prompt in prompts:\n", 157 | " d = json.loads(prompt)\n", 158 | " name += 1\n", 159 | " generate(d[\"discord_token\"], d[\"discord_channel_id\"], \"camenduru\", d[\"by\"], d[\"num_inference_steps\"], d[\"guidance_scale\"], d[\"sampler\"], d[\"width\"], d[\"height\"], d[\"prompt\"], d[\"negative_prompt\"], d[\"suffix\"], image_folder, name)\n", 160 | " else:\n", 161 | " image_folder += 1\n", 162 | " if os.path.exists(f\"{root_folder}/{image_folder:04}\") == False:\n", 163 | " os.mkdir(f\"{root_folder}/{image_folder:04}\")\n", 164 | " name = 0" 165 | ] 166 | } 167 | ], 168 | "metadata": { 169 | "accelerator": "GPU", 170 | "colab": { 171 | "provenance": [] 172 | }, 173 | "gpuClass": "standard", 174 | "kernelspec": { 175 | "display_name": "Python 3", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "name": "python" 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 0 184 | } 185 | -------------------------------------------------------------------------------- /pytorch_stable_diffusion_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-diffusion-diffusers-colab/blob/main/pytorch_stable_diffusion_3.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "#@title Install - Step 1\n", 19 | "!pip install -q transformers accelerate omegaconf xformers==0.0.20 opencv-contrib-python controlnet_aux\n", 20 | "!pip install -q git+https://github.com/huggingface/diffusers\n", 21 | "\n", 22 | "# !apt -y install -qq aria2\n", 23 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/sd_xl_refiner_1.0/resolve/main/sd_xl_refiner_1.0_0.9vae.safetensors -d /content/models -o sd_xl_refiner_1.0.safetensors\n", 24 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/ckpt/juggernaut-xl/resolve/main/juggernautXL_version2.safetensors -d /content/models -o juggernautXL_version2.safetensors\n", 25 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M 'https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny.pth' -d /content/models/controlnet -o control_v11p_sd15_canny.pth\n", 26 | "# !aria2c --console-log-level=error -c -x 16 -s 16 -k 1M 'https://huggingface.co/ckpt/ControlNet-v1-1/resolve/main/control_v11p_sd15_canny_fp16.safetensors' -d /content/models/controlnet -o control_v11p_sd15_canny_fp16.safetensors" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "#@title Install - Step 2\n", 36 | "import random, gc, torch\n", 37 | "\n", 38 | "def image_grid(imgs, rows, cols):\n", 39 | " w,h = imgs[0].size\n", 40 | " grid = Image.new('RGB', size=(cols*w, rows*h))\n", 41 | " for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h))\n", 42 | " return grid" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#@title Vanilla - Step 1\n", 52 | "from diffusers import StableDiffusionXLPipeline\n", 53 | "# pipeline = StableDiffusionXLPipeline.from_single_file(\"/content/models/juggernautXL_version2.safetensors\", torch_dtype=torch.float16, use_safetensors=True, safety_checker=None ).to(\"cuda\")\n", 54 | "pipeline = StableDiffusionXLPipeline.from_pretrained(\"stabilityai/stable-diffusion-xl-base-1.0\", torch_dtype=torch.float16, variant=\"fp16\", use_safetensors=True, safety_checker=None ).to(\"cuda\")\n", 55 | "pipeline.enable_xformers_memory_efficient_attention()" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "#@title Vanilla - Step 2\n", 65 | "# pipeline.load_lora_weights(\"minimaxir/sdxl-wrong-lora\")\n", 66 | "image = pipeline(prompt=\"a photo of a dog in a bucket\", negative_prompt=\"blurry\", num_inference_steps=50, height=1024, width=1024, guidance_scale=7).images[0]\n", 67 | "image" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "#@title ControlNet #Canny - Step 1\n", 77 | "import torch\n", 78 | "from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel\n", 79 | "# controlnet = ControlNetModel.from_single_file('/content/models/controlnet/control_v11p_sd15_canny.pth', torch_dtype=torch.float16) # invalid load key, '<'.\n", 80 | "# controlnet = ControlNetModel.from_single_file('https://huggingface.co/lllyasviel/ControlNet-v1-1/blob/main/control_v11p_sd15_canny.pth', torch_dtype=torch.float16).to(\"cuda\")\n", 81 | "controlnet = ControlNetModel.from_pretrained(\"diffusers/controlnet-canny-sdxl-1.0\", variant=\"fp16\", torch_dtype=torch.float16).to(\"cuda\")\n", 82 | "pipeline = StableDiffusionXLControlNetPipeline.from_pretrained(\"stabilityai/stable-diffusion-xl-base-1.0\", controlnet=controlnet, torch_dtype=torch.float16, variant=\"fp16\", use_safetensors=True).to(\"cuda\")\n", 83 | "# pipeline.enable_model_cpu_offload()\n", 84 | "pipeline.enable_xformers_memory_efficient_attention()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "#@title ControlNet #Canny - Step 2\n", 94 | "from diffusers import UniPCMultistepScheduler\n", 95 | "pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "#@title ControlNet #Canny - Step 3\n", 105 | "from diffusers.utils import load_image\n", 106 | "image = load_image(\"https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png\")\n", 107 | "image_raw = image\n", 108 | "\n", 109 | "import cv2\n", 110 | "from PIL import Image\n", 111 | "import numpy as np\n", 112 | "image = np.array(image)\n", 113 | "low_threshold = 100\n", 114 | "high_threshold = 200\n", 115 | "image = cv2.Canny(image, low_threshold, high_threshold)\n", 116 | "image = image[:, :, None]\n", 117 | "image = np.concatenate([image, image, image], axis=2)\n", 118 | "canny_image = Image.fromarray(image)\n", 119 | "image = image_grid([image_raw, canny_image], 1, 2)\n", 120 | "image" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "#@title ControlNet #Canny - Step 4\n", 130 | "controlnet_conditioning_scale = 0.5\n", 131 | "image = pipeline(prompt=\"a photo of a dog in a bucket\", controlnet_conditioning_scale=controlnet_conditioning_scale, image=canny_image, negative_prompt=\"blurry\", num_inference_steps=50, height=1024, width=1024, guidance_scale=7).images[0]\n", 132 | "image" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "#@title ControlNet #OpenPose - Step 1\n", 142 | "import torch\n", 143 | "from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel\n", 144 | "controlnet = ControlNetModel.from_pretrained(\"fusing/stable-diffusion-v1-5-controlnet-openpose\", torch_dtype=torch.float16).to(\"cuda\")\n", 145 | "pipeline = StableDiffusionXLControlNetPipeline.from_pretrained(\"stabilityai/stable-diffusion-xl-base-1.0\", controlnet=controlnet, torch_dtype=torch.float16, variant=\"fp16\", use_safetensors=True).to(\"cuda\")\n", 146 | "# pipeline.enable_model_cpu_offload()\n", 147 | "pipeline.enable_xformers_memory_efficient_attention()" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "#@title ControlNet #Canny - Step 2\n", 157 | "from diffusers import UniPCMultistepScheduler\n", 158 | "pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "#@title ControlNet #OpenPose - Step 3\n", 168 | "from PIL import Image\n", 169 | "from controlnet_aux import OpenposeDetector\n", 170 | "from diffusers.utils import load_image\n", 171 | "image = load_image(\"https://hf.co/datasets/YiYiXu/controlnet-testing/resolve/main/yoga1.jpeg\")\n", 172 | "image_raw = image\n", 173 | "model = OpenposeDetector.from_pretrained(\"lllyasviel/ControlNet\")\n", 174 | "pose = model(image)\n", 175 | "# image = image_grid([image_raw, pose], 1, 2)\n", 176 | "# image\n", 177 | "pose" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [ 186 | "#@title ControlNet #OpenPose - Step 4\n", 187 | "controlnet_conditioning_scale = 0.5\n", 188 | "image = pipeline(prompt=\"a photo of a dog in a bucket\", controlnet_conditioning_scale=controlnet_conditioning_scale, image=pose, negative_prompt=\"blurry\", num_inference_steps=50, height=1024, width=1024, guidance_scale=7).images[0]\n", 189 | "image" 190 | ] 191 | } 192 | ], 193 | "metadata": { 194 | "accelerator": "TPU", 195 | "colab": { 196 | "provenance": [] 197 | }, 198 | "gpuClass": "standard", 199 | "kernelspec": { 200 | "display_name": "Python 3", 201 | "name": "python3" 202 | }, 203 | "language_info": { 204 | "name": "python" 205 | } 206 | }, 207 | "nbformat": 4, 208 | "nbformat_minor": 0 209 | } 210 | --------------------------------------------------------------------------------