This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
visual_metrics.py
456 lines (379 loc) · 15.7 KB
/
visual_metrics.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/usr/bin/python
# Copyright 2014 Google.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Converts video encoding result data from text files to visualization
data source."""
__author__ = "jzern@google.com (James Zern),"
__author__ += "jimbankoski@google.com (Jim Bankoski)"
__author__ += "hta@gogle.com (Harald Alvestrand)"
import encoder
import gviz_api
import math
import mpeg_settings
import numpy
import optimizer
import re
import string
import pick_codec
def bdsnr(metric_set1, metric_set2):
"""
BJONTEGAARD Bjontegaard metric calculation
Bjontegaard's metric allows to compute the average gain in psnr between two
rate-distortion curves [1].
rate1,psnr1 - RD points for curve 1
rate2,psnr2 - RD points for curve 2
returns the calculated Bjontegaard metric 'dsnr'
code adapted from code written by : (c) 2010 Giuseppe Valenzise
http://www.mathworks.com/matlabcentral/fileexchange/27798-bjontegaard-metric/content/bjontegaard.m
"""
# pylint: disable=too-many-locals
# numpy seems to do tricks with its exports.
# pylint: disable=no-member
# map() is recommended against.
# pylint: disable=bad-builtin
rate1 = [x[0] for x in metric_set1]
psnr1 = [x[1] for x in metric_set1]
rate2 = [x[0] for x in metric_set2]
psnr2 = [x[1] for x in metric_set2]
log_rate1 = map(math.log, rate1)
log_rate2 = map(math.log, rate2)
# Best cubic poly fit for graph represented by log_ratex, psrn_x.
poly1 = numpy.polyfit(log_rate1, psnr1, 3)
poly2 = numpy.polyfit(log_rate2, psnr2, 3)
# Integration interval.
min_int = max([min(log_rate1), min(log_rate2)])
max_int = min([max(log_rate1), max(log_rate2)])
# Integrate poly1, and poly2.
p_int1 = numpy.polyint(poly1)
p_int2 = numpy.polyint(poly2)
# Calculate the integrated value over the interval we care about.
int1 = numpy.polyval(p_int1, max_int) - numpy.polyval(p_int1, min_int)
int2 = numpy.polyval(p_int2, max_int) - numpy.polyval(p_int2, min_int)
# Calculate the average improvement.
if max_int != min_int:
avg_diff = (int2 - int1) / (max_int - min_int)
else:
avg_diff = 0.0
return avg_diff
def bdrate(metric_set1, metric_set2):
"""
BJONTEGAARD Bjontegaard metric calculation
Bjontegaard's metric allows to compute the average % saving in bitrate
between two rate-distortion curves [1].
rate1,psnr1 - RD points for curve 1
rate2,psnr2 - RD points for curve 2
adapted from code from: (c) 2010 Giuseppe Valenzise
"""
# numpy plays games with its exported functions.
# pylint: disable=no-member
# pylint: disable=too-many-locals
# pylint: disable=bad-builtin
rate1 = [x[0] for x in metric_set1]
psnr1 = [x[1] for x in metric_set1]
rate2 = [x[0] for x in metric_set2]
psnr2 = [x[1] for x in metric_set2]
log_rate1 = map(math.log, rate1)
log_rate2 = map(math.log, rate2)
# Best cubic poly fit for graph represented by log_ratex, psrn_x.
poly1 = numpy.polyfit(psnr1, log_rate1, 3)
poly2 = numpy.polyfit(psnr2, log_rate2, 3)
# Integration interval.
min_int = max([min(psnr1), min(psnr2)])
max_int = min([max(psnr1), max(psnr2)])
# find integral
p_int1 = numpy.polyint(poly1)
p_int2 = numpy.polyint(poly2)
# Calculate the integrated value over the interval we care about.
int1 = numpy.polyval(p_int1, max_int) - numpy.polyval(p_int1, min_int)
int2 = numpy.polyval(p_int2, max_int) - numpy.polyval(p_int2, min_int)
# Calculate the average improvement.
avg_exp_diff = (int2 - int1) / (max_int - min_int)
# In really bad formed data the exponent can grow too large.
# clamp it.
if avg_exp_diff > 200:
avg_exp_diff = 200
# Convert to a percentage.
avg_diff = (math.exp(avg_exp_diff) - 1) * 100
return avg_diff
def FillForm(string_for_substitution, dictionary_of_vars):
"""
This function substitutes all matches of the command string //%% ... %%//
with the variable represented by ... .
"""
return_string = string_for_substitution
for i in re.findall("//%%(.*)%%//", string_for_substitution):
return_string = re.sub("//%%" + i + "%%//", dictionary_of_vars[i],
return_string)
return return_string
def HasMetrics(line):
"""
The metrics files produced by vpxenc are started with a B for headers.
"""
if line[0:1] != "B" and len(string.split(line)) > 0:
return True
return False
def ParseMetricFile(file_name, metric_column):
"""
Convert a metrics file into a set of numbers.
This returns a sorted list of tuples with the first number
being from the first column (bitrate) and the second being from
metric_column (counting from 0).
"""
metric_set1 = set([])
metric_file = open(file_name, "r")
for line in metric_file:
metrics = string.split(line)
if HasMetrics(line):
if metric_column < len(metrics):
my_tuple = float(metrics[0]), float(metrics[metric_column])
else:
my_tuple = float(metrics[0]), 0
metric_set1.add(my_tuple)
metric_set1_sorted = sorted(metric_set1)
return metric_set1_sorted
def GraphBetter(metric_set1_sorted, metric_set2_sorted, use_set2_as_base):
"""
Search through the sorted metric set for metrics on either side of
the metric from file 1. Since both lists are sorted we really
should not have to search through the entire range, but these
are small lists."""
# pylint: disable=too-many-locals
total_bitrate_difference_ratio = 0.0
count = 0
# TODO(hta): Replace whole thing with a call to numpy.interp()
for bitrate, metric in metric_set1_sorted:
for i in range(len(metric_set2_sorted) - 1):
s2_bitrate_0, s2_metric_0 = metric_set2_sorted[i]
s2_bitrate_1, s2_metric_1 = metric_set2_sorted[i + 1]
# We have a point on either side of our metric range.
if s2_metric_0 < metric <= s2_metric_1:
# Calculate a slope.
if s2_metric_1 - s2_metric_0 != 0:
metric_slope = ((s2_bitrate_1 - s2_bitrate_0) /
(s2_metric_1 - s2_metric_0))
else:
metric_slope = 0
estimated_s2_bitrate = (s2_bitrate_0 + (metric - s2_metric_0) *
metric_slope)
# Calculate percentage difference as given by base.
if use_set2_as_base:
bitrate_difference_ratio = ((bitrate - estimated_s2_bitrate) /
estimated_s2_bitrate)
else:
bitrate_difference_ratio = ((bitrate - estimated_s2_bitrate) /
bitrate)
total_bitrate_difference_ratio += bitrate_difference_ratio
count += 1
break
# Calculate the average improvement between graphs.
if count != 0:
avg = total_bitrate_difference_ratio / count
else:
avg = 0.0
return avg
def DataSetBetter(metric_set1, metric_set2, method):
"""
Compares two data sets and determines which is better and by how
much.
The input metric set is sorted on bitrate.
The first set is the one to compare, the second set is the baseline.
"""
# Be fair to both graphs by testing all the points in each.
if method == 'avg':
avg_improvement = 50 * (
GraphBetter(metric_set1, metric_set2,
use_set2_as_base=True) -
GraphBetter(metric_set2, metric_set1,
use_set2_as_base=False))
elif method == 'dsnr':
avg_improvement = bdsnr(metric_set1, metric_set2)
else:
avg_improvement = bdrate(metric_set2, metric_set1)
return avg_improvement
def FileBetter(file_name_1, file_name_2, metric_column, method):
"""
Compares two data files and determines which is better and by how
much.
metric_column is the metric.
"""
# Store and parse our two files into lists of unique tuples.
# Read the two files, parsing out lines starting with bitrate.
metric_set1_sorted = ParseMetricFile(file_name_1, metric_column)
metric_set2_sorted = ParseMetricFile(file_name_2, metric_column)
return DataSetBetter(metric_set1_sorted, metric_set2_sorted, method)
def HtmlPage(page_template, page_title="", page_subtitle="",
filestable="", snrs="", formatters=""):
"""
Creates a HTML page from the template and variables passed to it.
"""
# pylint: disable=too-many-arguments
# Build up a dictionary of the variables actually used in the template.
my_dict = {
'page_title': page_title,
'page_subtitle': page_subtitle,
'filestable_dpsnr': filestable['dsnr'],
'filestable_avg': filestable['avg'],
'filestable_drate': filestable['drate'],
'snrs': snrs,
'formatters': formatters
}
return FillForm(page_template, my_dict)
def ListOneTarget(codecs, rate, videofile, do_score, datatable,
score_function=None):
"""Extend a datatable with the info about one video file's scores."""
# pylint: disable=too-many-arguments
for codec_name in codecs:
# For testing:
# Allow for direct context injection rather than picking by name.
if isinstance(codec_name, basestring):
codec = pick_codec.PickCodec(codec_name)
my_optimizer = optimizer.Optimizer(codec, score_function=score_function)
else:
my_optimizer = codec_name
codec_name = my_optimizer.context.codec.name
best_encoding = my_optimizer.BestEncoding(rate, videofile)
if do_score and not best_encoding.Result():
best_encoding.Execute()
best_encoding.Store()
AddOneEncoding(codec_name, my_optimizer, best_encoding, videofile,
datatable)
def AddOneEncoding(codec_name, my_optimizer, this_encoding, videofile,
datatable):
assert this_encoding.Result()
# Ignore results that score less than zero.
if my_optimizer.Score(this_encoding) < 0.0:
return
# Datatable is a dictionary of codec name -> result sets.
# Each result set is an array containing result info.
# Each result info is a dictionary containing the
# ID of the configuration used, the
# target bitrate, the command line, the score and the result.
(datatable.setdefault(codec_name, {})
.setdefault(videofile.basename, [])
.append({'config_id': this_encoding.encoder.Hashname(),
'target_bitrate': this_encoding.bitrate,
'encode_command': this_encoding.EncodeCommandLine(),
'score': my_optimizer.Score(this_encoding),
'result': this_encoding.ResultWithoutFrameData()}))
def ListMpegResults(codecs, do_score, datatable, score_function=None):
"""List all scores for all tests in the MPEG test set for a set of codecs."""
# It is necessary to sort on target bitrate in order for graphs to display
# correctly.
for rate, filename in sorted(mpeg_settings.MpegFiles().AllFilesAndRates()):
videofile = encoder.Videofile(filename)
ListOneTarget(codecs, rate, videofile, do_score, datatable,
score_function)
def ListMpegSingleConfigResults(codecs, datatable, score_function=None):
encoder_list = {}
optimizer_list = {}
for codec_name in codecs:
codec = pick_codec.PickCodec(codec_name)
my_optimizer = optimizer.Optimizer(codec,
score_function=score_function, file_set=mpeg_settings.MpegFiles())
optimizer_list[codec_name] = my_optimizer
encoder_list[codec_name] = my_optimizer.BestOverallEncoder()
for rate, filename in sorted(mpeg_settings.MpegFiles().AllFilesAndRates()):
videofile = encoder.Videofile(filename)
for codec_name in codecs:
if encoder_list[codec_name]:
my_encoding = encoder_list[codec_name].Encoding(rate, videofile)
my_encoding.Recover()
AddOneEncoding(codec_name, optimizer_list[codec_name],
my_encoding, videofile, datatable)
def ExtractBitrateAndPsnr(datatable, codec, filename):
dataset = [(r['result']['bitrate'], r['result']['psnr'])
for r in datatable[codec][filename]]
return dataset
def BuildComparisonTable(datatable, metric, baseline_codec, other_codecs):
"""Builds a table of comparison data for this metric."""
# Find the metric files in the baseline codec.
videofile_name_list = datatable[baseline_codec].keys()
countoverall = {}
sumoverall = {}
for this_codec in other_codecs:
countoverall[this_codec] = 0
sumoverall[this_codec] = 0
# Data holds the data for the visualization, name given comes from
# gviz_api sample code.
data = []
for filename in videofile_name_list:
row = {'file': filename}
baseline_dataset = ExtractBitrateAndPsnr(datatable,
baseline_codec,
filename)
# Read the metric file from each of the directories in our list.
for this_codec in other_codecs:
# If there is a metric in this_codec, calculate the overall difference
# between it and the baseline codec's metric.
if (this_codec in datatable and filename in datatable[this_codec]
and filename in datatable[baseline_codec]):
this_dataset = ExtractBitrateAndPsnr(datatable,
this_codec,
filename)
overall = DataSetBetter(
baseline_dataset, this_dataset, metric)
if not math.isnan(overall):
# TODO(hta): figure out when DataSetBetter generates NaN
row[this_codec] = overall
sumoverall[this_codec] += overall
countoverall[this_codec] += 1
data.append(row)
# Add the overall numbers.
row = {"file": "OVERALL " + metric}
for this_codec in other_codecs:
if countoverall[this_codec]:
row[this_codec] = sumoverall[this_codec] / countoverall[this_codec]
data.append(row)
return data
def BuildGvizDataTable(datatable, metric, baseline_codec, other_codecs):
"""Builds a Gviz DataTable giving this metric for the files and codecs."""
description = {"file": ("string", "File")}
data = BuildComparisonTable(datatable, metric, baseline_codec, other_codecs)
for this_codec in other_codecs:
description[this_codec] = ("number", this_codec)
# Generate the gViz table
gviz_data_table = gviz_api.DataTable(description)
gviz_data_table.LoadData(data)
return gviz_data_table
def CrossPerformanceGvizTable(datatable, metric, codecs, criterion):
"""Build a square table of codecs and relative performance."""
# pylint: disable=too-many-locals
videofile_name_list = datatable[codecs[0]].keys()
description = {}
description['codec'] = ('string', 'Codec')
data = []
for codec in codecs:
description[codec] = ('string', codec)
for codec1 in codecs:
lineitem = {'codec': codec1}
for codec2 in codecs:
if codec1 != codec2:
count = 0
overall = 0.0
for filename in videofile_name_list:
if (codec1 in datatable and filename in datatable[codec1]
and codec2 in datatable and filename in datatable[codec2]):
overall += DataSetBetter(
ExtractBitrateAndPsnr(datatable, codec2, filename),
ExtractBitrateAndPsnr(datatable, codec1, filename), metric)
count += 1
if count > 0:
display = ('<a href=/results/show_result.html?' +
'codec1=%s&codec2=%s&criterion=%s>%5.2f</a>') % (
codec2, codec1, criterion, overall / count)
lineitem[codec2] = (overall / count, display)
data.append(lineitem)
gviz_data_table = gviz_api.DataTable(description)
gviz_data_table.LoadData(data)
return gviz_data_table