-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_augment.cpp
408 lines (371 loc) · 14.5 KB
/
data_augment.cpp
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
#include "caffe/cpm_data_transformer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/util/rng.hpp"
// include mask_miss
template<typename Dtype>
float CPMDataTransformer<Dtype>::augmentation_scale(Mat& img_src, Mat& img_temp, Mat& mask_miss, Mat& mask_all, MetaData& meta, int mode) {
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
float scale_multiplier;
//float scale = (param_.scale_max() - param_.scale_min()) * dice + param_.scale_min(); //linear shear into [scale_min, scale_max]
if(dice > param_.scale_prob()) {
img_temp = img_src.clone();
scale_multiplier = 1;
}
else {
float dice2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
scale_multiplier = (param_.scale_max() - param_.scale_min()) * dice2 + param_.scale_min(); //linear shear into [scale_min, scale_max]
}
float scale_abs = param_.target_dist()/meta.scale_self;
float scale = scale_abs * scale_multiplier;
//cout<<scale_multiplier<<","<<scale_abs<<","<<scale<<endl;
//cout<<img_src.cols<<","<<img_src.rows<<endl;
resize(img_src, img_temp, Size(), scale, scale, INTER_CUBIC);
if(mode>4 && has_mask){
resize(mask_miss, mask_miss, Size(), scale, scale, INTER_CUBIC);
}
if(mode>5 && has_mask){
resize(mask_all, mask_all, Size(), scale, scale, INTER_CUBIC);
}
//modify meta data
meta.objpos *= scale;
for(int i=0; i<np; i++){
meta.joint_self.joints[i] *= scale;
}
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p] *= scale;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i] *= scale;
}
}
return scale_multiplier;
}
template<typename Dtype>
Size CPMDataTransformer<Dtype>::augmentation_croppad(Mat& img_src, Mat& img_dst, Mat& mask_miss, Mat& mask_miss_aug, Mat& mask_all, Mat& mask_all_aug, MetaData& meta, int mode) {
float dice_x = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
float dice_y = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
int crop_x = param_.crop_size_x();
int crop_y = param_.crop_size_y();
float x_offset = int((dice_x - 0.5) * 2 * param_.center_perterb_max());
float y_offset = int((dice_y - 0.5) * 2 * param_.center_perterb_max());
//LOG(INFO) << "Size of img_temp is " << img_temp.cols << " " << img_temp.rows;
//LOG(INFO) << "ROI is " << x_offset << " " << y_offset << " " << min(800, img_temp.cols) << " " << min(256, img_temp.rows);
Point2i center = meta.objpos + Point2f(x_offset, y_offset);
int offset_left = -(center.x - (crop_x/2));
int offset_up = -(center.y - (crop_y/2));
// int to_pad_right = max(center.x + (crop_x - crop_x/2) - img_src.cols, 0);
// int to_pad_down = max(center.y + (crop_y - crop_y/2) - img_src.rows, 0);
img_dst = Mat::zeros(crop_y, crop_x, CV_8UC3) + Scalar(128,128,128);
mask_miss_aug = Mat::zeros(crop_y, crop_x, CV_8UC1) + Scalar(255); //for MPI, COCO with Scalar(255);
mask_all_aug = Mat::zeros(crop_y, crop_x, CV_8UC1);
for(int i=0;i<crop_y;i++){
for(int j=0;j<crop_x;j++){ //i,j on cropped
int coord_x_on_img = center.x - crop_x/2 + j;
int coord_y_on_img = center.y - crop_y/2 + i;
if(onPlane(Point(coord_x_on_img, coord_y_on_img), Size(img_src.cols, img_src.rows))){
img_dst.at<Vec3b>(i,j) = img_src.at<Vec3b>(coord_y_on_img, coord_x_on_img);
if(mode>4 && has_mask){
mask_miss_aug.at<uchar>(i,j) = mask_miss.at<uchar>(coord_y_on_img, coord_x_on_img);
}
if(mode>5 && has_mask){
mask_all_aug.at<uchar>(i,j) = mask_all.at<uchar>(coord_y_on_img, coord_x_on_img);
}
}
}
}
//modify meta data
Point2f offset(offset_left, offset_up);
meta.objpos += offset;
for(int i=0; i<np; i++){
meta.joint_self.joints[i] += offset;
}
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p] += offset;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i] += offset;
}
}
return Size(x_offset, y_offset);
}
template<typename Dtype>
bool CPMDataTransformer<Dtype>::augmentation_flip(Mat& img_src, Mat& img_aug, Mat& mask_miss, Mat& mask_all, MetaData& meta, int mode) {
bool doflip;
if(param_.aug_way() == "rand"){
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
doflip = (dice <= param_.flip_prob());
}
else if(param_.aug_way() == "table"){
doflip = (aug_flips[meta.write_number][meta.epoch % param_.num_total_augs()] == 1);
}
else {
doflip = 0;
LOG(INFO) << "Unhandled exception!!!!!!";
}
if(doflip){
flip(img_src, img_aug, 1);
int w = img_src.cols;
if(mode>4 && has_mask){
flip(mask_miss, mask_miss, 1);
}
if(mode>5 && has_mask){
flip(mask_all, mask_all, 1);
}
meta.objpos.x = w - 1 - meta.objpos.x;
for(int i=0; i<np; i++){
meta.joint_self.joints[i].x = w - 1 - meta.joint_self.joints[i].x;
}
if(param_.transform_body_joint())
swapLeftRight(meta.joint_self);
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p].x = w - 1 - meta.objpos_other[p].x;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i].x = w - 1 - meta.joint_others[p].joints[i].x;
}
if(param_.transform_body_joint())
swapLeftRight(meta.joint_others[p]);
}
}
else {
img_aug = img_src.clone();
}
return doflip;
}
template<typename Dtype>
float CPMDataTransformer<Dtype>::augmentation_rotate(Mat& img_src, Mat& img_dst, Mat& mask_miss, Mat& mask_all, MetaData& meta, int mode) {
float degree;
if(param_.aug_way() == "rand"){
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
degree = (dice - 0.5) * 2 * param_.max_rotate_degree();
}
else if(param_.aug_way() == "table"){
degree = aug_degs[meta.write_number][meta.epoch % param_.num_total_augs()];
}
else {
degree = 0;
LOG(INFO) << "Unhandled exception!!!!!!";
}
Point2f center(img_src.cols/2.0, img_src.rows/2.0);
Mat R = getRotationMatrix2D(center, degree, 1.0);
Rect bbox = RotatedRect(center, img_src.size(), degree).boundingRect();
// adjust transformation matrix
R.at<double>(0,2) += bbox.width/2.0 - center.x;
R.at<double>(1,2) += bbox.height/2.0 - center.y;
//LOG(INFO) << "R=[" << R.at<double>(0,0) << " " << R.at<double>(0,1) << " " << R.at<double>(0,2) << ";"
// << R.at<double>(1,0) << " " << R.at<double>(1,1) << " " << R.at<double>(1,2) << "]";
warpAffine(img_src, img_dst, R, bbox.size(), INTER_CUBIC, BORDER_CONSTANT, Scalar(128,128,128));
if(mode >4 && has_mask){
warpAffine(mask_miss, mask_miss, R, bbox.size(), INTER_CUBIC, BORDER_CONSTANT, Scalar(255)); //Scalar(0) for MPI, COCO with Scalar(255);
}
if(mode >5 && has_mask){
warpAffine(mask_all, mask_all, R, bbox.size(), INTER_CUBIC, BORDER_CONSTANT, Scalar(0));
}
//adjust meta data
RotatePoint(meta.objpos, R);
for(int i=0; i<np; i++){
RotatePoint(meta.joint_self.joints[i], R);
}
for(int p=0; p<meta.numOtherPeople; p++){
RotatePoint(meta.objpos_other[p], R);
for(int i=0; i<np; i++){
RotatePoint(meta.joint_others[p].joints[i], R);
}
}
return degree;
}
// end here
template<typename Dtype>
float CPMDataTransformer<Dtype>::augmentation_scale(Mat& img_src, Mat& img_temp, MetaData& meta) {
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
float scale_multiplier;
//float scale = (param_.scale_max() - param_.scale_min()) * dice + param_.scale_min(); //linear shear into [scale_min, scale_max]
if(dice > param_.scale_prob()) {
img_temp = img_src.clone();
scale_multiplier = 1;
}
else {
float dice2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
scale_multiplier = (param_.scale_max() - param_.scale_min()) * dice2 + param_.scale_min(); //linear shear into [scale_min, scale_max]
}
float scale_abs = param_.target_dist()/meta.scale_self;
float scale = scale_abs * scale_multiplier;
resize(img_src, img_temp, Size(), scale, scale, INTER_CUBIC);
//modify meta data
meta.objpos *= scale;
for(int i=0; i<np; i++){
meta.joint_self.joints[i] *= scale;
}
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p] *= scale;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i] *= scale;
}
}
return scale_multiplier;
}
template<typename Dtype>
bool CPMDataTransformer<Dtype>::onPlane(Point p, Size img_size) {
if(p.x < 0 || p.y < 0) return false;
if(p.x >= img_size.width || p.y >= img_size.height) return false;
return true;
}
template<typename Dtype>
Size CPMDataTransformer<Dtype>::augmentation_croppad(Mat& img_src, Mat& img_dst, MetaData& meta) {
float dice_x = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
float dice_y = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //[0,1]
int crop_x = param_.crop_size_x();
int crop_y = param_.crop_size_y();
float x_offset = int((dice_x - 0.5) * 2 * param_.center_perterb_max());
float y_offset = int((dice_y - 0.5) * 2 * param_.center_perterb_max());
//LOG(INFO) << "Size of img_temp is " << img_temp.cols << " " << img_temp.rows;
//LOG(INFO) << "ROI is " << x_offset << " " << y_offset << " " << min(800, img_temp.cols) << " " << min(256, img_temp.rows);
Point2i center = meta.objpos + Point2f(x_offset, y_offset);
int offset_left = -(center.x - (crop_x/2));
int offset_up = -(center.y - (crop_y/2));
// int to_pad_right = max(center.x + (crop_x - crop_x/2) - img_src.cols, 0);
// int to_pad_down = max(center.y + (crop_y - crop_y/2) - img_src.rows, 0);
img_dst = Mat::zeros(crop_y, crop_x, CV_8UC3) + Scalar(128,128,128);
for(int i=0;i<crop_y;i++){
for(int j=0;j<crop_x;j++){ //i,j on cropped
int coord_x_on_img = center.x - crop_x/2 + j;
int coord_y_on_img = center.y - crop_y/2 + i;
if(onPlane(Point(coord_x_on_img, coord_y_on_img), Size(img_src.cols, img_src.rows))){
img_dst.at<Vec3b>(i,j) = img_src.at<Vec3b>(coord_y_on_img, coord_x_on_img);
}
}
}
//modify meta data
Point2f offset(offset_left, offset_up);
meta.objpos += offset;
for(int i=0; i<np; i++){
meta.joint_self.joints[i] += offset;
}
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p] += offset;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i] += offset;
}
}
return Size(x_offset, y_offset);
}
template<typename Dtype>
void CPMDataTransformer<Dtype>::swapLeftRight(Joints& j) {
std::vector<int> right;
std::vector<int> left;
if(np==56){
right={3,4,5, 9,10,11,15,17};
left= {6,7,8,12,13,14,16,18};
}
else if(np==43){
right = {3,4,5,9,10,11};
left = {6,7,8,12,13,14};
}
else if(np==38){
right ={10,12,6,7,8};
left = {9,11,3,4,5};
}
else if(np==24){
right={2,5,7,9,12,13,15,17,19,23,24};
left={1,4,6,8,10,11,14,16,18,21,22};
}
else if(np==62){
right = {3,4,5, 9,10,11,15,17,20};
left = {6,7,8,12,13,14,16,18,19};
}
for(int i=0; i<right.size(); i++){
int ri = right[i] - 1;
int li = left[i] - 1;
Point2f temp = j.joints[ri];
j.joints[ri] = j.joints[li];
j.joints[li] = temp;
int temp_v = j.isVisible[ri];
j.isVisible[ri] = j.isVisible[li];
j.isVisible[li] = temp_v;
}
}
template<typename Dtype>
bool CPMDataTransformer<Dtype>::augmentation_flip(Mat& img_src, Mat& img_aug, MetaData& meta) {
bool doflip;
if(param_.aug_way() == "rand"){
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
doflip = (dice <= param_.flip_prob());
}
else if(param_.aug_way() == "table"){
doflip = (aug_flips[meta.write_number][meta.epoch % param_.num_total_augs()] == 1);
}
else {
doflip = 0;
LOG(ERROR) << "Unhandled exception!!!!!!";
}
if(doflip){
flip(img_src, img_aug, 1);
int w = img_src.cols;
meta.objpos.x = w - 1 - meta.objpos.x;
for(int i=0; i<np; i++){
meta.joint_self.joints[i].x = w - 1 - meta.joint_self.joints[i].x;
}
if(param_.transform_body_joint())
swapLeftRight(meta.joint_self);
for(int p=0; p<meta.numOtherPeople; p++){
meta.objpos_other[p].x = w - 1 - meta.objpos_other[p].x;
for(int i=0; i<np; i++){
meta.joint_others[p].joints[i].x = w - 1 - meta.joint_others[p].joints[i].x;
}
if(param_.transform_body_joint())
swapLeftRight(meta.joint_others[p]);
}
}
else {
img_aug = img_src.clone();
}
return doflip;
}
template<typename Dtype>
void CPMDataTransformer<Dtype>::RotatePoint(Point2f& p, Mat R){
Mat point(3,1,CV_64FC1);
point.at<double>(0,0) = p.x;
point.at<double>(1,0) = p.y;
point.at<double>(2,0) = 1;
Mat new_point = R * point;
p.x = new_point.at<double>(0,0);
p.y = new_point.at<double>(1,0);
}
template<typename Dtype>
float CPMDataTransformer<Dtype>::augmentation_rotate(Mat& img_src, Mat& img_dst, MetaData& meta) {
float degree;
if(param_.aug_way() == "rand"){
float dice = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
degree = (dice - 0.5) * 2 * param_.max_rotate_degree();
}
else if(param_.aug_way() == "table"){
degree = aug_degs[meta.write_number][meta.epoch % param_.num_total_augs()];
}
else {
degree = 0;
LOG(INFO) << "Unhandled exception!!!!!!";
}
Point2f center(img_src.cols/2.0, img_src.rows/2.0);
Mat R = getRotationMatrix2D(center, degree, 1.0);
Rect bbox = RotatedRect(center, img_src.size(), degree).boundingRect();
// adjust transformation matrix
R.at<double>(0,2) += bbox.width/2.0 - center.x;
R.at<double>(1,2) += bbox.height/2.0 - center.y;
//LOG(INFO) << "R=[" << R.at<double>(0,0) << " " << R.at<double>(0,1) << " " << R.at<double>(0,2) << ";"
// << R.at<double>(1,0) << " " << R.at<double>(1,1) << " " << R.at<double>(1,2) << "]";
warpAffine(img_src, img_dst, R, bbox.size(), INTER_CUBIC, BORDER_CONSTANT, Scalar(128,128,128));
//adjust meta data
RotatePoint(meta.objpos, R);
for(int i=0; i<np; i++){
RotatePoint(meta.joint_self.joints[i], R);
}
for(int p=0; p<meta.numOtherPeople; p++){
RotatePoint(meta.objpos_other[p], R);
for(int i=0; i<np; i++){
RotatePoint(meta.joint_others[p].joints[i], R);
}
}
return degree;
}