-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_osediff.py
136 lines (115 loc) · 5.55 KB
/
test_osediff.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
import os
import sys
sys.path.append(os.getcwd())
import glob
import argparse
import torch
from torchvision import transforms
import torchvision.transforms.functional as F
import numpy as np
from PIL import Image
from osediff import OSEDiff_test
from my_utils.wavelet_color_fix import adain_color_fix, wavelet_color_fix
from ram.models.ram_lora import ram
from ram import inference_ram as inference
tensor_transforms = transforms.Compose([
transforms.ToTensor(),
])
ram_transforms = transforms.Compose([
transforms.Resize((384, 384)),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
def get_validation_prompt(args, image, model, device='cuda'):
validation_prompt = ""
lq = tensor_transforms(image).unsqueeze(0).to(device)
lq_ram = ram_transforms(lq).to(dtype=weight_dtype)
captions = inference(lq_ram, model)
validation_prompt = f"{captions[0]}, {args.prompt},"
return validation_prompt, lq
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input_image', '-i', type=str, default='preset/datasets/test_dataset/input', help='path to the input image')
parser.add_argument('--output_dir', '-o', type=str, default='preset/datasets/test_dataset/output', help='the directory to save the output')
parser.add_argument('--pretrained_model_name_or_path', type=str, default=None, help='sd model path')
parser.add_argument('--seed', type=int, default=42, help='Random seed to be used')
parser.add_argument("--process_size", type=int, default=512)
parser.add_argument("--upscale", type=int, default=4)
parser.add_argument("--align_method", type=str, choices=['wavelet', 'adain', 'nofix'], default='adain')
parser.add_argument("--osediff_path", type=str, default='preset/models/osediff.pkl')
parser.add_argument('--prompt', type=str, default='', help='user prompts')
parser.add_argument('--ram_path', type=str, default=None)
parser.add_argument('--ram_ft_path', type=str, default=None)
parser.add_argument('--save_prompts', type=bool, default=True)
# precision setting
parser.add_argument("--mixed_precision", type=str, choices=['fp16', 'fp32'], default="fp16")
# merge lora
parser.add_argument("--merge_and_unload_lora", default=False) # merge lora weights before inference
# tile setting
parser.add_argument("--vae_decoder_tiled_size", type=int, default=224)
parser.add_argument("--vae_encoder_tiled_size", type=int, default=1024)
parser.add_argument("--latent_tiled_size", type=int, default=96)
parser.add_argument("--latent_tiled_overlap", type=int, default=32)
args = parser.parse_args()
# initialize the model
model = OSEDiff_test(args)
# get all input images
if os.path.isdir(args.input_image):
image_names = sorted(glob.glob(f'{args.input_image}/*.png'))
else:
image_names = [args.input_image]
# get ram model
DAPE = ram(pretrained=args.ram_path,
pretrained_condition=args.ram_ft_path,
image_size=384,
vit='swin_l')
DAPE.eval()
DAPE.to("cuda")
# weight type
weight_dtype = torch.float32
if args.mixed_precision == "fp16":
weight_dtype = torch.float16
# set weight type
DAPE = DAPE.to(dtype=weight_dtype)
if args.save_prompts:
txt_path = os.path.join(args.output_dir, 'txt')
os.makedirs(txt_path, exist_ok=True)
# make the output dir
os.makedirs(args.output_dir, exist_ok=True)
print(f'There are {len(image_names)} images.')
for image_name in image_names:
# make sure that the input image is a multiple of 8
input_image = Image.open(image_name).convert('RGB')
ori_width, ori_height = input_image.size
rscale = args.upscale
resize_flag = False
if ori_width < args.process_size//rscale or ori_height < args.process_size//rscale:
scale = (args.process_size//rscale)/min(ori_width, ori_height)
input_image = input_image.resize((int(scale*ori_width), int(scale*ori_height)))
resize_flag = True
input_image = input_image.resize((input_image.size[0]*rscale, input_image.size[1]*rscale))
new_width = input_image.width - input_image.width % 8
new_height = input_image.height - input_image.height % 8
input_image = input_image.resize((new_width, new_height), Image.LANCZOS)
bname = os.path.basename(image_name)
# get caption
validation_prompt, lq = get_validation_prompt(args, input_image, DAPE)
if args.save_prompts:
txt_save_path = f"{txt_path}/{bname.split('.')[0]}.txt"
with open(txt_save_path, 'w', encoding='utf-8') as f:
f.write(validation_prompt)
f.close()
print(f"process {image_name}, tag: {validation_prompt}".encode('utf-8'))
# translate the image
with torch.no_grad():
lq = lq*2-1
output_image = model(lq, prompt=validation_prompt)
output_pil = transforms.ToPILImage()(output_image[0].cpu() * 0.5 + 0.5)
if args.align_method == 'adain':
output_pil = adain_color_fix(target=output_pil, source=input_image)
elif args.align_method == 'wavelet':
output_pil = wavelet_color_fix(target=output_pil, source=input_image)
else:
pass
if resize_flag:
output_pil.resize((int(args.upscale*ori_width), int(args.upscale*ori_height)))
output_pil.save(os.path.join(args.output_dir, bname))