File size: 12,387 Bytes
5edd223 |
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
import os
import numpy as np
import torch
import argparse
from torch.utils.data import Dataset, DataLoader
from diffusers.image_processor import VaeImageProcessor
from tqdm import tqdm
from PIL import Image, ImageFilter
from model.pipeline import CatVTONPipeline
class InferenceDataset(Dataset):
def __init__(self, args):
self.args = args
self.vae_processor = VaeImageProcessor(vae_scale_factor=8)
self.mask_processor = VaeImageProcessor(vae_scale_factor=8, do_normalize=False, do_binarize=True, do_convert_grayscale=True)
self.data = self.load_data()
def load_data(self):
return []
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
data = self.data[idx]
person, cloth, mask = [Image.open(data[key]) for key in ['person', 'cloth', 'mask']]
return {
'index': idx,
'person_name': data['person_name'],
'person': self.vae_processor.preprocess(person, self.args.height, self.args.width)[0],
'cloth': self.vae_processor.preprocess(cloth, self.args.height, self.args.width)[0],
'mask': self.mask_processor.preprocess(mask, self.args.height, self.args.width)[0]
}
class VITONHDTestDataset(InferenceDataset):
def load_data(self):
assert os.path.exists(pair_txt:=os.path.join(self.args.data_root_path, 'test_pairs_unpaired.txt')), f"File {pair_txt} does not exist."
with open(pair_txt, 'r') as f:
lines = f.readlines()
self.args.data_root_path = os.path.join(self.args.data_root_path, "test")
output_dir = os.path.join(self.args.output_dir, "vitonhd", 'unpaired' if not self.args.eval_pair else 'paired')
data = []
for line in lines:
person_img, cloth_img = line.strip().split(" ")
if os.path.exists(os.path.join(output_dir, person_img)):
continue
if self.args.eval_pair:
cloth_img = person_img
data.append({
'person_name': person_img,
'person': os.path.join(self.args.data_root_path, 'image', person_img),
'cloth': os.path.join(self.args.data_root_path, 'cloth', cloth_img),
'mask': os.path.join(self.args.data_root_path, 'agnostic-mask', person_img.replace('.jpg', '_mask.png')),
})
return data
class DressCodeTestDataset(InferenceDataset):
def load_data(self):
data = []
for sub_folder in ['upper_body', 'lower_body', 'dresses']:
assert os.path.exists(os.path.join(self.args.data_root_path, sub_folder)), f"Folder {sub_folder} does not exist."
pair_txt = os.path.join(self.args.data_root_path, sub_folder, 'test_pairs_paired.txt' if self.args.eval_pair else 'test_pairs_unpaired.txt')
assert os.path.exists(pair_txt), f"File {pair_txt} does not exist."
with open(pair_txt, 'r') as f:
lines = f.readlines()
output_dir = os.path.join(self.args.output_dir, f"dresscode-{self.args.height}",
'unpaired' if not self.args.eval_pair else 'paired', sub_folder)
for line in lines:
person_img, cloth_img = line.strip().split(" ")
if os.path.exists(os.path.join(output_dir, person_img)):
continue
data.append({
'person_name': os.path.join(sub_folder, person_img),
'person': os.path.join(self.args.data_root_path, sub_folder, 'images', person_img),
'cloth': os.path.join(self.args.data_root_path, sub_folder, 'images', cloth_img),
'mask': os.path.join(self.args.data_root_path, sub_folder, 'agnostic_masks', person_img.replace('.jpg', '.png'))
})
return data
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument(
"--base_model_path",
type=str,
default="runwayml/stable-diffusion-inpainting",
help=(
"The path to the base model to use for evaluation. This can be a local path or a model identifier from the Model Hub."
),
)
parser.add_argument(
"--resume_path",
type=str,
default="zhengchong/CatVTON",
help=(
"The Path to the checkpoint of trained tryon model."
),
)
parser.add_argument(
"--dataset_name",
type=str,
required=True,
help="The datasets to use for evaluation.",
)
parser.add_argument(
"--data_root_path",
type=str,
required=True,
help="Path to the dataset to evaluate."
)
parser.add_argument(
"--output_dir",
type=str,
default="output",
help="The output directory where the model predictions will be written.",
)
parser.add_argument(
"--seed", type=int, default=555, help="A seed for reproducible evaluation."
)
parser.add_argument(
"--batch_size", type=int, default=8, help="The batch size for evaluation."
)
parser.add_argument(
"--num_inference_steps",
type=int,
default=50,
help="Number of inference steps to perform.",
)
parser.add_argument(
"--guidance_scale",
type=float,
default=2.5,
help="The scale of classifier-free guidance for inference.",
)
parser.add_argument(
"--width",
type=int,
default=384,
help=(
"The resolution for input images, all the images in the train/validation dataset will be resized to this"
" resolution"
),
)
parser.add_argument(
"--height",
type=int,
default=512,
help=(
"The resolution for input images, all the images in the train/validation dataset will be resized to this"
" resolution"
),
)
parser.add_argument(
"--repaint",
action="store_true",
help="Whether to repaint the result image with the original background."
)
parser.add_argument(
"--eval_pair",
action="store_true",
help="Whether or not to evaluate the pair.",
)
parser.add_argument(
"--concat_eval_results",
action="store_true",
help="Whether or not to concatenate the all conditions into one image.",
)
parser.add_argument(
"--allow_tf32",
action="store_true",
default=True,
help=(
"Whether or not to allow TF32 on Ampere GPUs. Can be used to speed up training. For more information, see"
" https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices"
),
)
parser.add_argument(
"--dataloader_num_workers",
type=int,
default=8,
help=(
"Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process."
),
)
parser.add_argument(
"--mixed_precision",
type=str,
default="bf16",
choices=["no", "fp16", "bf16"],
help=(
"Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >="
" 1.10.and an Nvidia Ampere GPU. Default to the value of accelerate config of the current system or the"
" flag passed with the `accelerate.launch` command. Use this argument to override the accelerate config."
),
)
parser.add_argument(
"--concat_axis",
type=str,
choices=["x", "y", 'random'],
default="y",
help="The axis to concat the cloth feature, select from ['x', 'y', 'random'].",
)
parser.add_argument(
"--enable_condition_noise",
action="store_true",
default=True,
help="Whether or not to enable condition noise.",
)
args = parser.parse_args()
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
if env_local_rank != -1 and env_local_rank != args.local_rank:
args.local_rank = env_local_rank
return args
def repaint(person, mask, result):
_, h = result.size
kernal_size = h // 50
if kernal_size % 2 == 0:
kernal_size += 1
mask = mask.filter(ImageFilter.GaussianBlur(kernal_size))
person_np = np.array(person)
result_np = np.array(result)
mask_np = np.array(mask) / 255
repaint_result = person_np * (1 - mask_np) + result_np * mask_np
repaint_result = Image.fromarray(repaint_result.astype(np.uint8))
return repaint_result
def to_pil_image(images):
images = (images / 2 + 0.5).clamp(0, 1)
images = images.cpu().permute(0, 2, 3, 1).float().numpy()
if images.ndim == 3:
images = images[None, ...]
images = (images * 255).round().astype("uint8")
if images.shape[-1] == 1:
# special case for grayscale (single channel) images
pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images]
else:
pil_images = [Image.fromarray(image) for image in images]
return pil_images
@torch.no_grad()
def main():
args = parse_args()
# Pipeline
pipeline = CatVTONPipeline(
attn_ckpt_version=args.dataset_name,
attn_ckpt=args.resume_path,
base_ckpt=args.base_model_path,
weight_dtype={
"no": torch.float32,
"fp16": torch.float16,
"bf16": torch.bfloat16,
}[args.mixed_precision],
# device="cuda",
device='cpu',
skip_safety_check=True
)
# Dataset
if args.dataset_name == "vitonhd":
dataset = VITONHDTestDataset(args)
elif args.dataset_name == "dresscode":
dataset = DressCodeTestDataset(args)
else:
raise ValueError(f"Invalid dataset name {args.dataset}.")
print(f"Dataset {args.dataset_name} loaded, total {len(dataset)} pairs.")
dataloader = DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.dataloader_num_workers
)
# Inference
# generator = torch.Generator(device='cuda').manual_seed(args.seed)
generator = torch.Generator(device='cpu').manual_seed(args.seed)
args.output_dir = os.path.join(args.output_dir, f"{args.dataset_name}-{args.height}", "paired" if args.eval_pair else "unpaired")
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
for batch in tqdm(dataloader):
person_images = batch['person']
cloth_images = batch['cloth']
masks = batch['mask']
results = pipeline(
person_images,
cloth_images,
masks,
num_inference_steps=args.num_inference_steps,
guidance_scale=args.guidance_scale,
height=args.height,
width=args.width,
generator=generator,
)
if args.concat_eval_results or args.repaint:
person_images = to_pil_image(person_images)
cloth_images = to_pil_image(cloth_images)
masks = to_pil_image(masks)
for i, result in enumerate(results):
person_name = batch['person_name'][i]
output_path = os.path.join(args.output_dir, person_name)
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
if args.repaint:
person_path, mask_path = dataset.data[batch['index'][i]]['person'], dataset.data[batch['index'][i]]['mask']
person_image= Image.open(person_path).resize(result.size, Image.LANCZOS)
mask = Image.open(mask_path).resize(result.size, Image.NEAREST)
result = repaint(person_image, mask, result)
if args.concat_eval_results:
w, h = result.size
concated_result = Image.new('RGB', (w*3, h))
concated_result.paste(person_images[i], (0, 0))
concated_result.paste(cloth_images[i], (w, 0))
concated_result.paste(result, (w*2, 0))
result = concated_result
result.save(output_path)
if __name__ == "__main__":
main() |