plugin  0.1.0
detection_model_yolo.h
1 /*
2 // Copyright (C) 2020-2024 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #pragma once
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <openvino/op/region_yolo.hpp>
27 #include <openvino/openvino.hpp>
28 
29 #include "models/detection_model.h"
30 
31 struct DetectedObject;
32 struct InferenceResult;
33 struct ResultBase;
34 
35 class ModelYolo : public DetectionModel {
36 protected:
37  class Region {
38  public:
39  int num = 0;
40  size_t classes = 0;
41  int coords = 0;
42  std::vector<float> anchors;
43  size_t outputWidth = 0;
44  size_t outputHeight = 0;
45 
46  Region(const std::shared_ptr<ov::op::v0::RegionYolo>& regionYolo);
47  Region(size_t classes,
48  int coords,
49  const std::vector<float>& anchors,
50  const std::vector<int64_t>& masks,
51  size_t outputWidth,
52  size_t outputHeight);
53  };
54 
55 public:
56  enum YoloVersion { YOLO_V1V2, YOLO_V3, YOLO_V4, YOLO_V4_TINY, YOLOF };
57 
73  ModelYolo(const std::string& modelFileName,
74  float confidenceThreshold,
75  bool useAutoResize,
76  bool useAdvancedPostprocessing = true,
77  float boxIOUThreshold = 0.5,
78  const std::vector<std::string>& labels = std::vector<std::string>(),
79  const std::vector<float>& anchors = std::vector<float>(),
80  const std::vector<int64_t>& masks = std::vector<int64_t>(),
81  const std::string& layout = "");
82 
83  std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
84 
85 protected:
86  void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
87 
88  void parseYOLOOutput(const std::string& output_name,
89  const ov::Tensor& tensor,
90  const unsigned long resized_im_h,
91  const unsigned long resized_im_w,
92  const unsigned long original_im_h,
93  const unsigned long original_im_w,
94  std::vector<DetectedObject>& objects);
95 
96  static int calculateEntryIndex(int entriesNum, int lcoords, size_t lclasses, int location, int entry);
97  static double intersectionOverUnion(const DetectedObject& o1, const DetectedObject& o2);
98 
99  std::map<std::string, Region> regions;
100  double boxIOUThreshold;
101  bool useAdvancedPostprocessing;
102  bool isObjConf = 1;
103  YoloVersion yoloVersion;
104  const std::vector<float> presetAnchors;
105  const std::vector<int64_t> presetMasks;
106  ov::Layout yoloRegionLayout = "NCHW";
107 };
Definition: detection_model.h:25
Definition: detection_model_yolo.h:37
Definition: detection_model_yolo.h:35
ModelYolo(const std::string &modelFileName, float confidenceThreshold, bool useAutoResize, bool useAdvancedPostprocessing=true, float boxIOUThreshold=0.5, const std::vector< std::string > &labels=std::vector< std::string >(), const std::vector< float > &anchors=std::vector< float >(), const std::vector< int64_t > &masks=std::vector< int64_t >(), const std::string &layout="")
Definition: detection_model_yolo.cpp:101
Definition: results.h:89
Definition: results.h:53
Definition: results.h:29