reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-17 f7c4a3cfd07adede3308f8d9d3d7315427d90a7c
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
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_
#define _OPENCV_DNN_OBJDETECT_CORE_DETECT_HPP_
 
#include <vector>
#include <memory>
 
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
 
/** @defgroup dnn_objdetect DNN used for object detection
*/
 
namespace cv
{
namespace dnn_objdetect
{
 
    //! @addtogroup dnn_objdetect
    //! @{
 
    /** @brief Structure to hold the details pertaining to a single bounding box
     */
    typedef struct
    {
      int xmin, xmax;
      int ymin, ymax;
      size_t class_idx;
      std::string label_name;
      double class_prob;
    } object;
 
 
    /** @brief A class to post process model predictions
     */
    class CV_EXPORTS InferBbox
    {
      public:
        /** @brief Default constructer
        @param _delta_bbox Blob containing relative coordinates of bounding boxes
        @param _class_scores Blob containing the probability values of each class
        @param _conf_scores Blob containing the confidence scores
         */
        InferBbox(Mat _delta_bbox, Mat _class_scores, Mat _conf_scores);
 
        /** @brief Filters the bounding boxes.
         */
        void filter(double thresh =  0.8);
 
        /** @brief Vector which holds the final detections of the model
         */
        std::vector<object> detections;
 
      protected:
        /** @brief Transform relative coordinates from ConvDet to bounding box coordinates
        @param bboxes Vector to hold the predicted bounding boxes
         */
        void transform_bboxes(std::vector<std::vector<double> > *bboxes);
 
        /** @brief Computes final probability values of each bounding box
        @param final_probs Vector to hold the probability values
         */
        void final_probability_dist(std::vector<std::vector<double> > *final_probs);
 
        /** @brief Transform bounding boxes from [x, y, h, w] to [xmin, ymin, xmax, ymax]
        @param pre Vector conatining initial co-ordinates
        @param post Vector containing the transformed co-ordinates
         */
        void transform_bboxes_inv(std::vector<std::vector<double> > *pre,
                                  std::vector<std::vector<double> > *post);
 
        /** @brief Ensures that the bounding box values are within image boundaries
        @param min_max_boxes Vector containing bounding boxes of the form [xmin, ymin, xmax, ymax]
         */
        void assert_predictions(std::vector<std::vector<double> > *min_max_boxes);
 
        /** @brief Filter top `n` predictions
        @param probs Final probability values of bounding boxes
        @param boxes Predicted bounding box co-ordinates
        @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes
        @param top_n_idxs Containes class indices of top `n` bounding boxes
        @param top_n_probs Contains probability values of top `n` bounding boxes
         */
        void filter_top_n(std::vector<std::vector<double> > *probs,
                          std::vector<std::vector<double> > *boxes,
                          std::vector<std::vector<double> > &top_n_boxes,
                          std::vector<size_t> &top_n_idxs,
                          std::vector<double> &top_n_probs);
 
        /** @brief Wrapper to apply Non-Maximal Supression
        @param top_n_boxes Contains bounding box co-ordinates of top `n` boxes
        @param top_n_idxs Containes class indices of top `n` bounding boxes
        @param top_n_probs Contains probability values of top `n` bounding boxes
         */
        void nms_wrapper(std::vector<std::vector<double> > &top_n_boxes,
                         std::vector<size_t> &top_n_idxs,
                         std::vector<double> &top_n_probs);
 
       /** @brief Applies Non-Maximal Supression
       @param boxes Bounding box co-ordinates belonging to one class
       @param probs Probability values of boxes belonging to one class
        */
        std::vector<bool> non_maximal_suppression(std::vector<std::vector<double> >
                                         *boxes, std::vector<double> *probs);
 
       /** @brief Computes intersection over union of bounding boxes
       @param boxes Vector of bounding box co-ordinates
       @param base_box Base box wrt which IOU is calculated
       @param iou Vector to store IOU values
        */
        void intersection_over_union(std::vector<std::vector<double> > *boxes,
                          std::vector<double> *base_box, std::vector<double> *iou);
 
        static inline bool comparator (std::pair<double, size_t> l1,
            std::pair<double, size_t> l2)
        {
          return l1.first > l2.first;
        }
 
      private:
        Mat delta_bbox;
        Mat class_scores;
        Mat conf_scores;
 
        unsigned int image_width;
        unsigned int image_height;
 
        unsigned int W, H;
        std::vector<std::vector<double> > anchors_values;
        std::vector<std::pair<double, double> > anchor_center;
        std::vector<std::pair<double, double> > anchor_shapes;
 
        std::vector<std::string> label_map;
 
        unsigned int num_classes;
        unsigned int anchors_per_grid;
        size_t anchors;
        double intersection_thresh;
        double nms_intersection_thresh;
        size_t n_top_detections;
        double epsilon;
    };
 
    //! @}
} // namespace dnn_objdetect
} // namespace cv
#endif