{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "411c59b3-f177-4a10-8925-d931ce572eaa", "metadata": {}, "outputs": [], "source": [ "import torch\n", "from diffusers import StableDiffusionXLPipeline\n", "from PIL import Image\n", "\n", "from ip_adapter import IPAdapterXL" ] }, { "cell_type": "code", "execution_count": 2, "id": "6b6dc69c-192d-4d74-8b1e-f0d9ccfbdb49", "metadata": {}, "outputs": [], "source": [ "base_model_path = \"stabilityai/stable-diffusion-xl-base-1.0\"\n", "image_encoder_path = \"sdxl_models/image_encoder\"\n", "ip_ckpt = \"sdxl_models/ip-adapter_sdxl.bin\"\n", "device = \"cuda\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "63ec542f-8474-4f38-9457-073425578073", "metadata": {}, "outputs": [], "source": [ "def image_grid(imgs, rows, cols):\n", " assert len(imgs) == rows*cols\n", "\n", " w, h = imgs[0].size\n", " grid = Image.new('RGB', size=(cols*w, rows*h))\n", " grid_w, grid_h = grid.size\n", " \n", " for i, img in enumerate(imgs):\n", " grid.paste(img, box=(i%cols*w, i//cols*h))\n", " return grid" ] }, { "cell_type": "code", "execution_count": 4, "id": "3849f9d0-5f68-4a49-9190-69dd50720cae", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8ca86746f27347ffa542f17add4f1e82", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Loading pipeline components...: 0%| | 0/7 [00:00" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# read image prompt\n", "image = Image.open(\"assets/images/woman.png\")\n", "image.resize((512, 512))" ] }, { "cell_type": "code", "execution_count": 7, "id": "b77f52de-a9e4-44e1-aeec-8165414f1273", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a033b680cd41488dacb0f7db88190219", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/30 [00:00" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate image variations with only image prompt\n", "num_samples = 2\n", "images = ip_model.generate(pil_image=image, num_samples=num_samples, num_inference_steps=30, seed=420)\n", "grid = image_grid(images, 1, num_samples)\n", "grid" ] }, { "cell_type": "code", "execution_count": 8, "id": "36ec1dce-7861-4ce2-90de-0de36bb28569", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6fd89a42baa4445692722af1b76568fe", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/30 [00:00" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multimodal prompts\n", "images = ip_model.generate(pil_image=image, num_samples=num_samples, num_inference_steps=30, seed=420,\n", " prompt=\"best quality, high quality, wearing sunglasses on the beach\", scale=0.6)\n", "grid = image_grid(images, 1, num_samples)\n", "grid" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }