-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfer.py
148 lines (99 loc) · 4.21 KB
/
infer.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
import os
from weights_init.weight_init_normal import weights_init_normal
os.environ['CUDA_VISIBLE_DEVICES'] = '1,2,3'
devicess = [0,1,2]
import re
import time
import argparse
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torch import nn
import torch.distributed as dist
import math
import warnings
from tqdm import tqdm
from torch.optim.lr_scheduler import ReduceLROnPlateau,StepLR,MultiStepLR
from torchvision import utils
from hparams import hparams as hp
from torch.autograd import Variable
from torch_warmup_lr import WarmupLR
from optimizer.LookAhead import Lookahead
from optimizer.RAdam import RAdam
from optimizer.Ranger import Ranger
warnings.filterwarnings("ignore")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
np_str_obj_array_pattern = re.compile(r'[SaUO]')
face_pool = torch.nn.AdaptiveAvgPool2d((256, 256))
def parse_testing_args(parser):
"""
Parse commandline arguments.
"""
parser.add_argument('-o', '--output_dir', type=str, default=hp.output_dir, required=False, help='Directory to save results')
parser.add_argument('--latest-checkpoint-file', type=str, default=hp.latest_checkpoint_file, help='Use the latest checkpoint in each epoch')
testing = parser.add_argument_group('testing setup')
testing.add_argument('--batch', type=int, default=1, help='batch-size')
testing.add_argument('--cudnn-enabled', default=True, help='Enable cudnn')
testing.add_argument('--cudnn-benchmark', default=True, help='Run cudnn benchmark')
return parser
def test():
parser = argparse.ArgumentParser(description=hp.description)
parser = parse_testing_args(parser)
args, _ = parser.parse_known_args()
args = parser.parse_args()
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = args.cudnn_enabled
torch.backends.cudnn.benchmark = args.cudnn_benchmark
os.makedirs(args.output_dir, exist_ok=True)
from stylegan2.stylegan2_infer import infer_face
class_generate = infer_face(hp.weight_path_pytorch)
n_styles = 2*int(math.log(hp.img_size, 2))-2
if hp.backbone == 'GradualStyleEncoder':
from models.fpn_encoders import GradualStyleEncoder
model = GradualStyleEncoder(num_layers=50,n_styles=n_styles)
elif hp.backbone == 'ResNetGradualStyleEncoder':
from models.fpn_encoders import ResNetGradualStyleEncoder
model = ResNetGradualStyleEncoder(n_styles=n_styles)
else:
Exception('Backbone error!')
model = torch.nn.DataParallel(model, device_ids=devicess)
print(os.path.join(args.output_dir, args.latest_checkpoint_file))
ckpt = torch.load(os.path.join(args.output_dir, args.latest_checkpoint_file), map_location=lambda storage, loc: storage)
model.load_state_dict(ckpt["model"])
# model cuda
model.cuda()
from data_function import ImageData
test_dataset = ImageData(hp.dataset_path, hp.transform['transform_inference'])
test_loader = DataLoader(test_dataset,
batch_size=args.batch,
shuffle=False,
pin_memory=False,
drop_last=True)
model.eval()
for i, batch in enumerate(test_loader):
img = batch.cuda()
outputs = model(img)
predicts = class_generate.generate_from_synthesis(outputs,None,randomize_noise=False,return_latents=True)
if hp.resize:
predicts = face_pool(predicts)
if hp.dataset_type == 'car':
predicts = predicts[:, :, 32:224, :]
with torch.no_grad():
utils.save_image(
predicts,
os.path.join(args.output_dir,("step-{}-predict.png").format(i)),
nrow=hp.row,
normalize=hp.norm,
range=hp.rangee,
)
with torch.no_grad():
utils.save_image(
img,
os.path.join(args.output_dir,("step-{}-origin.png").format(i)),
nrow=hp.row,
normalize=hp.norm,
range=hp.rangee,
)
if __name__ == '__main__':
test()