Create diffusers_image_outpaint_jupyter.ipynb
Browse files
diffusers_image_outpaint_jupyter.ipynb
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
%cd /content
|
2 |
+
!git clone -b dev https://github.com/camenduru/diffusers-image-outpaint-hf
|
3 |
+
%cd /content/diffusers-image-outpaint-hf
|
4 |
+
!pip install transformers accelerate diffusers
|
5 |
+
|
6 |
+
!apt -y install -qq aria2
|
7 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/model_index.json -d /content/model/lightning -o model_index.json
|
8 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/scheduler/scheduler_config.json -d /content/model/lightning/scheduler -o scheduler_config.json
|
9 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/text_encoder/config.json -d /content/model/lightning/text_encoder -o config.json
|
10 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/lightning/text_encoder/model.fp16.safetensors -d /content/model/lightning/text_encoder -o model.fp16.safetensors
|
11 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/text_encoder_2/config.json -d /content/model/lightning/text_encoder_2 -o config.json
|
12 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/lightning/text_encoder_2/model.fp16.safetensors -d /content/model/lightning/text_encoder_2 -o model.fp16.safetensors
|
13 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer/merges.txt -d /content/model/lightning/tokenizer -o merges.txt
|
14 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer/special_tokens_map.json -d /content/model/lightning/tokenizer -o special_tokens_map.json
|
15 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer/tokenizer_config.json -d /content/model/lightning/tokenizer -o tokenizer_config.json
|
16 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer/vocab.json -d /content/model/lightning/tokenizer -o vocab.json
|
17 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer_2/merges.txt -d /content/model/lightning/tokenizer_2 -o merges.txt
|
18 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer_2/special_tokens_map.json -d /content/model/lightning/tokenizer_2 -o special_tokens_map.json
|
19 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer_2/tokenizer_config.json -d /content/model/lightning/tokenizer_2 -o tokenizer_config.json
|
20 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/tokenizer_2/vocab.json -d /content/model/lightning/tokenizer_2 -o vocab.json
|
21 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/unet/config.json -d /content/model/lightning/unet -o config.json
|
22 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/lightning/unet/diffusion_pytorch_model.fp16.safetensors -d /content/model/lightning/unet -o diffusion_pytorch_model.fp16.safetensors
|
23 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/unet/diffusion_pytorch_model.safetensors.index.json -d /content/model/lightning/unet -o diffusion_pytorch_model.safetensors.index.json
|
24 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/lightning/vae/config.json -d /content/model/lightning/vae -o config.json
|
25 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/lightning/vae/diffusion_pytorch_model.fp16.safetensors -d /content/model/lightning/vae -o diffusion_pytorch_model.fp16.safetensors
|
26 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/vae-fix/config.json -d /content/model/vae-fix -o config.json
|
27 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/vae-fix/diffusion_pytorch_model.safetensors -d /content/model/vae-fix -o diffusion_pytorch_model.safetensors
|
28 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/raw/main/union/config_promax.json -d /content/model/union -o config_promax.json
|
29 |
+
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/camenduru/outpaint/resolve/main/union/diffusion_pytorch_model_promax.safetensors -d /content/model/union -o diffusion_pytorch_model_promax.safetensors
|
30 |
+
|
31 |
+
|
32 |
+
import torch
|
33 |
+
from diffusers import AutoencoderKL
|
34 |
+
from diffusers.models.model_loading_utils import load_state_dict
|
35 |
+
from controlnet_union import ControlNetModel_Union
|
36 |
+
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
37 |
+
from PIL import Image, ImageDraw
|
38 |
+
|
39 |
+
config = ControlNetModel_Union.load_config("/content/model/union/config_promax.json")
|
40 |
+
controlnet_model = ControlNetModel_Union.from_config(config)
|
41 |
+
state_dict = load_state_dict("/content/model/union/diffusion_pytorch_model_promax.safetensors")
|
42 |
+
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(controlnet_model, state_dict, "/content/model/union/diffusion_pytorch_model_promax.safetensors", "/content/model/union")
|
43 |
+
model.to(device="cuda", dtype=torch.float16)
|
44 |
+
vae = AutoencoderKL.from_pretrained("/content/model/vae-fix", torch_dtype=torch.float16).to("cuda")
|
45 |
+
pipe = StableDiffusionXLFillPipeline.from_pretrained("/content/model/lightning", torch_dtype=torch.float16, vae=vae, controlnet=model, variant="fp16").to("cuda")
|
46 |
+
|
47 |
+
def infer(image, width, height, overlap_width, num_inference_steps, prompt_input=None):
|
48 |
+
source = image
|
49 |
+
target_size = (width, height)
|
50 |
+
overlap = overlap_width
|
51 |
+
|
52 |
+
if source.width < target_size[0] and source.height < target_size[1]:
|
53 |
+
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
54 |
+
new_width = int(source.width * scale_factor)
|
55 |
+
new_height = int(source.height * scale_factor)
|
56 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
57 |
+
|
58 |
+
if source.width > target_size[0] or source.height > target_size[1]:
|
59 |
+
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
60 |
+
new_width = int(source.width * scale_factor)
|
61 |
+
new_height = int(source.height * scale_factor)
|
62 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
63 |
+
|
64 |
+
margin_x = (target_size[0] - source.width) // 2
|
65 |
+
margin_y = (target_size[1] - source.height) // 2
|
66 |
+
|
67 |
+
background = Image.new('RGB', target_size, (255, 255, 255))
|
68 |
+
background.paste(source, (margin_x, margin_y))
|
69 |
+
|
70 |
+
mask = Image.new('L', target_size, 255)
|
71 |
+
mask_draw = ImageDraw.Draw(mask)
|
72 |
+
mask_draw.rectangle([
|
73 |
+
(margin_x + overlap, margin_y + overlap),
|
74 |
+
(margin_x + source.width - overlap, margin_y + source.height - overlap)
|
75 |
+
], fill=0)
|
76 |
+
|
77 |
+
cnet_image = background.copy()
|
78 |
+
cnet_image.paste(0, (0, 0), mask)
|
79 |
+
|
80 |
+
final_prompt = "high quality"
|
81 |
+
if prompt_input and prompt_input.strip():
|
82 |
+
final_prompt += ", " + prompt_input
|
83 |
+
|
84 |
+
(
|
85 |
+
prompt_embeds,
|
86 |
+
negative_prompt_embeds,
|
87 |
+
pooled_prompt_embeds,
|
88 |
+
negative_pooled_prompt_embeds,
|
89 |
+
) = pipe.encode_prompt(final_prompt, "cuda", True)
|
90 |
+
|
91 |
+
results = []
|
92 |
+
|
93 |
+
for image in pipe(
|
94 |
+
prompt_embeds=prompt_embeds,
|
95 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
96 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
97 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
98 |
+
image=cnet_image,
|
99 |
+
num_inference_steps=num_inference_steps
|
100 |
+
):
|
101 |
+
results.append((cnet_image, image))
|
102 |
+
|
103 |
+
image = image.convert("RGBA")
|
104 |
+
cnet_image.paste(image, (0, 0), mask)
|
105 |
+
|
106 |
+
results.append((background, cnet_image))
|
107 |
+
|
108 |
+
return results
|