1
0
mirror of https://github.com/opencv/opencv_contrib.git synced 2025-10-17 07:04:18 +08:00

Merge pull request #1253 from kvmanohar22:GSoC17_dnn_objdetect

GSoC'17 Learning compact models for object detection (#1253)

* Final solver and model for SqueezeNet model

* update README

* update dependencies and CMakeLists

* add global pooling

* Add training scripts

* fix typo

* fix dependency of caffe

* fix whitespace

* Add squeezedet architecture

* Pascal pre process script

* Adding pre process scripts

* Generate the graph of the model

* more readable

* fix some bugs in the graph

* Post process class implementation

* Complete minimal post processing and standalone running

* Complete the base class

* remove c++11 features and fix bugs

* Complete example

* fix bugs

* Adding final scripts

* Classification scripts

* Update README.md

* Add example code and results

* Update README.md

* Re-order and fix some bugs

* fix build failure

* Document classes and functions

* Add instructions on how to use samples

* update instructionos

* fix docs failure

* fix conversion types

* fix type conversion warning

* Change examples to sample directoryu

* restructure directories

* add more references

* fix whitespace

* retain aspect ratio

* Add more examples

* fix docs warnings

* update with links to trained weights

* threshold update

* png -> jpg

* fix tutorial

* model files

* precomp.hpp , fix readme links, module dependencies

* copyrights

- no copyright in samples
- use new style OpenCV copyright header
- precomp.hpp
This commit is contained in:
Kv Manohar
2018-01-29 14:38:32 +05:30
committed by Alexander Alekhin
parent c0b298c5f3
commit 41a5a5eaf5
33 changed files with 5376 additions and 0 deletions

View File

@@ -0,0 +1,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