forked from selinakhan/stylistic-MTL-ukiyoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
322 lines (226 loc) · 10.5 KB
/
test_models.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
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
import torch
import os
import argparse
import numpy as np
import json
import cv2
from tqdm import tqdm
from data_utils import DataProcessor
from model import MultiTaskHS, SingleTaskRegression, SingleTaskClassification
# from train import get_class_weights
from mtl_repo.utils.config import create_config
from mtl_repo.utils.common_config import get_train_dataset, get_transformations,\
get_val_dataset, get_train_dataloader, get_val_dataloader,\
get_optimizer, get_model, adjust_learning_rate,\
get_criterion
from mtl_repo.utils.logger import Logger
from mtl_repo.train.train_utils import train_vanilla
from mtl_repo.evaluation.evaluate_utils import eval_model, validate_results, save_model_predictions,\
eval_all_results
from termcolor import colored
def conf_date(save_path, label2, pred2):
f = open(f"{save_path}/2.txt", "a+")
for a, b in zip(list(label2), list(pred2)):
pair = (a.item(), int(b.item()))
f.write(f'{pair}\n')
def conf_class(save_path, task, label, pred):
f = open(f"{save_path}/{task}.txt", "a+")
for a, b in zip(list(label), list(pred)):
pair = (a.item(), b.item())
f.write(f'{pair}\n')
def test_STL(path, task, test_loader, im_dir, divisor, save_path, test_classes):
model = SingleTaskClassification(256, 512, test_classes[task], divisor, model_config='fr_vit')
model = model.to(DEVICE)
print('Model architecture dimensions correct')
d = torch.load(path, map_location=torch.device('cpu'))
d = {f'{k.replace("module.", "")}': v for k, v in d.items()}
model.load_state_dict(d)
print('Model successfully loaded')
total = 0
correct = 0
predictions, labels = [], []
for i, data in enumerate(tqdm(test_loader)):
# Retrieve all inputs
image, label1, _, label3 = data
# Send tensors to device
image = image.to(DEVICE)
# Retrieve correct label for specified task
if task == 0:
label = label1.long()
if task == 1:
label = label3.long()
with torch.no_grad():
out, _ = model(image)
out = out.cpu()
# Make prediction
pred = torch.argmax(out, dim=1)
conf_class(save_path, task, label, pred)
correct += (pred == label).sum().item()
total += label.size(0)
result = 100 * (correct / total)
print(result)
f = open(f"{save_path}/results.txt", "a+")
f.write(f'Accuracy: {result}')
return result
# test_STL('models/trained_models/STL_3/Skewed/model_3_classification_single_epoch_99_skewed_flowingsound.pt', 1, 'data/date_uniform/data_date_clean_train.csv', 'data/date_uniform/data_date_clean_test.csv', 'images/1', 256, 512, 4)
def test_STL_regression(path, test_loader, im_dir, save_path):
model = SingleTaskRegression(256, 512)
model = model.to(DEVICE)
print('Model architecture dimensions correct')
d = torch.load(path, map_location=torch.device(DEVICE))
d = {f'{k.replace("module.", "")}': v for k, v in d.items()}
model.load_state_dict(d)
print('Model successfully loaded')
total = 0
correct = 0
regression_threshold = 20
for i, data in enumerate(tqdm(test_loader)):
# Retrieve label
image, _, label2, _, _ = data
# Send tensors to device
image = image.to(DEVICE)
with torch.no_grad():
out, _ = model(image)
out = out.cpu()
pred = out.squeeze(1)
conf_date(save_path, label2, pred)
correct += (abs(pred - label2) < regression_threshold).sum().item()
total += label2.size(0)
result = 100 * (correct / total)
f = open(f"{save_path}/results.txt", "a+")
f.write(f'Accuracy: {result}')
return result
def test_MTL(path, test_loader, im_dir, save_path, train_classes):
model = MultiTaskHS(256, 512, train_classes[0], train_classes[1], train_classes[2])
model = model.to(DEVICE)
print('Model architecture dimensions correct')
d = torch.load(path, map_location=torch.device(DEVICE))
d = {f'{k.replace("module.", "")}': v for k, v in d.items()}
model.load_state_dict(d)
print('Model successfully loaded')
total = 0
correct1 = 0
correct2 = 0
correct3 = 0
regression_threshold = 20
for i, data in enumerate(tqdm(test_loader)):
# Retrieve labels
image, label1, label2, label3 = data
# Send tensors to device
image = image.to(DEVICE)
# Retrieve model outputs
with torch.no_grad():
out1, out2, out3, _ = model(image)
out1 = out1.cpu()
out2 = out2.cpu()
out3 = out3.cpu()
# Make prediction
pred1 = torch.argmax(out1.cpu().data, 1)
pred2 = out2.cpu().squeeze(1)
pred3 = torch.argmax(out3.cpu().data, 1)
conf_date(save_path, label2, pred2)
conf_class(save_path, 1, label1, pred1)
conf_class(save_path, 3, label3, pred3)
correct1 += (pred1 == label1).sum().item()
correct2 += (abs(pred2 - label2) < regression_threshold).sum().item()
correct3 += (pred3 == label3).sum().item()
# Every image has label 1
total += label1.size(0)
a1, a2, a3 = (100 * (correct1 / total)), (100 * (correct2 / total)), (100 * (correct3 / total))
acc = (a1 + a2 + a3) / 4
f = open(f"{save_path}/results.txt", "a+")
f.write(f'Accuracy on artist: {a1}\n Accuracy on date: {a2}\n Accuracy on era {a3}')
return a1, a2, a3, acc
def test_MTL_config(path, test_loader, im_dir, save_path, train_classes):
cv2.setNumThreads(0)
p = create_config('mtl_repo/configs/env.yml', 'mtl_repo/configs/nyud/resnet50/cross_stitch.yml')
print(colored(p, 'red'))
# Get model
print(colored('Retrieve model', 'blue'))
model = get_model(p)
model = model.to(DEVICE)
print('Model succesfully retrieved')
d = torch.load(path, map_location=torch.device(DEVICE))
d = {f'{k.replace("module.", "")}': v for k, v in d.items()}
model.load_state_dict(d)
print('Model successfully loaded')
total = 0
correct1 = 0
correct2 = 0
correct3 = 0
regression_threshold = 20
for i, data in enumerate(tqdm(test_loader)):
# Retrieve labels
image, label1, label2, label3 = data
label1 = label1.to(DEVICE)
label2 = label2.to(DEVICE)
label3 = label3.to(DEVICE)
# Send tensors to device
image = image.to(DEVICE)
# Retrieve model outputs
with torch.no_grad():
out1, out2, out3 = model(image)
# Make prediction
pred1 = torch.argmax(out1.data, 1)
pred2 = out2.squeeze(1)
pred3 = torch.argmax(out3.data, 1)
conf_date(save_path, label2, pred2)
conf_class(save_path, 1, label1, pred1)
conf_class(save_path, 3, label3, pred3)
correct1 += (pred1 == label1).sum().item()
correct2 += (abs(pred2 - label2) < regression_threshold).sum().item()
correct3 += (pred3 == label3).sum().item()
# Every image has label 1
total += label1.size(0)
a1, a2, a3 = (100 * (correct1 / total)), (100 * (correct2 / total)), (100 * (correct3 / total))
acc = (a1 + a2 + a3) / 4
f = open(f"{save_path}/results.txt", "a+")
f.write(f'Accuracy on artist: {a1}\n Accuracy on date: {a2}\n Accuracy on era {a3}')
return a1, a2, a3, acc
# test_MTL('model_multi_epoch_299_date_era.pt', 'data/era/era_nonzero_train.csv', 'data/era/era_nonzero_validate.csv', 'images/1', 256, 512, 'models/trained_models/MTL/era_date/')
if __name__ == '__main__':
# Parse arguments
parser = argparse.ArgumentParser(description='Test a single/multitask model on a specified dataset.')
parser.add_argument('--model', dest='model', required=True, help='Path to trained model')
parser.add_argument('--train_data', dest='train_file', required=True, help='Path to training dataset')
parser.add_argument('--val_data', dest='val_file', required=True, help='Path to validation dataset')
parser.add_argument('--image_dir', dest='imdir', required=True, help='Path to images directory')
parser.add_argument('--save_path', dest='save_path', required=False, help='Directory to store results')
parser.add_argument('--task', dest='task', required=True, help='Task to train')
parser.add_argument('--single_task', dest='single_task', type=int, required=False, help='Singletask model to train')
parser.add_argument('--batch_size', dest='batch_size', type=int, default=2, required=False, help='Size of training batch')
args = parser.parse_args()
if args.task not in ['single', 'multi', 'mtl_conf']:
raise ValueError(f'Invalid task specification {args.task}. Select from: single, multi')
if not os.path.isfile(args.train_file):
raise FileExistsError('Training dataset does not exist.')
if not os.path.isfile(args.val_file):
raise FileExistsError('Validation dataset does not exist.')
if not os.path.isfile(args.model):
raise FileExistsError('Model does not exist.')
# Process data accordingly
train_data = DataProcessor(args.train_file, 256, 'train', args.imdir)
train_classes = train_data.get_labels()
mapping = json.load(open('data/mapping.json', 'r'))
json.dump(mapping, open(f'{args.save_path}/map.json', 'w+'))
val_data = DataProcessor(args.val_file, 256, 'test', args.imdir, mappings=mapping)
val_classes = val_data.get_labels()
val_loader = torch.utils.data.DataLoader(val_data, batch_size=args.batch_size, shuffle=False, num_workers=4)
# Select device
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Running on: {DEVICE}')
#Train single task models
if args.task == 'single':
if args.single_task == 1:
test_STL(args.model, 0, val_loader, args.imdir, 2, args.save_path, train_classes)
if args.single_task == 2:
test_STL_regression(args.model, val_loader, args.imdir, args.save_path)
if args.single_task == 3:
test_STL(args.model, 1, val_loader, args.imdir, 4, args.save_path, train_classes)
if args.single_task == 4:
test_STL(args.model, 2, val_loader, args.imdir, 2, args.save_path, train_classes)
# Train multi task models
if args.task == 'multi':
test_MTL(args.model, val_loader, args.imdir, args.save_path, train_classes)
if args.task == 'mtl_conf':
test_MTL_config(args.model, val_loader, args.imdir, args.save_path, train_classes)