-
Notifications
You must be signed in to change notification settings - Fork 61
/
inference_ootd.py
164 lines (145 loc) · 5.31 KB
/
inference_ootd.py
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
import os
import random
import sys
import time
from pathlib import Path
import torch
from diffusers import AutoencoderKL, UniPCMultistepScheduler
from transformers import (
AutoProcessor,
CLIPTextModel,
CLIPTokenizer,
CLIPVisionModelWithProjection,
)
from . import pipelines_ootd
from .humanparsing.run_parsing import Parsing
from .openpose.run_openpose import OpenPose
#! Necessary for OotdPipeline.from_pretrained
sys.modules["pipelines_ootd"] = pipelines_ootd
from .pipelines_ootd.pipeline_ootd import OotdPipeline
from .pipelines_ootd.unet_garm_2d_condition import UNetGarm2DConditionModel
from .pipelines_ootd.unet_vton_2d_condition import UNetVton2DConditionModel
class OOTDiffusion:
def __init__(self, root: str, model_type: str = "hd"):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.torch_dtype = torch.float32 if self.device == "cpu" else torch.float16
if model_type not in ("hd", "dc"):
raise ValueError(f"model_type must be 'hd' or 'dc', got {model_type!r}")
self.model_type = model_type
self.repo_root = root
VIT_PATH = f"openai/clip-vit-large-patch14"
MODEL_PATH = Path(root) / "checkpoints" / "ootd"
if model_type == "hd":
UNET_PATH = MODEL_PATH / "ootd_hd" / "checkpoint-36000"
else:
UNET_PATH = MODEL_PATH / "ootd_dc" / "checkpoint-36000"
atr_model_path = (
Path(root) / "checkpoints/humanparsing/parsing_atr.onnx"
)
lip_model_path = (
Path(root) / "checkpoints/humanparsing/parsing_lip.onnx"
)
self.parsing_model = Parsing(
atr_model_path=str(atr_model_path),
lip_model_path=str(lip_model_path),
)
body_pose_model_path = (
Path(root) / "checkpoints/openpose/ckpts/body_pose_model.pth"
)
self.openpose_model = OpenPose(
str(body_pose_model_path),
device=self.device,
)
unet_garm = UNetGarm2DConditionModel.from_pretrained(
UNET_PATH,
subfolder="unet_garm",
torch_dtype=self.torch_dtype,
use_safetensors=True,
)
unet_vton = UNetVton2DConditionModel.from_pretrained(
UNET_PATH,
subfolder="unet_vton",
torch_dtype=self.torch_dtype,
use_safetensors=True,
)
self.pipe = OotdPipeline.from_pretrained(
MODEL_PATH,
unet_garm=unet_garm,
unet_vton=unet_vton,
vae=AutoencoderKL.from_pretrained(
f"{MODEL_PATH}/vae",
torch_dtype=self.torch_dtype,
),
torch_dtype=self.torch_dtype,
variant="fp16",
use_safetensors=True,
safety_checker=None,
requires_safety_checker=False,
).to(self.device)
self.pipe.scheduler = UniPCMultistepScheduler.from_config(
self.pipe.scheduler.config
)
self.auto_processor = AutoProcessor.from_pretrained(VIT_PATH)
self.image_encoder = CLIPVisionModelWithProjection.from_pretrained(VIT_PATH).to(
self.device
)
self.tokenizer = self.pipe.tokenizer
self.text_encoder = self.pipe.text_encoder
def tokenize_captions(self, captions, max_length):
inputs = self.tokenizer(
captions,
max_length=max_length,
padding="max_length",
truncation=True,
return_tensors="pt",
)
return inputs.input_ids
def __call__(
self,
category="upperbody",
image_garm=None,
image_vton=None,
mask=None,
image_ori=None,
num_samples=1,
num_steps=20,
image_scale=1.0,
seed=-1,
):
if seed == -1:
random.seed(time.time())
seed = random.randint(0, 0xFFFFFFFFFFFFFFFF)
print("Initial seed: " + str(seed))
generator = torch.manual_seed(seed)
with torch.no_grad():
prompt_image = self.auto_processor(
images=image_garm, return_tensors="pt"
).to(self.device)
prompt_image = self.image_encoder(
prompt_image.data["pixel_values"]
).image_embeds
prompt_image = prompt_image.unsqueeze(1)
if self.model_type == "hd":
prompt_embeds = self.text_encoder(
self.tokenize_captions([""], 2).to(self.device)
)[0]
prompt_embeds[:, 1:] = prompt_image[:]
elif self.model_type == "dc":
prompt_embeds = self.text_encoder(
self.tokenize_captions([category], 3).to(self.device)
)[0]
prompt_embeds = torch.cat([prompt_embeds, prompt_image], dim=1)
else:
raise ValueError("model_type must be 'hd' or 'dc'!")
images = self.pipe(
prompt_embeds=prompt_embeds,
image_garm=image_garm,
image_vton=image_vton,
mask=mask,
image_ori=image_ori,
num_inference_steps=num_steps,
image_guidance_scale=image_scale,
num_images_per_prompt=num_samples,
generator=generator,
).images
return images