Commit
Β·
8df522a
1
Parent(s):
ad267aa
update
Browse files- app.py +97 -45
- cldm/cldm.py +3 -2
- ldm/models/autoencoder.py +3 -3
- ldm/models/diffusion/ddpm.py +2 -2
- utils_stableviton.py +14 -4
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from preprocess.detectron2.projects.DensePose.apply_net_gradio import DensePose4Gradio
|
| 2 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 3 |
from preprocess.openpose.run_openpose import OpenPose
|
| 4 |
-
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import time
|
|
@@ -17,14 +17,13 @@ import spaces
|
|
| 17 |
|
| 18 |
from cldm.model import create_model
|
| 19 |
from cldm.plms_hacked import PLMSSampler
|
| 20 |
-
from utils_stableviton import get_batch,
|
| 21 |
-
print("pip import done")
|
| 22 |
|
| 23 |
PROJECT_ROOT = Path(__file__).absolute().parents[1].absolute()
|
| 24 |
sys.path.insert(0, str(PROJECT_ROOT))
|
| 25 |
|
| 26 |
-
IMG_H =
|
| 27 |
-
IMG_W =
|
| 28 |
|
| 29 |
openpose_model_hd = OpenPose(0)
|
| 30 |
openpose_model_hd.preprocessor.body_estimation.model.to('cuda')
|
|
@@ -44,18 +43,27 @@ config.model.params.img_W = IMG_W
|
|
| 44 |
params = config.model.params
|
| 45 |
|
| 46 |
model = create_model(config_path=None, config=config)
|
| 47 |
-
model.load_state_dict(torch.load("./checkpoints/
|
| 48 |
model = model.cuda()
|
| 49 |
model.eval()
|
| 50 |
sampler = PLMSSampler(model)
|
| 51 |
-
# #### model init <<<<
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
def stable_viton_model_hd(
|
| 55 |
batch,
|
| 56 |
n_steps,
|
| 57 |
):
|
| 58 |
z, cond = model.get_input(batch, params.first_stage_key)
|
|
|
|
| 59 |
bs = z.shape[0]
|
| 60 |
c_crossattn = cond["c_crossattn"][0][:bs]
|
| 61 |
if c_crossattn.ndim == 4:
|
|
@@ -71,16 +79,16 @@ def stable_viton_model_hd(
|
|
| 71 |
|
| 72 |
ts = torch.full((1,), 999, device=z.device, dtype=torch.long)
|
| 73 |
start_code = model.q_sample(z, ts)
|
| 74 |
-
|
| 75 |
output, _, _ = sampler.sample(
|
| 76 |
n_steps,
|
| 77 |
bs,
|
| 78 |
-
(4, IMG_H
|
| 79 |
cond,
|
| 80 |
-
x_T=start_code,
|
| 81 |
verbose=False,
|
| 82 |
eta=0.0,
|
| 83 |
-
unconditional_conditioning=uc_full,
|
| 84 |
)
|
| 85 |
|
| 86 |
output = model.decode_first_stage(output)
|
|
@@ -88,61 +96,107 @@ def stable_viton_model_hd(
|
|
| 88 |
pil_output = Image.fromarray(output)
|
| 89 |
return pil_output
|
| 90 |
|
| 91 |
-
@
|
| 92 |
@torch.no_grad()
|
| 93 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
model_type = 'hd'
|
| 95 |
category = 0 # 0:upperbody; 1:lowerbody; 2:dress
|
| 96 |
|
| 97 |
stt = time.time()
|
| 98 |
print('load images... ', end='')
|
| 99 |
-
garm_img = Image.open(garm_img).resize((IMG_W, IMG_H))
|
| 100 |
-
vton_img = Image.open(vton_img).resize((IMG_W, IMG_H))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
print('%.2fs' % (time.time() - stt))
|
| 102 |
|
| 103 |
stt = time.time()
|
| 104 |
print('get agnostic map... ', end='')
|
| 105 |
keypoints = openpose_model_hd(vton_img.resize((IMG_W, IMG_H)))
|
| 106 |
model_parse, _ = parsing_model_hd(vton_img.resize((IMG_W, IMG_H)))
|
| 107 |
-
mask, mask_gray = get_mask_location(model_type, category_dict_utils[category], model_parse, keypoints)
|
| 108 |
mask = mask.resize((IMG_W, IMG_H), Image.NEAREST)
|
| 109 |
mask_gray = mask_gray.resize((IMG_W, IMG_H), Image.NEAREST)
|
| 110 |
masked_vton_img = Image.composite(mask_gray, vton_img, mask) # agnostic map
|
| 111 |
print('%.2fs' % (time.time() - stt))
|
| 112 |
|
|
|
|
|
|
|
| 113 |
stt = time.time()
|
| 114 |
print('get densepose... ', end='')
|
| 115 |
vton_img = vton_img.resize((IMG_W, IMG_H)) # size for densepose
|
| 116 |
densepose = densepose_model_hd.execute(vton_img) # densepose
|
| 117 |
-
|
| 118 |
-
# human_img_arg = _apply_exif_orientation(vton_img.resize((IMG_W, IMG_H)))
|
| 119 |
-
# human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
| 120 |
-
# args = apply_net.create_argument_parser().parse_args(('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'))
|
| 121 |
-
# verbosity = getattr(args, "verbosity", None)
|
| 122 |
-
# pose_img = args.func(args, human_img_arg)
|
| 123 |
-
# pose_img = pose_img[:, :, ::-1]
|
| 124 |
-
# pose_img = Image.fromarray(pose_img).resize((IMG_W, IMG_H))
|
| 125 |
-
|
| 126 |
print('%.2fs' % (time.time() - stt))
|
| 127 |
|
| 128 |
batch = get_batch(
|
| 129 |
-
vton_img,
|
| 130 |
-
garm_img,
|
| 131 |
-
densepose,
|
| 132 |
-
masked_vton_img,
|
| 133 |
-
mask,
|
| 134 |
-
IMG_H,
|
| 135 |
IMG_W
|
| 136 |
)
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return sample
|
| 143 |
|
| 144 |
|
| 145 |
-
example_path = opj(os.path.dirname(__file__), '
|
| 146 |
example_model_ps = sorted(glob(opj(example_path, "model/*")))
|
| 147 |
example_garment_ps = sorted(glob(opj(example_path, "garment/*")))
|
| 148 |
|
|
@@ -151,7 +205,7 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 151 |
"""
|
| 152 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 153 |
<div>
|
| 154 |
-
<h1>StableVITON Demo πππ</h1>
|
| 155 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 156 |
<a href='https://arxiv.org/abs/2312.01725'>
|
| 157 |
<img src="https://img.shields.io/badge/arXiv-2312.01725-red">
|
|
@@ -189,17 +243,15 @@ with gr.Blocks(css='style.css') as demo:
|
|
| 189 |
examples_per_page=14,
|
| 190 |
examples=example_garment_ps)
|
| 191 |
with gr.Column():
|
| 192 |
-
result_gallery = gr.Image(label='Output', show_label=False, scale=1)
|
| 193 |
-
# result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", scale=1)
|
| 194 |
with gr.Column():
|
| 195 |
run_button = gr.Button(value="Run")
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
n_steps = gr.Slider(label="Steps", minimum=20, maximum=70, value=25, step=1)
|
| 199 |
-
# guidance_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=5.0, value=2.0, step=0.1)
|
| 200 |
# seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=-1)
|
| 201 |
|
| 202 |
-
ips = [vton_img, garm_img, n_steps]
|
| 203 |
run_button.click(fn=process_hd, inputs=ips, outputs=[result_gallery])
|
| 204 |
|
| 205 |
demo.queue().launch()
|
|
|
|
| 1 |
from preprocess.detectron2.projects.DensePose.apply_net_gradio import DensePose4Gradio
|
| 2 |
from preprocess.humanparsing.run_parsing import Parsing
|
| 3 |
from preprocess.openpose.run_openpose import OpenPose
|
| 4 |
+
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import time
|
|
|
|
| 17 |
|
| 18 |
from cldm.model import create_model
|
| 19 |
from cldm.plms_hacked import PLMSSampler
|
| 20 |
+
from utils_stableviton import get_mask_location, get_batch, tensor2img, center_crop
|
|
|
|
| 21 |
|
| 22 |
PROJECT_ROOT = Path(__file__).absolute().parents[1].absolute()
|
| 23 |
sys.path.insert(0, str(PROJECT_ROOT))
|
| 24 |
|
| 25 |
+
IMG_H = 1024
|
| 26 |
+
IMG_W = 768
|
| 27 |
|
| 28 |
openpose_model_hd = OpenPose(0)
|
| 29 |
openpose_model_hd.preprocessor.body_estimation.model.to('cuda')
|
|
|
|
| 43 |
params = config.model.params
|
| 44 |
|
| 45 |
model = create_model(config_path=None, config=config)
|
| 46 |
+
model.load_state_dict(torch.load("./checkpoints/eternal_1024.ckpt", map_location="cpu")["state_dict"])
|
| 47 |
model = model.cuda()
|
| 48 |
model.eval()
|
| 49 |
sampler = PLMSSampler(model)
|
|
|
|
| 50 |
|
| 51 |
+
model2 = create_model(config_path=None, config=config)
|
| 52 |
+
model2.load_state_dict(torch.load("./checkpoints/VITONHD_1024.ckpt", map_location="cpu")["state_dict"])
|
| 53 |
+
model2 = model.cuda()
|
| 54 |
+
model2.eval()
|
| 55 |
+
sampler2 = PLMSSampler(model2)
|
| 56 |
|
| 57 |
+
# #### model init <<<<
|
| 58 |
+
@spaces.GPU
|
| 59 |
+
@torch.autocast("cuda")
|
| 60 |
+
@torch.no_grad()
|
| 61 |
def stable_viton_model_hd(
|
| 62 |
batch,
|
| 63 |
n_steps,
|
| 64 |
):
|
| 65 |
z, cond = model.get_input(batch, params.first_stage_key)
|
| 66 |
+
z = z
|
| 67 |
bs = z.shape[0]
|
| 68 |
c_crossattn = cond["c_crossattn"][0][:bs]
|
| 69 |
if c_crossattn.ndim == 4:
|
|
|
|
| 79 |
|
| 80 |
ts = torch.full((1,), 999, device=z.device, dtype=torch.long)
|
| 81 |
start_code = model.q_sample(z, ts)
|
| 82 |
+
torch.cuda.empty_cache()
|
| 83 |
output, _, _ = sampler.sample(
|
| 84 |
n_steps,
|
| 85 |
bs,
|
| 86 |
+
(4, IMG_H//8, IMG_W//8),
|
| 87 |
cond,
|
| 88 |
+
x_T=start_code,
|
| 89 |
verbose=False,
|
| 90 |
eta=0.0,
|
| 91 |
+
unconditional_conditioning=uc_full,
|
| 92 |
)
|
| 93 |
|
| 94 |
output = model.decode_first_stage(output)
|
|
|
|
| 96 |
pil_output = Image.fromarray(output)
|
| 97 |
return pil_output
|
| 98 |
|
| 99 |
+
@torch.autocast("cuda")
|
| 100 |
@torch.no_grad()
|
| 101 |
+
def stable_viton_model_hd2(
|
| 102 |
+
batch,
|
| 103 |
+
n_steps,
|
| 104 |
+
):
|
| 105 |
+
z, cond = model2.get_input(batch, params.first_stage_key)
|
| 106 |
+
z = z
|
| 107 |
+
bs = z.shape[0]
|
| 108 |
+
c_crossattn = cond["c_crossattn"][0][:bs]
|
| 109 |
+
if c_crossattn.ndim == 4:
|
| 110 |
+
c_crossattn = model2.get_learned_conditioning(c_crossattn)
|
| 111 |
+
cond["c_crossattn"] = [c_crossattn]
|
| 112 |
+
uc_cross = model2.get_unconditional_conditioning(bs)
|
| 113 |
+
uc_full = {"c_concat": cond["c_concat"], "c_crossattn": [uc_cross]}
|
| 114 |
+
uc_full["first_stage_cond"] = cond["first_stage_cond"]
|
| 115 |
+
for k, v in batch.items():
|
| 116 |
+
if isinstance(v, torch.Tensor):
|
| 117 |
+
batch[k] = v.cuda()
|
| 118 |
+
sampler2.model.batch = batch
|
| 119 |
+
|
| 120 |
+
ts = torch.full((1,), 999, device=z.device, dtype=torch.long)
|
| 121 |
+
start_code = model2.q_sample(z, ts)
|
| 122 |
+
torch.cuda.empty_cache()
|
| 123 |
+
output, _, _ = sampler2.sample(
|
| 124 |
+
n_steps,
|
| 125 |
+
bs,
|
| 126 |
+
(4, IMG_H//8, IMG_W//8),
|
| 127 |
+
cond,
|
| 128 |
+
x_T=start_code,
|
| 129 |
+
verbose=False,
|
| 130 |
+
eta=0.0,
|
| 131 |
+
unconditional_conditioning=uc_full,
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
output = model2.decode_first_stage(output)
|
| 135 |
+
output = tensor2img(output)
|
| 136 |
+
pil_output = Image.fromarray(output)
|
| 137 |
+
return pil_output
|
| 138 |
+
|
| 139 |
+
# @spaces.GPU # TODO: turn on when final upload
|
| 140 |
+
@torch.no_grad()
|
| 141 |
+
def process_hd(vton_img, garm_img, n_steps, is_custom):
|
| 142 |
model_type = 'hd'
|
| 143 |
category = 0 # 0:upperbody; 1:lowerbody; 2:dress
|
| 144 |
|
| 145 |
stt = time.time()
|
| 146 |
print('load images... ', end='')
|
| 147 |
+
# garm_img = Image.open(garm_img).resize((IMG_W, IMG_H))
|
| 148 |
+
# vton_img = Image.open(vton_img).resize((IMG_W, IMG_H))
|
| 149 |
+
garm_img = Image.open(garm_img)
|
| 150 |
+
vton_img = Image.open(vton_img)
|
| 151 |
+
|
| 152 |
+
vton_img = center_crop(vton_img)
|
| 153 |
+
garm_img = garm_img.resize((IMG_W, IMG_H))
|
| 154 |
+
vton_img = vton_img.resize((IMG_W, IMG_H))
|
| 155 |
+
|
| 156 |
print('%.2fs' % (time.time() - stt))
|
| 157 |
|
| 158 |
stt = time.time()
|
| 159 |
print('get agnostic map... ', end='')
|
| 160 |
keypoints = openpose_model_hd(vton_img.resize((IMG_W, IMG_H)))
|
| 161 |
model_parse, _ = parsing_model_hd(vton_img.resize((IMG_W, IMG_H)))
|
| 162 |
+
mask, mask_gray = get_mask_location(model_type, category_dict_utils[category], model_parse, keypoints, radius=5)
|
| 163 |
mask = mask.resize((IMG_W, IMG_H), Image.NEAREST)
|
| 164 |
mask_gray = mask_gray.resize((IMG_W, IMG_H), Image.NEAREST)
|
| 165 |
masked_vton_img = Image.composite(mask_gray, vton_img, mask) # agnostic map
|
| 166 |
print('%.2fs' % (time.time() - stt))
|
| 167 |
|
| 168 |
+
# breakpoint()
|
| 169 |
+
|
| 170 |
stt = time.time()
|
| 171 |
print('get densepose... ', end='')
|
| 172 |
vton_img = vton_img.resize((IMG_W, IMG_H)) # size for densepose
|
| 173 |
densepose = densepose_model_hd.execute(vton_img) # densepose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
print('%.2fs' % (time.time() - stt))
|
| 175 |
|
| 176 |
batch = get_batch(
|
| 177 |
+
vton_img,
|
| 178 |
+
garm_img,
|
| 179 |
+
densepose,
|
| 180 |
+
masked_vton_img,
|
| 181 |
+
mask,
|
| 182 |
+
IMG_H,
|
| 183 |
IMG_W
|
| 184 |
)
|
| 185 |
+
|
| 186 |
+
if is_custom:
|
| 187 |
+
sample = stable_viton_model_hd(
|
| 188 |
+
batch,
|
| 189 |
+
n_steps,
|
| 190 |
+
)
|
| 191 |
+
else:
|
| 192 |
+
sample = stable_viton_model_hd2(
|
| 193 |
+
batch,
|
| 194 |
+
n_steps,
|
| 195 |
+
)
|
| 196 |
return sample
|
| 197 |
|
| 198 |
|
| 199 |
+
example_path = opj(os.path.dirname(__file__), 'examples_eternal')
|
| 200 |
example_model_ps = sorted(glob(opj(example_path, "model/*")))
|
| 201 |
example_garment_ps = sorted(glob(opj(example_path, "garment/*")))
|
| 202 |
|
|
|
|
| 205 |
"""
|
| 206 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 207 |
<div>
|
| 208 |
+
<h1>Rdy2Wr.AI StableVITON Demo πππ</h1>
|
| 209 |
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
|
| 210 |
<a href='https://arxiv.org/abs/2312.01725'>
|
| 211 |
<img src="https://img.shields.io/badge/arXiv-2312.01725-red">
|
|
|
|
| 243 |
examples_per_page=14,
|
| 244 |
examples=example_garment_ps)
|
| 245 |
with gr.Column():
|
| 246 |
+
result_gallery = gr.Image(label='Output', show_label=False, preview=True, scale=1)
|
| 247 |
+
# result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", preview=True, scale=1)
|
| 248 |
with gr.Column():
|
| 249 |
run_button = gr.Button(value="Run")
|
| 250 |
+
n_steps = gr.Slider(label="Steps", minimum=10, maximum=50, value=20, step=1)
|
| 251 |
+
is_custom = gr.Checkbox(label="customized model")
|
|
|
|
|
|
|
| 252 |
# seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=-1)
|
| 253 |
|
| 254 |
+
ips = [vton_img, garm_img, n_steps, is_custom]
|
| 255 |
run_button.click(fn=process_hd, inputs=ips, outputs=[result_gallery])
|
| 256 |
|
| 257 |
demo.queue().launch()
|
cldm/cldm.py
CHANGED
|
@@ -32,6 +32,7 @@ class ControlLDM(LatentDiffusion):
|
|
| 32 |
*args,
|
| 33 |
**kwargs
|
| 34 |
):
|
|
|
|
| 35 |
self.control_stage_config = control_stage_config
|
| 36 |
self.use_pbe_weight = use_pbe_weight
|
| 37 |
self.u_cond_percent = u_cond_percent
|
|
@@ -62,7 +63,7 @@ class ControlLDM(LatentDiffusion):
|
|
| 62 |
control = control[:bs]
|
| 63 |
control = control.to(self.device)
|
| 64 |
control = einops.rearrange(control, 'b h w c -> b c h w')
|
| 65 |
-
control = control.to(memory_format=torch.contiguous_format)
|
| 66 |
control_lst.append(control)
|
| 67 |
control = control_lst
|
| 68 |
else:
|
|
@@ -71,7 +72,7 @@ class ControlLDM(LatentDiffusion):
|
|
| 71 |
control = control[:bs]
|
| 72 |
control = control.to(self.device)
|
| 73 |
control = einops.rearrange(control, 'b h w c -> b c h w')
|
| 74 |
-
control = control.to(memory_format=torch.contiguous_format)
|
| 75 |
control = [control]
|
| 76 |
cond_dict = dict(c_crossattn=[c], c_concat=control)
|
| 77 |
if self.first_stage_key_cond is not None:
|
|
|
|
| 32 |
*args,
|
| 33 |
**kwargs
|
| 34 |
):
|
| 35 |
+
self.device = torch.device("cuda")
|
| 36 |
self.control_stage_config = control_stage_config
|
| 37 |
self.use_pbe_weight = use_pbe_weight
|
| 38 |
self.u_cond_percent = u_cond_percent
|
|
|
|
| 63 |
control = control[:bs]
|
| 64 |
control = control.to(self.device)
|
| 65 |
control = einops.rearrange(control, 'b h w c -> b c h w')
|
| 66 |
+
control = control.to(memory_format=torch.contiguous_format)
|
| 67 |
control_lst.append(control)
|
| 68 |
control = control_lst
|
| 69 |
else:
|
|
|
|
| 72 |
control = control[:bs]
|
| 73 |
control = control.to(self.device)
|
| 74 |
control = einops.rearrange(control, 'b h w c -> b c h w')
|
| 75 |
+
control = control.to(memory_format=torch.contiguous_format)
|
| 76 |
control = [control]
|
| 77 |
cond_dict = dict(c_crossattn=[c], c_concat=control)
|
| 78 |
if self.first_stage_key_cond is not None:
|
ldm/models/autoencoder.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
-
import pytorch_lightning as pl
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
from contextlib import contextmanager
|
|
@@ -9,9 +9,9 @@ from ldm.modules.distributions.distributions import DiagonalGaussianDistribution
|
|
| 9 |
|
| 10 |
from ldm.util import instantiate_from_config
|
| 11 |
from ldm.modules.ema import LitEma
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
def __init__(self,
|
| 16 |
ddconfig,
|
| 17 |
lossconfig,
|
|
|
|
| 1 |
import torch
|
| 2 |
+
# import pytorch_lightning as pl
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
| 5 |
from contextlib import contextmanager
|
|
|
|
| 9 |
|
| 10 |
from ldm.util import instantiate_from_config
|
| 11 |
from ldm.modules.ema import LitEma
|
|
|
|
| 12 |
|
| 13 |
+
|
| 14 |
+
class AutoencoderKL(nn.Module):
|
| 15 |
def __init__(self,
|
| 16 |
ddconfig,
|
| 17 |
lossconfig,
|
ldm/models/diffusion/ddpm.py
CHANGED
|
@@ -9,7 +9,7 @@ https://github.com/CompVis/taming-transformers
|
|
| 9 |
import torch
|
| 10 |
import torch.nn as nn
|
| 11 |
import numpy as np
|
| 12 |
-
import pytorch_lightning as pl
|
| 13 |
from torch.optim.lr_scheduler import LambdaLR
|
| 14 |
from einops import rearrange, repeat
|
| 15 |
from contextlib import contextmanager, nullcontext
|
|
@@ -47,7 +47,7 @@ def disabled_train(self, mode=True):
|
|
| 47 |
def uniform_on_device(r1, r2, shape, device):
|
| 48 |
return (r1 - r2) * torch.rand(*shape, device=device) + r2
|
| 49 |
|
| 50 |
-
class DDPM(
|
| 51 |
# classic DDPM with Gaussian diffusion, in image space
|
| 52 |
def __init__(self,
|
| 53 |
unet_config,
|
|
|
|
| 9 |
import torch
|
| 10 |
import torch.nn as nn
|
| 11 |
import numpy as np
|
| 12 |
+
# import pytorch_lightning as pl
|
| 13 |
from torch.optim.lr_scheduler import LambdaLR
|
| 14 |
from einops import rearrange, repeat
|
| 15 |
from contextlib import contextmanager, nullcontext
|
|
|
|
| 47 |
def uniform_on_device(r1, r2, shape, device):
|
| 48 |
return (r1 - r2) * torch.rand(*shape, device=device) + r2
|
| 49 |
|
| 50 |
+
class DDPM(nn.Module):
|
| 51 |
# classic DDPM with Gaussian diffusion, in image space
|
| 52 |
def __init__(self,
|
| 53 |
unet_config,
|
utils_stableviton.py
CHANGED
|
@@ -24,7 +24,6 @@ label_map = {
|
|
| 24 |
"scarf": 17,
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
def extend_arm_mask(wrist, elbow, scale):
|
| 29 |
wrist = elbow + scale * (wrist - elbow)
|
| 30 |
return wrist
|
|
@@ -56,7 +55,7 @@ def refine_mask(mask):
|
|
| 56 |
return refine_mask
|
| 57 |
|
| 58 |
|
| 59 |
-
def get_mask_location(model_type, category, model_parse: Image.Image, keypoint: dict, width=384, height=512):
|
| 60 |
im_parse = model_parse.resize((width, height), Image.NEAREST)
|
| 61 |
parse_array = np.array(im_parse)
|
| 62 |
|
|
@@ -149,10 +148,10 @@ def get_mask_location(model_type, category, model_parse: Image.Image, keypoint:
|
|
| 149 |
parser_mask_fixed += hands_left + hands_right
|
| 150 |
|
| 151 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 152 |
-
parse_mask = cv2.dilate(parse_mask, np.ones((
|
| 153 |
if category == 'dresses' or category == 'upper_body':
|
| 154 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 155 |
-
neck_mask = cv2.dilate(neck_mask, np.ones((
|
| 156 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 157 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 158 |
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.uint16), iterations=4)
|
|
@@ -204,3 +203,14 @@ def tensor2img(x):
|
|
| 204 |
x = np.concatenate([x,x,x], axis=-1)
|
| 205 |
return x
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"scarf": 17,
|
| 25 |
}
|
| 26 |
|
|
|
|
| 27 |
def extend_arm_mask(wrist, elbow, scale):
|
| 28 |
wrist = elbow + scale * (wrist - elbow)
|
| 29 |
return wrist
|
|
|
|
| 55 |
return refine_mask
|
| 56 |
|
| 57 |
|
| 58 |
+
def get_mask_location(model_type, category, model_parse: Image.Image, keypoint: dict, width=384, height=512, radius=5):
|
| 59 |
im_parse = model_parse.resize((width, height), Image.NEAREST)
|
| 60 |
parse_array = np.array(im_parse)
|
| 61 |
|
|
|
|
| 148 |
parser_mask_fixed += hands_left + hands_right
|
| 149 |
|
| 150 |
parser_mask_fixed = np.logical_or(parser_mask_fixed, parse_head)
|
| 151 |
+
parse_mask = cv2.dilate(parse_mask, np.ones((radius, radius), np.uint16), iterations=5)
|
| 152 |
if category == 'dresses' or category == 'upper_body':
|
| 153 |
neck_mask = (parse_array == 18).astype(np.float32)
|
| 154 |
+
neck_mask = cv2.dilate(neck_mask, np.ones((radius, radius), np.uint16), iterations=1)
|
| 155 |
neck_mask = np.logical_and(neck_mask, np.logical_not(parse_head))
|
| 156 |
parse_mask = np.logical_or(parse_mask, neck_mask)
|
| 157 |
arm_mask = cv2.dilate(np.logical_or(im_arms_left, im_arms_right).astype('float32'), np.ones((5, 5), np.uint16), iterations=4)
|
|
|
|
| 203 |
x = np.concatenate([x,x,x], axis=-1)
|
| 204 |
return x
|
| 205 |
|
| 206 |
+
def center_crop(image):
|
| 207 |
+
width, height = image.size
|
| 208 |
+
new_height = height
|
| 209 |
+
new_width = height*3/4
|
| 210 |
+
left = (width - new_width)/2
|
| 211 |
+
top = (height - new_height)/2
|
| 212 |
+
right = (width + new_width)/2
|
| 213 |
+
bottom = (height + new_height)/2
|
| 214 |
+
|
| 215 |
+
image = image.crop((left, top, right, bottom))
|
| 216 |
+
return image
|