Turi Create  4.0
parameter_sampler.hpp
1 /* Copyright © 2019 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at
5  * https://opensource.org/licenses/BSD-3-Clause
6  */
7 
8 #ifndef TURI_PARAMETER_SAMPLER_H_
9 #define TURI_PARAMETER_SAMPLER_H_
10 
11 #include <Eigen/Core>
12 #include <Eigen/Dense>
13 
14 #include <algorithm>
15 #include <cmath>
16 #include <limits>
17 #include <random>
18 #include <vector>
19 
20 namespace turi {
21 namespace one_shot_object_detection {
22 
23 /* A ParameterSampler class to randomly generate different samples of parameters
24  * that can later be used to compute the transformation matrix necessary to
25  * create image projections.
26  */
27 class ParameterSampler {
28  public:
29  ParameterSampler(size_t starter_width, size_t starter_height, size_t dx,
30  size_t dy);
31 
32  /* Getters for all the parameters:
33  * theta: rotation around the x axis.
34  * phi: rotation around the y axis.
35  * gamma: rotation around the z axis.
36  * dz: distance of the object from the camera.
37  * focal: focal length of the camera used.
38  * transform: The transformation matrix built from the above parameters.
39  * warped_corners: The four corners of the object in the warped image.
40  */
41  double get_theta();
42  double get_phi();
43  double get_gamma();
44  std::vector<double> get_theta_means();
45  std::vector<double> get_phi_means();
46  std::vector<double> get_gamma_means();
47  double get_theta_stdev();
48  double get_phi_stdev();
49  double get_gamma_stdev();
50  size_t get_dz();
51  double get_focal();
52  Eigen::Matrix<float, 3, 3> get_transform();
53  std::vector<Eigen::Vector3f> get_warped_corners();
54 
55  /* Setter for warped_corners, built after applying the transformation
56  * matrix on the corners of the starter image.
57  * Order of warped_corners is top_left, top_right, bottom_left, bottom_right
58  */
59  void set_warped_corners(const std::vector<Eigen::Vector3f> &warped_corners);
60 
61  /* Function to sample all the parameters needed to build a transform, and
62  * then also build the transform.
63  */
64  void sample(size_t background_width, size_t background_height, size_t seed,
65  size_t row_number);
66 
67  private:
68  size_t starter_width_;
69  size_t starter_height_;
70  size_t max_depth_ = 13000;
71  double angle_stdev_ = 20.0;
72  double focal_stdev_ = 40.0;
73  std::vector<double> theta_means_ = {0.0};
74  std::vector<double> phi_means_ = {0.0};
75  std::vector<double> gamma_means_ = {0.0};
76  double theta_;
77  double phi_;
78  double gamma_;
79  size_t dx_;
80  size_t dy_;
81  size_t dz_;
82  double focal_;
83  Eigen::Matrix<float, 3, 3> transform_;
84  std::vector<Eigen::Vector3f> warped_corners_;
85 };
86 
87 } // namespace one_shot_object_detection
88 } // namespace turi
89 
90 #endif // TURI_PARAMETER_SAMPLER_H_