- Added rectangless.

- Moved all uses of PYBIND11_MAKE_OPAQUE to a common header to avoid possilbe ODR violations.
- Added python binding for make_bounding_box_regression_training_data().
This commit is contained in:
Davis King 2018-02-11 17:19:04 -05:00
parent beece2077b
commit 2a26521b0b
26 changed files with 171 additions and 28 deletions

View File

@ -4,6 +4,7 @@
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "opaque_types.h"
#include <dlib/string.h> #include <dlib/string.h>
#include <pybind11/stl_bind.h> #include <pybind11/stl_bind.h>
@ -12,22 +13,6 @@ using namespace std;
using namespace dlib; using namespace dlib;
namespace py = pybind11; namespace py = pybind11;
PYBIND11_MAKE_OPAQUE(std::vector<double>);
typedef std::vector<matrix<double,0,1>> column_vectors;
PYBIND11_MAKE_OPAQUE(column_vectors);
PYBIND11_MAKE_OPAQUE(std::vector<column_vectors>);
typedef pair<unsigned long,unsigned long> ulong_pair;
PYBIND11_MAKE_OPAQUE(ulong_pair);
PYBIND11_MAKE_OPAQUE(std::vector<ulong_pair>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<ulong_pair>>);
typedef pair<unsigned long,double> ulong_double_pair;
PYBIND11_MAKE_OPAQUE(ulong_double_pair);
PYBIND11_MAKE_OPAQUE(std::vector<ulong_double_pair>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<ulong_double_pair>>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<std::vector<ulong_double_pair> > >);
std::shared_ptr<std::vector<double> > array_from_object(py::object obj) std::shared_ptr<std::vector<double> > array_from_object(py::object obj)
{ {

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/statistics.h> #include <dlib/statistics.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2017 Davis E. King (davis@dlib.net) // Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/dnn.h> #include <dlib/dnn.h>
@ -13,8 +14,6 @@ using namespace std;
namespace py = pybind11; namespace py = pybind11;
PYBIND11_MAKE_OPAQUE(std::vector<mmod_rect>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<mmod_rect> >);
class cnn_face_detection_model_v1 class cnn_face_detection_model_v1
{ {

View File

@ -3,6 +3,7 @@
#ifndef DLIB_PYTHON_CONVERSION_H__ #ifndef DLIB_PYTHON_CONVERSION_H__
#define DLIB_PYTHON_CONVERSION_H__ #define DLIB_PYTHON_CONVERSION_H__
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/pixel.h> #include <dlib/pixel.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2014 Davis E. King (davis@dlib.net) // Copyright (C) 2014 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/geometry.h> #include <dlib/geometry.h>
#include <dlib/image_processing.h> #include <dlib/image_processing.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include "testing_results.h" #include "testing_results.h"
#include <dlib/svm.h> #include <dlib/svm.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2015 Davis E. King (davis@dlib.net) // Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <dlib/simd.h> #include <dlib/simd.h>
#include <string> #include <string>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2017 Davis E. King (davis@dlib.net) // Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/geometry/vector.h> #include <dlib/geometry/vector.h>
@ -17,7 +18,6 @@ using namespace std;
namespace py = pybind11; namespace py = pybind11;
PYBIND11_MAKE_OPAQUE(std::vector<full_object_detection>);
typedef matrix<double,0,1> cv; typedef matrix<double,0,1> cv;

View File

@ -1,6 +1,7 @@
// Copyright (C) 2017 Davis E. King (davis@dlib.net) // Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/global_optimization.h> #include <dlib/global_optimization.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>

View File

@ -1,5 +1,6 @@
#ifndef DLIB_NO_GUI_SUPPORT #ifndef DLIB_NO_GUI_SUPPORT
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/geometry.h> #include <dlib/geometry.h>
#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing/frontal_face_detector.h>

View File

@ -1,3 +1,4 @@
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include "dlib/pixel.h" #include "dlib/pixel.h"
#include <dlib/image_transforms.h> #include <dlib/image_transforms.h>

View File

@ -1,10 +1,13 @@
// Copyright (C) 2018 Davis E. King (davis@dlib.net) // Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/data_io.h> #include <dlib/data_io.h>
#include <dlib/image_processing.h>
#include <pybind11/stl_bind.h> #include <pybind11/stl_bind.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <iostream>
namespace pybind11 namespace pybind11
{ {
@ -81,8 +84,6 @@ using namespace dlib::image_dataset_metadata;
namespace py = pybind11; namespace py = pybind11;
typedef std::map<std::string,point> parts_list_type;
PYBIND11_MAKE_OPAQUE(parts_list_type);
dataset py_load_image_dataset_metadata( dataset py_load_image_dataset_metadata(
const std::string& filename const std::string& filename
@ -103,6 +104,31 @@ std::shared_ptr<std::map<std::string,point>> map_from_object(py::dict obj)
return ret; return ret;
} }
// ----------------------------------------------------------------------------------------
image_dataset_metadata::dataset py_make_bounding_box_regression_training_data (
const image_dataset_metadata::dataset& truth,
const py::object& detections
)
{
try
{
// if detections is a std::vector then call like this.
return make_bounding_box_regression_training_data(truth, detections.cast<const std::vector<std::vector<rectangle>>&>());
}
catch (py::cast_error&)
{
// otherwise, detections should be a list of std::vectors.
py::list dets(detections);
std::vector<std::vector<rectangle>> temp;
for (auto& d : dets)
temp.emplace_back(d.cast<const std::vector<rectangle>&>());
return make_bounding_box_regression_training_data(truth, temp);
}
}
// ----------------------------------------------------------------------------------------
void bind_image_dataset_metadata(py::module &m_) void bind_image_dataset_metadata(py::module &m_)
{ {
auto m = m_.def_submodule("image_dataset_metadata", "Routines and objects for working with dlib's image dataset metadata XML files."); auto m = m_.def_submodule("image_dataset_metadata", "Routines and objects for working with dlib's image dataset metadata XML files.");
@ -194,6 +220,60 @@ void bind_image_dataset_metadata(py::module &m_)
"Attempts to interpret filename as a file containing XML formatted data as produced " "Attempts to interpret filename as a file containing XML formatted data as produced "
"by the save_image_dataset_metadata() function. The data is loaded and returned as a dlib.image_dataset_metadata.dataset object." "by the save_image_dataset_metadata() function. The data is loaded and returned as a dlib.image_dataset_metadata.dataset object."
); );
m_.def("make_bounding_box_regression_training_data", &py_make_bounding_box_regression_training_data,
py::arg("truth"), py::arg("detections"),
"requires \n\
- len(truth.images) == len(detections) \n\
- detections == A dlib.rectangless object or a list of dlib.rectangles. \n\
ensures \n\
- Suppose you have an object detector that can roughly locate objects in an \n\
image. This means your detector draws boxes around objects, but these are \n\
*rough* boxes in the sense that they aren't positioned super accurately. For \n\
instance, HOG based detectors usually have a stride of 8 pixels. So the \n\
positional accuracy is going to be, at best, +/-8 pixels. \n\
\n\
If you want to get better positional accuracy one easy thing to do is train a \n\
shape_predictor to give you the corners of the object. The \n\
make_bounding_box_regression_training_data() routine helps you do this by \n\
creating an appropriate training dataset. It does this by taking the dataset \n\
you used to train your detector (the truth object), and combining that with \n\
the output of your detector on each image in the training dataset (the \n\
detections object). In particular, it will create a new annotated dataset \n\
where each object box is one of the rectangles from detections and that \n\
object has 4 part annotations, the corners of the truth rectangle \n\
corresponding to that detection rectangle. You can then take the returned \n\
dataset and train a shape_predictor on it. The resulting shape_predictor can \n\
then be used to do bounding box regression. \n\
- We assume that detections[i] contains object detections corresponding to \n\
the image truth.images[i]."
/*!
requires
- len(truth.images) == len(detections)
- detections == A dlib.rectangless object or a list of dlib.rectangles.
ensures
- Suppose you have an object detector that can roughly locate objects in an
image. This means your detector draws boxes around objects, but these are
*rough* boxes in the sense that they aren't positioned super accurately. For
instance, HOG based detectors usually have a stride of 8 pixels. So the
positional accuracy is going to be, at best, +/-8 pixels.
If you want to get better positional accuracy one easy thing to do is train a
shape_predictor to give you the corners of the object. The
make_bounding_box_regression_training_data() routine helps you do this by
creating an appropriate training dataset. It does this by taking the dataset
you used to train your detector (the truth object), and combining that with
the output of your detector on each image in the training dataset (the
detections object). In particular, it will create a new annotated dataset
where each object box is one of the rectangles from detections and that
object has 4 part annotations, the corners of the truth rectangle
corresponding to that detection rectangle. You can then take the returned
dataset and train a shape_predictor on it. The resulting shape_predictor can
then be used to do bounding box regression.
- We assume that detections[i] contains object detections corresponding to
the image truth.images[i].
!*/
);
} }

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/string.h> #include <dlib/string.h>

View File

@ -1,3 +1,4 @@
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include "dlib/pixel.h" #include "dlib/pixel.h"
#include <dlib/image_transforms.h> #include <dlib/image_transforms.h>

View File

@ -1,3 +1,4 @@
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include "dlib/pixel.h" #include "dlib/pixel.h"
#include <dlib/image_transforms.h> #include <dlib/image_transforms.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2015 Davis E. King (davis@dlib.net) // Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/geometry.h> #include <dlib/geometry.h>

View File

@ -0,0 +1,55 @@
// Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_PyTHON_OPAQUE_TYPES_H_
#define DLIB_PyTHON_OPAQUE_TYPES_H_
#include <dlib/python.h>
#include <dlib/geometry.h>
#include <pybind11/stl_bind.h>
#include <vector>
#include <dlib/matrix.h>
#include <dlib/image_processing/full_object_detection.h>
#include <map>
#include <dlib/svm/ranking_tools.h>
// All uses of PYBIND11_MAKE_OPAQUE need to be in this common header to avoid ODR
// violations.
PYBIND11_MAKE_OPAQUE(std::vector<dlib::rectangle>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<dlib::rectangle>>);
PYBIND11_MAKE_OPAQUE(std::vector<double>);
typedef std::vector<dlib::matrix<double,0,1>> column_vectors;
PYBIND11_MAKE_OPAQUE(column_vectors);
PYBIND11_MAKE_OPAQUE(std::vector<column_vectors>);
typedef std::pair<unsigned long,unsigned long> ulong_pair;
PYBIND11_MAKE_OPAQUE(ulong_pair);
PYBIND11_MAKE_OPAQUE(std::vector<ulong_pair>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<ulong_pair>>);
typedef std::pair<unsigned long,double> ulong_double_pair;
PYBIND11_MAKE_OPAQUE(ulong_double_pair);
PYBIND11_MAKE_OPAQUE(std::vector<ulong_double_pair>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<ulong_double_pair>>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<std::vector<ulong_double_pair> > >);
PYBIND11_MAKE_OPAQUE(std::vector<dlib::mmod_rect>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<dlib::mmod_rect> >);
PYBIND11_MAKE_OPAQUE(std::vector<dlib::full_object_detection>);
typedef std::map<std::string,dlib::point> parts_list_type;
PYBIND11_MAKE_OPAQUE(parts_list_type);
typedef std::vector<dlib::ranking_pair<dlib::matrix<double,0,1>>> ranking_pairs;
typedef std::vector<std::pair<unsigned long,double> > sparse_vect;
typedef std::vector<dlib::ranking_pair<sparse_vect> > sparse_ranking_pairs;
PYBIND11_MAKE_OPAQUE(ranking_pairs);
PYBIND11_MAKE_OPAQUE(sparse_ranking_pairs);
PYBIND11_MAKE_OPAQUE(std::vector<dlib::point>);
#endif // DLIB_PyTHON_OPAQUE_TYPES_H_

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/data_io.h> #include <dlib/data_io.h>

View File

@ -5,13 +5,13 @@
#include <dlib/geometry.h> #include <dlib/geometry.h>
#include <pybind11/stl_bind.h> #include <pybind11/stl_bind.h>
#include "indexing.h" #include "indexing.h"
#include "opaque_types.h"
using namespace dlib; using namespace dlib;
using namespace std; using namespace std;
namespace py = pybind11; namespace py = pybind11;
PYBIND11_MAKE_OPAQUE(std::vector<rectangle>);
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -120,6 +120,7 @@ void bind_rectangles(py::module& m)
.def(py::self != py::self) .def(py::self != py::self)
.def(py::pickle(&getstate<type>, &setstate<type>)); .def(py::pickle(&getstate<type>, &setstate<type>));
} }
{ {
typedef std::vector<rectangle> type; typedef std::vector<rectangle> type;
py::bind_vector<type>(m, "rectangles", "An array of rectangle objects.") py::bind_vector<type>(m, "rectangles", "An array of rectangle objects.")
@ -128,6 +129,15 @@ void bind_rectangles(py::module& m)
.def("extend", extend_vector_with_python_list<rectangle>) .def("extend", extend_vector_with_python_list<rectangle>)
.def(py::pickle(&getstate<type>, &setstate<type>)); .def(py::pickle(&getstate<type>, &setstate<type>));
} }
{
typedef std::vector<std::vector<rectangle>> type;
py::bind_vector<type>(m, "rectangless", "An array of arrays of rectangle objects.")
.def("clear", &type::clear)
.def("resize", resize<type>)
.def("extend", extend_vector_with_python_list<rectangle>)
.def(py::pickle(&getstate<type>, &setstate<type>));
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/svm_threaded.h> #include <dlib/svm_threaded.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2014 Davis E. King (davis@dlib.net) // Copyright (C) 2014 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/geometry.h> #include <dlib/geometry.h>
#include <dlib/image_processing.h> #include <dlib/image_processing.h>

View File

@ -3,6 +3,7 @@
#ifndef DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__ #ifndef DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__
#define DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__ #define DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/geometry.h> #include <dlib/geometry.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include "testing_results.h" #include "testing_results.h"
#include <dlib/matrix.h> #include <dlib/matrix.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/svm.h> #include <dlib/svm.h>
@ -12,12 +13,7 @@ using namespace std;
namespace py = pybind11; namespace py = pybind11;
typedef matrix<double,0,1> sample_type; typedef matrix<double,0,1> sample_type;
typedef std::vector<std::pair<unsigned long,double> > sparse_vect;
typedef std::vector<ranking_pair<sample_type> > ranking_pairs;
typedef std::vector<ranking_pair<sparse_vect> > sparse_ranking_pairs;
PYBIND11_MAKE_OPAQUE(ranking_pairs);
PYBIND11_MAKE_OPAQUE(sparse_ranking_pairs);
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/svm.h> #include <dlib/svm.h>

View File

@ -1,6 +1,7 @@
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h> #include <dlib/python.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/geometry/vector.h> #include <dlib/geometry/vector.h>
@ -12,7 +13,6 @@ using namespace std;
typedef matrix<double,0,1> cv; typedef matrix<double,0,1> cv;
PYBIND11_MAKE_OPAQUE(std::vector<point>);
void cv_set_size(cv& m, long s) void cv_set_size(cv& m, long s)
{ {