-
Notifications
You must be signed in to change notification settings - Fork 2
/
ladder_generation.py
265 lines (242 loc) · 10.1 KB
/
ladder_generation.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
# Copyright (C) 2024 Amritha Premkumar, Prajit T Rajendran, Vignesh V Menon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import math
import os
import sys
import warnings
import joblib
# Importing necessary libraries
import pandas as pd
# Disable all warnings
warnings.filterwarnings("ignore")
# Creating a class to handle ladder generation
class LadderGenerator:
# Constructor
def __init__(self, max_enc_time, max_dec_time, codec, result_csv, bitrates, dataset_path, resolutions_list, r_max,
max_xpsnr, jnd):
self.models_enc_time = None
self.models_time = None
self.models_xpsnr = None
self.models_qp = None
self.max_enc_time = max_enc_time
self.max_dec_time = max_dec_time
self.actual_max_enc_time = max_enc_time - 0.5
self.actual_max_dec_time = max_dec_time - 0.5
self.codec = codec
self.result_csv = result_csv
self.bitrates_list = bitrates
self.df_consolidated = pd.read_csv(dataset_path)
self.resolutions_list = resolutions_list
self.max_resolution = r_max
self.max_xpsnr = max_xpsnr
self.jnd = jnd
# Load corresponding prediction models
def load_models(self):
# Set path to model
if self.codec == "vvenc":
model_path = "./models/vvenc-faster"
else:
print("Codec not supported")
sys.exit()
# Load models for different resolutions
self.models_xpsnr = {
'single': joblib.load(open(os.path.join(model_path, 'xpsnr', 'xpsnr_model.pkl'), 'rb')),
}
self.models_qp = {
'minimum': joblib.load(open(os.path.join(model_path, 'qp', 'qp_10_br_model.pkl'), 'rb')),
'maximum': joblib.load(open(os.path.join(model_path, 'qp', 'qp_50_br_model.pkl'), 'rb')),
}
self.models_enc_time = {
'minimum': joblib.load(open(os.path.join(model_path, 'enc_time', 'qp_10_enc_time_model.pkl'), 'rb')),
'maximum': joblib.load(open(os.path.join(model_path, 'enc_time', 'qp_50_enc_time_model.pkl'), 'rb')),
}
# Get the predicted resolution and qp as a list based on the features and the predicted time
def get_resolution_and_qp(
self,
xpsnr_features,
qp_or_time_features_list,
bitrate,
enc_time,
previous_resolution,
):
resolution_predicted_features_list = self.select_best_resolution(
xpsnr_features, qp_or_time_features_list, enc_time, previous_resolution, bitrate
)
qp = self.predict_qp(qp_or_time_features_list, resolution_predicted_features_list[0], bitrate)
result_list = [resolution_predicted_features_list[0], qp, resolution_predicted_features_list[1],
resolution_predicted_features_list[2]]
return result_list
# Get the best resolution based the predicted and target time
def select_best_resolution(
self, xpsnr_features, qp_or_time_features_list, tl, previous_resolution, bitrate
):
result_list = []
predicted_resolution = self.resolutions_list[0]
xpsnr = []
time = []
for resolution in self.resolutions_list:
xpsnr.append(self.predict_xpsnr(xpsnr_features, resolution, bitrate))
time.append(
self.predict_enc_time(qp_or_time_features_list, resolution, bitrate)
)
highest_xpsnr = -1
for i in range(len(xpsnr)):
if time[i] < tl:
if xpsnr[i] > highest_xpsnr:
highest_xpsnr = xpsnr[i]
if highest_xpsnr != -1:
index = xpsnr.index(highest_xpsnr)
predicted_resolution = self.resolutions_list[index]
resolution = self.get_resolution_based_on_bitrate(predicted_resolution, previous_resolution, bitrate)
index = self.resolutions_list.index(resolution)
predicted_xpsnr = xpsnr[index]
predicted_time = time[index]
result_list.extend([resolution, predicted_xpsnr, predicted_time])
return result_list
@staticmethod
def get_resolution_based_on_bitrate(predicted_resolution, previous_resolution, bitrate):
if bitrate < 1000:
if predicted_resolution == 2160:
return previous_resolution
else:
if predicted_resolution > previous_resolution:
return predicted_resolution
else:
return previous_resolution
else:
return predicted_resolution
def predict_enc_time(self, features, resolution, bitrate):
vector = []
vector.extend(features)
vector.append(resolution / 2160)
test_vector = [vector]
min_model = self.models_enc_time['minimum']
max_model = self.models_enc_time['maximum']
cur_enc_time_10 = min_model.predict(test_vector)[0]
cur_enc_time_50 = max_model.predict(test_vector)[0]
x1 = 10
x2 = 50
x = self.predict_qp(features, resolution, bitrate)
m = (cur_enc_time_50 - cur_enc_time_10) / (x2 - x1)
cur_enc_time = float(cur_enc_time_50 + m * (x - x2))
return 2 ** cur_enc_time
def predict_xpsnr(self, features, resolution, bitrate):
vector = []
vector.extend(features)
vector.append(resolution / 2160)
vector.append(bitrate)
test_vector = [vector]
model = self.models_xpsnr['single']
predictions = model.predict(test_vector)
return predictions[0]
def predict_qp(self, features, resolution, bitrate):
vector = []
vector.extend(features)
vector.append(resolution / 2160)
test_vector = [vector]
min_model = self.models_qp['minimum']
max_model = self.models_qp['maximum']
b1 = min_model.predict(test_vector)[0]
b2 = max_model.predict(test_vector)[0]
x1 = 10
x2 = 50
y = math.log2(bitrate)
m = (b2 - b1) / (x2 - x1)
b = b1 - m * x1
qp_pred = int((y - b) / m)
if qp_pred > 50:
qp_pred = 50
elif qp_pred < 10:
qp_pred = 10
return int(qp_pred)
def jnd_elimination(self, jnd_feature_list):
if self.jnd == 0:
return jnd_feature_list
else:
bitrate_list_len = len(self.bitrates_list)
representations = [jnd_feature_list[0]]
prev_index = 0
if jnd_feature_list[0][13] > self.max_xpsnr:
return representations
index = 1
while index < bitrate_list_len:
if (jnd_feature_list[index][13] - jnd_feature_list[prev_index][13]) >= self.jnd:
representations.append(jnd_feature_list[index])
prev_index = index
if jnd_feature_list[index][13] >= self.max_xpsnr:
return representations
index = index + 1
return representations
def generate_ladder(self):
new_features_list = []
condition1 = self.df_consolidated['Train'] == 0
df_test = self.df_consolidated[condition1]
video_names = df_test["VideoName"].unique().tolist()
for video_name in video_names:
jnd_feature_list = []
filter_condition = df_test["VideoName"] == video_name
filtered_df = df_test[filter_condition]
xpsnr_features_list = filtered_df[
["AvgE", "Avgh", "AvgL", "avgU", "avgV", "energyU", "energyV"]].values.tolist()
qp_or_time_features_list = filtered_df[
["AvgE", "Avgh", "AvgL", "avgU", "avgV", "energyU", "energyV"]
].values.tolist()
previous_resolution = 360
for bitrate in self.bitrates_list:
final_parameters = []
resolution_qp_xpsnr_time_list = self.get_resolution_and_qp(
xpsnr_features_list[0],
qp_or_time_features_list[0],
bitrate,
self.actual_max_enc_time,
previous_resolution,
)
previous_resolution = resolution_qp_xpsnr_time_list[0]
predicted_resolution = resolution_qp_xpsnr_time_list[0]
predicted_qp = resolution_qp_xpsnr_time_list[1]
predicted_xpsnr = resolution_qp_xpsnr_time_list[2]
final_parameters.append(video_name)
final_parameters.extend(qp_or_time_features_list[0])
final_parameters.append(bitrate)
final_parameters.append(self.max_enc_time)
final_parameters.append(self.max_dec_time)
final_parameters.append(predicted_resolution)
final_parameters.append(predicted_qp)
final_parameters.append(predicted_xpsnr)
jnd_feature_list.append(final_parameters)
representations = self.jnd_elimination(jnd_feature_list)
new_features_list.extend(representations)
final_csv_df = pd.DataFrame(
new_features_list,
columns=[
"VideoName",
"E_Y",
"h",
"L_Y",
"L_U",
"E_U",
"L_V",
"E_V",
"targetBitrate",
"timeLimitEnc",
"timeLimitDec",
"resolution",
"qp",
"xpsnr"
],
)
final_csv_df = final_csv_df.drop(columns=['xpsnr'])
# Write the DataFrame to a CSV file
final_csv_df.to_csv(self.result_csv, index=False)