File size: 8,958 Bytes
f0f2b99 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"source": [
"Install the library"
],
"metadata": {
"id": "BV2F3IkW7lT5"
}
},
{
"cell_type": "code",
"source": [
"!pip install diffusers\n",
"!pip install Pillow"
],
"metadata": {
"id": "f5RXwnZi7ql2",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "61617fd7-6aa0-42bc-d07b-1cd1c67ec43d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting diffusers\n",
" Downloading diffusers-0.29.2-py3-none-any.whl.metadata (19 kB)\n",
"Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.10/dist-packages (from diffusers) (8.2.0)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from diffusers) (3.15.4)\n",
"Requirement already satisfied: huggingface-hub>=0.23.2 in /usr/local/lib/python3.10/dist-packages (from diffusers) (0.23.5)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from diffusers) (1.26.4)\n",
"Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from diffusers) (2024.5.15)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from diffusers) (2.31.0)\n",
"Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from diffusers) (0.4.3)\n",
"Requirement already satisfied: Pillow in /usr/local/lib/python3.10/dist-packages (from diffusers) (9.4.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.2->diffusers) (2024.6.1)\n",
"Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.2->diffusers) (24.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.2->diffusers) (6.0.1)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.2->diffusers) (4.66.4)\n",
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.2->diffusers) (4.12.2)\n",
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.10/dist-packages (from importlib-metadata->diffusers) (3.19.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->diffusers) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->diffusers) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->diffusers) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->diffusers) (2024.7.4)\n",
"Downloading diffusers-0.29.2-py3-none-any.whl (2.2 MB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m38.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: diffusers\n",
"Successfully installed diffusers-0.29.2\n",
"Requirement already satisfied: Pillow in /usr/local/lib/python3.10/dist-packages (9.4.0)\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Importation Class Fron Library"
],
"metadata": {
"id": "yCM1UVvK87D6"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oQZ9MSf33sJo",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1cf4be43-fc01-4f17-f3f5-38ff0c1b5fc7"
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/diffusers/models/vq_model.py:20: FutureWarning: `VQEncoderOutput` is deprecated and will be removed in version 0.31. Importing `VQEncoderOutput` from `diffusers.models.vq_model` is deprecated and this will be removed in a future version. Please use `from diffusers.models.autoencoders.vq_model import VQEncoderOutput`, instead.\n",
" deprecate(\"VQEncoderOutput\", \"0.31\", deprecation_message)\n",
"/usr/local/lib/python3.10/dist-packages/diffusers/models/vq_model.py:25: FutureWarning: `VQModel` is deprecated and will be removed in version 0.31. Importing `VQModel` from `diffusers.models.vq_model` is deprecated and this will be removed in a future version. Please use `from diffusers.models.autoencoders.vq_model import VQModel`, instead.\n",
" deprecate(\"VQModel\", \"0.31\", deprecation_message)\n"
]
}
],
"source": [
"import torch\n",
"from diffusers import AutoPipelineForImage2Image\n",
"from diffusers.utils import load_image, make_image_grid\n",
"from PIL import Image"
]
},
{
"cell_type": "markdown",
"source": [
"Defiende Model"
],
"metadata": {
"id": "Hdz_Sdza5dH0"
}
},
{
"cell_type": "code",
"source": [
"Model_ID = \"kandinsky-community/kandinsky-2-2-decoder\""
],
"metadata": {
"id": "EWWt67554KUE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Load a checkpoint into the AutoPipelineForImage2Image class"
],
"metadata": {
"id": "ZF7YAu_E4-66"
}
},
{
"cell_type": "code",
"source": [
"!pip inst"
],
"metadata": {
"id": "z0PK-qjXjfg4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pipeline = AutoPipelineForImage2Image.from_pretrained(\n",
" Model_ID, torch_dtype=torch.float16, use_safetensors=True\n",
")\n",
"pipeline.enable_model_cpu_offload()\n",
"# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed\n",
"pipeline.enable_xformers_memory_efficient_attention()"
],
"metadata": {
"id": "ouPl34nS3-J-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Load an image to pass to the pipeline:"
],
"metadata": {
"id": "anRfKcIW4xTz"
}
},
{
"cell_type": "code",
"source": [
"init_image = Image.open('/content/sample_data/New file (2).png')"
],
"metadata": {
"id": "HsAxLauF5pnE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Write Prompt"
],
"metadata": {
"id": "oJSACBLt5_Sj"
}
},
{
"cell_type": "code",
"source": [
"prompt = \"3D Mesh Model Head Cat, Rendrer Object Mode, detailed, cute, adorable, 8k\""
],
"metadata": {
"id": "3ngRjHTY6ISS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Pass a prompt and image to the pipeline to generate an image:"
],
"metadata": {
"id": "HBFMaceZ6bp3"
}
},
{
"cell_type": "code",
"source": [
"image = pipeline(prompt, image=init_image).images[0]"
],
"metadata": {
"id": "T1EoPGoR6e0Z"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Display the image (optional)"
],
"metadata": {
"id": "xqEmoIFD8lsH"
}
},
{
"cell_type": "code",
"source": [
"make_image_grid([init_image, image], rows=1, cols=2)"
],
"metadata": {
"id": "QPwjH7th8rQB"
},
"execution_count": null,
"outputs": []
}
]
} |