-
Notifications
You must be signed in to change notification settings - Fork 19
/
demo_eval_BVQA_feats_one_dataset.py
292 lines (263 loc) · 12.6 KB
/
demo_eval_BVQA_feats_one_dataset.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
# -*- coding: utf-8 -*-
"""
Evaluate a BVQA model/features on a given dataset trained with support
vector machine trained for 100 iterations of train-test (80%-20%) split.
SVR hyperparameters were selected by a grid-search tuning on a random 20%
of the training set.
Input:
- feature matrix:
eg, features/KONVID_1K_VIDEVAL_feats.mat
- MOS files:
eg, features/KONVID_1K_metadata.csv
Output:
- evaluation results:
eg, results/KONVID_1K_VIDEVAL_corr.mat
"""
# Load libraries
from sklearn import model_selection
import os
import warnings
import time
import pandas
import math
import random as rnd
import scipy.stats
import scipy.io
from scipy.optimize import curve_fit
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler
# ignore all warnings
warnings.filterwarnings("ignore")
# ===========================================================================
# Here starts the main part of the script
#
'''======================== parameters ================================'''
model_name = 'SVR' # regression model
data_name = 'KONVID_1K' # dataset name
algo_name = 'VIDEVAL' # evaluated model
color_only = True # if True, it is YouTube-UGCc dataset; if False it is YouTube-UGC
csv_file = os.path.join('features', data_name+'_metadata.csv')
mat_file = os.path.join('features', data_name+'_'+algo_name+'_feats.mat')
result_file = os.path.join('results', data_name+'_'+algo_name+'_corr.mat')
print("Evaluating algorithm {} with {} on dataset {} ...".format(algo_name,
model_name, data_name))
'''======================== read files =============================== '''
df = pandas.read_csv(csv_file, skiprows=[], header=None)
array = df.values
if data_name == 'YOUTUBE_UGC':
y = array[1:,4] ## for YOUTUBE_UGC
else:
y = array[1:,1] ## for LIVE-VQC & KONVID_1k
y = np.array(list(y), dtype=np.float)
X_mat = scipy.io.loadmat(mat_file)
X = np.asarray(X_mat['feats_mat'], dtype=np.float)
#### 57 grayscale videos in YOUTUBE_UGC dataset, we do not consider them for fair comparison ####
if color_only:
gray_indices = [3,6,10,22,23,46,51,52,68,74,77,99,103,122,136,141,158,173,368,426,467,477,506,563,594,\
639,654,657,666,670,671,681,690,697,702,703,710,726,736,764,768,777,786,796,977,990,1012,\
1015,1023,1091,1118,1205,1282,1312,1336,1344,1380]
gray_indices = [idx - 1 for idx in gray_indices]
X = np.delete(X, gray_indices, axis=0)
y = np.delete(y, gray_indices, axis=0)
X[np.isnan(X)] = 0
X[np.isinf(X)] = 0
'''======================== Main Body ==========================='''
model_params_all_repeats = []
PLCC_all_repeats_test = []
SRCC_all_repeats_test = []
KRCC_all_repeats_test = []
RMSE_all_repeats_test = []
PLCC_all_repeats_train = []
SRCC_all_repeats_train = []
KRCC_all_repeats_train = []
RMSE_all_repeats_train = []
# #############################################################################
# Train classifiers
#
# For an initial search, a logarithmic grid with basis
# 10 is often helpful. Using a basis of 2, a finer
# tuning can be achieved but at a much higher cost.
#
if algo_name == 'CORNIA10K' or algo_name == 'HOSA':
C_range = [0.1, 1, 10]
gamma_range = [0.01, 0.1, 1]
else:
C_range = np.logspace(1, 10, 10, base=2)
gamma_range = np.logspace(-8, 1, 10, base=2)
params_grid = dict(gamma=gamma_range, C=C_range)
# 100 random splits
for i in range(1,101):
print(i, 'th repeated 80-20 hold out test')
t0 = time.time()
# parameters for each hold out
model_params_all = []
PLCC_all_train = []
SRCC_all_train = []
KRCC_all_train = []
RMSE_all_train = []
PLCC_all_test = []
SRCC_all_test = []
KRCC_all_test = []
RMSE_all_test = []
# Split data to test and validation sets randomly
test_size = 0.2
X_train, X_test, y_train, y_test = \
model_selection.train_test_split(X, y, test_size=test_size, random_state=math.ceil(8.8*i))
# SVR grid search in the TRAINING SET ONLY
validation_size = 0.2
X_param_train, X_param_valid, y_param_train, y_param_valid = \
model_selection.train_test_split(X_train, y_train, test_size=validation_size, random_state=math.ceil(6.6*i))
# grid search
for C in C_range:
for gamma in gamma_range:
model_params_all.append((C, gamma))
if algo_name == 'CORNIA10K' or \
algo_name == 'HOSA':
model = SVR(kernel='linear', gamma=gamma, C=C)
else:
model = SVR(kernel='rbf', gamma=gamma, C=C)
# Standard min-max normalization of features
scaler = MinMaxScaler().fit(X_param_train)
X_param_train = scaler.transform(X_param_train)
# Fit training set to the regression model
model.fit(X_param_train, y_param_train)
# Apply scaling
X_param_valid = scaler.transform(X_param_valid)
# Predict MOS for the validation set
y_param_valid_pred = model.predict(X_param_valid)
y_param_train_pred = model.predict(X_param_train)
# define 4-parameter logistic regression
def logistic_func(X, bayta1, bayta2, bayta3, bayta4):
logisticPart = 1 + np.exp(np.negative(np.divide(X - bayta3, np.abs(bayta4))))
yhat = bayta2 + np.divide(bayta1 - bayta2, logisticPart)
return yhat
y_param_valid = np.array(list(y_param_valid), dtype=np.float)
y_param_train = np.array(list(y_param_train), dtype=np.float)
try:
# logistic regression
beta = [np.max(y_param_valid), np.min(y_param_valid), np.mean(y_param_valid_pred), 0.5]
popt, _ = curve_fit(logistic_func, y_param_valid_pred, \
y_param_valid, p0=beta, maxfev=100000000)
y_param_valid_pred_logistic = logistic_func(y_param_valid_pred, *popt)
# logistic regression
beta = [np.max(y_param_train), np.min(y_param_train), np.mean(y_param_train_pred), 0.5]
popt, _ = curve_fit(logistic_func, y_param_valid_pred, \
y_param_valid, p0=beta, maxfev=100000000)
y_param_train_pred_logistic = logistic_func(y_param_train_pred, *popt)
except:
raise Exception('Fitting logistic function time-out!!')
plcc_valid_tmp = scipy.stats.pearsonr(y_param_valid, y_param_valid_pred_logistic)[0]
rmse_valid_tmp = np.sqrt(mean_squared_error(y_param_valid, y_param_valid_pred_logistic))
srcc_valid_tmp = scipy.stats.spearmanr(y_param_valid, y_param_valid_pred)[0]
krcc_valid_tmp = scipy.stats.kendalltau(y_param_valid, y_param_valid_pred)[0]
plcc_train_tmp = scipy.stats.pearsonr(y_param_train, y_param_train_pred_logistic)[0]
rmse_train_tmp = np.sqrt(mean_squared_error(y_param_train, y_param_train_pred_logistic))
srcc_train_tmp = scipy.stats.spearmanr(y_param_train, y_param_train_pred)[0]
try:
krcc_train_tmp = scipy.stats.kendalltau(y_param_train, y_param_train_pred)[0]
except:
krcc_train_tmp = scipy.stats.kendalltau(y_param_train, y_param_train_pred, method='asymptotic')[0]
# save results
PLCC_all_test.append(plcc_valid_tmp)
RMSE_all_test.append(rmse_valid_tmp)
SRCC_all_test.append(srcc_valid_tmp)
KRCC_all_test.append(krcc_valid_tmp)
PLCC_all_train.append(plcc_train_tmp)
RMSE_all_train.append(rmse_train_tmp)
SRCC_all_train.append(srcc_train_tmp)
KRCC_all_train.append(krcc_train_tmp)
# using the best chosen parameters to test on testing set
param_idx = np.argmin(np.asarray(RMSE_all_test, dtype=np.float))
C_opt, gamma_opt = model_params_all[param_idx]
if algo_name == 'CORNIA10K' or \
algo_name == 'HOSA':
model = SVR(kernel='linear', gamma=gamma_opt, C=C_opt)
else:
model = SVR(kernel='rbf', gamma=gamma_opt, C=C_opt)
# Standard min-max normalization of features
scaler = MinMaxScaler().fit(X_train)
X_train = scaler.transform(X_train)
# Fit training set to the regression model
model.fit(X_train, y_train)
# Apply scaling
X_test = scaler.transform(X_test)
# Predict MOS for the test set
y_test_pred = model.predict(X_test)
y_train_pred = model.predict(X_train)
y_test = np.array(list(y_test), dtype=np.float)
y_train = np.array(list(y_train), dtype=np.float)
try:
# logistic regression
beta = [np.max(y_test), np.min(y_test), np.mean(y_test_pred), 0.5]
popt, _ = curve_fit(logistic_func, y_test_pred, \
y_test, p0=beta, maxfev=100000000)
y_test_pred_logistic = logistic_func(y_test_pred, *popt)
# logistic regression
beta = [np.max(y_train), np.min(y_train), np.mean(y_train_pred), 0.5]
popt, _ = curve_fit(logistic_func, y_train_pred, \
y_train, p0=beta, maxfev=100000000)
y_train_pred_logistic = logistic_func(y_train_pred, *popt)
except:
raise Exception('Fitting logistic function time-out!!')
plcc_test_opt = scipy.stats.pearsonr(y_test, y_test_pred_logistic)[0]
rmse_test_opt = np.sqrt(mean_squared_error(y_test, y_test_pred_logistic))
srcc_test_opt = scipy.stats.spearmanr(y_test, y_test_pred)[0]
krcc_test_opt = scipy.stats.kendalltau(y_test, y_test_pred)[0]
plcc_train_opt = scipy.stats.pearsonr(y_train, y_train_pred_logistic)[0]
rmse_train_opt = np.sqrt(mean_squared_error(y_train, y_train_pred_logistic))
srcc_train_opt = scipy.stats.spearmanr(y_train, y_train_pred)[0]
krcc_train_opt = scipy.stats.kendalltau(y_train, y_train_pred)[0]
model_params_all_repeats.append((C_opt, gamma_opt))
SRCC_all_repeats_test.append(srcc_test_opt)
KRCC_all_repeats_test.append(krcc_test_opt)
PLCC_all_repeats_test.append(plcc_test_opt)
RMSE_all_repeats_test.append(rmse_test_opt)
SRCC_all_repeats_train.append(srcc_train_opt)
KRCC_all_repeats_train.append(krcc_train_opt)
PLCC_all_repeats_train.append(plcc_train_opt)
RMSE_all_repeats_train.append(rmse_train_opt)
# print results for each iteration
print('======================================================')
print('Best results in CV grid search in one split')
print('SRCC_train: ', srcc_train_opt)
print('KRCC_train: ', krcc_train_opt)
print('PLCC_train: ', plcc_train_opt)
print('RMSE_train: ', rmse_train_opt)
print('======================================================')
print('SRCC_test: ', srcc_test_opt)
print('KRCC_test: ', krcc_test_opt)
print('PLCC_test: ', plcc_test_opt)
print('RMSE_test: ', rmse_test_opt)
print('MODEL: ', (C_opt, gamma_opt))
print('======================================================')
print(' -- ' + str(time.time()-t0) + ' seconds elapsed...\n\n')
print('\n\n')
print('======================================================')
print('Average training results among all repeated 80-20 holdouts:')
print('SRCC: ',np.median(SRCC_all_repeats_train),'( std:',np.std(SRCC_all_repeats_train),')')
print('KRCC: ',np.median(KRCC_all_repeats_train),'( std:',np.std(KRCC_all_repeats_train),')')
print('PLCC: ',np.median(PLCC_all_repeats_train),'( std:',np.std(PLCC_all_repeats_train),')')
print('RMSE: ',np.median(RMSE_all_repeats_train),'( std:',np.std(RMSE_all_repeats_train),')')
print('======================================================')
print('Average testing results among all repeated 80-20 holdouts:')
print('SRCC: ',np.median(SRCC_all_repeats_test),'( std:',np.std(SRCC_all_repeats_test),')')
print('KRCC: ',np.median(KRCC_all_repeats_test),'( std:',np.std(KRCC_all_repeats_test),')')
print('PLCC: ',np.median(PLCC_all_repeats_test),'( std:',np.std(PLCC_all_repeats_test),')')
print('RMSE: ',np.median(RMSE_all_repeats_test),'( std:',np.std(RMSE_all_repeats_test),')')
print('======================================================')
print('\n\n')
#================================================================================
# save mats
scipy.io.savemat(result_file, \
mdict={'SRCC_train': np.asarray(SRCC_all_repeats_train,dtype=np.float), \
'KRCC_train': np.asarray(KRCC_all_repeats_train,dtype=np.float), \
'PLCC_train': np.asarray(PLCC_all_repeats_train,dtype=np.float), \
'RMSE_train': np.asarray(RMSE_all_repeats_train,dtype=np.float), \
'SRCC_test': np.asarray(SRCC_all_repeats_test,dtype=np.float), \
'KRCC_test': np.asarray(KRCC_all_repeats_test,dtype=np.float), \
'PLCC_test': np.asarray(PLCC_all_repeats_test,dtype=np.float), \
'RMSE_test': np.asarray(RMSE_all_repeats_test,dtype=np.float),\
})