forked from kiritigowda/MIVisionX-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.cpp
More file actions
1161 lines (1093 loc) · 50 KB
/
classifier.cpp
File metadata and controls
1161 lines (1093 loc) · 50 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
/*
MIT License
Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* This file is generated by nnir2openvx.py on 2018-11-12T08:03:54.930527-08:00 */
#include "caffeModels.h"
#include <vx_ext_amd.h>
#include <vx_amd_nn.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#include <chrono>
#include <unistd.h>
#include <math.h>
#include <immintrin.h>
#include<fstream>
#if ENABLE_OPENCV
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#define CVUI_IMPLEMENTATION
#include "cvui.h"
using namespace cv;
#endif
#define MIVisionX_LEGEND "MIVisionX Image Classification"
unsigned char colors[20][3]={
{0,255,0},
{0, 0,255},
{255, 0,0},
{ 250, 150, 70},
{102,102,156},
{190,153,153},
{ 0, 0, 0},
{250,170, 30},
{220,220, 0},
{0, 255, 0},
{152,251,152},
{ 135,206,250},
{220, 20, 60},
{255, 0, 0},
{ 0, 0,255},
{ 0, 0, 70},
{ 0, 60,100},
{ 0, 80,100},
{ 0, 0,230},
{119, 11, 32}
};
std::string classificationModels[20] = {
"InceptionV4",
"Resnet50",
"VGG16",
"GoogleNet",
"Resnet101",
"Resnet152",
"VGG19",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified",
"Unclassified"
};
// probability track bar
const int threshold_slider_max = 100;
int threshold_slider;
double thresholdValue = 0.5;
void threshold_on_trackbar( int, void* ){
thresholdValue = (double) threshold_slider/threshold_slider_max ;
return;
}
bool runInception, runResnet50, runVgg16, runGooglenet, runResnet101, runResnet152, runVgg19;
float inceptionV4Time_g, resnet50Time_g, vgg16Time_g, googlenetTime_g, resnet101Time_g, resnet152Time_g, vgg19Time_g;
void createLegendImage()
{
// create display legend image
int fontFace = CV_FONT_HERSHEY_DUPLEX;
double fontScale = 0.75;
int thickness = 1.3;
cv::Size legendGeometry = cv::Size(625, (10 * 40) + 40);
Mat legend = Mat::zeros(legendGeometry,CV_8UC3);
Rect roi = Rect(0,0,625,(10 * 40) + 40);
legend(roi).setTo(cv::Scalar(128,128,128));
int l = 0, model = 0;
int red, green, blue;
std::string className;
std::string bufferName;
char buffer [50];
// add headers
bufferName = "MIVisionX Image Classification";
putText(legend, bufferName, Point(20, (l * 40) + 30), fontFace, 1.2, cv::Scalar(66,13,9), thickness,5);
l++;
bufferName = "Model";
putText(legend, bufferName, Point(100, (l * 40) + 30), fontFace, 1, Scalar::all(0), thickness,5);
bufferName = "ms/frame";
putText(legend, bufferName, Point(300, (l * 40) + 30), fontFace, 1, Scalar::all(0), thickness,5);
bufferName = "Color";
putText(legend, bufferName, Point(525, (l * 40) + 30), fontFace, 1, Scalar::all(0), thickness,5);
l++;
// add legend items
thickness = 1;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", inceptionV4Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runInception);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", resnet50Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runResnet50);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", vgg16Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runVgg16);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", googlenetTime_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runGooglenet);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", resnet101Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runResnet101);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", resnet152Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runResnet152);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
red = (colors[model][2]); green = (colors[model][1]) ; blue = (colors[model][0]) ;
className = classificationModels[model];
sprintf (buffer, " %.2f ", vgg19Time_g );
cvui::checkbox(legend, 30, (l * 40) + 15,"", &runVgg19);
putText(legend, className, Point(80, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
putText(legend, buffer, Point(320, (l * 40) + 30), fontFace, fontScale, Scalar::all(0), thickness,3);
rectangle(legend, Point(550, (l * 40)) , Point(575, (l * 40) + 40), Scalar(red,green,blue),-1);
l++; model++;
cvui::trackbar(legend, 100, (l * 40)+10, 450, &threshold_slider, 0, 100);
cvui::update();
cv::imshow(MIVisionX_LEGEND, legend);
thresholdValue = (double) threshold_slider/threshold_slider_max ;
}
#define ERROR_CHECK_OBJECT(obj) { vx_status status = vxGetStatus((vx_reference)(obj)); if(status != VX_SUCCESS) { vxAddLogEntry((vx_reference)context, status , "ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); return status; } }
#define ERROR_CHECK_STATUS(call) { vx_status status = (call); if(status != VX_SUCCESS) { printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status, __LINE__); return -1; } }
static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[])
{
size_t len = strlen(string);
if (len > 0) {
printf("%s", string);
if (string[len - 1] != '\n')
printf("\n");
fflush(stdout);
}
}
inline int64_t clockCounter()
{
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}
inline int64_t clockFrequency()
{
return std::chrono::high_resolution_clock::period::den / std::chrono::high_resolution_clock::period::num;
}
int main(int argc, const char ** argv)
{
// check command-line usage
if(argc < 8) {
printf(
"\n"
"Usage: ./classifier <inceptionV4 weights.bin> <resnet50 weights.bin> <vgg16 weights.bin> <googlenet weights.bin> "
"<resnet101 weights.bin> <resnet152 weights.bin> <vgg19 weights.bin> [ --label <label text> --video <video file>/<--capture 0> ] \n"
"\n"
);
return -1;
}
const char * binaryFilename_inception = argv[1];
const char * binaryFilename_resnet = argv[2];
const char * binaryFilename_vgg = argv[3];
const char * binaryFilename_googlenet = argv[4];
const char * binaryFilename_resnet101 = argv[5];
const char * binaryFilename_resnet152 = argv[6];
const char * binaryFilename_vgg19 = argv[7];
argc -= 8;
argv += 8;
std::string videoFile = "empty";
std::string labelFileName = "empty";
std::string labelText[1000];
int captureID = -1;
bool captureFromVideo = false;
if (argc && !strcasecmp(*argv, "--label"))
{
argc--;
argv++;
labelFileName = *argv;
std::string line;
std::ifstream out(labelFileName);
int lineNum = 0;
while(getline(out, line)) {
labelText[lineNum] = line;
lineNum++;
}
out.close();
argc--;
argv++;
}
if (argc && !strcasecmp(*argv, "--video"))
{
argv++;
videoFile = *argv;
captureFromVideo = true;
}
else if (argc && !strcasecmp(*argv, "--capture"))
{
argv++;
captureID = atoi(*argv);
}
// create context, input, output, and graph
vxRegisterLogCallback(NULL, log_callback, vx_false_e);
vx_context context = vxCreateContext();
vx_status status = vxGetStatus((vx_reference)context);
if(status) {
printf("ERROR: vxCreateContext() failed\n");
return -1;
}
vxRegisterLogCallback(context, log_callback, vx_false_e);
// load vx_nn kernels
ERROR_CHECK_STATUS(vxLoadKernels(context, "vx_nn"));
// creation of graphs
vx_graph graph_inception = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_inception);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_resnet = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_resnet);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_vgg = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_vgg);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_googlenet = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_googlenet);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_resnet101 = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_resnet101);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_resnet152 = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_resnet152);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
vx_graph graph_vgg19 = vxCreateGraph(context);
status = vxGetStatus((vx_reference)graph_vgg19);
if(status) {
printf("ERROR: vxCreateGraph(...) failed (%d)\n", status);
return -1;
}
// create and initialize input tensor data
vx_size dims_data_299x299[4] = { 299, 299, 3, 1 };
vx_size dims_data_224x224[4] = { 224, 224, 3, 1 };
// create data for different sizes
vx_tensor data_299x299 = vxCreateTensor(context, 4, dims_data_299x299, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)data_299x299)) {
printf("ERROR: vxCreateTensor() failed for data\n");
return -1;
}
vx_tensor data_224x224 = vxCreateTensor(context, 4, dims_data_224x224, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)data_224x224)) {
printf("ERROR: vxCreateTensor() failed for data\n");
return -1;
}
// create output tensor prob
vx_size dims_prob[4] = { 1, 1, 1000, 1 };
vx_tensor prob_inception = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_inception)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_resnet = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_resnet)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_vgg = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_vgg)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_googlenet = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_googlenet)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_resnet101 = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_resnet101)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_resnet152 = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_resnet152)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
vx_tensor prob_vgg19 = vxCreateTensor(context, 4, dims_prob, VX_TYPE_FLOAT32, 0);
if(vxGetStatus((vx_reference)prob_vgg19)) {
printf("ERROR: vxCreateTensor() failed for prob\n");
return -1;
}
// build graph using annmodule
int64_t freq = clockFrequency(), t0, t1;
t0 = clockCounter();
status = annAddToGraph_inception(graph_inception, data_299x299, prob_inception, binaryFilename_inception);
if(status) {
printf("ERROR: inception annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_inception);
if(status) {
printf("ERROR: inception vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_resnet(graph_resnet, data_224x224, prob_resnet, binaryFilename_resnet);
if(status) {
printf("ERROR: resnet annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_resnet);
if(status) {
printf("ERROR: resnet vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_vgg(graph_vgg, data_224x224, prob_vgg, binaryFilename_vgg);
if(status) {
printf("ERROR: vgg annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_vgg);
if(status) {
printf("ERROR: vgg vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_googleNet(graph_googlenet, data_224x224, prob_googlenet, binaryFilename_googlenet);
if(status) {
printf("ERROR: googlenet annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_googlenet);
if(status) {
printf("ERROR: googlenet vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_resnet101(graph_resnet101, data_224x224, prob_resnet101, binaryFilename_resnet101);
if(status) {
printf("ERROR: resnet101 annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_resnet101);
if(status) {
printf("ERROR: resnet101 vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_resnet152(graph_resnet152, data_224x224, prob_resnet152, binaryFilename_resnet152);
if(status) {
printf("ERROR: resnet152 annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_resnet152);
if(status) {
printf("ERROR: resnet`52 vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
status = annAddToGraph_vgg19(graph_vgg19, data_224x224, prob_vgg19, binaryFilename_vgg19);
if(status) {
printf("ERROR: vgg19 annAddToGraph() failed (%d)\n", status);
return -1;
}
status = vxVerifyGraph(graph_vgg);
if(status) {
printf("ERROR: vgg19 vxVerifyGraph(...) failed (%d)\n", status);
return -1;
}
t1 = clockCounter();
printf("OK: graph initialization with annAddToGraph() took %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
t0 = clockCounter();
status = vxProcessGraph(graph_inception);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_resnet);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_vgg);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_googlenet);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_resnet101);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_resnet152);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
status = vxProcessGraph(graph_vgg19);
if(status != VX_SUCCESS) {
printf("ERROR: vxProcessGraph() failed (%d)\n", status);
return -1;
}
t1 = clockCounter();
printf("OK: vxProcessGraph() took %.3f msec (1st iteration)\n", (float)(t1-t0)*1000.0f/(float)freq);
int N = 100;
float inceptionV4Time, resnet50Time, vgg16Time, googlenetTime, resnet101Time, resnet152Time, vgg19Time;
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_inception);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
inceptionV4Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: inceptionV4 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_resnet);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
resnet50Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: resnet50 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_vgg);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
vgg16Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: vgg16 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_googlenet);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
googlenetTime = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: googlenet took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_resnet101);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
resnet101Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: resnet101 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_resnet152);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
resnet152Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: resnet152 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
t0 = clockCounter();
for(int i = 0; i < N; i++) {
status = vxProcessGraph(graph_vgg19);
if(status != VX_SUCCESS)
break;
}
t1 = clockCounter();
vgg19Time = (float)(t1-t0)*1000.0f/(float)freq/(float)N;
printf("OK: vgg19 took %.3f msec (average over %d iterations)\n", (float)(t1-t0)*1000.0f/(float)freq/(float)N, N);
/***** OPENCV Additions *****/
// create display windows
cv::namedWindow(MIVisionX_LEGEND);
cvui::init(MIVisionX_LEGEND);
cv::namedWindow("MIVisionX Image Classification - LIVE", cv::WINDOW_GUI_EXPANDED);
//create a probability track bar
threshold_slider = 50;
//cv::createTrackbar("Probability Threshold", MIVisionX_LEGEND, &threshold_slider, threshold_slider_max, threshold_on_trackbar);
// create display legend image
runInception = true; runResnet50 = true; runVgg16 = true;
runGooglenet = true; runResnet101 = true;
runResnet152 = true; runVgg19 = true;
inceptionV4Time_g = inceptionV4Time; resnet50Time_g = resnet50Time;
vgg16Time_g = vgg16Time; resnet101Time_g = resnet101Time;
googlenetTime_g = googlenetTime; resnet152Time_g = resnet152Time; vgg19Time_g = vgg19Time;
createLegendImage();
// define variables for run
cv::Mat frame;
int total_size = 1000;
int outputImgWidth = 1080, outputImgHeight = 720;
float threshold = 0.01;
cv::Size output_geometry = cv::Size(outputImgWidth, outputImgHeight);
Mat inputDisplay, outputDisplay;
cv::Mat inputFrame_299x299, inputFrame_224x224;
int fontFace = CV_FONT_HERSHEY_DUPLEX;
double fontScale = 1;
int thickness = 1.5;
float *outputBuffer[7];
for(int models = 0; models < 7; models++){
outputBuffer[models] = new float[total_size];
}
int loopSeg = 1;
while(argc && loopSeg)
{
VideoCapture cap;
if (captureFromVideo) {
cap.open(videoFile);
if(!cap.isOpened()) {
std::cout << "Unable to open the video: " << videoFile << std::endl;
return 0;
}
}
else {
cap.open(captureID);
if(!cap.isOpened()) {
std::cout << "Unable to open the camera feed: " << captureID << std::endl;
return 0;
}
}
int frameCount = 0;
float msFrame = 0, fpsAvg = 0, frameMsecs = 0;
for(;;)
{
msFrame = 0;
// capture image frame
t0 = clockCounter();
cap >> frame;
if( frame.empty() ) break; // end of video stream
t1 = clockCounter();
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("\n\nLIVE: OpenCV Frame Capture Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
// preprocess image frame
t0 = clockCounter();
if(runInception){
cv::resize(frame, inputFrame_299x299, cv::Size(299,299));
}
cv::resize(frame, inputFrame_224x224, cv::Size(224,224));
t1 = clockCounter();
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: OpenCV Frame Resize Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
// Copy Image frame into the input tensor
t0 = clockCounter();
vx_enum usage = VX_WRITE_ONLY;
vx_enum data_type = VX_TYPE_FLOAT32;
vx_size num_of_dims = 4, dims[4] = { 1, 1, 1, 1 }, stride[4];
vx_map_id map_id;
float * ptr;
vx_size count;
// copy - 299x299
if(runInception)
{
vxQueryTensor(data_299x299, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(data_299x299, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(data_299x299, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
vx_status status = vxMapTensorPatch(data_299x299, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
Mat srcImg;
for(size_t n = 0; n < dims[3]; n++) {
srcImg = inputFrame_299x299;
for(vx_size y = 0; y < dims[1]; y++) {
unsigned char * src = srcImg.data + y*dims[0]*3;
float * dstR = ptr + ((n * stride[3] + y * stride[1]) >> 2);
float * dstG = dstR + (stride[2] >> 2);
float * dstB = dstG + (stride[2] >> 2);
for(vx_size x = 0; x < dims[0]; x++, src += 3) {
*dstR++ = (src[2] * 0.007843137) - 1;
*dstG++ = (src[1] * 0.007843137) - 1;
*dstB++ = (src[0] * 0.007843137) - 1;
}
}
}
status = vxUnmapTensorPatch(data_299x299, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// copy - 224x224
if(runResnet50 || runVgg16 || runGooglenet || runResnet101 || runResnet152 || runVgg19)
{
vxQueryTensor(data_224x224, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(data_224x224, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(data_224x224, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
vx_status status = vxMapTensorPatch(data_224x224, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
Mat srcImg;
for(size_t n = 0; n < dims[3]; n++) {
srcImg = inputFrame_224x224;
for(vx_size y = 0; y < dims[1]; y++) {
unsigned char * src = srcImg.data + y*dims[0]*3;
float * dstR = ptr + ((n * stride[3] + y * stride[1]) >> 2);
float * dstG = dstR + (stride[2] >> 2);
float * dstB = dstG + (stride[2] >> 2);
for(vx_size x = 0; x < dims[0]; x++, src += 3) {
*dstR++ = src[2];
*dstG++ = src[1];
*dstB++ = src[0];
}
}
}
status = vxUnmapTensorPatch(data_224x224, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
t1 = clockCounter();
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Convert Image to Tensor Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
// process graph for the input
if(runInception)
{
t0 = clockCounter();
status = vxProcessGraph(graph_inception);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
inceptionV4Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process InceptionV4 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runResnet50)
{
t0 = clockCounter();
status = vxProcessGraph(graph_resnet);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
resnet50Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process Resnet50 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runVgg16)
{
t0 = clockCounter();
status = vxProcessGraph(graph_vgg);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
vgg16Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process VGG16 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runGooglenet)
{
t0 = clockCounter();
status = vxProcessGraph(graph_googlenet);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
googlenetTime_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process VGG16 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runResnet101)
{
t0 = clockCounter();
status = vxProcessGraph(graph_resnet101);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
resnet101Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process Resnet101 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runResnet152)
{
t0 = clockCounter();
status = vxProcessGraph(graph_resnet152);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
resnet152Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process Resnet152 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
if(runVgg19)
{
t0 = clockCounter();
status = vxProcessGraph(graph_vgg19);
if(status != VX_SUCCESS) break;
t1 = clockCounter();
vgg19Time_g = (float)(t1-t0)*1000.0f/(float)freq;
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Process VGG16 Classification Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
}
// copy output data into local buffer
t0 = clockCounter();
usage = VX_READ_ONLY;
// inception copy
if(runInception)
{
vxQueryTensor(prob_inception, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_inception, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_inception, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_inception, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[0], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_inception, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// resnet copy
if(runResnet50)
{
vxQueryTensor(prob_resnet, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_resnet, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_resnet, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_resnet, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[1], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_resnet, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// vgg copy
if(runVgg16)
{
vxQueryTensor(prob_vgg, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_vgg, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_vgg, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_vgg, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[2], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_vgg, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// googlenet copy
if(runGooglenet)
{
vxQueryTensor(prob_googlenet, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_googlenet, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_googlenet, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_googlenet, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[3], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_googlenet, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// resnet 101 copy
if(runResnet101)
{
vxQueryTensor(prob_resnet101, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_resnet101, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_resnet101, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_resnet101, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[4], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_resnet101, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// resnet 152 copy
if(runResnet152)
{
vxQueryTensor(prob_resnet152, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_resnet152, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_resnet152, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_resnet152, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[5], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_resnet152, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
// vgg copy
if(runVgg19)
{
vxQueryTensor(prob_vgg19, VX_TENSOR_DATA_TYPE, &data_type, sizeof(data_type));
vxQueryTensor(prob_vgg19, VX_TENSOR_NUMBER_OF_DIMS, &num_of_dims, sizeof(num_of_dims));
vxQueryTensor(prob_vgg19, VX_TENSOR_DIMS, &dims, sizeof(dims[0])*num_of_dims);
if(data_type != VX_TYPE_FLOAT32) {
std::cerr << "ERROR: copyTensor() supports only VX_TYPE_FLOAT32: invalid for " << std::endl;
return -1;
}
count = dims[0] * dims[1] * dims[2] * dims[3];
status = vxMapTensorPatch(prob_vgg19, num_of_dims, nullptr, nullptr, &map_id, stride, (void **)&ptr, usage, VX_MEMORY_TYPE_HOST, 0);
if(status) {
std::cerr << "ERROR: vxMapTensorPatch() failed for " << std::endl;
return -1;
}
memcpy(outputBuffer[6], ptr, (count*sizeof(float)));
status = vxUnmapTensorPatch(prob_vgg19, map_id);
if(status) {
std::cerr << "ERROR: vxUnmapTensorPatch() failed for " << std::endl;
return -1;
}
}
t1 = clockCounter();
msFrame += (float)(t1-t0)*1000.0f/(float)freq;
//printf("LIVE: Copy probability Output Time -- %.3f msec\n", (float)(t1-t0)*1000.0f/(float)freq);
// process probabilty
t0 = clockCounter();
threshold = (float)thresholdValue;
const int N = 1000;
int inceptionID, resnetID, vggID, googlenetID, resnet101ID, resnet152ID, vgg19ID;
if(runInception)
{