-
Notifications
You must be signed in to change notification settings - Fork 36
/
train_maniqa.py
325 lines (267 loc) · 11.5 KB
/
train_maniqa.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
import os
import torch
import numpy as np
import logging
import time
import torch.nn as nn
import random
from torchvision import transforms
from torch.utils.data import DataLoader
from models.maniqa import MANIQA
from config import Config
from utils.process import RandCrop, ToTensor, Normalize, five_point_crop
from utils.process import split_dataset_kadid10k, split_dataset_koniq10k
from utils.process import RandRotation, RandHorizontalFlip
from scipy.stats import spearmanr, pearsonr
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
def setup_seed(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def set_logging(config):
if not os.path.exists(config.log_path):
os.makedirs(config.log_path)
filename = os.path.join(config.log_path, config.log_file)
logging.basicConfig(
level=logging.INFO,
filename=filename,
filemode='w',
format='[%(asctime)s %(levelname)-8s] %(message)s',
datefmt='%Y%m%d %H:%M:%S'
)
def train_epoch(epoch, net, criterion, optimizer, scheduler, train_loader):
losses = []
net.train()
# save data for one epoch
pred_epoch = []
labels_epoch = []
for data in tqdm(train_loader):
x_d = data['d_img_org'].cuda()
labels = data['score']
labels = torch.squeeze(labels.type(torch.FloatTensor)).cuda()
pred_d = net(x_d)
optimizer.zero_grad()
loss = criterion(torch.squeeze(pred_d), labels)
losses.append(loss.item())
loss.backward()
optimizer.step()
scheduler.step()
# save results in one epoch
pred_batch_numpy = pred_d.data.cpu().numpy()
labels_batch_numpy = labels.data.cpu().numpy()
pred_epoch = np.append(pred_epoch, pred_batch_numpy)
labels_epoch = np.append(labels_epoch, labels_batch_numpy)
# compute correlation coefficient
rho_s, _ = spearmanr(np.squeeze(pred_epoch), np.squeeze(labels_epoch))
rho_p, _ = pearsonr(np.squeeze(pred_epoch), np.squeeze(labels_epoch))
ret_loss = np.mean(losses)
logging.info('train epoch:{} / loss:{:.4} / SRCC:{:.4} / PLCC:{:.4}'.format(epoch + 1, ret_loss, rho_s, rho_p))
return ret_loss, rho_s, rho_p
def eval_epoch(config, epoch, net, criterion, test_loader):
with torch.no_grad():
losses = []
net.eval()
# save data for one epoch
pred_epoch = []
labels_epoch = []
for data in tqdm(test_loader):
pred = 0
for i in range(config.num_avg_val):
x_d = data['d_img_org'].cuda()
labels = data['score']
labels = torch.squeeze(labels.type(torch.FloatTensor)).cuda()
x_d = five_point_crop(i, d_img=x_d, config=config)
pred += net(x_d)
pred /= config.num_avg_val
# compute loss
loss = criterion(torch.squeeze(pred), labels)
losses.append(loss.item())
# save results in one epoch
pred_batch_numpy = pred.data.cpu().numpy()
labels_batch_numpy = labels.data.cpu().numpy()
pred_epoch = np.append(pred_epoch, pred_batch_numpy)
labels_epoch = np.append(labels_epoch, labels_batch_numpy)
# compute correlation coefficient
rho_s, _ = spearmanr(np.squeeze(pred_epoch), np.squeeze(labels_epoch))
rho_p, _ = pearsonr(np.squeeze(pred_epoch), np.squeeze(labels_epoch))
logging.info('Epoch:{} ===== loss:{:.4} ===== SRCC:{:.4} ===== PLCC:{:.4}'.format(epoch + 1, np.mean(losses), rho_s, rho_p))
return np.mean(losses), rho_s, rho_p
if __name__ == '__main__':
cpu_num = 1
os.environ['OMP_NUM_THREADS'] = str(cpu_num)
os.environ['OPENBLAS_NUM_THREADS'] = str(cpu_num)
os.environ['MKL_NUM_THREADS'] = str(cpu_num)
os.environ['VECLIB_MAXIMUM_THREADS'] = str(cpu_num)
os.environ['NUMEXPR_NUM_THREADS'] = str(cpu_num)
torch.set_num_threads(cpu_num)
setup_seed(20)
# config file
config = Config({
# dataset path
"dataset_name": "koniq10k",
# PIPAL
"train_dis_path": "/mnt/IQA_dataset/PIPAL22/Train_dis/",
"val_dis_path": "/mnt/IQA_dataset/PIPAL22/Val_dis/",
"pipal22_train_label": "./data/PIPAL22/pipal22_train.txt",
"pipal22_val_txt_label": "./data/PIPAL22/pipal22_val.txt",
# KADID-10K
"kadid10k_path": "/mnt/IQA_dataset/kadid10k/images/",
"kadid10k_label": "./data/kadid10k/kadid10k_label.txt",
# KONIQ-10K
"koniq10k_path": "/mnt/IQA_dataset/1024x768/",
"koniq10k_label": "./data/koniq10k/koniq10k_label.txt",
# optimization
"batch_size": 8,
"learning_rate": 1e-5,
"weight_decay": 1e-5,
"n_epoch": 300,
"val_freq": 1,
"T_max": 50,
"eta_min": 0,
"num_avg_val": 1, # if training koniq10k, num_avg_val is set to 1
"num_workers": 8,
# data
"split_seed": 20,
"train_keep_ratio": 1.0,
"val_keep_ratio": 1.0,
"crop_size": 224,
"prob_aug": 0.7,
# model
"patch_size": 8,
"img_size": 224,
"embed_dim": 768,
"dim_mlp": 768,
"num_heads": [4, 4],
"window_size": 4,
"depths": [2, 2],
"num_outputs": 1,
"num_tab": 2,
"scale": 0.8,
# load & save checkpoint
"model_name": "koniq10k-base_s20",
"type_name": "Koniq10k",
"ckpt_path": "./output/models/", # directory for saving checkpoint
"log_path": "./output/log/",
"log_file": ".log",
"tensorboard_path": "./output/tensorboard/"
})
config.log_file = config.model_name + ".log"
config.tensorboard_path = os.path.join(config.tensorboard_path, config.type_name)
config.tensorboard_path = os.path.join(config.tensorboard_path, config.model_name)
config.ckpt_path = os.path.join(config.ckpt_path, config.type_name)
config.ckpt_path = os.path.join(config.ckpt_path, config.model_name)
config.log_path = os.path.join(config.log_path, config.type_name)
if not os.path.exists(config.ckpt_path):
os.makedirs(config.ckpt_path)
if not os.path.exists(config.tensorboard_path):
os.makedirs(config.tensorboard_path)
set_logging(config)
logging.info(config)
writer = SummaryWriter(config.tensorboard_path)
if config.dataset_name == 'kadid10k':
from data.kadid10k.kadid10k import Kadid10k
train_name, val_name = split_dataset_kadid10k(
txt_file_name=config.kadid10k_label,
split_seed=config.split_seed
)
dis_train_path = config.kadid10k_path
dis_val_path = config.kadid10k_path
label_train_path = config.kadid10k_label
label_val_path = config.kadid10k_label
Dataset = Kadid10k
elif config.dataset_name == 'pipal':
from data.PIPAL22.pipal import PIPAL
dis_train_path = config.train_dis_path
dis_val_path = config.val_dis_path
label_train_path = config.pipal22_train_label
label_val_path = config.pipal22_val_txt_label
Dataset = PIPAL
elif config.dataset_name == 'koniq10k':
from data.koniq10k.koniq10k import Koniq10k
train_name, val_name = split_dataset_koniq10k(
txt_file_name=config.koniq10k_label,
split_seed=config.split_seed
)
dis_train_path = config.koniq10k_path
dis_val_path = config.koniq10k_path
label_train_path = config.koniq10k_label
label_val_path = config.koniq10k_label
Dataset = Koniq10k
else:
pass
# data load
train_dataset = Dataset(
dis_path=dis_train_path,
txt_file_name=label_train_path,
list_name=train_name,
transform=transforms.Compose([RandCrop(patch_size=config.crop_size),
Normalize(0.5, 0.5), RandHorizontalFlip(prob_aug=config.prob_aug), ToTensor()]),
keep_ratio=config.train_keep_ratio
)
val_dataset = Dataset(
dis_path=dis_val_path,
txt_file_name=label_val_path,
list_name=val_name,
transform=transforms.Compose([RandCrop(patch_size=config.crop_size),
Normalize(0.5, 0.5), ToTensor()]),
keep_ratio=config.val_keep_ratio
)
logging.info('number of train scenes: {}'.format(len(train_dataset)))
logging.info('number of val scenes: {}'.format(len(val_dataset)))
# load the data
train_loader = DataLoader(dataset=train_dataset, batch_size=config.batch_size,
num_workers=config.num_workers, drop_last=True, shuffle=True)
val_loader = DataLoader(dataset=val_dataset, batch_size=config.batch_size,
num_workers=config.num_workers, drop_last=True, shuffle=False)
# model defination
net = MANIQA(embed_dim=config.embed_dim, num_outputs=config.num_outputs, dim_mlp=config.dim_mlp,
patch_size=config.patch_size, img_size=config.img_size, window_size=config.window_size,
depths=config.depths, num_heads=config.num_heads, num_tab=config.num_tab, scale=config.scale)
logging.info('{} : {} [M]'.format('#Params', sum(map(lambda x: x.numel(), net.parameters())) / 10 ** 6))
net = nn.DataParallel(net)
net = net.cuda()
# loss function
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(
net.parameters(),
lr=config.learning_rate,
weight_decay=config.weight_decay,
)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=config.T_max, eta_min=config.eta_min)
# train & validation
losses, scores = [], []
best_srocc = 0
best_plcc = 0
main_score = 0
for epoch in range(0, config.n_epoch):
start_time = time.time()
logging.info('Running training epoch {}'.format(epoch + 1))
loss_val, rho_s, rho_p = train_epoch(epoch, net, criterion, optimizer, scheduler, train_loader)
writer.add_scalar("Train_loss", loss_val, epoch)
writer.add_scalar("SRCC", rho_s, epoch)
writer.add_scalar("PLCC", rho_p, epoch)
if (epoch + 1) % config.val_freq == 0:
logging.info('Starting eval...')
logging.info('Running testing in epoch {}'.format(epoch + 1))
loss, rho_s, rho_p = eval_epoch(config, epoch, net, criterion, val_loader)
logging.info('Eval done...')
if rho_s + rho_p > main_score:
main_score = rho_s + rho_p
best_srocc = rho_s
best_plcc = rho_p
logging.info('======================================================================================')
logging.info('============================== best main score is {} ================================='.format(main_score))
logging.info('======================================================================================')
# save weights
model_name = "epoch{}.pt".format(epoch + 1)
model_save_path = os.path.join(config.ckpt_path, model_name)
torch.save(net.module.state_dict(), model_save_path)
logging.info('Saving weights and model of epoch{}, SRCC:{}, PLCC:{}'.format(epoch + 1, best_srocc, best_plcc))
logging.info('Epoch {} done. Time: {:.2}min'.format(epoch + 1, (time.time() - start_time) / 60))