Repository navigation
Expand file tree
/
Copy pathroutine.py
More file actions
757 lines (628 loc) · 30.6 KB
/
Copy pathroutine.py
File metadata and controls
757 lines (628 loc) · 30.6 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import numpy as np
from vmaf import plt, to_list
from vmaf.core.cross_validation import ModelCrossValidation
from vmaf.core.feature_assembler import FeatureAssembler
from vmaf.core.quality_runner import VmafQualityRunner
from vmaf.core.result_store import FileSystemResultStore
from vmaf.tools.misc import indices, get_stdout_logger, import_python_file, close_logger, get_file_name_without_extension
from vmaf.config import VmafConfig, DisplayConfig
from vmaf.core.asset import Asset
from vmaf.core.train_test_model import TrainTestModel, RegressorMixin, ClassifierMixin
from vmaf.core.local_explainer import LocalExplainer
__copyright__ = "Copyright 2016-2018, Netflix, Inc."
__license__ = "Apache, Version 2.0"
def read_dataset(dataset, **kwargs):
groundtruth_key = kwargs['groundtruth_key'] if 'groundtruth_key' in kwargs else None
skip_asset_with_none_groundtruth = kwargs['skip_asset_with_none_groundtruth'] \
if 'skip_asset_with_none_groundtruth' in kwargs else False
# asserts, can add more to the list...
assert hasattr(dataset, 'dataset_name')
assert hasattr(dataset, 'ref_videos')
assert hasattr(dataset, 'dis_videos')
assert hasattr(dataset, 'yuv_fmt') or all(['yuv_fmt' in ref_video for ref_video in dataset.ref_videos])
data_set_name = dataset.dataset_name
ref_videos = dataset.ref_videos
dis_videos = dataset.dis_videos
width = dataset.width if hasattr(dataset, 'width') else None
height = dataset.height if hasattr(dataset, 'height') else None
yuv_fmt = dataset.yuv_fmt if hasattr(dataset, 'yuv_fmt') else None
quality_width = dataset.quality_width if hasattr(dataset, 'quality_width') else None
quality_height = dataset.quality_height if hasattr(dataset, 'quality_height') else None
resampling_type = dataset.resampling_type if hasattr(dataset, 'resampling_type') else None
crop_cmd = dataset.crop_cmd if hasattr(dataset, 'crop_cmd') else None
pad_cmd = dataset.pad_cmd if hasattr(dataset, 'pad_cmd') else None
workfile_yuv_type = dataset.workfile_yuv_type if hasattr(dataset, 'workfile_yuv_type') else None
duration_sec = dataset.duration_sec if hasattr(dataset, 'duration_sec') else None
ref_dict = {} # dictionary of content_id -> path for ref videos
for ref_video in ref_videos:
ref_dict[ref_video['content_id']] = ref_video
assets = []
for dis_video in dis_videos:
if groundtruth_key is not None:
groundtruth = dis_video[groundtruth_key]
else:
if 'dmos' in dis_video:
groundtruth = dis_video['dmos']
elif 'mos' in dis_video:
groundtruth = dis_video['mos']
elif 'groundtruth' in dis_video:
groundtruth = dis_video['groundtruth']
else:
groundtruth = None
if 'os' in dis_video:
raw_groundtruth = dis_video['os']
else:
raw_groundtruth = None
if 'groundtruth_std' in dis_video:
groundtruth_std = dis_video['groundtruth_std']
else:
groundtruth_std = None
if 'fps' in dis_video:
fps = dis_video['fps']
else:
fps = None
if 'rebuf_indices' in dis_video:
rebuf_indices = dis_video['rebuf_indices']
else:
rebuf_indices = None
ref_video = ref_dict[dis_video['content_id']]
ref_path = ref_video['path']
ref_yuv_fmt_ = yuv_fmt if yuv_fmt is not None else ref_dict[dis_video['content_id']]['yuv_fmt']
dis_yuv_fmt_ = dis_video['yuv_fmt'] if 'yuv_fmt' in dis_video else ref_yuv_fmt_
if width is not None:
width_ = width
elif 'width' in ref_video:
width_ = ref_video['width'] # NOTE: width in ref_video not dis_video
else:
width_ = None
if height is not None:
height_ = height
elif 'height' in ref_video:
height_ = ref_video['height'] # NOTE: height in ref_video not dis_video
else:
height_ = None
if quality_width is not None:
quality_width_ = quality_width
elif 'quality_width' in dis_video:
quality_width_ = dis_video['quality_width']
else:
quality_width_ = None
if quality_height is not None:
quality_height_ = quality_height
elif 'quality_height' in dis_video:
quality_height_ = dis_video['quality_height']
else:
quality_height_ = None
if resampling_type is not None:
resampling_type_ = resampling_type
elif 'resampling_type' in dis_video:
resampling_type_ = dis_video['resampling_type']
else:
resampling_type_ = None
if crop_cmd is not None:
crop_cmd_ = crop_cmd
elif 'crop_cmd' in dis_video:
crop_cmd_ = dis_video['crop_cmd']
else:
crop_cmd_ = None
if pad_cmd is not None:
pad_cmd_ = pad_cmd
elif 'pad_cmd' in dis_video:
pad_cmd_ = dis_video['pad_cmd']
else:
pad_cmd_ = None
if duration_sec is not None:
duration_sec_ = duration_sec
elif 'duration_sec' in dis_video:
duration_sec_ = dis_video['duration_sec']
else:
duration_sec_ = None
asset_dict = {'ref_yuv_type': ref_yuv_fmt_, 'dis_yuv_type': dis_yuv_fmt_}
if width_ is not None:
if asset_dict['ref_yuv_type'] != 'notyuv':
asset_dict['ref_width'] = width_
if asset_dict['dis_yuv_type'] != 'notyuv':
asset_dict['dis_width'] = width_
if height_ is not None:
if asset_dict['ref_yuv_type'] != 'notyuv':
asset_dict['ref_height'] = height_
if asset_dict['dis_yuv_type'] != 'notyuv':
asset_dict['dis_height'] = height_
if groundtruth is not None:
asset_dict['groundtruth'] = groundtruth
if raw_groundtruth is not None:
asset_dict['raw_groundtruth'] = raw_groundtruth
if groundtruth_std is not None:
asset_dict['groundtruth_std'] = groundtruth_std
if quality_width_ is not None:
asset_dict['quality_width'] = quality_width_
if quality_height_ is not None:
asset_dict['quality_height'] = quality_height_
if resampling_type_ is not None:
asset_dict['resampling_type'] = resampling_type_
if crop_cmd_ is not None:
asset_dict['crop_cmd'] = crop_cmd_
if pad_cmd_ is not None:
asset_dict['pad_cmd'] = pad_cmd_
if duration_sec_ is not None:
asset_dict['duration_sec'] = duration_sec_
if workfile_yuv_type is not None:
asset_dict['workfile_yuv_type'] = workfile_yuv_type
if fps is not None:
asset_dict['fps'] = fps
if rebuf_indices is not None:
asset_dict['rebuf_indices'] = rebuf_indices
if groundtruth is None and skip_asset_with_none_groundtruth:
pass
else:
asset = Asset(dataset=data_set_name,
content_id=dis_video['content_id'],
asset_id=dis_video['asset_id'],
workdir_root=VmafConfig.workdir_path(),
ref_path=ref_path,
dis_path=dis_video['path'],
asset_dict=asset_dict,
)
assets.append(asset)
return assets
def run_test_on_dataset(test_dataset, runner_class, ax,
result_store, model_filepath,
parallelize=True, fifo_mode=True,
aggregate_method=np.mean,
type='regressor',
**kwargs):
test_assets = read_dataset(test_dataset, **kwargs)
test_raw_assets = None
try:
for test_asset in test_assets:
assert test_asset.groundtruth is not None
except AssertionError:
# no groundtruth, try do subjective modeling
from sureal.dataset_reader import RawDatasetReader
from sureal.subjective_model import DmosModel
subj_model_class = kwargs['subj_model_class'] if 'subj_model_class' in kwargs and kwargs['subj_model_class'] is not None else DmosModel
dataset_reader_class = kwargs['dataset_reader_class'] if 'dataset_reader_class' in kwargs else RawDatasetReader
subjective_model = subj_model_class(dataset_reader_class(test_dataset))
subjective_model.run_modeling(**kwargs)
test_dataset_aggregate = subjective_model.to_aggregated_dataset(**kwargs)
test_raw_assets = test_assets
test_assets = read_dataset(test_dataset_aggregate, **kwargs)
if model_filepath is not None:
optional_dict = {'model_filepath': model_filepath}
if 'model_720_filepath' in kwargs and kwargs['model_720_filepath'] is not None:
optional_dict['720model_filepath'] = kwargs['model_720_filepath']
if 'model_480_filepath' in kwargs and kwargs['model_480_filepath'] is not None:
optional_dict['480model_filepath'] = kwargs['model_480_filepath']
else:
optional_dict = None
if 'enable_transform_score' in kwargs and kwargs['enable_transform_score'] is not None:
if not optional_dict:
optional_dict = {}
optional_dict['enable_transform_score'] = kwargs['enable_transform_score']
if 'disable_clip_score' in kwargs and kwargs['disable_clip_score'] is not None:
if not optional_dict:
optional_dict = {}
optional_dict['disable_clip_score'] = kwargs['disable_clip_score']
if 'subsample' in kwargs and kwargs['subsample'] is not None:
if not optional_dict:
optional_dict = {}
optional_dict['subsample'] = kwargs['subsample']
# run
runner = runner_class(
test_assets,
None, fifo_mode=fifo_mode,
delete_workdir=True,
result_store=result_store,
optional_dict=optional_dict,
optional_dict2=None,
)
runner.run(parallelize=parallelize)
results = runner.results
for result in results:
result.set_score_aggregate_method(aggregate_method)
try:
model_type = runner.get_train_test_model_class()
except:
if type == 'regressor':
model_type = RegressorMixin
elif type == 'classifier':
model_type = ClassifierMixin
else:
assert False
# plot
groundtruths = to_list(map(lambda asset: asset.groundtruth, test_assets))
predictions = to_list(map(lambda result: result[runner_class.get_score_key()], results))
raw_grountruths = None if test_raw_assets is None else \
to_list(map(lambda asset: asset.raw_groundtruth, test_raw_assets))
groundtruths_std = None if test_assets is None else \
to_list(map(lambda asset: asset.groundtruth_std, test_assets))
try:
predictions_bagging = to_list(map(lambda result: result[runner_class.get_bagging_score_key()], results))
predictions_stddev = to_list(map(lambda result: result[runner_class.get_stddev_score_key()], results))
predictions_ci95_low = to_list(map(lambda result: result[runner_class.get_ci95_low_score_key()], results))
predictions_ci95_high = to_list(map(lambda result: result[runner_class.get_ci95_high_score_key()], results))
predictions_all_models = to_list(map(lambda result: result[runner_class.get_all_models_score_key()], results))
# need to revert the list of lists, so that the outer list has the predictions for each model separately
predictions_all_models = np.array(predictions_all_models).T.tolist()
num_models = np.shape(predictions_all_models)[0]
stats = model_type.get_stats(groundtruths, predictions,
ys_label_raw=raw_grountruths,
ys_label_pred_bagging=predictions_bagging,
ys_label_pred_stddev=predictions_stddev,
ys_label_pred_ci95_low=predictions_ci95_low,
ys_label_pred_ci95_high=predictions_ci95_high,
ys_label_pred_all_models=predictions_all_models,
ys_label_stddev=groundtruths_std)
except Exception as e:
print('Stats calculation failed, using default stats calculation. Error cause: ')
print(e)
stats = model_type.get_stats(groundtruths, predictions,
ys_label_raw=raw_grountruths,
ys_label_stddev=groundtruths_std)
num_models = 1
print('Stats on testing data: {}'.format(model_type.format_stats_for_print(stats)))
# printing stats if multiple models are present
if 'SRCC_across_model_distribution' in stats \
and 'PCC_across_model_distribution' in stats \
and 'RMSE_across_model_distribution' in stats:
print('Stats on testing data (across multiple models, using all test indices): {}'.format(
model_type.format_across_model_stats_for_print(model_type.extract_across_model_stats(stats))))
if ax is not None:
content_ids = to_list(map(lambda asset: asset.content_id, test_assets))
if 'point_label' in kwargs:
if kwargs['point_label'] == 'asset_id':
point_labels = to_list(map(lambda asset: asset.asset_id, test_assets))
elif kwargs['point_label'] == 'dis_path':
point_labels = to_list(map(lambda asset: get_file_name_without_extension(asset.dis_path), test_assets))
else:
raise AssertionError("Unknown point_label {}".format(kwargs['point_label']))
else:
point_labels = None
model_type.plot_scatter(ax, stats, content_ids=content_ids, point_labels=point_labels, **kwargs)
ax.set_xlabel('True Score')
ax.set_ylabel("Predicted Score")
ax.grid()
ax.set_title("{runner}{num_models}\n{stats}".format(
dataset=test_assets[0].dataset,
runner=runner_class.TYPE,
stats=model_type.format_stats_for_plot(stats),
num_models=", {} models".format(num_models) if num_models > 1 else "",
))
return test_assets, results
def print_matplotlib_warning():
print("Warning: cannot import matplotlib, no picture displayed. " \
"If you are on Mac OS and have installed matplotlib, you " \
"possibly need to run: \nsudo pip uninstall python-dateutil \n" \
"sudo pip install python-dateutil==2.2 \n" \
"Refer to: http://stackoverflow.com/questions/27630114/matplotlib-issue-on-os-x-importerror-cannot-import-name-thread")
def train_test_vmaf_on_dataset(train_dataset, test_dataset,
feature_param, model_param,
train_ax, test_ax, result_store,
parallelize=True, logger=None, fifo_mode=True,
output_model_filepath=None,
aggregate_method=np.mean,
**kwargs):
train_assets = read_dataset(train_dataset, **kwargs)
train_raw_assets = None
try:
for train_asset in train_assets:
assert train_asset.groundtruth is not None
except AssertionError:
# no groundtruth, try do subjective modeling
from sureal.dataset_reader import RawDatasetReader
from sureal.subjective_model import DmosModel
subj_model_class = kwargs['subj_model_class'] if 'subj_model_class' in kwargs and kwargs['subj_model_class'] is not None else DmosModel
dataset_reader_class = kwargs['dataset_reader_class'] if 'dataset_reader_class' in kwargs else RawDatasetReader
subjective_model = subj_model_class(dataset_reader_class(train_dataset))
subjective_model.run_modeling(**kwargs)
train_dataset_aggregate = subjective_model.to_aggregated_dataset(**kwargs)
train_raw_assets = train_assets
train_assets = read_dataset(train_dataset_aggregate, **kwargs)
train_fassembler = FeatureAssembler(
feature_dict=feature_param.feature_dict,
feature_option_dict=None,
assets=train_assets,
logger=logger,
fifo_mode=fifo_mode,
delete_workdir=True,
result_store=result_store,
optional_dict=None,
optional_dict2=None,
parallelize=parallelize,
)
train_fassembler.run()
train_features = train_fassembler.results
for result in train_features:
result.set_score_aggregate_method(aggregate_method)
model_type = model_param.model_type
model_param_dict = model_param.model_param_dict
model_class = TrainTestModel.find_subclass(model_type)
train_xys = model_class.get_xys_from_results(train_features)
train_xs = model_class.get_xs_from_results(train_features)
train_ys = model_class.get_ys_from_results(train_features)
model = model_class(model_param_dict, logger)
model.train(train_xys, **kwargs)
# append additional information to model before saving, so that
# VmafQualityRunner can read and process
model.append_info('feature_dict', feature_param.feature_dict)
if 'score_clip' in model_param_dict:
VmafQualityRunner.set_clip_score(model, model_param_dict['score_clip'])
if 'score_transform' in model_param_dict:
VmafQualityRunner.set_transform_score(model, model_param_dict['score_transform'])
train_ys_pred = VmafQualityRunner.predict_with_model(model, train_xs, **kwargs)['ys_pred']
raw_groundtruths = None if train_raw_assets is None else \
to_list(map(lambda asset: asset.raw_groundtruth, train_raw_assets))
train_stats = model.get_stats(train_ys['label'], train_ys_pred, ys_label_raw=raw_groundtruths)
log = 'Stats on training data: {}'.format(model.format_stats_for_print(train_stats))
if logger:
logger.info(log)
else:
print(log)
# save model
if output_model_filepath is not None:
model.to_file(output_model_filepath)
if train_ax is not None:
train_content_ids = to_list(map(lambda asset: asset.content_id, train_assets))
model_class.plot_scatter(train_ax, train_stats, content_ids=train_content_ids)
train_ax.set_xlabel('True Score')
train_ax.set_ylabel("Predicted Score")
train_ax.grid()
train_ax.set_title("Dataset: {dataset}, Model: {model}\n{stats}".format(
dataset=train_dataset.dataset_name,
model=model.model_id,
stats=model_class.format_stats_for_plot(train_stats)
))
# === test model on test dataset ===
if test_dataset is None:
test_assets = None
test_stats = None
test_fassembler = None
else:
test_assets = read_dataset(test_dataset, **kwargs)
test_raw_assets = None
try:
for test_asset in test_assets:
assert test_asset.groundtruth is not None
except AssertionError:
# no groundtruth, try do subjective modeling
from sureal.dataset_reader import RawDatasetReader
from sureal.subjective_model import DmosModel
subj_model_class = kwargs['subj_model_class'] if 'subj_model_class' in kwargs and kwargs['subj_model_class'] is not None else DmosModel
dataset_reader_class = kwargs['dataset_reader_class'] if 'dataset_reader_class' in kwargs else RawDatasetReader
subjective_model = subj_model_class(dataset_reader_class(test_dataset))
subjective_model.run_modeling(**kwargs)
test_dataset_aggregate = subjective_model.to_aggregated_dataset(**kwargs)
test_raw_assets = test_assets
test_assets = read_dataset(test_dataset_aggregate, **kwargs)
test_fassembler = FeatureAssembler(
feature_dict=feature_param.feature_dict,
feature_option_dict=None,
assets=test_assets,
logger=logger,
fifo_mode=fifo_mode,
delete_workdir=True,
result_store=result_store,
optional_dict=None,
optional_dict2=None,
parallelize=True,
)
test_fassembler.run()
test_features = test_fassembler.results
for result in test_features:
result.set_score_aggregate_method(aggregate_method)
test_xs = model_class.get_xs_from_results(test_features)
test_ys = model_class.get_ys_from_results(test_features)
test_ys_pred = VmafQualityRunner.predict_with_model(model, test_xs, **kwargs)['ys_pred']
raw_groundtruths = None if test_raw_assets is None else \
to_list(map(lambda asset: asset.raw_groundtruth, test_raw_assets))
test_stats = model.get_stats(test_ys['label'], test_ys_pred, ys_label_raw=raw_groundtruths)
log = 'Stats on testing data: {}'.format(model_class.format_stats_for_print(test_stats))
if logger:
logger.info(log)
else:
print(log)
if test_ax is not None:
test_content_ids = to_list(map(lambda asset: asset.content_id, test_assets))
model_class.plot_scatter(test_ax, test_stats, content_ids=test_content_ids)
test_ax.set_xlabel('True Score')
test_ax.set_ylabel("Predicted Score")
test_ax.grid()
test_ax.set_title("Dataset: {dataset}, Model: {model}\n{stats}".format(
dataset=test_dataset.dataset_name,
model=model.model_id,
stats=model_class.format_stats_for_plot(test_stats)
))
return train_fassembler, train_assets, train_stats, test_fassembler, test_assets, test_stats, model
def construct_kfold_list(assets, contentid_groups):
# construct cross validation kfold input list
content_ids = to_list(map(lambda asset: asset.content_id, assets))
kfold = []
for curr_content_group in contentid_groups:
curr_indices = indices(content_ids, lambda x: x in curr_content_group)
kfold.append(curr_indices)
return kfold
def cv_on_dataset(dataset, feature_param, model_param, ax, result_store,
contentid_groups, logger=None, aggregate_method=np.mean):
assets = read_dataset(dataset)
kfold = construct_kfold_list(assets, contentid_groups)
fassembler = FeatureAssembler(
feature_dict=feature_param.feature_dict,
feature_option_dict=None,
assets=assets,
logger=logger,
delete_workdir=True,
result_store=result_store,
optional_dict=None,
optional_dict2=None,
parallelize=True, fifo_mode=True,
# parallelize=False, fifo_mode=False, # VQM
)
fassembler.run()
results = fassembler.results
for result in results:
result.set_score_aggregate_method(aggregate_method)
model_class = TrainTestModel.find_subclass(model_param.model_type)
# run nested kfold cv for each combintation
cv_output = ModelCrossValidation.run_kfold_cross_validation(
model_class,
model_param.model_param_dict,
results,
kfold,
logger=logger,
)
print('Feature parameters: {}'.format(feature_param.feature_dict))
print('Model type: {}'.format(model_param.model_type))
print('Model parameters: {}'.format(model_param.model_param_dict))
print('Stats: {}'.format(model_class.format_stats_for_print(cv_output['aggr_stats'])))
if ax is not None:
model_class.plot_scatter(ax, cv_output['aggr_stats'], cv_output['contentids'])
ax.set_xlabel('True Score')
ax.set_ylabel("Predicted Score")
ax.grid()
ax.set_title("Dataset: {dataset}, Model: {model},\n{stats}".format(
dataset=dataset.dataset_name,
model=model_param.model_type,
stats=model_class.format_stats_for_plot(cv_output['aggr_stats'])
))
return assets, cv_output
def run_remove_results_for_dataset(result_store, dataset, executor_class):
assets = read_dataset(dataset)
executor = executor_class(assets=assets, logger=None, result_store=result_store)
executor.remove_results()
def run_vmaf_cv(train_dataset_filepath,
test_dataset_filepath,
param_filepath,
output_model_filepath=None,
**kwargs):
logger = get_stdout_logger()
result_store = FileSystemResultStore()
train_dataset = import_python_file(train_dataset_filepath)
test_dataset = import_python_file(test_dataset_filepath) if test_dataset_filepath is not None else None
param = import_python_file(param_filepath)
# === plot scatter ===
nrows = 1
ncols = 2
fig, axs = plt.subplots(figsize=(5*ncols, 5*nrows), nrows=nrows, ncols=ncols)
train_test_vmaf_on_dataset(train_dataset, test_dataset, param, param, axs[0], axs[1],
result_store, parallelize=True, logger=None,
output_model_filepath=output_model_filepath,
**kwargs)
if 'xlim' in kwargs:
axs[0].set_xlim(kwargs['xlim'])
axs[1].set_xlim(kwargs['xlim'])
if 'ylim' in kwargs:
axs[0].set_ylim(kwargs['ylim'])
axs[1].set_ylim(kwargs['ylim'])
bbox = {'facecolor':'white', 'alpha':1, 'pad':20}
axs[0].annotate('Training Set', xy=(0.1, 0.85), xycoords='axes fraction', bbox=bbox)
axs[1].annotate('Testing Set', xy=(0.1, 0.85), xycoords='axes fraction', bbox=bbox)
plt.tight_layout()
# === clean up ===
close_logger(logger)
def run_vmaf_kfold_cv(dataset_filepath,
contentid_groups,
param_filepath,
aggregate_method,
):
logger = get_stdout_logger()
result_store = FileSystemResultStore()
dataset = import_python_file(dataset_filepath)
param = import_python_file(param_filepath)
fig, ax = plt.subplots(figsize=(5, 5), nrows=1, ncols=1)
cv_on_dataset(dataset, param, param, ax, result_store, contentid_groups,
logger, aggregate_method)
ax.set_xlim([0, 120])
ax.set_ylim([0, 120])
plt.tight_layout()
# === clean up ===
close_logger(logger)
def explain_model_on_dataset(model, test_assets_selected_indexs,
test_dataset_filepath):
def print_assets(test_assets):
print('\n'.join(map(
lambda tasset: "Asset {i}: {name}".format(
i=tasset[0], name=get_file_name_without_extension(tasset[1].dis_path)),
enumerate(test_assets)
)))
test_dataset = import_python_file(test_dataset_filepath)
test_assets = read_dataset(test_dataset)
print_assets(test_assets)
print("Assets selected for local explanation: {}".format(
test_assets_selected_indexs))
result_store = FileSystemResultStore()
test_assets = [test_assets[i] for i in test_assets_selected_indexs]
test_fassembler = FeatureAssembler(
feature_dict=model.model_dict['feature_dict'],
feature_option_dict=None,
assets=test_assets,
logger=None,
fifo_mode=True,
delete_workdir=True,
result_store=result_store,
optional_dict=None,
optional_dict2=None,
parallelize=True,
)
test_fassembler.run()
test_feature_results = test_fassembler.results
test_xs = model.get_xs_from_results(test_feature_results)
test_ys = model.get_ys_from_results(test_feature_results)
test_ys_pred = model.predict(test_xs)['ys_label_pred']
explainer = LocalExplainer(neighbor_samples=1000)
test_exps = explainer.explain(model, test_xs)
explainer.print_explanations(test_exps, assets=test_assets, ys=test_ys, ys_pred=test_ys_pred)
explainer.plot_explanations(test_exps, assets=test_assets, ys=test_ys, ys_pred=test_ys_pred)
DisplayConfig.show()
def generate_dataset_from_raw(raw_dataset_filepath, output_dataset_filepath, **kwargs):
if raw_dataset_filepath:
from sureal.subjective_model import DmosModel
subj_model_class = kwargs['subj_model_class'] if 'subj_model_class' in kwargs else DmosModel
content_ids = kwargs['content_ids'] if 'content_ids' in kwargs else None
asset_ids = kwargs['asset_ids'] if 'asset_ids' in kwargs else None
subjective_model = subj_model_class.from_dataset_file(raw_dataset_filepath,
content_ids=content_ids,
asset_ids=asset_ids)
subjective_model.run_modeling(**kwargs)
subjective_model.to_aggregated_dataset_file(output_dataset_filepath, **kwargs)
def run_vmaf_cv_from_raw(train_dataset_raw_filepath, test_dataset_raw_filepath,
param_filepath, output_model_filepath, **kwargs):
if 'train_quality_wh' in kwargs and kwargs['train_quality_wh'] is not None:
train_quality_width, train_quality_height = kwargs['train_quality_wh']
else:
train_quality_width = None
train_quality_height = None
if 'test_quality_wh' in kwargs and kwargs['test_quality_wh'] is not None:
test_quality_width, test_quality_height = kwargs['test_quality_wh']
else:
test_quality_width = None
test_quality_height = None
if 'train_transform_final' in kwargs and kwargs['train_transform_final'] is not None:
train_transform_final = kwargs['train_transform_final']
else:
train_transform_final = None
if 'test_transform_final' in kwargs and kwargs['test_transform_final'] is not None:
test_transform_final = kwargs['test_transform_final']
else:
test_transform_final = None
train_output_dataset_filepath = VmafConfig.workspace_path('dataset', 'train_dataset.py')
generate_dataset_from_raw(raw_dataset_filepath=train_dataset_raw_filepath,
output_dataset_filepath=train_output_dataset_filepath,
quality_width=train_quality_width,
quality_height=train_quality_height,
transform_final=train_transform_final,
**kwargs)
test_output_dataset_filepath = VmafConfig.workspace_path('dataset', 'test_dataset.py') \
if test_dataset_raw_filepath is not None else None
generate_dataset_from_raw(raw_dataset_filepath=test_dataset_raw_filepath,
output_dataset_filepath=test_output_dataset_filepath,
quality_width=test_quality_width,
quality_height=test_quality_height,
transform_final=test_transform_final,
**kwargs)
run_vmaf_cv(
train_dataset_filepath=train_output_dataset_filepath,
test_dataset_filepath=test_output_dataset_filepath,
param_filepath=param_filepath,
output_model_filepath=output_model_filepath,
**kwargs
)