-
Notifications
You must be signed in to change notification settings - Fork 1
/
track_images.py
179 lines (142 loc) · 6.07 KB
/
track_images.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import argparse
import numpy as np
import os
import torch
import utils
import models.curves as curves
import dataset
import models.autoencoders as autoencoders
from lpips_pytorch import LPIPS
parser = argparse.ArgumentParser(description='Connection evaluation')
parser.add_argument('--start', type=int, default=None,
help='number of first checkpoint')
parser.add_argument('--end', type=int, default=None,
help='number of second checkpoint')
parser.add_argument('--dir', type=str, default='./tmp/eval', metavar='DIR',
help='training directory (default: ./tmp/eval)')
parser.add_argument('--device', type=str, default='cuda:0',
choices=['cpu', f"cuda:{0}"], help='device for calculations')
parser.add_argument('--data_path', type=str, default='./data/', metavar='PATH',
help='path to datasets location (default: /data/)')
parser.add_argument('--batch_size', type=int, default=64, metavar='N',
help='input batch size (default: 64)')
parser.add_argument('--num_workers', type=int, default=2, metavar='N',
help='number of workers (default: 2)')
parser.add_argument('--curve', type=str, default='Bezier', metavar='CURVE',
help='curve type to use (default: None)')
parser.add_argument('--num_bends', type=int, default=3, metavar='N',
help='number of curve bends (default: 3)')
parser.add_argument('--ckpt', type=str, default=None, metavar='CKPT',
help='checkpoint to eval (default: None)')
parser.add_argument('--init_start', type=str, default=None, metavar='CKPT',
help='checkpoint to init start point (default: None)')
parser.add_argument('--init_end', type=str, default=None, metavar='CKPT',
help='checkpoint to init end point (default: None)')
parser.add_argument('--num_points', type=int, default=10, metavar='N',
help='number of points on the curve (default: 10)')
parser.add_argument('--lpips', dest='lpips', action='store_true',
help='flag to evaluate LPIPS on curve')
parser.add_argument('--latent_dim', type=int, default=128,
help='dimensionality of latent representation')
parser.add_argument('--conv_init', type=str, default='normal',
choices=['normal', 'kaiming_uniform', 'kaiming_normal'], help='weights init in conv layers')
args = parser.parse_args()
def get_weights(model):
return np.concatenate([p.data.cpu().numpy().ravel() for p in model.parameters()])
def evaluate(args):
os.makedirs(args.dir, exist_ok=True)
loaders = dataset.build_loader(
dataset.CelebADataset,
args.data_path,
args.batch_size,
args.num_workers
)
kwargs = {
'init_num_filters': 64,
'lrelu_slope': 0.2,
'embedding_dim': args.latent_dim,
'conv_init': args.conv_init,
'nc': 3,
'dropout': 0.05
}
print(f"Init CurveNet")
curve = getattr(curves, args.curve)
model_curve = curves.CurveNet(
curve,
autoencoders.CelebaAutoencoderCurve,
args.num_bends,
architecture_kwargs=kwargs,
)
model_curve.to(args.device)
model_curve.eval()
checkpoint = torch.load(args.ckpt)
model_curve.load_state_dict(checkpoint['model_state'])
print(f"Init AE")
model = autoencoders.CelebaAutoencoder(**kwargs)
model.to(args.device)
model.load_state_dict(torch.load(args.init_start)['model_state'])
w_1 = get_weights(model)
model.load_state_dict(torch.load(args.init_end)['model_state'])
w_2 = get_weights(model)
T = args.num_points
ts = np.linspace(0.0, 1.0, T)
eval_images = next(iter(loaders['test']))
eval_images = eval_images[:4].to(args.device)
images_dynamics_curve = []
images_dynamics_seg = []
lpips_stat_curve = np.zeros(T)
lpips_stat_seg = np.zeros(T)
t = torch.FloatTensor([0.0]).to(args.device)
print(f"Init LPIPS scorer")
if args.lpips:
scorer = LPIPS().to(args.device)
for i, t_value in enumerate(ts):
print(f"t: {t_value}")
kwargs_curve = dict()
w = (1.0 - t_value) * w_1 + t_value * w_2
offset = 0
t.data.fill_(t_value)
for parameter in model.parameters():
size = np.prod(parameter.size())
value = w[offset:offset+size].reshape(parameter.size())
parameter.data.copy_(torch.from_numpy(value))
offset += size
kwargs_curve['t'] = t
utils.update_bn(loaders['train'], model_curve, args.device, **kwargs_curve)
with torch.no_grad():
img_rec = model(eval_images)
img_curve = model_curve(eval_images, **kwargs_curve)
images_dynamics_curve.append(img_curve.detach().cpu().numpy())
images_dynamics_seg.append(img_rec.detach().cpu().numpy())
if args.lpips:
lpips = scorer(img_curve, eval_images).squeeze().item() / img_curve.size(0)
lpips_stat_curve[i] = lpips
lpips = scorer(img_rec, eval_images).squeeze().item() / img_rec.size(0)
lpips_stat_seg[i] = lpips
if args.lpips:
lpips_stat_curve = np.array(lpips_stat_curve)
filepath = os.path.join(args.dir, f'curve_lpipses{args.start}{args.end}.npz')
np.savez(
filepath,
lpips=lpips_stat_curve
)
lpips_stat_seg = np.array(lpips_stat_seg)
filepath = os.path.join(args.dir, f'seg_lpipses{args.start}{args.end}.npz')
np.savez(
filepath,
lpips=lpips_stat_seg
)
images_dynamics_curve = np.array(images_dynamics_curve)
filepath = os.path.join(args.dir, f'curve_images{args.start}{args.end}.npz')
np.savez(
filepath,
images_dynamics=images_dynamics_curve
)
images_dynamics_seg = np.array(images_dynamics_seg)
filepath = os.path.join(args.dir, f'seg_images{args.start}{args.end}.npz')
np.savez(
filepath,
images_dynamics=images_dynamics_seg
)
if __name__ == '__main__':
evaluate(args)