Merge pull request #5184 from shaibagon/fix_batch_norm_param_upgrade
[platform/upstream/caffeonacl.git] / include / caffe / data_transformer.hpp
1 #ifndef CAFFE_DATA_TRANSFORMER_HPP
2 #define CAFFE_DATA_TRANSFORMER_HPP
3
4 #include <vector>
5
6 #include "caffe/blob.hpp"
7 #include "caffe/common.hpp"
8 #include "caffe/proto/caffe.pb.h"
9
10 namespace caffe {
11
12 /**
13  * @brief Applies common transformations to the input data, such as
14  * scaling, mirroring, substracting the image mean...
15  */
16 template <typename Dtype>
17 class DataTransformer {
18  public:
19   explicit DataTransformer(const TransformationParameter& param, Phase phase);
20   virtual ~DataTransformer() {}
21
22   /**
23    * @brief Initialize the Random number generations if needed by the
24    *    transformation.
25    */
26   void InitRand();
27
28   /**
29    * @brief Applies the transformation defined in the data layer's
30    * transform_param block to the data.
31    *
32    * @param datum
33    *    Datum containing the data to be transformed.
34    * @param transformed_blob
35    *    This is destination blob. It can be part of top blob's data if
36    *    set_cpu_data() is used. See data_layer.cpp for an example.
37    */
38   void Transform(const Datum& datum, Blob<Dtype>* transformed_blob);
39
40   /**
41    * @brief Applies the transformation defined in the data layer's
42    * transform_param block to a vector of Datum.
43    *
44    * @param datum_vector
45    *    A vector of Datum containing the data to be transformed.
46    * @param transformed_blob
47    *    This is destination blob. It can be part of top blob's data if
48    *    set_cpu_data() is used. See memory_layer.cpp for an example.
49    */
50   void Transform(const vector<Datum> & datum_vector,
51                 Blob<Dtype>* transformed_blob);
52
53 #ifdef USE_OPENCV
54   /**
55    * @brief Applies the transformation defined in the data layer's
56    * transform_param block to a vector of Mat.
57    *
58    * @param mat_vector
59    *    A vector of Mat containing the data to be transformed.
60    * @param transformed_blob
61    *    This is destination blob. It can be part of top blob's data if
62    *    set_cpu_data() is used. See memory_layer.cpp for an example.
63    */
64   void Transform(const vector<cv::Mat> & mat_vector,
65                 Blob<Dtype>* transformed_blob);
66
67   /**
68    * @brief Applies the transformation defined in the data layer's
69    * transform_param block to a cv::Mat
70    *
71    * @param cv_img
72    *    cv::Mat containing the data to be transformed.
73    * @param transformed_blob
74    *    This is destination blob. It can be part of top blob's data if
75    *    set_cpu_data() is used. See image_data_layer.cpp for an example.
76    */
77   void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob);
78 #endif  // USE_OPENCV
79
80   /**
81    * @brief Applies the same transformation defined in the data layer's
82    * transform_param block to all the num images in a input_blob.
83    *
84    * @param input_blob
85    *    A Blob containing the data to be transformed. It applies the same
86    *    transformation to all the num images in the blob.
87    * @param transformed_blob
88    *    This is destination blob, it will contain as many images as the
89    *    input blob. It can be part of top blob's data.
90    */
91   void Transform(Blob<Dtype>* input_blob, Blob<Dtype>* transformed_blob);
92
93   /**
94    * @brief Infers the shape of transformed_blob will have when
95    *    the transformation is applied to the data.
96    *
97    * @param datum
98    *    Datum containing the data to be transformed.
99    */
100   vector<int> InferBlobShape(const Datum& datum);
101   /**
102    * @brief Infers the shape of transformed_blob will have when
103    *    the transformation is applied to the data.
104    *    It uses the first element to infer the shape of the blob.
105    *
106    * @param datum_vector
107    *    A vector of Datum containing the data to be transformed.
108    */
109   vector<int> InferBlobShape(const vector<Datum> & datum_vector);
110   /**
111    * @brief Infers the shape of transformed_blob will have when
112    *    the transformation is applied to the data.
113    *    It uses the first element to infer the shape of the blob.
114    *
115    * @param mat_vector
116    *    A vector of Mat containing the data to be transformed.
117    */
118 #ifdef USE_OPENCV
119   vector<int> InferBlobShape(const vector<cv::Mat> & mat_vector);
120   /**
121    * @brief Infers the shape of transformed_blob will have when
122    *    the transformation is applied to the data.
123    *
124    * @param cv_img
125    *    cv::Mat containing the data to be transformed.
126    */
127   vector<int> InferBlobShape(const cv::Mat& cv_img);
128 #endif  // USE_OPENCV
129
130  protected:
131    /**
132    * @brief Generates a random integer from Uniform({0, 1, ..., n-1}).
133    *
134    * @param n
135    *    The upperbound (exclusive) value of the random number.
136    * @return
137    *    A uniformly random integer value from ({0, 1, ..., n-1}).
138    */
139   virtual int Rand(int n);
140
141   void Transform(const Datum& datum, Dtype* transformed_data);
142   // Tranformation parameters
143   TransformationParameter param_;
144
145
146   shared_ptr<Caffe::RNG> rng_;
147   Phase phase_;
148   Blob<Dtype> data_mean_;
149   vector<Dtype> mean_values_;
150 };
151
152 }  // namespace caffe
153
154 #endif  // CAFFE_DATA_TRANSFORMER_HPP_