{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "54f1a5b8-ed8b-4add-83a0-fa40732f80cc", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/root/miniconda3/envs/control/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "logging improved.\n", "Enabled sliced_attention.\n", "No module 'xformers'. Proceeding without it.\n", "ControlInpaintLDM: Running in eps-prediction mode\n", "DiffusionWrapper has 859.52 M params.\n", "making attention of type 'vanilla' with 512 in_channels\n", "Working with z of shape (1, 4, 32, 32) = 4096 dimensions.\n", "making attention of type 'vanilla' with 512 in_channels\n", "Loaded model config from [models/cldm_v15-mask.yaml]\n", "Loaded state_dict from [/storage/ckpts15/c-20999.ckpt]\n" ] } ], "source": [ "from share import *\n", "import config\n", "\n", "import einops\n", "import gradio as gr\n", "import numpy as np\n", "import torch\n", "import random\n", "\n", "from pytorch_lightning import seed_everything\n", "from annotator.util import resize_image, HWC3\n", "from cldm.model import create_model, load_state_dict\n", "from cldm.ddim_hacked import DDIMSampler\n", "\n", "\n", "model = create_model('models/cldm_v15-mask.yaml').cpu()\n", "model.load_state_dict(load_state_dict('/storage/ckpts15/c-20999.ckpt', location='cuda'))\n", "model = model.cuda()\n", "ddim_sampler = DDIMSampler(model)" ] }, { "cell_type": "code", "execution_count": 2, "id": "1ca4b664-22b5-4007-a663-696bbffa5d0a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Automatic pdb calling has been turned ON\n" ] } ], "source": [ "%pdb on" ] }, { "cell_type": "code", "execution_count": 3, "id": "bfbe23ec-793b-4f0c-a312-9f67177b8b43", "metadata": {}, "outputs": [], "source": [ "def process(ref_image, control_img, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta):\n", " with torch.no_grad():\n", " ref = resize_image(HWC3(ref_image), image_resolution)\n", " H, W, C = ref.shape\n", " \n", " control = resize_image(HWC3(control_img), image_resolution)\n", "\n", " control = torch.from_numpy(np.array(control).astype(np.float32)).cuda() / 255.0\n", " control = torch.stack([control for _ in range(num_samples)], dim=0)\n", " control = einops.rearrange(control, 'b h w c -> b c h w').clone()\n", " \n", " ref = torch.from_numpy(np.array(ref).astype(np.float32)).cuda() / 255.0\n", " ref = torch.stack([ref for _ in range(num_samples)], dim=0)\n", " ref = einops.rearrange(ref, 'b h w c -> b c h w').clone()\n", " \n", " if seed == -1:\n", " seed = random.randint(0, 65535)\n", " seed_everything(seed)\n", "\n", " if config.save_memory:\n", " model.low_vram_shift(is_diffusing=False)\n", "\n", " cond = {\"c_concat\": [control], \"c_crossattn\": [model.get_learned_conditioning(ref * num_samples)]}\n", " #un_cond = {\"c_concat\": None if guess_mode else [control], \"c_crossattn\": [model.get_learned_conditioning([c] * num_samples)]}\n", " shape = (4, H // 8, W // 8)\n", "\n", " if config.save_memory:\n", " model.low_vram_shift(is_diffusing=True)\n", "\n", " model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01\n", " samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,\n", " shape, cond, verbose=False, eta=eta,\n", " unconditional_guidance_scale=1,\n", " unconditional_conditioning=None)\n", "\n", " if config.save_memory:\n", " model.low_vram_shift(is_diffusing=False)\n", "\n", " x_samples = model.decode_first_stage(samples)\n", " x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)\n", "\n", " results = [x_samples[i] for i in range(num_samples)]\n", " return [255 - detected_map] + results" ] }, { "cell_type": "code", "execution_count": null, "id": "389293fb-0b9e-4a7c-9b49-e6e13af53143", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://af551707-d6f9-4083.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "