Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / validation_app / Processor.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <iostream>
8 #include <limits>
9 #include <string>
10 #include <memory>
11
12 #include <samples/common.hpp>
13
14 #include "inference_engine.hpp"
15
16 #include "samples/csv_dumper.hpp"
17 #include "image_decoder.hpp"
18 #include "samples/console_progress.hpp"
19
20 using namespace std;
21
22 #define OUTPUT_FLOATING(val) std::fixed << std::setprecision(2) << val
23
24 class Processor {
25 public:
26     struct InferenceMetrics {
27         int nRuns = 0;
28         double minDuration = std::numeric_limits<double>::max();
29         double maxDuration = 0;
30         double totalTime = 0;
31
32         virtual ~InferenceMetrics() { }  // Type has to be polymorphic
33     };
34
35 protected:
36     std::string modelFileName;
37     std::string targetDevice;
38     std::string imagesPath;
39     size_t batch;
40     InferenceEngine::InferRequest inferRequest;
41     InferenceEngine::InputsDataMap inputInfo;
42     InferenceEngine::OutputsDataMap outInfo;
43     InferenceEngine::CNNNetReader networkReader;
44     InferenceEngine::SizeVector inputDims;
45     InferenceEngine::SizeVector outputDims;
46     double loadDuration;
47     PreprocessingOptions preprocessingOptions;
48
49     CsvDumper& dumper;
50     InferencePlugin plugin;
51
52     std::string approach;
53
54     double Infer(ConsoleProgress& progress, int filesWatched, InferenceMetrics& im);
55
56 public:
57     Processor(const std::string& flags_m, const std::string& flags_d, const std::string& flags_i, int flags_b,
58             InferenceEngine::InferencePlugin plugin, CsvDumper& dumper, const std::string& approach, PreprocessingOptions preprocessingOptions);
59
60     virtual shared_ptr<InferenceMetrics> Process(bool stream_output = false) = 0;
61     virtual void Report(const InferenceMetrics& im) {
62         double averageTime = im.totalTime / im.nRuns;
63
64         slog::info << "Inference report:\n";
65         slog::info << "\tNetwork load time: " << loadDuration << "ms" << "\n";
66         slog::info << "\tModel: " << modelFileName << "\n";
67         slog::info << "\tModel Precision: " << networkReader.getNetwork().getPrecision().name() << "\n";
68         slog::info << "\tBatch size: " << batch << "\n";
69         slog::info << "\tValidation dataset: " << imagesPath << "\n";
70         slog::info << "\tValidation approach: " << approach;
71         slog::info << slog::endl;
72
73         if (im.nRuns > 0) {
74             slog::info << "Average infer time (ms): " << averageTime << " (" << OUTPUT_FLOATING(1000.0 / (averageTime / batch))
75                     << " images per second with batch size = " << batch << ")" << slog::endl;
76         } else {
77             slog::warn << "No images processed" << slog::endl;
78         }
79     }
80
81     virtual ~Processor() {}
82 };