File: pipeline.hpp

package info (click to toggle)
opencv 4.6.0%2Bdfsg-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 276,172 kB
  • sloc: cpp: 1,079,020; xml: 682,526; python: 43,885; lisp: 30,943; java: 25,642; ansic: 7,968; javascript: 5,956; objc: 2,039; sh: 1,017; cs: 601; perl: 494; makefile: 179
file content (205 lines) | stat: -rw-r--r-- 6,245 bytes parent folder | download
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
#ifndef OPENCV_GAPI_PIPELINE_MODELING_TOOL_PIPELINE_HPP
#define OPENCV_GAPI_PIPELINE_MODELING_TOOL_PIPELINE_HPP

struct PerfReport {
    std::string               name;
    double  avg_latency       = 0.0;
    double  throughput        = 0.0;
    int64_t first_run_latency = 0;
    int64_t elapsed           = 0;
    int64_t compilation_time  = 0;
    std::vector<int64_t> latencies;

    std::string toStr(bool expanded = false) const;
};

std::string PerfReport::toStr(bool expand) const {
    std::stringstream ss;
    ss << name << ": Compilation time: " << compilation_time << " ms; "
       << "Average latency: " << avg_latency << " ms; Throughput: "
       << throughput << " FPS; First latency: "
       << first_run_latency << " ms";

    if (expand) {
        ss << "\nTotal processed frames: " << latencies.size()
           << "\nTotal elapsed time: "     << elapsed << " ms" << std::endl;
        for (size_t i = 0; i < latencies.size(); ++i) {
            ss << std::endl;
            ss << "Frame:" << i << "\nLatency: "
               << latencies[i] << " ms";
        }
    }

    return ss.str();
}

class Pipeline {
public:
    using Ptr = std::shared_ptr<Pipeline>;

    Pipeline(std::string&&                       name,
             cv::GComputation&&                  comp,
             cv::gapi::wip::IStreamSource::Ptr&& src,
             cv::GCompileArgs&&                  args,
             const size_t                        num_outputs);

    void compile();
    void run(double work_time_ms);
    const PerfReport& report() const;
    const std::string& name() const { return m_name;}

    virtual ~Pipeline() = default;

protected:
    struct RunPerf {
        int64_t              elapsed   = 0;
        std::vector<int64_t> latencies;
    };

    virtual void _compile() = 0;
    virtual RunPerf _run(double work_time_ms) = 0;

    std::string                       m_name;
    cv::GComputation                  m_comp;
    cv::gapi::wip::IStreamSource::Ptr m_src;
    cv::GCompileArgs                  m_args;
    size_t                            m_num_outputs;
    PerfReport                        m_perf;
};

Pipeline::Pipeline(std::string&&                       name,
                   cv::GComputation&&                  comp,
                   cv::gapi::wip::IStreamSource::Ptr&& src,
                   cv::GCompileArgs&&                  args,
                   const size_t                        num_outputs)
    : m_name(std::move(name)),
      m_comp(std::move(comp)),
      m_src(std::move(src)),
      m_args(std::move(args)),
      m_num_outputs(num_outputs) {
    m_perf.name = m_name;
}

void Pipeline::compile() {
    m_perf.compilation_time =
        utils::measure<std::chrono::milliseconds>([this]() {
        _compile();
    });
}

void Pipeline::run(double work_time_ms) {
    auto run_perf = _run(work_time_ms);

    m_perf.elapsed   = run_perf.elapsed;
    m_perf.latencies = std::move(run_perf.latencies);

    m_perf.avg_latency =
        std::accumulate(m_perf.latencies.begin(),
                        m_perf.latencies.end(),
                        0.0) / static_cast<double>(m_perf.latencies.size());
    m_perf.throughput =
        (m_perf.latencies.size() / static_cast<double>(m_perf.elapsed)) * 1000;

    m_perf.first_run_latency = m_perf.latencies[0];
}

const PerfReport& Pipeline::report() const {
    return m_perf;
}

class StreamingPipeline : public Pipeline {
public:
    using Pipeline::Pipeline;

private:
    void _compile() override {
        m_compiled =
            m_comp.compileStreaming({m_src->descr_of()},
                                     cv::GCompileArgs(m_args));
    }

    Pipeline::RunPerf _run(double work_time_ms) override {
        // NB: Setup.
        using namespace std::chrono;
        // NB: N-1 buffers + timestamp.
        std::vector<cv::Mat> out_mats(m_num_outputs - 1);
        int64_t start_ts = -1;
        cv::GRunArgsP pipeline_outputs;
        for (auto& m : out_mats) {
            pipeline_outputs += cv::gout(m);
        }
        pipeline_outputs += cv::gout(start_ts);
        m_compiled.setSource(m_src);

        // NB: Start execution & measure performance statistics.
        Pipeline::RunPerf perf;
        auto start = high_resolution_clock::now();
        m_compiled.start();
        while (m_compiled.pull(cv::GRunArgsP{pipeline_outputs})) {
            int64_t latency = utils::timestamp<milliseconds>() - start_ts;

            perf.latencies.push_back(latency);
            perf.elapsed = duration_cast<milliseconds>(
                    high_resolution_clock::now() - start).count();

            if (perf.elapsed >= work_time_ms) {
                m_compiled.stop();
                break;
            }
        };
        return perf;
    }

    cv::GStreamingCompiled m_compiled;
};

class RegularPipeline : public Pipeline {
public:
    using Pipeline::Pipeline;

private:
    void _compile() override {
        m_compiled =
            m_comp.compile({m_src->descr_of()},
                            cv::GCompileArgs(m_args));
    }

    Pipeline::RunPerf _run(double work_time_ms) override {
        // NB: Setup
        using namespace std::chrono;
        cv::gapi::wip::Data d;
        std::vector<cv::Mat> out_mats(m_num_outputs);
        cv::GRunArgsP pipeline_outputs;
        for (auto& m : out_mats) {
            pipeline_outputs += cv::gout(m);
        }

        // NB: Start execution & measure performance statistics.
        Pipeline::RunPerf perf;
        auto start = high_resolution_clock::now();
        while (m_src->pull(d)) {
            auto in_mat = cv::util::get<cv::Mat>(d);
            int64_t latency = utils::measure<milliseconds>([&]{
                m_compiled(cv::gin(in_mat), cv::GRunArgsP{pipeline_outputs});
            });

            perf.latencies.push_back(latency);
            perf.elapsed = duration_cast<milliseconds>(
                    high_resolution_clock::now() - start).count();

            if (perf.elapsed >= work_time_ms) {
                break;
            }
        };
        return perf;
    }

    cv::GCompiled m_compiled;
};

enum class PLMode {
    REGULAR,
    STREAMING
};

#endif // OPENCV_GAPI_PIPELINE_MODELING_TOOL_PIPELINE_HPP