Repository navigation
Expand file tree
/
Copy pathvmaf.cpp
More file actions
1211 lines (1071 loc) · 49.5 KB
/
Copy pathvmaf.cpp
File metadata and controls
1211 lines (1071 loc) · 49.5 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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Copyright 2016-2019 Netflix, Inc.
*
* 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.
*
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <numeric>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <cmath>
#include <iomanip>
#include "libvmaf.h"
#include "vmaf.h"
#include "combo.h"
#include "pugixml/pugixml.hpp"
#include "timer.h"
#include "jsonprint.h"
#include "debug.h"
#define VAL_EQUAL_STR(V,S) (Stringize((V)).compare((S))==0)
#define VAL_IS_LIST(V) ((V).tag=='n') /* check ocval.cc */
#define VAL_IS_NONE(V) ((V).tag=='Z') /* check ocval.cc */
#define VAL_IS_DICT(V) ((V).tag=='t') /* check ocval.cc */
//#define MIN(A,B) ((A)<(B)?(A):(B))
#if !defined(min)
template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
#endif
inline double _round_to_digit(double val, int digit);
std::string _get_file_name(const std::string& s);
void SvmDelete::operator()(void *svm)
{
svm_free_and_destroy_model((svm_model **)&svm);
}
void LibsvmNusvrTrainTestModel::_assert_model_type(Val model_type) {
if (!VAL_EQUAL_STR(model_type, "'LIBSVMNUSVR'")) {
printf("Expect model type LIBSVMNUSVR, "
"but got %s\n", Stringize(model_type).c_str());
throw VmafException("Incompatible model_type");
}
}
void LibsvmNusvrTrainTestModel::_read_and_assert_model(const char *model_path, Val& feature_names,
Val& norm_type, Val& slopes, Val& intercepts, Val& score_clip, Val& score_transform)
{
/* TrainTestModel._assert_trained() in Python:
* assert 'model_type' in self.model_dict # need this to recover class
assert 'feature_names' in self.model_dict
assert 'norm_type' in self.model_dict
assert 'model' in self.model_dict
norm_type = self.model_dict['norm_type']
assert norm_type == 'none' or norm_type == 'linear_rescale'
if norm_type == 'linear_rescale':
assert 'slopes' in self.model_dict
assert 'intercepts' in self.model_dict
*/
Val model, model_type;
char errmsg[1024];
try
{
LoadValFromFile(model_path, model, SERIALIZE_P0);
model_type = model["model_dict"]["model_type"];
feature_names = model["model_dict"]["feature_names"];
norm_type = model["model_dict"]["norm_type"];
slopes = model["model_dict"]["slopes"];
intercepts = model["model_dict"]["intercepts"];
score_clip = model["model_dict"]["score_clip"];
score_transform = model["model_dict"]["score_transform"];
}
catch (std::runtime_error& e)
{
printf("Input model at %s cannot be read successfully.\n", model_path);
sprintf(errmsg, "Error loading model (.pkl): %s", e.what());
throw VmafException(errmsg);
}
_assert_model_type(model_type);
if (!VAL_IS_LIST(feature_names))
{
printf("feature_names in model must be a list.\n");
throw VmafException("Incompatible feature_names");
}
if (!(VAL_EQUAL_STR(norm_type, "'none'") ||
VAL_EQUAL_STR(norm_type, "'linear_rescale'")
)
)
{
printf("norm_type in model must be either 'none' or 'linear_rescale'.\n");
throw VmafException("Incompatible norm_type");
}
if ( VAL_EQUAL_STR(norm_type, "'linear_rescale'") &&
(!VAL_IS_LIST(slopes) || !VAL_IS_LIST(intercepts))
)
{
printf("if norm_type in model is 'linear_rescale', "
"both slopes and intercepts must be a list.\n");
throw VmafException("Incompatible slopes or intercepts");
}
if (!(VAL_IS_NONE(score_clip) || VAL_IS_LIST(score_clip)))
{
printf("score_clip in model must be either None or list.\n");
throw VmafException("Incompatible score_clip");
}
if (!(VAL_IS_NONE(score_transform) || VAL_IS_DICT(score_transform)))
{
printf("score_transform in model must be either None or dictionary (table).\n");
throw VmafException("Incompatible score_transform");
}
}
void LibsvmNusvrTrainTestModel::load_model()
{
dbg_printf("Read input model (pkl) at %s ...\n", model_path);
_read_and_assert_model(model_path, feature_names, norm_type, slopes, intercepts, score_clip, score_transform);
/* follow the convention that if model_path is a/b.c, the libsvm_model_path is always a/b.c.model */
std::string libsvm_model_path_ = std::string(model_path) + std::string(".model");
const char *libsvm_model_path = libsvm_model_path_.c_str();
dbg_printf("Read input model (libsvm) at %s ...\n", libsvm_model_path);
svm_model_ptr = _read_and_assert_svm_model(libsvm_model_path);
}
std::unique_ptr<svm_model, SvmDelete> LibsvmNusvrTrainTestModel::_read_and_assert_svm_model(const char* libsvm_model_path)
{
std::unique_ptr<svm_model, SvmDelete> svm_model_ptr { svm_load_model(libsvm_model_path) };
if (!svm_model_ptr) {
printf("Error loading SVM model.\n");
throw VmafException("Error loading SVM model");
}
return svm_model_ptr;
}
void LibsvmNusvrTrainTestModel::populate_and_normalize_nodes_at_frm(size_t i_frm,
svm_node*& nodes, StatVector& adm2,
StatVector& adm_scale0, StatVector& adm_scale1,
StatVector& adm_scale2, StatVector& adm_scale3, StatVector& motion,
StatVector& vif_scale0, StatVector& vif_scale1, StatVector& vif_scale2,
StatVector& vif_scale3, StatVector& vif, StatVector& motion2) {
if (VAL_EQUAL_STR(norm_type, "'linear_rescale'")) {
for (size_t j = 0; j < feature_names.length(); j++) {
nodes[j].index = j + 1;
if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm2_score'") == 0)
nodes[j].value = double(slopes[j + 1]) * adm2.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale0_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* adm_scale0.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale1_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* adm_scale1.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale2_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* adm_scale2.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale3_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* adm_scale3.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_motion_score'") == 0)
nodes[j].value = double(slopes[j + 1]) * motion.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale0_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* vif_scale0.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale1_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* vif_scale1.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale2_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* vif_scale2.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale3_score'") == 0)
nodes[j].value = double(slopes[j + 1])
* vif_scale3.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_score'") == 0)
nodes[j].value = double(slopes[j + 1]) * vif.at(i_frm)
+ double(intercepts[j + 1]);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_motion2_score'") == 0)
nodes[j].value = double(slopes[j + 1]) * motion2.at(i_frm)
+ double(intercepts[j + 1]);
else {
printf("Unknown feature name: %s.\n",
Stringize(feature_names[j]).c_str());
throw VmafException("Unknown feature name");
}
}
} else {
for (size_t j = 0; j < feature_names.length(); j++) {
nodes[j].index = j + 1;
if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm2_score'") == 0)
nodes[j].value = adm2.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale0_score'") == 0)
nodes[j].value = adm_scale0.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale1_score'") == 0)
nodes[j].value = adm_scale1.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale2_score'") == 0)
nodes[j].value = adm_scale2.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_adm_scale3_score'") == 0)
nodes[j].value = adm_scale3.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_motion_score'") == 0)
nodes[j].value = motion.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale0_score'") == 0)
nodes[j].value = vif_scale0.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale1_score'") == 0)
nodes[j].value = vif_scale1.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale2_score'") == 0)
nodes[j].value = vif_scale2.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_scale3_score'") == 0)
nodes[j].value = vif_scale3.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_vif_score'") == 0)
nodes[j].value = vif.at(i_frm);
else if (strcmp(Stringize(feature_names[j]).c_str(),
"'VMAF_feature_motion2_score'") == 0)
nodes[j].value = motion2.at(i_frm);
else {
printf("Unknown feature name: %s.\n",
Stringize(feature_names[j]).c_str());
throw VmafException("Unknown feature name");
}
}
}
}
VmafPredictionStruct LibsvmNusvrTrainTestModel::predict(svm_node* nodes) {
double prediction = svm_predict(svm_model_ptr.get(), nodes);
VmafPredictionStruct predictionStruct;
/* denormalize score */
_denormalize_prediction(prediction);
predictionStruct.vmafPrediction[VmafPredictionReturnType::SCORE] = prediction;
return predictionStruct;
}
void LibsvmNusvrTrainTestModel::_denormalize_prediction(double& prediction) {
if (VAL_EQUAL_STR(norm_type, "'linear_rescale'")) {
/* denormalize */
prediction = (prediction - double(intercepts[0]))
/ double(slopes[0]);
} else {
;
}
}
std::string BootstrapLibsvmNusvrTrainTestModel::_get_model_i_filename(const char* model_path, int i_model)
{
if (i_model == 0) {
return std::string(model_path);
}
else {
std::stringstream ss;
ss << '.' << std::setw(4) << std::setfill('0') << i_model;
std::string s = ss.str();
std::string model_path_i = std::string(model_path) + s;
return model_path_i;
}
}
void BootstrapLibsvmNusvrTrainTestModel::_assert_model_type(Val model_type) {
if (!VAL_EQUAL_STR(model_type, "'RESIDUEBOOTSTRAP_LIBSVMNUSVR'") &&
!VAL_EQUAL_STR(model_type, "'BOOTSTRAP_LIBSVMNUSVR'")) {
printf("Expect model type BOOTSTRAP_LIBSVMNUSVR or "
"RESIDUEBOOTSTRAP_LIBSVMNUSVR, but got %s\n", Stringize(model_type).c_str());
throw VmafException("Incompatible model_type");
}
}
void BootstrapLibsvmNusvrTrainTestModel::_read_and_assert_model(const char *model_path, Val& feature_names, Val& norm_type, Val& slopes,
Val& intercepts, Val& score_clip, Val& score_transform, int& numModels)
{
LibsvmNusvrTrainTestModel::_read_and_assert_model(model_path, feature_names, norm_type, slopes, intercepts, score_clip, score_transform);
Val model, model_type, numModelsVal;
char errmsg[1024];
try
{
LoadValFromFile(model_path, model, SERIALIZE_P0);
numModelsVal = model["param_dict"]["num_models"];
}
catch (std::runtime_error& e)
{
printf("num_models at %s cannot be read successfully.\n", model_path);
sprintf(errmsg, "Error loading model (.pkl): %s", e.what());
throw VmafException(errmsg);
}
if (VAL_IS_NONE(numModelsVal))
{
printf("num_models cannot be none.\n");
throw VmafException("num_models cannot be none.");
}
numModels = numModelsVal;
}
void BootstrapLibsvmNusvrTrainTestModel::load_model()
{
int numModels;
std::string model_path_0 = _get_model_i_filename(this->model_path, 0);
dbg_printf("Read input model (pkl) at %s ...\n", model_path_0.c_str());
_read_and_assert_model(model_path_0.c_str(), feature_names, norm_type, slopes, intercepts, score_clip, score_transform, numModels);
dbg_printf("Number of models: %d\n", numModels);
for (size_t i=0; i<numModels; i++)
{
std::string model_path_i = _get_model_i_filename(this->model_path, i);
/* follow the convention that if model_path is a/b.c, the libsvm_model_path is always a/b.c.model */
std::string libsvm_model_path_i = model_path_i + std::string(".model");
dbg_printf("Read input model (libsvm) at %s ...\n", libsvm_model_path_i.c_str());
if (i == 0)
{
svm_model_ptr = _read_and_assert_svm_model(libsvm_model_path_i.c_str());
}
else
{
bootstrap_svm_model_ptrs.push_back(_read_and_assert_svm_model(libsvm_model_path_i.c_str()));
}
}
}
VmafPredictionStruct BootstrapLibsvmNusvrTrainTestModel::predict(svm_node* nodes) {
VmafPredictionStruct predictionStruct = LibsvmNusvrTrainTestModel::predict(nodes);
StatVector bootstrapPredictions;
double bootstrapPrediction;
for (size_t i=0; i<bootstrap_svm_model_ptrs.size(); i++) {
bootstrapPrediction = svm_predict(bootstrap_svm_model_ptrs.at(i).get(), nodes);
_denormalize_prediction(bootstrapPrediction);
bootstrapPredictions.append(bootstrapPrediction);
}
predictionStruct.vmafPrediction[VmafPredictionReturnType::BAGGING_SCORE] = bootstrapPredictions.mean();
predictionStruct.vmafPrediction[VmafPredictionReturnType::STDDEV] = bootstrapPredictions.std();
predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_LOW] = bootstrapPredictions.percentile(2.5);
predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_HIGH] = bootstrapPredictions.percentile(97.5);
predictionStruct.vmafMultiModelPrediction = bootstrapPredictions.getVector();
return predictionStruct;
}
void VmafQualityRunner::_transform_value(LibsvmNusvrTrainTestModel& model,
double& prediction) {
if (!VAL_IS_NONE(model.score_transform)) {
double value = 0.0;
/* quadratic transform */
if (!VAL_IS_NONE(model.score_transform["p0"])) {
value += double(model.score_transform["p0"]);
}
if (!VAL_IS_NONE(model.score_transform["p1"])) {
value += double(model.score_transform["p1"]) * prediction;
}
if (!VAL_IS_NONE(model.score_transform["p2"])) {
value += double(model.score_transform["p2"]) * prediction
* prediction;
}
/* rectification */
if (!VAL_IS_NONE(model.score_transform["out_lte_in"])
&& VAL_EQUAL_STR(model.score_transform["out_lte_in"], "'true'")) {
if (value > prediction) {
value = prediction;
}
}
if (!VAL_IS_NONE(model.score_transform["out_gte_in"])
&& VAL_EQUAL_STR(model.score_transform["out_gte_in"], "'true'")) {
if (value < prediction) {
value = prediction;
}
}
prediction = value;
}
}
void VmafQualityRunner::_clip_value(LibsvmNusvrTrainTestModel& model,
double& prediction) {
if (!VAL_IS_NONE(model.score_clip)) {
if (prediction < double(model.score_clip[0])) {
prediction = double(model.score_clip[0]);
} else if (prediction > double(model.score_clip[1])) {
prediction = double(model.score_clip[1]);
}
}
}
void VmafQualityRunner::_postproc_predict(
VmafPredictionStruct& predictionStruct) {
;
}
void VmafQualityRunner::_transform_score(LibsvmNusvrTrainTestModel& model,
VmafPredictionStruct& predictionStruct) {
double& prediction = predictionStruct.vmafPrediction[VmafPredictionReturnType::SCORE];
_transform_value(model, prediction);
}
void VmafQualityRunner::_clip_score(LibsvmNusvrTrainTestModel& model,
VmafPredictionStruct& predictionStruct) {
double& prediction = predictionStruct.vmafPrediction[VmafPredictionReturnType::SCORE];
_clip_value(model, prediction);
}
void VmafQualityRunner::_postproc_transform_clip(
VmafPredictionStruct& predictionStruct) {
;
}
void VmafQualityRunner::_normalize_predict_denormalize_transform_clip(
LibsvmNusvrTrainTestModel& model, size_t num_frms,
StatVector& adm2, StatVector& adm_scale0, StatVector& adm_scale1,
StatVector& adm_scale2, StatVector& adm_scale3, StatVector& motion,
StatVector& vif_scale0, StatVector& vif_scale1, StatVector& vif_scale2,
StatVector& vif_scale3, StatVector& vif, StatVector& motion2,
bool enable_transform, bool disable_clip,
std::vector<VmafPredictionStruct>& predictionStructs) {
/* IMPORTANT: always allocate one more spot and put a -1 at the last one's
* index, so that libsvm will stop looping when seeing the -1 !!!
* see https://github.com/cjlin1/libsvm */
svm_node* nodes = (svm_node*) alloca(
sizeof(svm_node) * (model.feature_names.length() + 1));
nodes[model.feature_names.length()].index = -1;
size_t i_subsampled;
for (size_t i_frm=0; i_frm<num_frms; i_frm++) {
model.populate_and_normalize_nodes_at_frm(i_frm, nodes, adm2,
adm_scale0, adm_scale1, adm_scale2, adm_scale3, motion,
vif_scale0, vif_scale1, vif_scale2, vif_scale3, vif, motion2);
VmafPredictionStruct predictionStruct = model.predict(nodes);
_postproc_predict(predictionStruct);
if (enable_transform) {
_transform_score(model, predictionStruct);
}
if (!disable_clip) {
_clip_score(model, predictionStruct);
}
_postproc_transform_clip(predictionStruct);
dbg_printf("frame: %zu, ", i_frm);
dbg_printf("adm2: %f, ", adm2.at(i_frm));
dbg_printf("adm_scale0: %f, ", adm_scale0.at(i_frm));
dbg_printf("adm_scale1: %f, ", adm_scale1.at(i_frm));
dbg_printf("adm_scale2: %f, ", adm_scale2.at(i_frm));
dbg_printf("adm_scale3: %f, ", adm_scale3.at(i_frm));
dbg_printf("motion: %f, ", motion.at(i_frm));
dbg_printf("vif_scale0: %f, ", vif_scale0.at(i_frm));
dbg_printf("vif_scale1: %f, ", vif_scale1.at(i_frm));
dbg_printf("vif_scale2: %f, ", vif_scale2.at(i_frm));
dbg_printf("vif_scale3: %f, ", vif_scale3.at(i_frm));
dbg_printf("vif: %f, ", vif.at(i_frm));
dbg_printf("motion2: %f, ", motion2.at(i_frm));
dbg_printf("\n");
predictionStructs.push_back(predictionStruct);
}
}
std::unique_ptr<LibsvmNusvrTrainTestModel> VmafQualityRunner::_load_model(const char *model_path)
{
std::unique_ptr<LibsvmNusvrTrainTestModel> model_ptr = std::unique_ptr<LibsvmNusvrTrainTestModel>(new LibsvmNusvrTrainTestModel(model_path));
model_ptr->load_model();
return model_ptr;
}
void VmafQualityRunner::_set_prediction_result(
std::vector<VmafPredictionStruct> predictionStructs,
Result& result) {
StatVector score;
for (size_t i = 0; i < predictionStructs.size(); i++) {
score.append(predictionStructs.at(i).vmafPrediction[VmafPredictionReturnType::SCORE]);
}
result.set_scores("vmaf", score);
}
Result VmafQualityRunner::run(Asset asset, int (*read_frame)(float *ref_data, float *main_data, float *temp_data,
int stride, void *user_data), void *user_data, bool disable_clip, bool enable_transform,
bool do_psnr, bool do_ssim, bool do_ms_ssim, int n_thread, int n_subsample)
{
std::unique_ptr<LibsvmNusvrTrainTestModel> model_ptr = _load_model(model_path);
LibsvmNusvrTrainTestModel& model = *model_ptr;
dbg_printf("Initialize storage arrays...\n");
int w = asset.getWidth();
int h = asset.getHeight();
const char* fmt = asset.getFmt();
char errmsg[1024];
DArray adm_num_array, adm_den_array, adm_num_scale0_array,
adm_den_scale0_array, adm_num_scale1_array, adm_den_scale1_array,
adm_num_scale2_array, adm_den_scale2_array, adm_num_scale3_array,
adm_den_scale3_array, motion_array, motion2_array,
vif_num_scale0_array, vif_den_scale0_array, vif_num_scale1_array,
vif_den_scale1_array, vif_num_scale2_array, vif_den_scale2_array,
vif_num_scale3_array, vif_den_scale3_array, vif_array, psnr_array,
ssim_array, ms_ssim_array;
/* use the following ptrs as flags to turn on/off optional metrics */
DArray *psnr_array_ptr, *ssim_array_ptr, *ms_ssim_array_ptr;
init_array(&adm_num_array, INIT_FRAMES);
init_array(&adm_den_array, INIT_FRAMES);
init_array(&adm_num_scale0_array, INIT_FRAMES);
init_array(&adm_den_scale0_array, INIT_FRAMES);
init_array(&adm_num_scale1_array, INIT_FRAMES);
init_array(&adm_den_scale1_array, INIT_FRAMES);
init_array(&adm_num_scale2_array, INIT_FRAMES);
init_array(&adm_den_scale2_array, INIT_FRAMES);
init_array(&adm_num_scale3_array, INIT_FRAMES);
init_array(&adm_den_scale3_array, INIT_FRAMES);
init_array(&motion_array, INIT_FRAMES);
init_array(&motion2_array, INIT_FRAMES);
init_array(&vif_num_scale0_array, INIT_FRAMES);
init_array(&vif_den_scale0_array, INIT_FRAMES);
init_array(&vif_num_scale1_array, INIT_FRAMES);
init_array(&vif_den_scale1_array, INIT_FRAMES);
init_array(&vif_num_scale2_array, INIT_FRAMES);
init_array(&vif_den_scale2_array, INIT_FRAMES);
init_array(&vif_num_scale3_array, INIT_FRAMES);
init_array(&vif_den_scale3_array, INIT_FRAMES);
init_array(&vif_array, INIT_FRAMES);
init_array(&psnr_array, INIT_FRAMES);
init_array(&ssim_array, INIT_FRAMES);
init_array(&ms_ssim_array, INIT_FRAMES);
/* optional output arrays */
if (do_psnr) {
psnr_array_ptr = &psnr_array;
} else {
psnr_array_ptr = NULL;
}
if (do_ssim) {
ssim_array_ptr = &ssim_array;
} else {
ssim_array_ptr = NULL;
}
if (do_ms_ssim) {
ms_ssim_array_ptr = &ms_ssim_array;
} else {
ms_ssim_array_ptr = NULL;
}
dbg_printf("Extract atom features...\n");
int ret = combo(read_frame, user_data, w, h, fmt, &adm_num_array,
&adm_den_array, &adm_num_scale0_array, &adm_den_scale0_array,
&adm_num_scale1_array, &adm_den_scale1_array, &adm_num_scale2_array,
&adm_den_scale2_array, &adm_num_scale3_array, &adm_den_scale3_array,
&motion_array, &motion2_array, &vif_num_scale0_array,
&vif_den_scale0_array, &vif_num_scale1_array, &vif_den_scale1_array,
&vif_num_scale2_array, &vif_den_scale2_array, &vif_num_scale3_array,
&vif_den_scale3_array, &vif_array, psnr_array_ptr, ssim_array_ptr,
ms_ssim_array_ptr, errmsg, n_thread, n_subsample);
if (ret) {
throw VmafException(errmsg);
}
size_t num_frms = motion_array.used;
bool num_frms_is_consistent = (adm_num_array.used == num_frms)
&& (adm_den_array.used == num_frms)
&& (motion2_array.used == num_frms)
&& (adm_num_scale0_array.used == num_frms)
&& (adm_den_scale0_array.used == num_frms)
&& (adm_num_scale1_array.used == num_frms)
&& (adm_den_scale1_array.used == num_frms)
&& (adm_num_scale2_array.used == num_frms)
&& (adm_den_scale2_array.used == num_frms)
&& (adm_num_scale3_array.used == num_frms)
&& (adm_den_scale3_array.used == num_frms)
&& (vif_num_scale0_array.used == num_frms)
&& (vif_den_scale0_array.used == num_frms)
&& (vif_num_scale1_array.used == num_frms)
&& (vif_den_scale1_array.used == num_frms)
&& (vif_num_scale2_array.used == num_frms)
&& (vif_den_scale2_array.used == num_frms)
&& (vif_num_scale3_array.used == num_frms)
&& (vif_den_scale3_array.used == num_frms)
&& (vif_array.used == num_frms);
if (psnr_array_ptr != NULL) {
num_frms_is_consistent = num_frms_is_consistent
&& (psnr_array.used == num_frms);
}
if (ssim_array_ptr != NULL) {
num_frms_is_consistent = num_frms_is_consistent
&& (ssim_array.used == num_frms);
}
if (ms_ssim_array_ptr != NULL) {
num_frms_is_consistent = num_frms_is_consistent
&& (ms_ssim_array.used == num_frms);
}
if (!num_frms_is_consistent) {
sprintf(errmsg,
"Output feature vectors are of inconsistent dimensions: motion (%zu), motion2 (%zu), adm_num (%zu), adm_den (%zu), vif_num_scale0 (%zu), vif_den_scale0 (%zu), vif_num_scale1 (%zu), vif_den_scale1 (%zu), vif_num_scale2 (%zu), vif_den_scale2 (%zu), vif_num_scale3 (%zu), vif_den_scale3 (%zu), vif (%zu), psnr (%zu), ssim (%zu), ms_ssim (%zu), adm_num_scale0 (%zu), adm_den_scale0 (%zu), adm_num_scale1 (%zu), adm_den_scale1 (%zu), adm_num_scale2 (%zu), adm_den_scale2 (%zu), adm_num_scale3 (%zu), adm_den_scale3 (%zu)",
motion_array.used, motion2_array.used, adm_num_array.used,
adm_den_array.used, vif_num_scale0_array.used,
vif_den_scale0_array.used, vif_num_scale1_array.used,
vif_den_scale1_array.used, vif_num_scale2_array.used,
vif_den_scale2_array.used, vif_num_scale3_array.used,
vif_num_scale3_array.used, vif_array.used, psnr_array.used,
ssim_array.used, ms_ssim_array.used, adm_num_scale0_array.used,
adm_den_scale0_array.used, adm_num_scale1_array.used,
adm_den_scale1_array.used, adm_num_scale2_array.used,
adm_den_scale2_array.used, adm_num_scale3_array.used,
adm_num_scale3_array.used);
throw VmafException(errmsg);
}
dbg_printf(
"Generate final features (including derived atom features)...\n");
double ADM2_CONSTANT = 0.0;
double ADM_SCALE_CONSTANT = 0.0;
StatVector adm2, motion, vif_scale0, vif_scale1, vif_scale2, vif_scale3,
vif, motion2;
StatVector adm_scale0, adm_scale1, adm_scale2, adm_scale3;
StatVector psnr, ssim, ms_ssim;
std::vector<VmafPredictionStruct> predictionStructs;
for (size_t i = 0; i < num_frms; i += n_subsample) {
adm2.append(
(get_at(&adm_num_array, i) + ADM2_CONSTANT)
/ (get_at(&adm_den_array, i) + ADM2_CONSTANT));
adm_scale0.append(
(get_at(&adm_num_scale0_array, i) + ADM_SCALE_CONSTANT)
/ (get_at(&adm_den_scale0_array, i) + ADM_SCALE_CONSTANT));
adm_scale1.append(
(get_at(&adm_num_scale1_array, i) + ADM_SCALE_CONSTANT)
/ (get_at(&adm_den_scale1_array, i) + ADM_SCALE_CONSTANT));
adm_scale2.append(
(get_at(&adm_num_scale2_array, i) + ADM_SCALE_CONSTANT)
/ (get_at(&adm_den_scale2_array, i) + ADM_SCALE_CONSTANT));
adm_scale3.append(
(get_at(&adm_num_scale3_array, i) + ADM_SCALE_CONSTANT)
/ (get_at(&adm_den_scale3_array, i) + ADM_SCALE_CONSTANT));
motion.append(get_at(&motion_array, i));
motion2.append(get_at(&motion2_array, i));
vif_scale0.append(
get_at(&vif_num_scale0_array, i)
/ get_at(&vif_den_scale0_array, i));
vif_scale1.append(
get_at(&vif_num_scale1_array, i)
/ get_at(&vif_den_scale1_array, i));
vif_scale2.append(
get_at(&vif_num_scale2_array, i)
/ get_at(&vif_den_scale2_array, i));
vif_scale3.append(
get_at(&vif_num_scale3_array, i)
/ get_at(&vif_den_scale3_array, i));
vif.append(get_at(&vif_array, i));
if (psnr_array_ptr != NULL) {
psnr.append(get_at(&psnr_array, i));
}
if (ssim_array_ptr != NULL) {
ssim.append(get_at(&ssim_array, i));
}
if (ms_ssim_array_ptr != NULL) {
ms_ssim.append(get_at(&ms_ssim_array, i));
}
}
dbg_printf(
"Normalize features, SVM regression, denormalize score, clip...\n");
size_t num_frms_subsampled = 0;
for (size_t i = 0; i < num_frms; i += n_subsample) {
num_frms_subsampled++;
}
_normalize_predict_denormalize_transform_clip(model, num_frms_subsampled,
adm2, adm_scale0, adm_scale1, adm_scale2, adm_scale3, motion,
vif_scale0, vif_scale1, vif_scale2, vif_scale3, vif, motion2,
enable_transform, disable_clip, predictionStructs);
Result result { };
result.set_scores("adm2", adm2);
result.set_scores("adm_scale0", adm_scale0);
result.set_scores("adm_scale1", adm_scale1);
result.set_scores("adm_scale2", adm_scale2);
result.set_scores("adm_scale3", adm_scale3);
result.set_scores("motion", motion);
result.set_scores("vif_scale0", vif_scale0);
result.set_scores("vif_scale1", vif_scale1);
result.set_scores("vif_scale2", vif_scale2);
result.set_scores("vif_scale3", vif_scale3);
result.set_scores("vif", vif);
result.set_scores("motion2", motion2);
for (size_t j = 0; j < model.feature_names.length(); j++) {
if ((strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_adm2_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_adm_scale0_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_adm_scale1_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_adm_scale2_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_adm_scale3_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_motion_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_motion2_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_vif_scale0_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_vif_scale1_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_vif_scale2_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_vif_scale3_score'") != 0)
&& (strcmp(Stringize(model.feature_names[j]).c_str(),
"'VMAF_feature_vif_score'") != 0)) {
printf("Unknown feature name: %s.\n", Stringize(model.feature_names[j]).c_str());
throw VmafException("Unknown feature name");
}
}
if (psnr_array_ptr != NULL) {
result.set_scores("psnr", psnr);
}
if (ssim_array_ptr != NULL) {
result.set_scores("ssim", ssim);
}
if (ms_ssim_array_ptr != NULL) {
result.set_scores("ms_ssim", ms_ssim);
}
_set_prediction_result(predictionStructs, result);
free_array(&adm_num_array);
free_array(&adm_den_array);
free_array(&adm_num_scale0_array);
free_array(&adm_den_scale0_array);
free_array(&adm_num_scale1_array);
free_array(&adm_den_scale1_array);
free_array(&adm_num_scale2_array);
free_array(&adm_den_scale2_array);
free_array(&adm_num_scale3_array);
free_array(&adm_den_scale3_array);
free_array(&motion_array);
free_array(&motion2_array);
free_array(&vif_num_scale0_array);
free_array(&vif_den_scale0_array);
free_array(&vif_num_scale1_array);
free_array(&vif_den_scale1_array);
free_array(&vif_num_scale2_array);
free_array(&vif_den_scale2_array);
free_array(&vif_num_scale3_array);
free_array(&vif_den_scale3_array);
free_array(&vif_array);
free_array(&psnr_array);
free_array(&ssim_array);
free_array(&ms_ssim_array);
return result;
}
std::unique_ptr<LibsvmNusvrTrainTestModel> BootstrapVmafQualityRunner::_load_model(const char *model_path)
{
std::unique_ptr<LibsvmNusvrTrainTestModel> model_ptr = std::unique_ptr<BootstrapLibsvmNusvrTrainTestModel>(new BootstrapLibsvmNusvrTrainTestModel(model_path));
model_ptr->load_model();
return model_ptr;
}
void BootstrapVmafQualityRunner::_postproc_predict(
VmafPredictionStruct& predictionStruct) {
double baggingScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::BAGGING_SCORE];
double scorePlusDelta = baggingScore + BootstrapVmafQualityRunner::DELTA;
double scoreMinusDelta = baggingScore - BootstrapVmafQualityRunner::DELTA;
predictionStruct.vmafPrediction[VmafPredictionReturnType::PLUS_DELTA] = scorePlusDelta;
predictionStruct.vmafPrediction[VmafPredictionReturnType::MINUS_DELTA] = scoreMinusDelta;
}
void BootstrapVmafQualityRunner::_transform_score(LibsvmNusvrTrainTestModel& model,
VmafPredictionStruct& predictionStruct) {
double& score = predictionStruct.vmafPrediction[VmafPredictionReturnType::SCORE];
double& baggingScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::BAGGING_SCORE];
double& ci95LowScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_LOW];
double& ci95HighScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_HIGH];
double& scorePlusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::PLUS_DELTA];
double& scoreMinusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::MINUS_DELTA];
_transform_value(model, score);
_transform_value(model, baggingScore);
_transform_value(model, ci95LowScore);
_transform_value(model, ci95HighScore);
_transform_value(model, scorePlusDelta);
_transform_value(model, scoreMinusDelta);
// transform bootstrap model scores
size_t num_models = predictionStruct.vmafMultiModelPrediction.size();
for (size_t model_i = 0; model_i < num_models; model_i++)
{
_transform_value(model, predictionStruct.vmafMultiModelPrediction.at(model_i));
}
}
void BootstrapVmafQualityRunner::_clip_score(LibsvmNusvrTrainTestModel& model,
VmafPredictionStruct& predictionStruct) {
double& score = predictionStruct.vmafPrediction[VmafPredictionReturnType::SCORE];
double& baggingScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::BAGGING_SCORE];
double& ci95LowScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_LOW];
double& ci95HighScore = predictionStruct.vmafPrediction[VmafPredictionReturnType::CI95_HIGH];
double& scorePlusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::PLUS_DELTA];
double& scoreMinusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::MINUS_DELTA];
_clip_value(model, score);
_clip_value(model, baggingScore);
_clip_value(model, ci95LowScore);
_clip_value(model, ci95HighScore);
_clip_value(model, scorePlusDelta);
_clip_value(model, scoreMinusDelta);
// clip bootstrap model scores
size_t num_models = predictionStruct.vmafMultiModelPrediction.size();
for (size_t model_i = 0; model_i < num_models; model_i++)
{
_clip_value(model, predictionStruct.vmafMultiModelPrediction.at(model_i));
}
}
void BootstrapVmafQualityRunner::_postproc_transform_clip(
VmafPredictionStruct& predictionStruct) {
double scorePlusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::PLUS_DELTA];
double scoreMinusDelta = predictionStruct.vmafPrediction[VmafPredictionReturnType::MINUS_DELTA];
double& scoreStdDev = predictionStruct.vmafPrediction[VmafPredictionReturnType::STDDEV];
double slope = (scorePlusDelta - scoreMinusDelta) / (2.0 * BootstrapVmafQualityRunner::DELTA);
scoreStdDev *= slope;
}
static const int BOOTSTRAP_MODEL_NAME_PRECISION = 4;
std::string to_zero_lead(const int value, const unsigned precision)
{
std::ostringstream oss;
oss << std::setw(precision) << std::setfill('0') << value;
return oss.str();
}
void BootstrapVmafQualityRunner::_set_prediction_result(
std::vector<VmafPredictionStruct> predictionStructs,
Result& result) {
VmafQualityRunner::_set_prediction_result(predictionStructs, result);
StatVector baggingScore, stdDev, ci95LowScore, ci95HighScore;
for (size_t i = 0; i < predictionStructs.size(); i++) {
baggingScore.append(predictionStructs.at(i).vmafPrediction[VmafPredictionReturnType::BAGGING_SCORE]);
stdDev.append(predictionStructs.at(i).vmafPrediction[VmafPredictionReturnType::STDDEV]);
ci95LowScore.append(predictionStructs.at(i).vmafPrediction[VmafPredictionReturnType::CI95_LOW]);
ci95HighScore.append(predictionStructs.at(i).vmafPrediction[VmafPredictionReturnType::CI95_HIGH]);
}
result.set_scores("bagging", baggingScore);
result.set_scores("stddev", stdDev);
result.set_scores("ci95_low", ci95LowScore);
result.set_scores("ci95_high", ci95HighScore);
// num_models is same across frames, so just use first frame length
size_t num_models = 0;
if (predictionStructs.size() > 0) {
num_models = predictionStructs.at(0).vmafMultiModelPrediction.size();
}
std::vector<double> perModelScore;
// name of the vmaf bootstrap model, e.g. vmaf_0001 is the first one
for (size_t j = 0; j < num_models; j++) {
for (size_t i = 0; i < predictionStructs.size(); i++) {
perModelScore.push_back(predictionStructs.at(i).vmafMultiModelPrediction.at(j));
}
result.set_scores(BOOSTRAP_VMAF_MODEL_PREFIX + to_zero_lead(j + 1, BOOTSTRAP_MODEL_NAME_PRECISION), perModelScore);
perModelScore.clear();
}
}
static const char VMAFOSS_DOC_VERSION[] = "1.3.15";
double RunVmaf(const char* fmt, int width, int height,
int (*read_frame)(float *ref_data, float *main_data, float *temp_data, int stride, void *user_data),
void *user_data, const char *model_path, const char *log_path, const char *log_fmt,
bool disable_clip, bool enable_transform,
bool do_psnr, bool do_ssim, bool do_ms_ssim,
const char *pool_method, int n_thread, int n_subsample, bool enable_conf_interval)
{
if (width <= 0)
{
throw VmafException("Invalid width value (must be > 0)");
}
if (height <= 0)
{
throw VmafException("Invalid height value (must be > 0)");
}
if (n_thread < 0)
{
throw VmafException("Invalid n_thread value (must be >= 0)");
}
if (n_subsample <= 0)
{
throw VmafException("Invalid n_subsample value (must be > 0)");
}
Asset asset(width, height, fmt);
std::unique_ptr<IVmafQualityRunner> runner_ptr = VmafQualityRunnerFactory::createVmafQualityRunner(model_path, enable_conf_interval);
Timer timer;
timer.start();
Result result = runner_ptr->run(asset, read_frame, user_data, disable_clip, enable_transform,
do_psnr, do_ssim, do_ms_ssim, n_thread, n_subsample);
timer.stop();
if (pool_method != NULL && (strcmp(pool_method, "min")==0))
{
result.setScoreAggregateMethod(ScoreAggregateMethod::MINIMUM);
}
else if (pool_method != NULL && (strcmp(pool_method, "harmonic_mean")==0))
{
result.setScoreAggregateMethod(ScoreAggregateMethod::HARMONIC_MEAN);
}
else // mean or default
{
result.setScoreAggregateMethod(ScoreAggregateMethod::MEAN);
}
size_t num_frames_subsampled = result.get_scores("vmaf").size();