{ "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": [] } ] }