-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
194 lines (163 loc) · 6.42 KB
/
test.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from torch.serialization import save
import numpy as np
import torch
import json
import matplotlib.pyplot as plt
import networks.network_2d as nt_2d
import networks.network_3d as nt_3d
import utils.data_utils as dt
import utils.network_utils as nt
import utils.losses as ls
from torch import optim
from argparse import ArgumentParser
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
import os.path as osp
def redundancy_removal(model, model_type, loader, config, device, num_out):
'''
Identifies the points with less threshold and get rid of them
'''
if model_type == '3d':
img_l = config['image_length']
num_land = config['num_landmarks']
input_channels = config['input_channels']
model.eval()
model = model.float()
red_metric = np.zeros([num_land, 200])
count = 0
for x in loader:
x = x.to(device)
# base loss
outImg, landT, landS, A = model(x.float(), 0, False, 0, 0)
[imgS, imgT] = x.split([input_channels, input_channels], 1)
loss, rel_loss, denon = ls.l2_loss(imgT.float(), outImg, device)
landS = landS.cpu().detach().numpy()
landT = landT.cpu().detach().numpy()
# Now compute the things for different landmarks and stuff
for i in range(num_land):
idx = np.arange(num_land)
idx = np.delete(idx, i)
newlandT = landT[:, idx, :]
newlandS = landS[:, idx, :]
# now run the TPS warp with this parameters
[imgS, imgT] = x.split([input_channels, input_channels], 1)
outImg, t, s, A = model(x.float(), 0, True, torch.from_numpy(newlandS).to(device), torch.from_numpy(newlandT).to(device))
loss_i, rel_loss_i, denon = ls.l2_loss(imgT.float(), outImg, device)
temp = (loss - loss_i)**2
red_metric[i, count] = temp.detach().cpu().numpy()
t = t.detach().cpu().numpy()
s = s.detach().cpu().numpy()
A = A.detach().cpu().numpy()
imgS = imgS.detach().cpu().numpy()
imgT = imgT.detach().cpu().numpy()
count += 1
if count > 199:
break
# red_metric = red_metric/count
# perform thresholding
red_metric = red_metric.mean(1)
red_ids = np.flip(np.argsort(red_metric))
return red_ids[:num_out]
def test(model, model_type, loader, config, device, red_metric=None):
'''
Testing
'''
save_dir = config['save_dir']
img_h = config['image_height']
img_w = config['image_width']
if model_type == '3d':
img_l = config['image_length']
num_land = config['num_landmarks']
input_channels = config['input_channels']
model.eval()
model = model.float()
count = 0
for x in loader:
x = x.to(device)
# the loader is constructed with batch size of 1 ==> to travel through
# all the images in the dataset we need to get the source image
# for the first size.loader times and then break the process.
outImg, landT, landS, A = model(x.float(), 0, False, 0, 0)
[imgS, imgT] = x.split([input_channels, input_channels], 1)
if red_metric is not None:
prj = red_metric
else:
prj = np.arange(num_land)
parent_dir = save_dir + '/Test/'
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
if model_type == '2d':
imgS = imgS[0, ...].reshape(1, input_channels, img_h, img_w)
imgT = imgT[0, ...].reshape(1, input_channels, img_h, img_w)
imgS = imgS.permute(0, 2, 3 ,1)
imgS = imgS.squeeze().cpu().detach().numpy()
imgT = imgT.permute(0, 2, 3 ,1)
imgT = imgT.squeeze().cpu().detach().numpy()
plt.imsave(parent_dir + 'imgS' + str(count) + '.png', imgS.astype(np.uint8))
plt.imsave(parent_dir + 'imgT' + str(count) + '.png', imgT.astype(np.uint8))
outImg = outImg[0, ...].reshape(1, input_channels, img_h, img_w)
finalOut = outImg.permute(0, 2, 3, 1)
finalOut = finalOut.squeeze().cpu().detach().numpy()
plt.imsave(parent_dir + 'regImg' + str(count) + '.png', finalOut.astype(np.uint8))
outPoints = landS.cpu().detach().numpy()
outPoints = outPoints[0, ...]
outPoints = outPoints[prj, :]
outPoints[:, 0] = (outPoints[:, 0]+1)*0.5*(img_w - 1)
outPoints[:, 1] = (outPoints[:, 1]+1)*0.5*(img_h - 1)
np.save(parent_dir + 'landS' + str(count) + '.npy', outPoints)
fig, ax = plt.subplots()
plt.imshow(imgS.astype(np.uint8))
for i in range(outPoints.shape[0]):
ax.scatter(outPoints[i, 0], outPoints[i, 1], c='c', edgecolors='k')
ax.text(outPoints[i, 0]+0.3, outPoints[i, 1]+0.3, str(prj[i]), fontsize=9, c='m')
plt.savefig(parent_dir + 'overlayImg_source' + str(count) + '.png')
plt.clf()
fig, axis = plt.subplots(figsize=(5, 5))
axis.imshow(imgT.astype(np.uint8), cmap='Reds', alpha=0.6)
axis.imshow(finalOut.astype(np.uint8), cmap='Blues', alpha=0.6)
axis.set_title('Images overlayed')
plt.savefig(parent_dir + 'regAcc' + str(count) + '.png')
plt.clf()
if count < len(loader):
count += 1
else:
break
def runFunction(args):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config = json.load(open(args.config_file))
# create the save directory
save_dir = config['save_dir']
if not osp.exists(save_dir):
os.makedirs(save_dir)
# TODO: add an option to print to a log file
print("///////////////////////////////////////")
print("Defining Model")
print("///////////////////////////////////////")
model_type = args.model
if model_type == "2d":
model = nt_2d.self_supervised_model_2d(config, device).to(device)
else:
model = nt_3d.self_supervised_model_3d(config, device).to(device)
# get the loader
data_dir = config['data_dir']
save_dir = config['save_dir']
if args.use_best:
model_path = save_dir + '/best_model.pt'
else:
model_path = save_dir + '/final_model.pt'
model = torch.load(model_path).to(device)
loader = dt.get_dataset(model_type, data_dir, 1, file_type='npy', data_type='test', noise=False)
if args.redu_remove:
red_metric = redundancy_removal(model, model_type, loader, config, device, args.num_out)
test(model, model_type, loader, config, device, red_metric=red_metric)
else:
test(model, model_type, loader, config, device)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--model", type=str, help="2d or 3d")
parser.add_argument("--redu_remove", type=bool, default=False, help="True or False, basically means that the testing is to be does using the reduced points or the same.")
parser.add_argument("--num_out", type=int, default=10, help="number of points to be retained")
parser.add_argument("--use_best", type=bool, default=True, help="True or false, if false it uses the final model")
parser.add_argument("--config_file", type=str, help="configFile for the parameters")
args = parser.parse_args()
runFunction(args)