-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.py
336 lines (277 loc) · 13.5 KB
/
main.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""
PyTorch 1.3 implementation of the following paper:
Kang, Le, et al. "Simultaneous estimation of image quality and distortion via multi-task convolutional neural networks."
IEEE International Conference on Image Processing IEEE, 2015:2791-2795.
Usage:
Start tensorboard:
```bash
tensorboard --logdir=tensorboard_logs --port=6006
```
Run the main.py:
```bash
CUDA_VISIBLE_DEVICES=0 python main.py --exp_id=0
```
Implemented by Dingquan Li
Email: dingquanli@pku.edu.cn
Date: 2019/11/8
"""
from argparse import ArgumentParser
import torch
from torch.utils.data import DataLoader
from torch import nn
import torch.nn.functional as F
from torch.optim import Adam
from IQADataset import IQADataset
import numpy as np
import random
from scipy import stats
import os
import yaml
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
from ignite.metrics.metric import Metric
from tensorboardX import SummaryWriter
import datetime
def ensure_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def loss_fn(y_pred, y):
return config['alpha_q'] * F.l1_loss(y_pred[0], y[0]) + \
config['alpha_d'] * F.cross_entropy(y_pred[1], y[2].long().squeeze(1))
class IQAPerformance(Metric):
"""
Evaluation of IQA methods using SROCC, KROCC, PLCC, RMSE, MAE, OR.
`update` must receive output of the form (y_pred, y).
"""
def reset(self):
self._y_pred = []
self._y = []
self._y_std = []
def update(self, output):
pred, y = output
self._y.append(y[0].item())
self._y_std.append(y[1].item())
self._y_pred.append(torch.mean(pred[0]).item())
def compute(self):
sq = np.reshape(np.asarray(self._y), (-1,))
sq_std = np.reshape(np.asarray(self._y_std), (-1,))
q = np.reshape(np.asarray(self._y_pred), (-1,))
srocc = stats.spearmanr(sq, q)[0]
krocc = stats.stats.kendalltau(sq, q)[0]
plcc = stats.pearsonr(sq, q)[0]
rmse = np.sqrt(((sq - q) ** 2).mean())
mae = np.abs((sq - q)).mean()
outlier_ratio = (np.abs(sq - q) > 2 * sq_std).mean()
return srocc, krocc, plcc, rmse, mae, outlier_ratio
class IDCPerformance(Metric):
"""
Accuracy of image distortion classification.
`update` must receive output of the form (y_pred, y).
"""
def reset(self):
self._d_pred = []
self._d = []
def update(self, output):
pred, y = output
self._d.append(y[2].item())
self._d_pred.append(torch.max(torch.mean(pred[1], 0), 0)[1].item())
def compute(self):
acc = np.mean([self._d[i] == float(self._d_pred[i]) for i in range(len(self._d))])
return acc
class CNNIQAplusnet(nn.Module):
def __init__(self, n_classes, ker_size=7, n_kers=50, n1_nodes=800, n2_nodes=800):
super(CNNIQAplusnet, self).__init__()
self.conv1 = nn.Conv2d(1, n_kers, ker_size)
self.fc1 = nn.Linear(2 * n_kers, n1_nodes)
self.fc2 = nn.Linear(n1_nodes, n2_nodes)
self.fc3_q = nn.Linear(n2_nodes, 1)
self.fc3_d = nn.Linear(n2_nodes, n_classes)
self.dropout = nn.Dropout()
def forward(self, x):
x = x.view(-1, x.size(-3), x.size(-2), x.size(-1)) #
h = self.conv1(x)
h1 = F.max_pool2d(h, (h.size(-2), h.size(-1)))
h2 = -F.max_pool2d(-h, (h.size(-2), h.size(-1)))
h = torch.cat((h1, h2), 1) #
h = h.squeeze(3).squeeze(2)
h = F.relu(self.fc1(h))
h = self.dropout(h)
h = F.relu(self.fc2(h))
q = self.fc3_q(h)
d = self.fc3_d(h)
return q, d
class CNNIQAplusplusnet(nn.Module):
def __init__(self, n_distortions, ker_size=3, n1_kers=8, pool_size=2, n2_kers=32, n1_nodes=128, n2_nodes=512):
super(CNNIQAplusplusnet, self).__init__()
self.conv1 = nn.Conv2d(1, n1_kers, ker_size)
self.pool1 = nn.MaxPool2d(pool_size)
self.conv2 = nn.Conv2d(n1_kers, n2_kers, ker_size)
self.fc1 = nn.Linear(2 * n2_kers, n1_nodes)
self.fc2 = nn.Linear(n1_nodes, n2_nodes)
self.fc3_q = nn.Linear(n2_nodes, 1)
self.fc3_d = nn.Linear(n2_nodes, n_distortions)
self.dropout = nn.Dropout()
def forward(self, x):
x = x.view(-1, x.size(-3), x.size(-2), x.size(-1)) #
h = self.conv1(x)
h = self.pool1(h)
h = self.conv2(h)
h1 = F.max_pool2d(h, (h.size(-2), h.size(-1)))
h2 = -F.max_pool2d(-h, (h.size(-2), h.size(-1)))
h = torch.cat((h1, h2), 1) #
h = h.squeeze(3).squeeze(2)
h = F.relu(self.fc1(h))
h = self.dropout(h)
h = F.relu(self.fc2(h))
q = self.fc3_q(h)
d = self.fc3_d(h)
return q, d
def get_data_loaders(config, train_batch_size, exp_id=0):
train_dataset = IQADataset(config, exp_id, 'train')
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size=train_batch_size,
shuffle=True,
num_workers=4)
val_dataset = IQADataset(config, exp_id, 'val')
val_loader = torch.utils.data.DataLoader(val_dataset)
if config['test_ratio']:
test_dataset = IQADataset(config, exp_id, 'test')
test_loader = torch.utils.data.DataLoader(test_dataset)
return train_loader, val_loader, test_loader
return train_loader, val_loader
def run(train_batch_size, epochs, lr, weight_decay, model_name, config, exp_id, log_dir, trained_model_file, save_result_file, disable_gpu=False):
if config['test_ratio']:
train_loader, val_loader, test_loader = get_data_loaders(config, train_batch_size, exp_id)
else:
train_loader, val_loader = get_data_loaders(config, train_batch_size, exp_id)
device = torch.device("cuda" if not disable_gpu and torch.cuda.is_available() else "cpu")
if model_name == 'CNNIQAplus' or model_name == 'CNNIQA':
model = CNNIQAplusnet(n_distortions=config['n_distortions'],
ker_size=config['kernel_size'],
n_kers=config['n_kernels'],
n1_nodes=config['n1_nodes'],
n2_nodes=config['n2_nodes'])
else:
model = CNNIQAplusplusnet(n_distortions=config['n_distortions'],
ker_size=config['kernel_size'],
n1_kers=config['n1_kernels'],
pool_size=config['pool_size'],
n2_kers=config['n2_kernels'],
n1_nodes=config['n1_nodes'],
n2_nodes=config['n2_nodes'])
writer = SummaryWriter(log_dir=log_dir)
model = model.to(device)
print(model)
# if multi_gpu and torch.cuda.device_count() > 1:
# model = nn.DataParallel(model)
optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
global best_criterion
best_criterion = -1 # SROCC>=-1
trainer = create_supervised_trainer(model, optimizer, loss_fn, device=device)
evaluator = create_supervised_evaluator(model,
metrics={'IQA_performance': IQAPerformance(),
'IDC_performance': IDCPerformance()},
device=device)
@trainer.on(Events.ITERATION_COMPLETED)
def log_training_loss(engine):
writer.add_scalar("training/loss", engine.state.output, engine.state.iteration)
@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results(engine):
evaluator.run(val_loader)
metrics = evaluator.state.metrics
SROCC, KROCC, PLCC, RMSE, MAE, OR = metrics['IQA_performance']
Acc = metrics['IDC_performance']
print("Validation Results - Epoch: {} Acc: {:.2f}% SROCC: {:.4f} KROCC: {:.4f} PLCC: {:.4f} RMSE: {:.4f} MAE: {:.4f} OR: {:.2f}%"
.format(engine.state.epoch, 100 * Acc, SROCC, KROCC, PLCC, RMSE, MAE, 100 * OR))
writer.add_scalar("validation/SROCC", SROCC, engine.state.epoch)
writer.add_scalar("validation/KROCC", KROCC, engine.state.epoch)
writer.add_scalar("validation/PLCC", PLCC, engine.state.epoch)
writer.add_scalar("validation/RMSE", RMSE, engine.state.epoch)
writer.add_scalar("validation/MAE", MAE, engine.state.epoch)
writer.add_scalar("validation/OR", OR, engine.state.epoch)
writer.add_scalar("validation/Acc", Acc, engine.state.epoch)
global best_criterion
global best_epoch
if SROCC > best_criterion:
best_criterion = SROCC
best_epoch = engine.state.epoch
torch.save(model.state_dict(), trained_model_file)
@trainer.on(Events.EPOCH_COMPLETED)
def log_testing_results(engine):
if config["test_ratio"] > 0 and config['test_during_training']:
evaluator.run(test_loader)
metrics = evaluator.state.metrics
SROCC, KROCC, PLCC, RMSE, MAE, OR = metrics['IQA_performance']
Acc = metrics['IDC_performance']
print("Testing Results - Epoch: {} Acc: {:.2f}% SROCC: {:.4f} KROCC: {:.4f} PLCC: {:.4f} RMSE: {:.4f} MAE: {:.4f} OR: {:.2f}%"
.format(engine.state.epoch, 100 * Acc, SROCC, KROCC, PLCC, RMSE, MAE, 100 * OR))
writer.add_scalar("testing/SROCC", SROCC, engine.state.epoch)
writer.add_scalar("testing/KROCC", KROCC, engine.state.epoch)
writer.add_scalar("testing/PLCC", PLCC, engine.state.epoch)
writer.add_scalar("testing/RMSE", RMSE, engine.state.epoch)
writer.add_scalar("testing/MAE", MAE, engine.state.epoch)
writer.add_scalar("testing/OR", OR, engine.state.epoch)
writer.add_scalar("testing/Acc", Acc, engine.state.epoch)
@trainer.on(Events.COMPLETED)
def final_testing_results(engine):
if config["test_ratio"] > 0:
model.load_state_dict(torch.load(trained_model_file))
evaluator.run(test_loader)
metrics = evaluator.state.metrics
SROCC, KROCC, PLCC, RMSE, MAE, OR = metrics['IQA_performance']
Acc = metrics['IDC_performance']
global best_epoch
print("Final Test Results - Epoch: {} Acc: {:.2f}% SROCC: {:.4f} KROCC: {:.4f} PLCC: {:.4f} RMSE: {:.4f} MAE: {:.4f} OR: {:.2f}%"
.format(best_epoch, 100 * Acc, SROCC, KROCC, PLCC, RMSE, MAE, 100 * OR))
np.save(save_result_file, (Acc, SROCC, KROCC, PLCC, RMSE, MAE, OR))
# kick everything off
trainer.run(train_loader, max_epochs=epochs)
writer.close()
if __name__ == "__main__":
parser = ArgumentParser(description='PyTorch CNNIQA')
parser.add_argument("--seed", type=int, default=19920517)
parser.add_argument('--batch_size', type=int, default=128,
help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=500,
help='number of epochs to train (default: 500)')
parser.add_argument('--lr', type=float, default=0.0001,
help='learning rate (default: 0.0001)')
parser.add_argument('--weight_decay', type=float, default=0.0,
help='weight decay (default: 0.0)')
parser.add_argument('--config', default='config.yaml', type=str,
help='config file path (default: config.yaml)')
parser.add_argument('--exp_id', default='0', type=str,
help='exp id (default: 0)')
parser.add_argument('--database', default='LIVE', type=str,
help='database name (default: LIVE)')
parser.add_argument('--model', default='CNNIQAplusplus', type=str,
help='model name (default: CNNIQAplusplus)')
# parser.add_argument('--resume', default=None, type=str,
# help='path to latest checkpoint (default: None)')
parser.add_argument("--log_dir", type=str, default="logger",
help="log directory for Tensorboard log output")
parser.add_argument('--disable_gpu', action='store_true',
help='flag whether to disable GPU')
# parser.add_argument('--multi_gpu', action='store_true',
# help='flag whether to use multiple GPUs')
args = parser.parse_args()
torch.manual_seed(args.seed) #
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(args.seed)
random.seed(args.seed)
torch.utils.backcompat.broadcast_warning.enabled = True
with open(args.config) as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print('exp id: ' + args.exp_id)
print('database: ' + args.database)
print('model: ' + args.model)
config.update(config[args.database])
config.update(config[args.model])
log_dir = '{}/EXP{}-{}-{}-lr={}-{}'.format(args.log_dir, args.exp_id, args.database, args.model, args.lr,
datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
ensure_dir('checkpoints')
trained_model_file = 'checkpoints/{}-{}-EXP{}-lr={}'.format(args.model, args.database, args.exp_id, args.lr)
ensure_dir('results')
save_result_file = 'results/{}-{}-EXP{}-lr={}'.format(args.model, args.database, args.exp_id, args.lr)
run(args.batch_size, args.epochs, args.lr, args.weight_decay, args.model, config, args.exp_id,
log_dir, trained_model_file, save_result_file, args.disable_gpu)