-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_image_quality_synthetic.py
442 lines (398 loc) · 15.7 KB
/
train_image_quality_synthetic.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import time
import copy
import datetime
import os
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from PIL import Image
import numpy as np
import pandas as pd
from skimage.filters import gaussian
from skimage.util import img_as_ubyte
from scipy.stats import pearsonr
import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import DataLoader
from torch.utils.data import Dataset
from torchvision import transforms
import torchvision.models as models
from biasLoss import biasLoss
#%% ---- TRAINING OPTIONS ----------------------------------------------------
# Main training options
opts = {
'lr': 0.0001, # learning rate
'bs': 32, # mini-batch size
'epochs': 1000, # maximum training epochs
'early_stop': 100, # early stopping on validation Pearson's correlation
'num_workers': 0, # number of workers of DataLoaders
'model_name': 'resnet18',
'pretrained': False, # image net pretraining
'augmented': True, # random crop training images
'bias_fun': 'first_order', # either "first_order" or "third_order" bias estimation
'r_th': 0.7, # minimum correlation on training set before first bias estimation
'anchor_db': 'train_1', # string with dataset name to which biases should be anchored / None if no anchoring used
'mse_weight': 0.0, # weight of "vanilla MSE loss" added to bias loss
}
main_folder = './'
dataset_folder = './LIVE IQA R2/refimgs'
results_subfolder = 'results'
images_mixed = False # if True, the same reference images are used in different datasets
plot_sigma2mos_mapping = True # show mapping between sigma and mos
plot_images = True # plot 10 images with applied bluriness
plot_biases = True # plot artifically introduced biases
plot_every_epoch = True # show training process and bias estimation every epoch
plot_final_results = True # show final results
# Artificially introduced biases
b = np.array([
[0, 1, 0, 0],
[0.5, 0.5, 0, 0],
[3, 0.3, 0, 0],
[-2.3, 5.03133896, -1.6883704 ,0.19968759]])
#%% ---- LOAD CSV / SIMULATE MOS ----------------------------------------------
# Runname and savepath
runname = datetime.datetime.now().strftime("%y%m%d_%H%M%S%f")
resultspath = os.path.join(main_folder, results_subfolder)
if not os.path.exists(resultspath):
os.makedirs(resultspath)
resultspath = os.path.join(resultspath, runname)
# Load dataset csv files. If True use same reference images in different datasets
if images_mixed:
dfile_train = pd.read_csv(os.path.join(main_folder, 'iqb_train_mixed.csv'))
else:
dfile_train = pd.read_csv(os.path.join(main_folder, 'iqb_train.csv'))
dfile_val = pd.read_csv(os.path.join(main_folder, 'iqb_val.csv'))
# Map the bluriness factor sigma to simulated MOS values
def sigma2mos(sigma):
sigma_min = 1
sigma_max = 3
mos = (sigma-sigma_min) * 100 / (sigma_max-sigma_min)
mos = -mos+100
mos = 1 + 0.035*mos+mos*(mos-60)*(100-mos)*7e-6
mos = mos.clip(min=1).reshape(-1,1).astype('float32')
return mos
dfile_train['mos'] = sigma2mos( dfile_train['sigma'].to_numpy() )
dfile_val['mos'] = sigma2mos( dfile_val['sigma'].to_numpy() )
# Get unique dataset names and apply artifical bias
def calc_mapped(x,b):
if b.ndim==1:
x = b[0] + x * b[1] + x**2 * b[2] + x**3 * b[3]
elif b.ndim==2:
x = b[:,0] + x * b[:,1] + x**2 * b[:,2] + x**3 * b[:,3]
else:
raise ValueError
return x
train_dbs = dfile_train.db.unique()
val_dbs = dfile_val.db.unique()
for i, db in enumerate(dfile_train.db.unique()):
dfile_train.loc[dfile_train.db==db, 'mos'] = calc_mapped(
dfile_train.loc[dfile_train.db==db, 'mos'], b[i] )
#%% ---- DATASET AND PLOTS ----------------------------------------------------
# Dataset to load images with index that is used to assign samples to dataset during training
class ImageBlurIdxDataset(Dataset):
'''
ImageBlurIdxDataset class.
Loads images, applies bluriness, loads them to RAM. Outputs image, MOS,
and index, which is needed to assign the images to their corresponding
dataset.
'''
def __init__(self, main_dir, df, augmented=False):
self.df = df
self.main_dir = main_dir
self.augmented = augmented
self._get_transform()
self._load_images()
def _load_images(self):
self.images = []
for index, row in self.df.iterrows():
image = np.asarray(Image.open( os.path.join(self.main_dir, row['src_image']) ))
image = gaussian(image, sigma=row['sigma'], multichannel=True)
image = img_as_ubyte( image.clip(min=-1, max=1) )
self.images.append(image)
self.mos = self.df['mos'].to_numpy().reshape(-1,1).astype('float32')
def _get_transform(self):
if self.augmented:
self.transform = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
else:
self.transform = transforms.Compose([
transforms.ToPILImage(),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def __getitem__(self, index):
img = self.images[index]
img = self.transform(img)
return img, self.mos[index], index
def __len__(self):
return len(self.images)
ds_train = ImageBlurIdxDataset(dataset_folder, dfile_train, augmented=opts['augmented'])
ds_val = ImageBlurIdxDataset(dataset_folder, dfile_val, augmented=False)
# plot the mapping of bluriness sigma to MOS
if plot_sigma2mos_mapping:
x = np.linspace(1,3,1000)
y = sigma2mos(x)
plt.figure(figsize=(3.0, 3.0))
plt.plot(x,y)
plt.xlabel('$\sigma$')
plt.ylabel('MOS')
plt.yticks(np.arange(1,5,0.5))
plt.show()
# Plot 10 random validation images with their MOS value
if plot_images:
random_index = np.random.choice(len(ds_val), 10, replace=False)
for random_index in random_index:
x, y, idx = ds_val[random_index]
inp = x.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
plt.grid(None)
plt.axis('off')
plt.title('MOS: {:0.2f}'.format(y[0]))
plt.show()
# plot the artifical biases applied to the datasets
if plot_biases:
plt.figure(figsize=(3.0, 3.0))
x = np.linspace(1,5,100)
for i in range(len(b)):
y = calc_mapped(x, b[i])
plt.plot(x,y)
plt.axis([1,4.5,1,4.5])
plt.xlabel('Artificial biases')
plt.xlabel('MOS')
plt.ylabel('Biased MOS')
plt.yticks(np.arange(1,5,0.5))
plt.xticks(np.arange(1,5,0.5))
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
#%% ---- MODEL AND EVALUATION FUNCTION ----------------------------------------
# Select model
if opts['model_name']=='resnet18':
model = models.resnet18(pretrained=opts['pretrained'])
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 1)
elif opts['model_name']=='resnet50':
model = models.resnet50(pretrained=opts['pretrained'])
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 1)
elif opts['model_name']=='resnet101':
model = models.resnet101(pretrained=opts['pretrained'])
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 1)
else:
raise NotImplementedError
# Model evaluation function
def eval_model(
model,
ds,
target_mos='mos',
do_plot=False,
do_print=False,
bs=16,
num_workers=0):
# Dataloader without shuffling
dl = DataLoader(
ds,
batch_size=bs,
shuffle=False,
drop_last=False,
num_workers=num_workers)
# Get predictions
model.eval()
with torch.no_grad():
y_hat = [model(xb.to(dev)).cpu().detach().numpy() for xb, yb, idx in dl]
y_hat = np.concatenate(y_hat).reshape(-1)
y = ds.df[target_mos].to_numpy().reshape(-1)
# Evaluate each database
results_db = []
for db_name in ds.df.db.unique():
idx_db = (ds.df.db==db_name).to_numpy().nonzero()[0]
y_hat_db = y_hat[idx_db]
y_db = y[idx_db]
rmse = np.sqrt( np.mean( (y_hat_db-y_db)**2 ) )
r = pearsonr(y_db.reshape(-1), y_hat_db.reshape(-1))[0]
results_db.append({'db': db_name, 'r': r, 'rmse': rmse})
# Plot
if do_plot:
plt.figure(figsize=(5.0, 5.0))
plt.clf()
plt.plot(y_hat_db, y_db, 'o', label='Original data', markersize=5)
plt.plot([0, 5], [0, 5], 'k')
plt.axis([1, 5, 1, 5])
plt.gca().set_aspect('equal', adjustable='box')
plt.grid(True)
plt.xticks(np.arange(1, 6))
plt.yticks(np.arange(1, 6))
plt.title(db_name)
plt.ylabel('Subjective MOS')
plt.xlabel('Predicted MOS')
plt.show()
# Print
if do_print:
print('%-30s r: %0.2f, rmse: %0.2f'
% (db_name+':', r, rmse))
results_db = pd.DataFrame(results_db)
results = {
'r': results_db.r.to_numpy().mean(),
'rmse': results_db.rmse.to_numpy().mean(),
}
return results, y, y_hat
#%% --- TRAINING LOOP --------------------------------------------------------
# Load biasLoss class
bias_loss = biasLoss(
ds_train.df.db,
anchor_db=opts['anchor_db'],
mapping=opts['bias_fun'],
r_th=opts['r_th'],
mse_weight=opts['mse_weight'],
)
dl_train = DataLoader(
ds_train,
batch_size=opts['bs'],
shuffle=True,
drop_last=True,
num_workers=opts['num_workers'])
dev = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('running on:')
print(dev)
model.to(dev)
opt = optim.Adam(model.parameters(), lr=opts['lr'])
best_model_wts = copy.deepcopy(model.state_dict())
# Ini early stopping
best_r = 0
es = 0
tic_overall = time.time()
print('--> start training')
results = []
for epoch in range(1,opts['epochs']+1):
tic_epoch = time.time()
# Optimize model weights
k = 0
loss = 0.0
model.train()
for xb, yb, idx in dl_train:
yb = yb.to(dev)
yb_hat = model(xb.to(dev))
lossb = bias_loss.get_loss(yb, yb_hat, idx)
lossb.backward()
opt.step()
opt.zero_grad()
loss += lossb.item()
k += 1
loss = loss/k
# Evaluate after each epoch
results_train, y_train, y_train_hat = eval_model(model, ds_train, do_plot=False, do_print=False)
results_val, y_val, y_val_hat = eval_model(model, ds_val, do_plot=False, do_print=False)
# Update bias for loss
bias_loss.update_bias(y_train, y_train_hat)
# Plot
if plot_every_epoch:
x = np.linspace(-10,20,100)
plt.figure(figsize=(12, 2))
dfile_train['mos_hat'] = y_train_hat
dfile_train['b'] = bias_loss.b.tolist()
for i, db in enumerate(dfile_train.db.unique()):
y_db = dfile_train.loc[dfile_train.db==db, 'mos'].to_numpy()
y_hat_db = dfile_train.loc[dfile_train.db==db, 'mos_hat'].to_numpy()
b_db = np.vstack(dfile_train.loc[dfile_train.db==db, 'b'])[0]
y_est = calc_mapped(x, b_db)
y_orig = calc_mapped(x, b[i])
plt.subplot(1,5,i+1)
plt.plot(y_hat_db, y_db, 'o', markersize=2)
plt.plot(x, y_est)
plt.plot(x, y_orig)
plt.yticks(np.arange(-10,10))
plt.axis([y_hat_db.min().clip(max=1),y_hat_db.max().clip(min=5),y_db.min().clip(max=1),y_db.max().clip(min=5)])
plt.title(db)
y_est = calc_mapped(x, np.array([0, 1, 0, 0]))
y_orig = calc_mapped(x, np.array([0, 1, 0, 0]))
plt.subplot(1,5,5)
plt.plot(y_val_hat, y_val, 'o', markersize=2)
plt.plot(x, y_est)
plt.plot(x, y_orig)
plt.yticks(np.arange(-10,10))
plt.axis([y_val_hat.min().clip(max=1),y_val_hat.max().clip(min=5),y_val.min().clip(max=1),y_val.max().clip(min=5)])
plt.title('val')
plt.show()
# Early stopping
if results_val['r'] > best_r:
best_r = results_val['r']
best_model_wts = copy.deepcopy(model.state_dict())
es = 0
else:
es+=1
if es>=opts['early_stop']:
break
# Print results
toc_epoch = time.time() - tic_epoch
print('epoch {}, runtime {:.2f}s, loss {:.3f}, r_train_mean {:.3f}, rmse_val {:.3f}, r_val {:.3f}'.format(
epoch, toc_epoch, loss, results_train['r'], results_val['rmse'], results_val['r']) )
# Save results history
results.append({
'runname': runname,
'epoch': epoch,
**opts,
'train_dbs': train_dbs,
'val_dbs': val_dbs,
**results_val,
})
pd.DataFrame(results).to_csv(resultspath+'__results.csv', index=False)
#%% --- EVALUATE BEST MODEL ---------------------------------------------------
print('training finished!')
model.load_state_dict(best_model_wts)
results_train, y_train, y_train_hat = eval_model(model, ds_train, do_print=True)
results_val, y_val, y_val_hat = eval_model(model, ds_val, do_print=True)
toc_overall = time.time() - tic_overall
print('epochs {}, runtime {:.0f}s, rmse_val {:.3f}, r_val {:.3f}'.format(epoch+1, toc_overall, results_val['rmse'], results_val['r']) )
# Plot
if plot_final_results:
x = np.linspace(-10,20,100)
plt.figure(figsize=(12, 3.5))
dfile_train['mos_hat'] = y_train_hat
bias_loss.update_bias(y_train, y_train_hat)
dfile_train['b'] = bias_loss.b.tolist()
for i, db in enumerate(dfile_train.db.unique()):
y_db = dfile_train.loc[dfile_train.db==db, 'mos'].to_numpy()
y_hat_db = dfile_train.loc[dfile_train.db==db, 'mos_hat'].to_numpy()
b_db = np.vstack(dfile_train.loc[dfile_train.db==db, 'b'])[0]
y_est = calc_mapped(x, b_db)
y_orig = calc_mapped(x, b[i])
plt.subplot(1,5,i+1)
plt.plot(y_hat_db, y_db, 'o', markersize=2)
plt.plot(x, y_est)
plt.plot(x, y_orig)
plt.xticks(np.arange(-10,10))
plt.yticks(np.arange(-10,10))
plt.axis([y_hat_db.min().clip(max=1),y_hat_db.max().clip(min=5),y_db.min().clip(max=1),y_db.max().clip(min=5)])
plt.xlabel('Predicted MOS')
plt.ylabel('Subjective MOS')
plt.gca().set_aspect('equal', adjustable='box')
plt.title(db)
y_est = calc_mapped(x, np.array([0, 1, 0, 0]))
y_orig = calc_mapped(x, np.array([0, 1, 0, 0]))
plt.subplot(1,5,5)
plt.plot(y_val_hat, y_val, 'o', markersize=2)
plt.plot(x, y_est)
plt.plot(x, y_orig)
plt.xticks(np.arange(-10,10))
plt.yticks(np.arange(-10,10))
plt.axis([y_val_hat.min().clip(max=1),y_val_hat.max().clip(min=5),y_val.min().clip(max=1),y_val.max().clip(min=5)])
plt.xlabel('Predicted MOS')
plt.ylabel('Subjective MOS')
plt.title('val final')
plt.gca().set_aspect('equal', adjustable='box')
plt.tight_layout()
plt.show()