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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {
|
7 |
+
"id": "5rbnEz_FTJKL"
|
8 |
+
},
|
9 |
+
"outputs": [],
|
10 |
+
"source": [
|
11 |
+
"\n",
|
12 |
+
"######################################################################\n",
|
13 |
+
"!pip install -q --upgrade diffusers\n",
|
14 |
+
"!pip install -q --upgrade transformers\n",
|
15 |
+
"!pip install -q --upgrade tokenizers\n",
|
16 |
+
"######################################################################\n",
|
17 |
+
"!pip install -q --upgrade peft\n",
|
18 |
+
"######################################################################\n",
|
19 |
+
"#!pip install diffusers==0.21.1 transformers==4.33.2 tokenizers==0.13.3\n",
|
20 |
+
"######################################################################\n",
|
21 |
+
"\n",
|
22 |
+
"!pip install -q accelerate\n",
|
23 |
+
"!pip install -q safetensors\n",
|
24 |
+
"!pip install -q einops\n",
|
25 |
+
"!pip install -q onnxruntime-gpu\n",
|
26 |
+
"!pip install -q dlib\n",
|
27 |
+
"!pip install -q opencv-python\n",
|
28 |
+
"!pip install -q git+https://github.com/tencent-ailab/IP-Adapter.git"
|
29 |
+
]
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"cell_type": "code",
|
33 |
+
"execution_count": null,
|
34 |
+
"metadata": {
|
35 |
+
"id": "hThiB6kxTJKM"
|
36 |
+
},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"import torch\n",
|
40 |
+
"from diffusers.utils import load_image\n",
|
41 |
+
"from PIL import Image\n",
|
42 |
+
"import os\n"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": null,
|
48 |
+
"metadata": {
|
49 |
+
"id": "RB9wDFckTJKN"
|
50 |
+
},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"\n",
|
54 |
+
"os.makedirs(\"images\", exist_ok=True)\n",
|
55 |
+
"!wget --continue https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/1665_Girl_with_a_Pearl_Earring.jpg/800px-1665_Girl_with_a_Pearl_Earring.jpg -O images/Girl_with_a_Pearl_Earring.jpg\n",
|
56 |
+
"\n",
|
57 |
+
"!wget --continue https://huggingface.co/datasets/Norod78/ip-adapter-face-full-test/resolve/main/shape_predictor_5_face_landmarks.dat?download=true -O shape_predictor_5_face_landmarks.dat\n",
|
58 |
+
"\n",
|
59 |
+
"!wget --continue https://huggingface.co/datasets/Norod78/ip-adapter-face-full-test/raw/main/crop_head_dlib5.py -O crop_head_dlib5.py"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": null,
|
65 |
+
"metadata": {
|
66 |
+
"id": "kfvMkQD6TJKP"
|
67 |
+
},
|
68 |
+
"outputs": [],
|
69 |
+
"source": [
|
70 |
+
"import torch\n",
|
71 |
+
"from diffusers import StableDiffusionXLPipeline, DDIMScheduler, AutoencoderKL\n",
|
72 |
+
"from huggingface_hub import hf_hub_download\n",
|
73 |
+
"from diffusers.utils import load_image\n",
|
74 |
+
"from ip_adapter import IPAdapterFull\n",
|
75 |
+
"from PIL import Image\n",
|
76 |
+
"\n",
|
77 |
+
"def image_grid(imgs, rows, cols):\n",
|
78 |
+
" assert len(imgs) == rows*cols\n",
|
79 |
+
"\n",
|
80 |
+
" w, h = imgs[0].size\n",
|
81 |
+
" grid = Image.new('RGB', size=(cols*w, rows*h))\n",
|
82 |
+
" grid_w, grid_h = grid.size\n",
|
83 |
+
"\n",
|
84 |
+
" for i, img in enumerate(imgs):\n",
|
85 |
+
" grid.paste(img, box=(i%cols*w, i//cols*h))\n",
|
86 |
+
" return grid\n",
|
87 |
+
"\n",
|
88 |
+
"noise_scheduler = DDIMScheduler(\n",
|
89 |
+
" num_train_timesteps=1000,\n",
|
90 |
+
" beta_start=0.00085,\n",
|
91 |
+
" beta_end=0.012,\n",
|
92 |
+
" beta_schedule=\"scaled_linear\",\n",
|
93 |
+
" clip_sample=False,\n",
|
94 |
+
" set_alpha_to_one=False,\n",
|
95 |
+
" steps_offset=1\n",
|
96 |
+
")\n",
|
97 |
+
"\n",
|
98 |
+
"base_model_path = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
|
99 |
+
"vae_model_path = \"madebyollin/sdxl-vae-fp16-fix\"\n",
|
100 |
+
"ip_ckpt = hf_hub_download(repo_id=\"h94/IP-Adapter\", subfolder=\"sdxl_models\", filename=\"ip-adapter-plus-face_sdxl_vit-h.safetensors\", repo_type=\"model\")\n",
|
101 |
+
"image_encoder_path = \"laion/CLIP-ViT-H-14-laion2B-s32B-b79K\"\n",
|
102 |
+
"device = \"cuda\"\n",
|
103 |
+
"\n",
|
104 |
+
"with torch.no_grad():\n",
|
105 |
+
" print(\"Loading vae\")\n",
|
106 |
+
" vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)\n",
|
107 |
+
" print(\"Loading pipeline\")\n",
|
108 |
+
" pipe = StableDiffusionXLPipeline.from_pretrained(\n",
|
109 |
+
" base_model_path,\n",
|
110 |
+
" variant=\"fp16\",\n",
|
111 |
+
" torch_dtype=torch.float16,\n",
|
112 |
+
" scheduler=noise_scheduler,\n",
|
113 |
+
" vae=vae,\n",
|
114 |
+
" feature_extractor=None,\n",
|
115 |
+
" safety_checker=None\n",
|
116 |
+
" )\n",
|
117 |
+
"print(\"Done\")\n",
|
118 |
+
"\n"
|
119 |
+
]
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"cell_type": "code",
|
123 |
+
"source": [
|
124 |
+
"\n",
|
125 |
+
"#lora_weights = hf_hub_download(repo_id=\"Norod78/SDXL-YarnArtStyle-LoRA\", filename=\"SDXL_Yarn_Art_Style.safetensors\", repo_type=\"model\")\n",
|
126 |
+
"lora_weights = hf_hub_download(repo_id=\"Norod78/SDXL-Psychemelt-style-LoRA\" , filename=\"SDXL_Psychemelt_style_LoRA-000007.safetensors\", repo_type=\"model\")\n",
|
127 |
+
"\n",
|
128 |
+
"############################################################################################################################################\n",
|
129 |
+
"pipe.load_lora_weights(lora_weights)\n",
|
130 |
+
"############################################################################################################################################\n"
|
131 |
+
],
|
132 |
+
"metadata": {
|
133 |
+
"id": "byQMEGplF0FD"
|
134 |
+
},
|
135 |
+
"execution_count": null,
|
136 |
+
"outputs": []
|
137 |
+
},
|
138 |
+
{
|
139 |
+
"cell_type": "code",
|
140 |
+
"execution_count": null,
|
141 |
+
"metadata": {
|
142 |
+
"id": "ksM5XrlmTJKQ"
|
143 |
+
},
|
144 |
+
"outputs": [],
|
145 |
+
"source": [
|
146 |
+
"from ip_adapter import IPAdapterPlusXL\n",
|
147 |
+
"# load ip-adapter\n",
|
148 |
+
"with torch.no_grad():\n",
|
149 |
+
" ip_model = IPAdapterPlusXL(pipe, image_encoder_path, ip_ckpt, device, num_tokens=16)\n"
|
150 |
+
]
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"cell_type": "code",
|
154 |
+
"source": [
|
155 |
+
"import crop_head_dlib5\n",
|
156 |
+
"# Detect and crop the head\n",
|
157 |
+
"cropped_head = crop_head_dlib5.detect_and_crop_head(\"images/Girl_with_a_Pearl_Earring.jpg\", factor=4.5)\n",
|
158 |
+
"image = cropped_head.resize((224, 224))\n",
|
159 |
+
"image"
|
160 |
+
],
|
161 |
+
"metadata": {
|
162 |
+
"id": "2xz5rU5OIA-R"
|
163 |
+
},
|
164 |
+
"execution_count": null,
|
165 |
+
"outputs": []
|
166 |
+
},
|
167 |
+
{
|
168 |
+
"cell_type": "code",
|
169 |
+
"execution_count": null,
|
170 |
+
"metadata": {
|
171 |
+
"id": "xGjbgEdBTJKQ"
|
172 |
+
},
|
173 |
+
"outputs": [],
|
174 |
+
"source": [
|
175 |
+
"prompt = \"full body photo of a woman standing outdoors in a field, wearing a colorful dress in an LSD trip psychemelt style\"\n",
|
176 |
+
"negative_prompt = \"monochrome, lowres, bad anatomy, worst quality, low quality, blurry, extra limbs, nude, naked, nsfw\"\n",
|
177 |
+
"with torch.no_grad():\n",
|
178 |
+
" images = ip_model.generate(\n",
|
179 |
+
" prompt=prompt, negative_prompt=negative_prompt, pil_image=image, scale=0.85, guidance_scale=7.5, num_samples=4, width=1024, height=1280, num_inference_steps=30, seed=2024,\n",
|
180 |
+
" )\n",
|
181 |
+
"\n",
|
182 |
+
"grid = image_grid(images, 2, 2)\n",
|
183 |
+
"grid\n"
|
184 |
+
]
|
185 |
+
},
|
186 |
+
{
|
187 |
+
"cell_type": "code",
|
188 |
+
"source": [
|
189 |
+
"!wget --continue https://upload.wikimedia.org/wikipedia/commons/0/0f/SDCC_2015_-_Gal_Gadot_%2819742671671%29_%28cropped%29.jpg -O images/gal_gadot_full_body.jpg\n",
|
190 |
+
"cropped_head = crop_head_dlib5.detect_and_crop_head(\"images/gal_gadot_full_body.jpg\", factor=2.5)\n",
|
191 |
+
"image = cropped_head.resize((224, 224))\n",
|
192 |
+
"image"
|
193 |
+
],
|
194 |
+
"metadata": {
|
195 |
+
"id": "RePEtucoHcyS"
|
196 |
+
},
|
197 |
+
"execution_count": null,
|
198 |
+
"outputs": []
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"cell_type": "code",
|
202 |
+
"source": [
|
203 |
+
"\n",
|
204 |
+
"with torch.no_grad():\n",
|
205 |
+
" images = ip_model.generate(\n",
|
206 |
+
" prompt=prompt, negative_prompt=negative_prompt, pil_image=image, scale=0.85, guidance_scale=7.5, num_samples=4, width=1024, height=1280, num_inference_steps=30, seed=2024,\n",
|
207 |
+
" )\n",
|
208 |
+
"\n",
|
209 |
+
"grid = image_grid(images, 2, 2)\n",
|
210 |
+
"grid"
|
211 |
+
],
|
212 |
+
"metadata": {
|
213 |
+
"id": "Ic2r3eJoHn6B"
|
214 |
+
},
|
215 |
+
"execution_count": null,
|
216 |
+
"outputs": []
|
217 |
+
}
|
218 |
+
],
|
219 |
+
"metadata": {
|
220 |
+
"accelerator": "GPU",
|
221 |
+
"colab": {
|
222 |
+
"provenance": [],
|
223 |
+
"machine_shape": "hm",
|
224 |
+
"gpuType": "L4",
|
225 |
+
"private_outputs": true
|
226 |
+
},
|
227 |
+
"kernelspec": {
|
228 |
+
"display_name": "Python 3",
|
229 |
+
"name": "python3"
|
230 |
+
},
|
231 |
+
"language_info": {
|
232 |
+
"codemirror_mode": {
|
233 |
+
"name": "ipython",
|
234 |
+
"version": 3
|
235 |
+
},
|
236 |
+
"file_extension": ".py",
|
237 |
+
"mimetype": "text/x-python",
|
238 |
+
"name": "python",
|
239 |
+
"nbconvert_exporter": "python",
|
240 |
+
"pygments_lexer": "ipython3",
|
241 |
+
"version": "3.11.5"
|
242 |
+
}
|
243 |
+
},
|
244 |
+
"nbformat": 4,
|
245 |
+
"nbformat_minor": 0
|
246 |
+
}
|