-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
280 lines (244 loc) · 12 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project : FG-NIC
# @Author : Xiaoyu LIN
# @File : test.py
# @Description : This file is used to test proposed and baseline methods.
from typing import Optional
from torch.nn import Module
import torch
import torch.nn as nn
import time
import os
import copy
import numpy as np
import random
from tqdm import tqdm
import pickle
from collections import defaultdict
from degradation import get_degradation, Normalize
from util import prepare_parser, set_cwd, get_level, set_parameter_requires_grad, prepare_ablation
from classification import initialize_classification, split_classification
from restoration import initialize_restoration
from ptflops import get_model_complexity_info
from DeepCorrect import DeepCorrect
from baseline.WaveCNet.wavecnet import initialize_wavecnet
def test(model: Module,
task: str,
device: torch.device,
dataloader: torch.utils.data.DataLoader,
mode: str = None,
num_round: int = 5,
restoration: Optional[Callable] = None,
MEAN: list = [0.485, 0.456, 0.406],
STD: list = [0.229, 0.224, 0.225]
) -> np.ndarray:
""" Test fine tuned classifier on test dataset with various degradation.
Args:
classifier (Module): Classifier model to test.
device (torch.device): Device where classifier model located.
datalodaer (torch.utils.data.DataLoader): Dataloader for test data.
round_num (int): Total number of round for testing, since degradation is introduced on data,
reture average results.
restoration (object): Restoration network applied on the degraded image before sending
it into classifier.
Return:
acc_numpy (ndarray): Array of history accuary among all rounds.
"""
since = time.time()
accuracy = []
# Test for each round
for rounds in range(num_round):
running_accuracy = 0
# Iterate over data.
for inputs, origins, labels in tqdm(dataloader, ncols=70, leave=False, unit='b',
desc='Testing round {}/{}'.format(rounds+1, num_round)):
inputs = inputs.to(device)
labels = labels.to(device)
# restoration
inputs = restoration(inputs).clamp_(0.0, 1.0) if restoration is not None else inputs
inputs = Normalize(MEAN, STD)(inputs) if task.lower() in ('classification', 'deepcorrect', 'wavecnet') else inputs
# forward
if mode and 'oracle' in mode.lower():
outputs = model(inputs, origins)
else:
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
# statistics
running_accuracy += torch.sum(preds == labels.data)
current_accuracy = running_accuracy.double() / len(dataloader.dataset)
accuracy.append(current_accuracy)
print('Round {} Accuracy: {:.6f}'.format(rounds + 1, current_accuracy))
accuracy = np.array([i.cpu().detach().numpy() for i in accuracy])
time_elapsed = time.time() - since
print('Testing complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
with open('last_test_batch.pickle', 'wb') as file:
pickle.dump(inputs, file)
return accuracy
if __name__ == '__main__':
args = prepare_parser()
level = get_level(args.level, args.level_min)
# Set GPU
os.environ['CUDA_VISIBLE_DEVICES'] = args.dev
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# For reproduction
random.seed(args.SEED)
np.random.seed(args.SEED)
torch.manual_seed(args.SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set current working directory
project_dir = os.getcwd()
set_cwd(args, 'test')
# Check model
if not os.path.isfile('model.pth'):
print("No trained directory exit. Please fine tune model first!")
exit()
# Record logs
logs = open('test-logs.txt', mode='a')
# Initialize and load the classification network
print('Preparing classification network {} ..... '.format(args.classification), end='')
classification, input_size = initialize_classification(args.classification, args.num_classes, use_pretrained=False)
if args.task.lower() in ('model', 'deepcorrect'):
classification.load_state_dict(torch.load(os.path.join(project_dir, args.CLASSIFIER_DIR,
'-'.join([args.dataset.lower(), args.classification, 'clean']),
'model.pth')))
feature_extractor, classifier, feature_size = split_classification(args.classification, classification)
feature_extractor.eval()
set_parameter_requires_grad(feature_extractor, False)
classifier.eval()
set_parameter_requires_grad(classifier, False)
classification = None
elif args.task.lower() in 'wavecnet':
model = initialize_wavecnet(classification=args.classification,
num_classes=args.num_classes,
wavename='haar',
pretrained=False)
model.load_state_dict(torch.load('model.pth'))
set_parameter_requires_grad(model, False)
model.eval()
else:
classification.load_state_dict(torch.load('model.pth'))
set_parameter_requires_grad(classification, False)
classification.eval()
model = classification
print('Done!')
# Prepare test datasets and pre-processing.
print('Preparing Datasets and Dataloaders.....', end='')
data_transforms, level = get_degradation(degradation_type=args.degradation,
level=level,
vary=args.vary,
phase = 'test',
)
# datasets
if 'caltech256' in args.dataset.lower():
from data import Caltech256 as Dataset
elif 'caltech101' in args.dataset.lower():
from data import Caltech101 as Dataset
test_image_dataset = Dataset(root=os.path.join(project_dir, args.DATA_DIR, args.dataset.lower()), phase='test', transform=data_transforms)
# Create training and validation dataloaders
test_dataloader = torch.utils.data.DataLoader(test_image_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=8
)
print('Done!')
print('Degradation type: {}; Degradation level {} varying {}'.format(args.degradation, str(level), args.vary))
logs.write('Degradation type: {}; Degradation level {} varying {}. '.format(args.degradation, str(level), args.vary) + '\n')
# Initialize and load the restoration network
restoration = None
if args.restoration is not None:
print('Preparing restoration network {} ...... '.format(args.restoration) , end='')
logs.write('Restoration model: {}.\n'.format(args.restoration))
restoration = initialize_restoration(name=args.restoration,
dataset=args.dataset.lower(),
path=os.path.join(project_dir, args.RESTORATION_DIR),
use_pretrain=True
)
restoration.eval()
set_parameter_requires_grad(restoration, False)
print('Done!')
# Load fidelity map
if 'model' in args.task.lower():
if not args.ablation:
from model import Model
else:
Model = prepare_ablation(args.ablation)
print('Preparing fidelity map estimator ...... ', end='')
fidelity = None
if args.fidelity_input is not None and 'oracle' not in args.mode.lower():
fidelity = initialize_restoration(name='dncnn', dataset=args.dataset.lower(), path=args.RESTORATION_DIR, use_pretrain=False)
fidelity.load_state_dict(torch.load(os.path.join(project_dir, args.FIDELITY_DIR, \
'-'.join([args.dataset.lower(), args.fidelity_input, args.fidelity_output, args.restoration]), 'model.pth')))
fidelity.eval()
set_parameter_requires_grad(fidelity, False)
# Load our model
model = Model(mode=args.mode,
restoration=copy.deepcopy(restoration),
fidelity_input=args.fidelity_input,
fidelity_output=args.fidelity_output,
feature_extractor=feature_extractor,
feature_size=feature_size,
classifier=classifier,
downsample=args.downsample,
fidelity=copy.deepcopy(fidelity),
increase=args.increase,
num_channel=args.num_channel,
MEAN=args.MEAN,
STD=args.STD
)
restoration = None
fidelity = None
model.load_state_dict(torch.load('model.pth'))
model.eval()
set_parameter_requires_grad(model, False)
model.is_ensemble=args.is_ensemble
print('Done!')
print('Using ensemble') if args.is_ensemble else print('Using single model')
logs.write('Ensemble\n') if args.is_ensemble else logs.write('Single model\n')
if 'deepcorrect' in args.task.lower():
model = DeepCorrect(feature_extractor=feature_extractor,
classifier=classifier,
MEAN=args.MEAN,
STD=args.STD
)
model.load_state_dict(torch.load('model.pth'))
# Send the networks to GPU
if torch.cuda.is_available() and torch.cuda.device_count() > 1:
print("Using", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
model = model.to(device)
if restoration is not None:
if torch.cuda.is_available() and torch.cuda.device_count() > 1:
restoration = nn.DataParallel(restoration)
restoration = restoration.to(device)
# Train and evaluate
num_round = 1 if isinstance(level,(int, float)) and level == 0 else args.num_round
accuracy = test(model=model,
task=args.task,
mode=args.mode,
device=device,
dataloader=test_dataloader,
num_round=num_round,
restoration=restoration,
MEAN=args.MEAN,
STD=args.STD
)
print("Average Accuracy: {:.6f}; Std: {:.6f}".format(accuracy.mean().item(), accuracy.std().item()))
logs.write("Average Accuracy: {:.6f}; Std: {:.6f}\n".format(accuracy.mean().item(), accuracy.std().item()))
# Save data
try:
results = pickle.load(open('accuracy.pickle', 'rb'))
except FileNotFoundError:
results = defaultdict(dict)
with open('accuracy.pickle', 'wb') as file:
if args.vary.lower() in ('1d', '2d'):
level = (level, args.vary)
setup = args.degradation + '-' + args.restoration if args.restoration else args.degradation
if args.task == 'model':
setup = setup + '-ensemble' if args.is_ensemble else setup + '-single'
results[setup][level] = accuracy
pickle.dump(results, file)
logs.write('Done.\n')
logs.close()
print('Done.')