forked from eric612/MobileNet-YOLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_transformer.hpp
270 lines (239 loc) · 9.38 KB
/
data_transformer.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef CAFFE_DATA_TRANSFORMER_HPP
#define CAFFE_DATA_TRANSFORMER_HPP
#include <vector>
#include "google/protobuf/repeated_field.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
using google::protobuf::RepeatedPtrField;
namespace caffe {
/**
* @brief Applies common transformations to the input data, such as
* scaling, mirroring, substracting the image mean...
*/
template <typename Dtype>
class DataTransformer {
public:
explicit DataTransformer(const TransformationParameter& param, Phase phase);
virtual ~DataTransformer() {}
/**
* @brief Initialize the Random number generations if needed by the
* transformation.
*/
void InitRand();
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to the data.
*
* @param datum
* Datum containing the data to be transformed.
* @param transformed_blob
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See data_layer.cpp for an example.
*/
void Transform(const Datum& datum, Blob<Dtype>* transformed_blob, int policy_num=0);
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to a vector of Datum.
*
* @param datum_vector
* A vector of Datum containing the data to be transformed.
* @param transformed_blob
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See memory_layer.cpp for an example.
*/
void Transform(const vector<Datum> & datum_vector,
Blob<Dtype>* transformed_blob);
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to the annotated data.
*
* @param anno_datum
* AnnotatedDatum containing the data and annotation to be transformed.
* @param transformed_blob
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See annotated_data_layer.cpp for an example.
* @param transformed_anno_vec
* This is destination annotation.
*/
void Transform(const AnnotatedDatum& anno_datum,
Blob<Dtype>* transformed_blob,
RepeatedPtrField<AnnotationGroup>* transformed_anno_vec, int policy_num = 0);
void Transform(const AnnotatedDatum& anno_datum,
Blob<Dtype>* transformed_blob,
RepeatedPtrField<AnnotationGroup>* transformed_anno_vec,
bool* do_mirror, int policy_num = 0);
void Transform(const AnnotatedDatum& anno_datum,
Blob<Dtype>* transformed_blob,
vector<AnnotationGroup>* transformed_anno_vec,
bool* do_mirror, int policy_num = 0);
void Transform(const AnnotatedDatum& anno_datum,
Blob<Dtype>* transformed_blob,
vector<AnnotationGroup>* transformed_anno_vec, int policy_num = 0);
/**
* @brief Transform the annotation according to the transformation applied
* to the datum.
*
* @param anno_datum
* AnnotatedDatum containing the data and annotation to be transformed.
* @param do_resize
* If true, resize the annotation accordingly before crop.
* @param crop_bbox
* The cropped region applied to anno_datum.datum()
* @param do_mirror
* If true, meaning the datum has mirrored.
* @param transformed_anno_group_all
* Stores all transformed AnnotationGroup.
*/
void TransformAnnotation(
const AnnotatedDatum& anno_datum, const bool do_resize,
const NormalizedBBox& crop_bbox, const bool do_mirror,
RepeatedPtrField<AnnotationGroup>* transformed_anno_group_all, int policy_num = 0);
/**
* @brief Crops the datum according to bbox.
*/
void CropImage(const Datum& datum, const NormalizedBBox& bbox,
Datum* crop_datum);
/**
* @brief Crops the datum and AnnotationGroup according to bbox.
*/
void CropImage(const AnnotatedDatum& anno_datum, const NormalizedBBox& bbox,
AnnotatedDatum* cropped_anno_datum , bool has_anno = true);
/**
* @brief Expand the datum.
*/
void ExpandImage(const Datum& datum, const float expand_ratio,
NormalizedBBox* expand_bbox, Datum* expanded_datum);
/**
* @brief Expand the datum and adjust AnnotationGroup.
*/
void ExpandImage(const AnnotatedDatum& anno_datum,
AnnotatedDatum* expanded_anno_datum);
/**
* @brief Apply distortion to the datum.
*/
void DistortImage(const Datum& datum, Datum* distort_datum);
#ifdef USE_OPENCV
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to a vector of Mat.
*
* @param mat_vector
* A vector of Mat containing the data to be transformed.
* @param transformed_blob
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See memory_layer.cpp for an example.
*/
void Transform(const vector<cv::Mat> & mat_vector,
Blob<Dtype>* transformed_blob);
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to a cv::Mat
*
* @param cv_img
* cv::Mat containing the data to be transformed.
* @param transformed_blob
* This is destination blob. It can be part of top blob's data if
* set_cpu_data() is used. See image_data_layer.cpp for an example.
* @param preserve_pixel_vals
* Use with dense label images to preserve the input pixel values
* which would be labels (and thus cannot be interpolated or scaled).
*/
void Transform2(const std::vector<cv::Mat> cv_imgs, Blob<Dtype>* transformed_blob,
bool preserve_pixel_vals = false);
void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob,
NormalizedBBox* crop_bbox, bool* do_mirror, int policy_num = 0);
void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob);
/**
* @brief Crops img according to bbox.
*/
void CropImage(const cv::Mat& img, const NormalizedBBox& bbox,
cv::Mat* crop_img);
/**
* @brief Expand img to include mean value as background.
*/
void ExpandImage(const cv::Mat& img, const float expand_ratio,
NormalizedBBox* expand_bbox, cv::Mat* expand_img);
void TransformInv(const Blob<Dtype>* blob, vector<cv::Mat>* cv_imgs);
void TransformInv(const Dtype* data, cv::Mat* cv_img, const int height,
const int width, const int channels);
#endif // USE_OPENCV
/**
* @brief Applies the same transformation defined in the data layer's
* transform_param block to all the num images in a input_blob.
*
* @param input_blob
* A Blob containing the data to be transformed. It applies the same
* transformation to all the num images in the blob.
* @param transformed_blob
* This is destination blob, it will contain as many images as the
* input blob. It can be part of top blob's data.
*/
void Transform(Blob<Dtype>* input_blob, Blob<Dtype>* transformed_blob);
/**
* @brief Infers the shape of transformed_blob will have when
* the transformation is applied to the data.
*
* @param datum
* Datum containing the data to be transformed.
*/
vector<int> InferBlobShape(const Datum& datum,int policy_num = 0);
/**
* @brief Infers the shape of transformed_blob will have when
* the transformation is applied to the data.
* It uses the first element to infer the shape of the blob.
*
* @param datum_vector
* A vector of Datum containing the data to be transformed.
*/
vector<int> InferBlobShape(const vector<Datum> & datum_vector);
/**
* @brief Infers the shape of transformed_blob will have when
* the transformation is applied to the data.
* It uses the first element to infer the shape of the blob.
*
* @param mat_vector
* A vector of Mat containing the data to be transformed.
*/
#ifdef USE_OPENCV
vector<int> InferBlobShape(const vector<cv::Mat> & mat_vector);
/**
* @brief Infers the shape of transformed_blob will have when
* the transformation is applied to the data.
*
* @param cv_img
* cv::Mat containing the data to be transformed.
*/
vector<int> InferBlobShape(const cv::Mat& cv_img, int policy_num = 0);
#endif // USE_OPENCV
bool mirror_param_;
bool get_mirror() { return mirror_param_; }
protected:
/**
* @brief Generates a random integer from Uniform({0, 1, ..., n-1}).
*
* @param n
* The upperbound (exclusive) value of the random number.
* @return
* A uniformly random integer value from ({0, 1, ..., n-1}).
*/
virtual int Rand(int n);
// Transform and return the transformation information.
void Transform(const Datum& datum, Dtype* transformed_data,
NormalizedBBox* crop_bbox, bool* do_mirror);
void Transform(const Datum& datum, Dtype* transformed_data);
/**
* @brief Applies the transformation defined in the data layer's
* transform_param block to the data and return transform information.
*/
void Transform(const Datum& datum, Blob<Dtype>* transformed_blob,
NormalizedBBox* crop_bbox, bool* do_mirror, int policy_num = 0);
// Tranformation parameters
TransformationParameter param_;
shared_ptr<Caffe::RNG> rng_;
Phase phase_;
Blob<Dtype> data_mean_;
vector<Dtype> mean_values_;
};
} // namespace caffe
#endif // CAFFE_DATA_TRANSFORMER_HPP_