Instructions to use 8BitStudio/Aniimage-1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use 8BitStudio/Aniimage-1 with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("8BitStudio/Aniimage-1", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Delete Aniimage_1_Colab.ipynb
Browse files- Aniimage_1_Colab.ipynb +0 -290
Aniimage_1_Colab.ipynb
DELETED
|
@@ -1,290 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"nbformat": 4,
|
| 3 |
-
"nbformat_minor": 5,
|
| 4 |
-
"metadata": {
|
| 5 |
-
"colab": {
|
| 6 |
-
"provenance": [],
|
| 7 |
-
"gpuType": "T4"
|
| 8 |
-
},
|
| 9 |
-
"kernelspec": {
|
| 10 |
-
"name": "python3",
|
| 11 |
-
"display_name": "Python 3"
|
| 12 |
-
},
|
| 13 |
-
"language_info": {
|
| 14 |
-
"name": "python"
|
| 15 |
-
},
|
| 16 |
-
"accelerator": "GPU"
|
| 17 |
-
},
|
| 18 |
-
"cells": [
|
| 19 |
-
{
|
| 20 |
-
"cell_type": "markdown",
|
| 21 |
-
"metadata": {},
|
| 22 |
-
"source": [
|
| 23 |
-
"# Aniimage-1 Colab Generator\n",
|
| 24 |
-
"\n",
|
| 25 |
-
"Hugging Face's default `DiffusionPipeline.from_pretrained(...)` snippet does not work for this repo because `8BitStudio/Aniimage-1` is not packaged as a full Diffusers pipeline. It ships the Aniimage UNet weights/config, so this notebook manually loads:\n",
|
| 26 |
-
"\n",
|
| 27 |
-
"- Aniimage-1 UNet from `8BitStudio/Aniimage-1`\n",
|
| 28 |
-
"- SD VAE from `stabilityai/sd-vae-ft-mse`\n",
|
| 29 |
-
"- CLIP text encoder/tokenizer from `openai/clip-vit-large-patch14`\n",
|
| 30 |
-
"\n",
|
| 31 |
-
"Use Colab GPU runtime: `Runtime > Change runtime type > T4 GPU` or better."
|
| 32 |
-
"If you have any errors or issues, open a discussion on the hugging face."
|
| 33 |
-
]
|
| 34 |
-
},
|
| 35 |
-
{
|
| 36 |
-
"cell_type": "code",
|
| 37 |
-
"execution_count": null,
|
| 38 |
-
"metadata": {},
|
| 39 |
-
"outputs": [],
|
| 40 |
-
"source": [
|
| 41 |
-
"!pip -q install -U diffusers transformers accelerate safetensors huggingface_hub pillow"
|
| 42 |
-
]
|
| 43 |
-
},
|
| 44 |
-
{
|
| 45 |
-
"cell_type": "code",
|
| 46 |
-
"execution_count": null,
|
| 47 |
-
"metadata": {},
|
| 48 |
-
"outputs": [],
|
| 49 |
-
"source": [
|
| 50 |
-
"import json\n",
|
| 51 |
-
"from pathlib import Path\n",
|
| 52 |
-
"\n",
|
| 53 |
-
"import numpy as np\n",
|
| 54 |
-
"import torch\n",
|
| 55 |
-
"import torch.nn.functional as F\n",
|
| 56 |
-
"from PIL import Image, ImageEnhance, ImageFilter\n",
|
| 57 |
-
"from IPython.display import display\n",
|
| 58 |
-
"from huggingface_hub import hf_hub_download\n",
|
| 59 |
-
"from safetensors.torch import load_file\n",
|
| 60 |
-
"from diffusers import (\n",
|
| 61 |
-
" AutoencoderKL,\n",
|
| 62 |
-
" DDIMScheduler,\n",
|
| 63 |
-
" DPMSolverMultistepScheduler,\n",
|
| 64 |
-
" EulerAncestralDiscreteScheduler,\n",
|
| 65 |
-
" EulerDiscreteScheduler,\n",
|
| 66 |
-
" UNet2DConditionModel,\n",
|
| 67 |
-
")\n",
|
| 68 |
-
"from transformers import CLIPTextModel, CLIPTokenizer\n",
|
| 69 |
-
"\n",
|
| 70 |
-
"REPO_ID = \"8BitStudio/Aniimage-1\"\n",
|
| 71 |
-
"VAE_ID = \"stabilityai/sd-vae-ft-mse\"\n",
|
| 72 |
-
"CLIP_ID = \"openai/clip-vit-large-patch14\"\n",
|
| 73 |
-
"MODEL_DIR = Path(\"/content/aniimage_1\")\n",
|
| 74 |
-
"\n",
|
| 75 |
-
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 76 |
-
"autocast_dtype = torch.float16 if device == \"cuda\" else torch.float32\n",
|
| 77 |
-
"\n",
|
| 78 |
-
"if device == \"cuda\":\n",
|
| 79 |
-
" torch.backends.cuda.matmul.allow_tf32 = True\n",
|
| 80 |
-
" torch.backends.cudnn.allow_tf32 = True\n",
|
| 81 |
-
"\n",
|
| 82 |
-
"print(\"Device:\", device)\n",
|
| 83 |
-
"if device == \"cuda\":\n",
|
| 84 |
-
" print(\"GPU:\", torch.cuda.get_device_name(0))"
|
| 85 |
-
]
|
| 86 |
-
},
|
| 87 |
-
{
|
| 88 |
-
"cell_type": "code",
|
| 89 |
-
"execution_count": null,
|
| 90 |
-
"metadata": {},
|
| 91 |
-
"outputs": [],
|
| 92 |
-
"source": [
|
| 93 |
-
"def make_scheduler(name=\"DPM++ 2M Karras\"):\n",
|
| 94 |
-
" base = dict(\n",
|
| 95 |
-
" num_train_timesteps=1000,\n",
|
| 96 |
-
" beta_schedule=\"scaled_linear\",\n",
|
| 97 |
-
" prediction_type=\"epsilon\",\n",
|
| 98 |
-
" )\n",
|
| 99 |
-
" if name == \"DPM++ 2M Karras\":\n",
|
| 100 |
-
" return DPMSolverMultistepScheduler(\n",
|
| 101 |
-
" **base,\n",
|
| 102 |
-
" algorithm_type=\"dpmsolver++\",\n",
|
| 103 |
-
" solver_order=2,\n",
|
| 104 |
-
" use_karras_sigmas=True,\n",
|
| 105 |
-
" )\n",
|
| 106 |
-
" if name == \"DPM++ SDE Karras\":\n",
|
| 107 |
-
" return DPMSolverMultistepScheduler(\n",
|
| 108 |
-
" **base,\n",
|
| 109 |
-
" algorithm_type=\"sde-dpmsolver++\",\n",
|
| 110 |
-
" solver_order=2,\n",
|
| 111 |
-
" use_karras_sigmas=True,\n",
|
| 112 |
-
" )\n",
|
| 113 |
-
" if name == \"Euler a\":\n",
|
| 114 |
-
" return EulerAncestralDiscreteScheduler(**base)\n",
|
| 115 |
-
" if name == \"Euler\":\n",
|
| 116 |
-
" return EulerDiscreteScheduler(**base)\n",
|
| 117 |
-
" if name == \"DDIM\":\n",
|
| 118 |
-
" return DDIMScheduler(**base, clip_sample=False, set_alpha_to_one=False)\n",
|
| 119 |
-
" raise ValueError(f\"Unknown scheduler: {name}\")\n",
|
| 120 |
-
"\n",
|
| 121 |
-
"\n",
|
| 122 |
-
"def load_aniimage1():\n",
|
| 123 |
-
" MODEL_DIR.mkdir(parents=True, exist_ok=True)\n",
|
| 124 |
-
"\n",
|
| 125 |
-
" print(\"Downloading Aniimage-1 UNet config/weights if needed...\")\n",
|
| 126 |
-
" config_path = hf_hub_download(\n",
|
| 127 |
-
" repo_id=REPO_ID,\n",
|
| 128 |
-
" filename=\"config.json\",\n",
|
| 129 |
-
" local_dir=str(MODEL_DIR),\n",
|
| 130 |
-
" )\n",
|
| 131 |
-
" weights_path = hf_hub_download(\n",
|
| 132 |
-
" repo_id=REPO_ID,\n",
|
| 133 |
-
" filename=\"diffusion_pytorch_model.safetensors\",\n",
|
| 134 |
-
" local_dir=str(MODEL_DIR),\n",
|
| 135 |
-
" )\n",
|
| 136 |
-
"\n",
|
| 137 |
-
" print(\"Loading VAE...\")\n",
|
| 138 |
-
" vae = AutoencoderKL.from_pretrained(VAE_ID).to(device).eval()\n",
|
| 139 |
-
" vae.requires_grad_(False)\n",
|
| 140 |
-
"\n",
|
| 141 |
-
" print(\"Loading CLIP tokenizer/text encoder...\")\n",
|
| 142 |
-
" tokenizer = CLIPTokenizer.from_pretrained(CLIP_ID)\n",
|
| 143 |
-
" text_encoder = CLIPTextModel.from_pretrained(CLIP_ID).to(device).eval()\n",
|
| 144 |
-
" text_encoder.requires_grad_(False)\n",
|
| 145 |
-
"\n",
|
| 146 |
-
" print(\"Loading Aniimage-1 UNet...\")\n",
|
| 147 |
-
" with open(config_path, \"r\", encoding=\"utf-8\") as f:\n",
|
| 148 |
-
" unet_config = json.load(f)\n",
|
| 149 |
-
" unet = UNet2DConditionModel.from_config(unet_config)\n",
|
| 150 |
-
" state = load_file(weights_path, device=\"cpu\")\n",
|
| 151 |
-
" unet.load_state_dict(state)\n",
|
| 152 |
-
" unet.to(device).eval()\n",
|
| 153 |
-
" unet.requires_grad_(False)\n",
|
| 154 |
-
"\n",
|
| 155 |
-
" print(\"Aniimage-1 ready.\")\n",
|
| 156 |
-
" return vae, tokenizer, text_encoder, unet\n",
|
| 157 |
-
"\n",
|
| 158 |
-
"\n",
|
| 159 |
-
"vae, tokenizer, text_encoder, unet = load_aniimage1()"
|
| 160 |
-
]
|
| 161 |
-
},
|
| 162 |
-
{
|
| 163 |
-
"cell_type": "code",
|
| 164 |
-
"execution_count": null,
|
| 165 |
-
"metadata": {},
|
| 166 |
-
"outputs": [],
|
| 167 |
-
"source": [
|
| 168 |
-
"def encode_prompt(prompt, negative_prompt=\"\"):\n",
|
| 169 |
-
" prompts = [negative_prompt or \"\", prompt]\n",
|
| 170 |
-
" tokens = tokenizer(\n",
|
| 171 |
-
" prompts,\n",
|
| 172 |
-
" padding=\"max_length\",\n",
|
| 173 |
-
" max_length=tokenizer.model_max_length,\n",
|
| 174 |
-
" truncation=True,\n",
|
| 175 |
-
" return_tensors=\"pt\",\n",
|
| 176 |
-
" )\n",
|
| 177 |
-
" with torch.no_grad():\n",
|
| 178 |
-
" return text_encoder(tokens.input_ids.to(device))[0]\n",
|
| 179 |
-
"\n",
|
| 180 |
-
"\n",
|
| 181 |
-
"def sharpen_latents(latents, amount=0.08):\n",
|
| 182 |
-
" blurred = F.avg_pool2d(latents, kernel_size=3, stride=1, padding=1)\n",
|
| 183 |
-
" return latents + amount * (latents - blurred)\n",
|
| 184 |
-
"\n",
|
| 185 |
-
"\n",
|
| 186 |
-
"def post_process(img):\n",
|
| 187 |
-
" img = img.filter(ImageFilter.UnsharpMask(radius=1.5, percent=40, threshold=2))\n",
|
| 188 |
-
" img = ImageEnhance.Contrast(img).enhance(1.06)\n",
|
| 189 |
-
" img = ImageEnhance.Color(img).enhance(1.10)\n",
|
| 190 |
-
" return img\n",
|
| 191 |
-
"\n",
|
| 192 |
-
"\n",
|
| 193 |
-
"@torch.no_grad()\n",
|
| 194 |
-
"def decode_latents(latents, polish=True):\n",
|
| 195 |
-
" scaled = latents / vae.config.scaling_factor\n",
|
| 196 |
-
" # Keep decode in fp32. It is a little slower but avoids washed/banded VAE output.\n",
|
| 197 |
-
" image = vae.decode(scaled.float()).sample\n",
|
| 198 |
-
" image = (image.float() / 2 + 0.5).clamp(0, 1)\n",
|
| 199 |
-
" image = image.cpu().permute(0, 2, 3, 1).numpy()[0]\n",
|
| 200 |
-
" image = (image * 255).round().astype(\"uint8\")\n",
|
| 201 |
-
" img = Image.fromarray(image)\n",
|
| 202 |
-
" return post_process(img) if polish else img\n",
|
| 203 |
-
"\n",
|
| 204 |
-
"\n",
|
| 205 |
-
"@torch.no_grad()\n",
|
| 206 |
-
"def generate_aniimage1(\n",
|
| 207 |
-
" prompt,\n",
|
| 208 |
-
" negative_prompt=\"\",\n",
|
| 209 |
-
" scheduler_name=\"DPM++ 2M Karras\",\n",
|
| 210 |
-
" steps=25,\n",
|
| 211 |
-
" guidance_scale=7.5,\n",
|
| 212 |
-
" seed=-1,\n",
|
| 213 |
-
" polish=True,\n",
|
| 214 |
-
"):\n",
|
| 215 |
-
" if seed is None or int(seed) < 0:\n",
|
| 216 |
-
" seed = int(torch.randint(0, 2**31 - 1, (1,)).item())\n",
|
| 217 |
-
" else:\n",
|
| 218 |
-
" seed = int(seed)\n",
|
| 219 |
-
"\n",
|
| 220 |
-
" generator = torch.Generator(device=device).manual_seed(seed)\n",
|
| 221 |
-
" text_embeddings = encode_prompt(prompt, negative_prompt)\n",
|
| 222 |
-
"\n",
|
| 223 |
-
" scheduler = make_scheduler(scheduler_name)\n",
|
| 224 |
-
" scheduler.set_timesteps(int(steps), device=device)\n",
|
| 225 |
-
"\n",
|
| 226 |
-
" latents = torch.randn(\n",
|
| 227 |
-
" (1, 4, 32, 32),\n",
|
| 228 |
-
" generator=generator,\n",
|
| 229 |
-
" device=device,\n",
|
| 230 |
-
" dtype=torch.float32,\n",
|
| 231 |
-
" )\n",
|
| 232 |
-
" latents = latents * scheduler.init_noise_sigma\n",
|
| 233 |
-
"\n",
|
| 234 |
-
" for t in scheduler.timesteps:\n",
|
| 235 |
-
" latent_input = torch.cat([latents, latents], dim=0)\n",
|
| 236 |
-
" latent_input = scheduler.scale_model_input(latent_input, t)\n",
|
| 237 |
-
"\n",
|
| 238 |
-
" with torch.autocast(\n",
|
| 239 |
-
" device_type=\"cuda\",\n",
|
| 240 |
-
" dtype=autocast_dtype,\n",
|
| 241 |
-
" enabled=(device == \"cuda\"),\n",
|
| 242 |
-
" ):\n",
|
| 243 |
-
" noise_pred = unet(\n",
|
| 244 |
-
" latent_input,\n",
|
| 245 |
-
" t,\n",
|
| 246 |
-
" encoder_hidden_states=text_embeddings,\n",
|
| 247 |
-
" ).sample\n",
|
| 248 |
-
"\n",
|
| 249 |
-
" noise_uncond, noise_text = noise_pred.chunk(2)\n",
|
| 250 |
-
" noise_pred = noise_uncond + float(guidance_scale) * (noise_text - noise_uncond)\n",
|
| 251 |
-
" latents = scheduler.step(noise_pred, t, latents).prev_sample\n",
|
| 252 |
-
"\n",
|
| 253 |
-
" if polish:\n",
|
| 254 |
-
" latents = sharpen_latents(latents)\n",
|
| 255 |
-
" return decode_latents(latents, polish=polish), seed"
|
| 256 |
-
]
|
| 257 |
-
},
|
| 258 |
-
{
|
| 259 |
-
"cell_type": "code",
|
| 260 |
-
"execution_count": null,
|
| 261 |
-
"metadata": {},
|
| 262 |
-
"outputs": [],
|
| 263 |
-
"source": [
|
| 264 |
-
"prompt = \"an anime girl with long blue hair\" #@param {type:\"string\"}\n",
|
| 265 |
-
"negative_prompt = \"low quality, ugly, blurry, distorted, deformed, bad anatomy, bad proportions, extra limbs, missing limbs, watermark, text, signature, washed out, flat colors, manga panel, disfigured, poorly drawn, jpeg artifacts, cropped, out of frame\" #@param {type:\"string\"}\n",
|
| 266 |
-
"scheduler_name = \"DPM++ 2M Karras\" #@param [\"DPM++ 2M Karras\", \"DPM++ SDE Karras\", \"Euler a\", \"Euler\", \"DDIM\"]\n",
|
| 267 |
-
"steps = 25 #@param {type:\"slider\", min:10, max:80, step:1}\n",
|
| 268 |
-
"guidance_scale = 7.5 #@param {type:\"slider\", min:1, max:15, step:0.1}\n",
|
| 269 |
-
"seed = -1 #@param {type:\"integer\"}\n",
|
| 270 |
-
"polish = False #@param {type:\"boolean\"}\n",
|
| 271 |
-
"\n",
|
| 272 |
-
"image, used_seed = generate_aniimage1(\n",
|
| 273 |
-
" prompt=prompt,\n",
|
| 274 |
-
" negative_prompt=negative_prompt,\n",
|
| 275 |
-
" scheduler_name=scheduler_name,\n",
|
| 276 |
-
" steps=steps,\n",
|
| 277 |
-
" guidance_scale=guidance_scale,\n",
|
| 278 |
-
" seed=seed,\n",
|
| 279 |
-
" polish=polish,\n",
|
| 280 |
-
")\n",
|
| 281 |
-
"\n",
|
| 282 |
-
"out_path = \"/content/aniimage1_output.png\"\n",
|
| 283 |
-
"image.save(out_path)\n",
|
| 284 |
-
"print(\"Seed:\", used_seed)\n",
|
| 285 |
-
"print(\"Saved:\", out_path)\n",
|
| 286 |
-
"display(image)"
|
| 287 |
-
]
|
| 288 |
-
}
|
| 289 |
-
]
|
| 290 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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