{ "cells": [ { "cell_type": "markdown", "source": [ "# Trinket! Stable Inversion\n", "A cheap alternative to finetuning stable diffusion, by [crumb](https://twitter.com/aicrumb)\n", "\n", "Finetunes the embedding layer like Textual-Inversion does, but on CLIP Text/Image pairs instead of reconstruction loss from Stable Diffusion. Lower memory requirements + (sometimes) faster than finetuning the traditional way." ], "metadata": { "id": "mSXDq5-qTSF6" }, "id": "mSXDq5-qTSF6" }, { "cell_type": "code", "source": [ "!pip install git+https://github.com/openai/CLIP -q\n", "!pip install bitsandbytes -q" ], "metadata": { "id": "yB-XeS51NmI2" }, "id": "yB-XeS51NmI2", "execution_count": 1, "outputs": [] }, { "cell_type": "code", "source": [ "import bitsandbytes as bnb\n", "import torchvision\n", "from torchvision import transforms\n", "from tqdm.auto import *\n", "from torch import nn, optim\n", "from torch.nn import functional as F\n", "from PIL import Image\n", "import requests\n", "from io import BytesIO\n", "import torch\n", "import random\n", "import clip\n", "import pandas as pd" ], "metadata": { "id": "x1OogszooEi6", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a55a628d-aafc-407e-a2a9-fb03b8a0896a" }, "id": "x1OogszooEi6", "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "===================================BUG REPORT===================================\n", "Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n", "For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link\n", "================================================================================\n", "CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching /usr/local/cuda/lib64...\n", "CUDA SETUP: CUDA runtime path found: /usr/local/cuda/lib64/libcudart.so\n", "CUDA SETUP: Highest compute capability among GPUs detected: 7.5\n", "CUDA SETUP: Detected CUDA version 111\n", "CUDA SETUP: Loading binary /usr/local/lib/python3.7/dist-packages/bitsandbytes/libbitsandbytes_cuda111.so...\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.7/dist-packages/bitsandbytes/cuda_setup/paths.py:99: UserWarning: /usr/lib64-nvidia did not contain libcudart.so as expected! Searching further paths...\n", " f'{candidate_env_vars[\"LD_LIBRARY_PATH\"]} did not contain '\n", "/usr/local/lib/python3.7/dist-packages/bitsandbytes/cuda_setup/paths.py:21: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('{\"kernelManagerProxyPort\"'), PosixPath('\"/usr/local/bin/dap_multiplexer\",\"enableLsp\"'), PosixPath('\"172.28.0.3\",\"jupyterArgs\"'), PosixPath('[\"--ip=172.28.0.2\"],\"debugAdapterMultiplexerPath\"'), PosixPath('true}'), PosixPath('6000,\"kernelManagerProxyHost\"')}\n", " \"WARNING: The following directories listed in your path were found to \"\n", "/usr/local/lib/python3.7/dist-packages/bitsandbytes/cuda_setup/paths.py:21: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/env/python')}\n", " \"WARNING: The following directories listed in your path were found to \"\n", "/usr/local/lib/python3.7/dist-packages/bitsandbytes/cuda_setup/paths.py:21: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('module'), PosixPath('//ipykernel.pylab.backend_inline')}\n", " \"WARNING: The following directories listed in your path were found to \"\n" ] } ] }, { "cell_type": "code", "execution_count": 3, "id": "22b6861e-289f-4779-bbf1-40bfd1327823", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "22b6861e-289f-4779-bbf1-40bfd1327823", "outputId": "fd96d25d-8cbf-43d2-8d5b-983856bf2a3a" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Loaded CLIP\n" ] } ], "source": [ "# the stable diffusion model uses L/14 by default\n", "clip_model, _ = clip.load(\"ViT-L/14\", jit=False)\n", "clip_model = clip_model.cuda()\n", "print(\"Loaded CLIP\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "eaa9bde8-3fdd-48df-aa36-8b88cec285be", "metadata": { "id": "eaa9bde8-3fdd-48df-aa36-8b88cec285be" }, "outputs": [], "source": [ "bs = 16 # need a large batch size for the contrastive loss to work properly\n", "steps = 64\n", "epochs = 4\n", "lr = 1e-3" ] }, { "cell_type": "code", "execution_count": 5, "id": "0667d187-2c3e-40b8-a2fd-b408d5f144c5", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "0667d187-2c3e-40b8-a2fd-b408d5f144c5", "outputId": "b8f6056a-c2ac-4b89-e222-0d47705617e9" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Unnamed: 0 url \\\n", "0 0 https://cdn.donmai.us/original/51/bb/51bb44d69... \n", "1 1 https://cdn.donmai.us/original/4f/8e/4f8e5eaba... \n", "2 2 https://cdn.donmai.us/original/c8/54/c85428d89... \n", "3 3 https://cdn.donmai.us/original/e9/fc/e9fcc788e... \n", "4 4 https://cdn.donmai.us/original/8a/63/8a639d21b... \n", "\n", " prompt \n", "0 genshin_impact boo_tao_(genshin_impact) hu_tao... \n", "1 genshin_impact arlecchino_(genshin_impact) cap... \n", "2 genshin_impact fischl_(ein_immernachtstraum)_(... \n", "3 genshin_impact eula_(genshin_impact) 1girl :o ... \n", "4 genshin_impact kuki_shinobu 1girl breasts brid... " ], "text/html": [ "\n", "
\n", " | Unnamed: 0 | \n", "url | \n", "prompt | \n", "
---|---|---|---|
0 | \n", "0 | \n", "https://cdn.donmai.us/original/51/bb/51bb44d69... | \n", "genshin_impact boo_tao_(genshin_impact) hu_tao... | \n", "
1 | \n", "1 | \n", "https://cdn.donmai.us/original/4f/8e/4f8e5eaba... | \n", "genshin_impact arlecchino_(genshin_impact) cap... | \n", "
2 | \n", "2 | \n", "https://cdn.donmai.us/original/c8/54/c85428d89... | \n", "genshin_impact fischl_(ein_immernachtstraum)_(... | \n", "
3 | \n", "3 | \n", "https://cdn.donmai.us/original/e9/fc/e9fcc788e... | \n", "genshin_impact eula_(genshin_impact) 1girl :o ... | \n", "
4 | \n", "4 | \n", "https://cdn.donmai.us/original/8a/63/8a639d21b... | \n", "genshin_impact kuki_shinobu 1girl breasts brid... | \n", "