-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
235 lines (211 loc) · 11.1 KB
/
train.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
"""train VQModel"""
# tensorboard --logdir=logs --port=6006
# CUDA_VISIBLE_DEVICES=1 python train.py --database=KoNViD-1k --exp_id=0
from argparse import ArgumentParser
import os
import h5py
import torch
from torch.optim import Adam, lr_scheduler
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
import numpy as np
import random
from scipy import stats
from tensorboardX import SummaryWriter
import datetime
from model import VQModel, VQADataset
from extractfeatures import get_features
if __name__ == "__main__":
parser = ArgumentParser(description='"VQ train: Quality Assessment')
parser.add_argument("--seed", type=int, default=20200610)
parser.add_argument('--lr', type=float, default=0.00001,
help='learning rate (default: 0.00001)')
parser.add_argument('--batch_size', type=int, default=16,
help='input batch size for training (default: 16)')
parser.add_argument('--epochs', type=int, default=2000,
help='number of epochs to train (default: 2000)')
parser.add_argument('--database', default='KoNViD-1k', type=str,
help='database name (default: KoNViD-1k)')
parser.add_argument('--model', default='VQModel', type=str,
help='model name (default: VQModel)')
parser.add_argument('--exp_id', default=0, type=int,
help='exp id for train-val-test splits (default: 0)')
parser.add_argument('--test_ratio', type=float, default=0.2,
help='test ratio (default: 0.2)')
parser.add_argument('--val_ratio', type=float, default=0.2,
help='val ratio (default: 0.2)')
parser.add_argument('--weight_decay', type=float, default=0.0,
help='weight decay (default: 0.0)')
parser.add_argument("--notest_during_training", action='store_true',
help='flag whether to test during training')
parser.add_argument("--disable_visualization", action='store_true',
help='flag whether to enable TensorBoard visualization')
parser.add_argument("--log_dir", type=str, default="logs",
help="log directory for Tensorboard log output")
parser.add_argument('--disable_gpu', action='store_true',
help='flag whether to disable GPU')
args = parser.parse_args()
args.decay_interval = int(args.epochs/10)
args.decay_ratio = 0.8
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
if args.database == 'KoNViD-1k':
features_dir = 'traindata/KoNViD-1k_features/' # features dir
datainfo = 'dataset/KoNViD-1kinfo.mat' # database info: video_names, scores; video format, width, height, index, ref_ids, max_len, etc.
if args.database == 'CVD2014':
features_dir = 'traindata/CVD2014_features/'
datainfo = 'dataset/CVD2014info.mat'
if args.database == 'LIVE-Qualcomm':
features_dir = 'traindata/LIVE-Qualcomm_features/'
datainfo = 'dataset/LIVE-Qualcomminfo.mat'
print('EXP ID: {}'.format(args.exp_id))
print(args.database)
print(args.model)
device = torch.device("cuda" if not args.disable_gpu and torch.cuda.is_available() else "cpu")
Info = h5py.File(datainfo, 'r') # index, ref_ids
index = Info['index']
index = index[:, args.exp_id % index.shape[1]] # np.random.permutation(N)
ref_ids = Info['ref_ids'][0, :] #
max_len = int(Info['max_len'][0])
trainindex = index[0:int(np.ceil((1 - args.test_ratio - args.val_ratio) * len(index)))]
testindex = index[int(np.ceil((1 - args.test_ratio) * len(index))):len(index)]
train_index, val_index, test_index = [], [], []
for i in range(len(ref_ids)):
train_index.append(i) if (ref_ids[i] in trainindex) else \
test_index.append(i) if (ref_ids[i] in testindex) else \
val_index.append(i)
scale = Info['scores'][0, :].max() # label normalization factor
train_dataset = VQADataset(features_dir, train_index, max_len, scale=scale)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True)
val_dataset = VQADataset(features_dir, val_index, max_len, scale=scale)
val_loader = torch.utils.data.DataLoader(dataset=val_dataset)
if args.test_ratio > 0:
test_dataset = VQADataset(features_dir, test_index, max_len, scale=scale)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset)
model = VQModel().to(device) #
if not os.path.exists('models'):
os.makedirs('models')
trained_model_file = 'models/{}-{}-EXP{}'.format(args.model, args.database, args.exp_id)
if not os.path.exists('results'):
os.makedirs('results')
save_result_file = 'results/{}-{}-EXP{}'.format(args.model, args.database, args.exp_id)
if not args.disable_visualization: # Tensorboard Visualization
writer = SummaryWriter(log_dir='{}/EXP{}-{}-{}-{}-{}-{}-{}'
.format(args.log_dir, args.exp_id, args.database, args.model,
args.lr, args.batch_size, args.epochs,
datetime.datetime.now().strftime("%I%M%p%B%d%Y")))
criterion = nn.L1Loss() # L1 loss
optimizer = Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
scheduler = lr_scheduler.StepLR(optimizer, step_size=args.decay_interval, gamma=args.decay_ratio)
best_val_criterion = -1 # SROCC min
for epoch in range(args.epochs):
# Train
model.train()
L = 0
for i, (features, length, label) in enumerate(train_loader):
features = features.to(device).float()
label = label.to(device).float()
optimizer.zero_grad() #
outputs = model(features, length.float())
#outputs = model(features)
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
L = L + loss.item()
train_loss = L / (i + 1)
model.eval()
# Val
y_pred = np.zeros(len(val_index))
y_val = np.zeros(len(val_index))
L = 0
with torch.no_grad():
for i, (features, length, label) in enumerate(val_loader):
y_val[i] = scale * label.item() #
features = features.to(device).float()
label = label.to(device).float()
outputs = model(features, length.float())
#outputs = model(features)
y_pred[i] = scale * outputs.item()
loss = criterion(outputs, label)
L = L + loss.item()
val_loss = L / (i + 1)
val_PLCC = stats.pearsonr(y_pred, y_val)[0]
val_SROCC = stats.spearmanr(y_pred, y_val)[0]
val_RMSE = np.sqrt(((y_pred-y_val) ** 2).mean())
val_KROCC = stats.stats.kendalltau(y_pred, y_val)[0]
print("Validate results: val loss={:.4f}, SROCC={:.4f}, KROCC={:.4f}, PLCC={:.4f}, RMSE={:.4f}"
.format(val_loss, val_SROCC, val_KROCC, val_PLCC, val_RMSE))
# Test
if args.test_ratio > 0 and not args.notest_during_training:
y_pred = np.zeros(len(test_index))
y_test = np.zeros(len(test_index))
L = 0
with torch.no_grad():
for i, (features, length, label) in enumerate(test_loader):
y_test[i] = scale * label.item() #
features = features.to(device).float()
label = label.to(device).float()
outputs = model(features, length.float())
#outputs = model(features)
y_pred[i] = scale * outputs.item()
loss = criterion(outputs, label)
L = L + loss.item()
test_loss = L / (i + 1)
PLCC = stats.pearsonr(y_pred, y_test)[0]
SROCC = stats.spearmanr(y_pred, y_test)[0]
RMSE = np.sqrt(((y_pred-y_test) ** 2).mean())
KROCC = stats.stats.kendalltau(y_pred, y_test)[0]
if not args.disable_visualization: # record training curves
writer.add_scalar("loss/train", train_loss, epoch) #
writer.add_scalar("loss/val", val_loss, epoch) #
writer.add_scalar("SROCC/val", val_SROCC, epoch) #
writer.add_scalar("KROCC/val", val_KROCC, epoch) #
writer.add_scalar("PLCC/val", val_PLCC, epoch) #
writer.add_scalar("RMSE/val", val_RMSE, epoch) #
if args.test_ratio > 0 and not args.notest_during_training:
writer.add_scalar("loss/test", test_loss, epoch) #
writer.add_scalar("SROCC/test", SROCC, epoch) #
writer.add_scalar("KROCC/test", KROCC, epoch) #
writer.add_scalar("PLCC/test", PLCC, epoch) #
writer.add_scalar("RMSE/test", RMSE, epoch) #
# Update the model with the best val_SROCC
if val_SROCC > best_val_criterion:
print("EXP ID={}: Update best model using best_val_criterion in epoch {}".format(args.exp_id, epoch))
print("Val results: val loss={:.4f}, SROCC={:.4f}, KROCC={:.4f}, PLCC={:.4f}, RMSE={:.4f}"
.format(val_loss, val_SROCC, val_KROCC, val_PLCC, val_RMSE))
if args.test_ratio > 0 and not args.notest_during_training:
print("Test results: test loss={:.4f}, SROCC={:.4f}, KROCC={:.4f}, PLCC={:.4f}, RMSE={:.4f}"
.format(test_loss, SROCC, KROCC, PLCC, RMSE))
np.save(save_result_file, (y_pred, y_test, test_loss, SROCC, KROCC, PLCC, RMSE, test_index))
torch.save(model.state_dict(), trained_model_file)
best_val_criterion = val_SROCC # update best val SROCC
# Test
if args.test_ratio > 0:
model.load_state_dict(torch.load(trained_model_file)) #
model.eval()
with torch.no_grad():
y_pred = np.zeros(len(test_index))
y_test = np.zeros(len(test_index))
L = 0
for i, (features, length, label) in enumerate(test_loader):
y_test[i] = scale * label.item() #
features = features.to(device).float()
label = label.to(device).float()
outputs = model(features, length.float())
#outputs = model(features)
y_pred[i] = scale * outputs.item()
loss = criterion(outputs, label)
L = L + loss.item()
test_loss = L / (i + 1)
PLCC = stats.pearsonr(y_pred, y_test)[0]
SROCC = stats.spearmanr(y_pred, y_test)[0]
RMSE = np.sqrt(((y_pred-y_test) ** 2).mean())
KROCC = stats.stats.kendalltau(y_pred, y_test)[0]
print("Test results: test loss={:.4f}, SROCC={:.4f}, KROCC={:.4f}, PLCC={:.4f}, RMSE={:.4f}"
.format(test_loss, SROCC, KROCC, PLCC, RMSE))
np.save(save_result_file, (y_pred, y_test, test_loss, SROCC, KROCC, PLCC, RMSE, test_index))