hetong007 commented on a change in pull request #11027: Add standard ResNet
data augmentation for ImageRecordIter
URL: https://github.com/apache/incubator-mxnet/pull/11027#discussion_r195586629
##########
File path: src/io/image_aug_default.cc
##########
@@ -218,10 +265,93 @@ class DefaultImageAugmenter : public ImageAugmenter {
res = src;
}
+ if (param_.random_resized_crop) {
+ // random resize crop
+ CHECK(param_.min_random_scale == 1.0f &&
+ param_.max_random_scale == 1.0f &&
+ param_.min_crop_size == -1 &&
+ param_.max_crop_size == -1 &&
+ !param_.rand_crop) <<
+ "\nSetting random_resized_crop to true conflicts with "
+ "min_random_scale, max_random_scale, "
+ "min_crop_size, max_crop_size, "
+ "and rand_crop.";
+ if (param_.max_random_area != 1.0f || param_.min_random_area != 1.0f
+ || param_.max_aspect_ratio != 1.0f || param_.min_aspect_ratio !=
1.0f) {
+ CHECK(param_.min_aspect_ratio > 0.0f);
+ CHECK(param_.min_random_area <= param_.max_random_area);
+ CHECK(param_.min_aspect_ratio <= param_.max_aspect_ratio);
+ std::uniform_real_distribution<float>
rand_uniform_area(param_.min_random_area,
+
param_.max_random_area);
+ std::uniform_real_distribution<float>
rand_uniform_ratio(param_.min_aspect_ratio,
+
param_.max_aspect_ratio);
+ std::uniform_real_distribution<float> rand_uniform(0, 1);
+ float area = res.rows * res.cols;
+ bool attemp = false;
+ for (int i = 0; i < 10; ++i) {
+ float rand_area = rand_uniform_area(*prnd);
+ float ratio = rand_uniform_ratio(*prnd);
+ float target_area = area * rand_area;
+ int y_area = std::round(std::sqrt(target_area / ratio));
+ int x_area = std::round(std::sqrt(target_area * ratio));
+ if (rand_uniform(*prnd) > 0.5) {
+ float temp_y_area = y_area;
+ y_area = x_area;
+ x_area = temp_y_area;
+ }
+ if (y_area <= res.rows && x_area <= res.cols) {
+ // random crop
+ index_t rand_y_area =
+ std::uniform_int_distribution<index_t>(0, res.rows -
y_area)(*prnd);
+ index_t rand_x_area =
+ std::uniform_int_distribution<index_t>(0, res.cols -
x_area)(*prnd);
+ cv::Rect roi(rand_x_area, rand_y_area, x_area, y_area);
+ int interpolation_method = GetInterMethod(param_.inter_method,
x_area, y_area,
+ param_.data_shape[2],
+
param_.data_shape[1], prnd);
+ cv::resize(res(roi), res, cv::Size(param_.data_shape[2],
param_.data_shape[1]),
+ 0, 0, interpolation_method);
+ attemp = true;
+ break;
+ }
+ }
+ if (!attemp) {
+ // center crop
+ int interpolation_method = GetInterMethod(param_.inter_method,
res.cols, res.rows,
+ param_.data_shape[2],
+ param_.data_shape[1],
prnd);
+ if (res.rows < param_.data_shape[1]) {
+ index_t new_cols =
static_cast<index_t>(static_cast<float>(param_.data_shape[1]) /
+
static_cast<float>(res.rows) *
+
static_cast<float>(res.cols));
+ cv::resize(res, res, cv::Size(new_cols, param_.data_shape[1]),
+ 0, 0, interpolation_method);
+ }
+ if (res.cols < param_.data_shape[2]) {
+ index_t new_rows =
static_cast<index_t>(static_cast<float>(param_.data_shape[2]) /
+
static_cast<float>(res.cols) *
+
static_cast<float>(res.rows));
+ cv::resize(res, res, cv::Size(param_.data_shape[2], new_rows),
+ 0, 0, interpolation_method);
+ }
+ CHECK(static_cast<index_t>(res.rows) >= param_.data_shape[1]
+ && static_cast<index_t>(res.cols) >= param_.data_shape[2])
+ << "input image size smaller than input shape";
+ index_t center_y = res.rows - param_.data_shape[1];
+ index_t center_x = res.cols - param_.data_shape[2];
+ center_y /= 2;
+ center_x /= 2;
+ cv::Rect roi(center_x, center_y, param_.data_shape[2],
param_.data_shape[1]);
+ res = res(roi);
+ }
+ random_resized_crop_exec = true;
+ }
+ }
// normal augmentation by affine transformation.
if (param_.max_rotate_angle > 0 || param_.max_shear_ratio > 0.0f
Review comment:
It is possible that users want to add rotation on top of random resized crop.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services