提交 5d034a39 编写于 作者: C cheng cheng 提交者: Niu Chong

Dev crop with random size (#1468)

* random size crop proto

* ImagePreprocessImpl::<kCropWithRandomSize>

* clang format

* MaxVal


Former-commit-id: c027432320cc0f03248f9165994150fce058f00a
上级 49dd19bc
......@@ -17,10 +17,25 @@ message ImageCrop {
required int32 height = 5;
}
message ImageCropWithRandomSize {
message AspectRatioRange {
optional float min = 1 [default = 0.75];
optional float max = 2 [default = 1.33];
}
message AreaRange {
optional float min = 1 [default = 0.05];
optional float max = 2 [default = 1.0];
}
required AspectRatioRange aspect_ratio_range = 1;
required AreaRange area_range = 2;
optional int32 max_attempts = 3 [default = 100];
}
message ImagePreprocess {
oneof preprocess {
ImageResize resize = 1;
ImageCrop crop = 2;
ImageMirror mirror = 3;
ImageCropWithRandomSize crop_with_random_size = 3;
ImageMirror mirror = 4;
}
}
......@@ -2,6 +2,15 @@
namespace oneflow {
namespace {
float GetRandomFloatValue(float min, float max, std::function<int32_t(void)> NextRandomInt) {
float ratio = static_cast<float>(NextRandomInt()) / static_cast<float>(MaxVal<int32_t>());
return (max - min) * ratio + min;
}
} // namespace
void ImagePreprocessImpl<PreprocessCase::kResize>::DoPreprocess(
cv::Mat* image, const ImagePreprocess& preprocess_conf,
std::function<int32_t(void)> NextRandomInt) const {
......@@ -24,8 +33,10 @@ void ImagePreprocessImpl<PreprocessCase::kCrop>::DoPreprocess(
CHECK_LE(width, image->cols);
CHECK_LE(height, image->rows);
if (crop.random_xy()) {
x = NextRandomInt() % (image->cols - width);
y = NextRandomInt() % (image->rows - height);
int32_t x_max = (image->cols - width);
int32_t y_max = (image->rows - height);
x = x_max > 0 ? (NextRandomInt() % x_max) : x_max;
y = y_max > 0 ? (NextRandomInt() % y_max) : y_max;
} else {
CHECK_LE(x, image->cols - width);
CHECK_LE(y, image->rows - height);
......@@ -33,6 +44,38 @@ void ImagePreprocessImpl<PreprocessCase::kCrop>::DoPreprocess(
*image = (*image)(cv::Rect(x, y, width, height));
}
void ImagePreprocessImpl<PreprocessCase::kCropWithRandomSize>::DoPreprocess(
cv::Mat* image, const ImagePreprocess& preprocess_conf,
std::function<int32_t(void)> NextRandomInt) const {
CHECK(preprocess_conf.has_crop_with_random_size());
const ImageCropWithRandomSize& conf = preprocess_conf.crop_with_random_size();
int32_t max_attempts = conf.max_attempts();
float area_min = conf.area_range().min();
float area_max = conf.area_range().max();
float ratio_min = conf.aspect_ratio_range().min();
float ratio_max = conf.aspect_ratio_range().max();
CHECK_LE(area_min, area_max);
CHECK_GT(area_min, 0.0);
CHECK_LE(area_max, 1.0);
CHECK_LE(ratio_min, ratio_max);
CHECK_GT(ratio_min, 0.0);
while (max_attempts--) {
float area_size = GetRandomFloatValue(area_min, area_max, NextRandomInt) * image->total();
float aspect_ratio = GetRandomFloatValue(ratio_min, ratio_max, NextRandomInt);
float height_float = sqrt(area_size / aspect_ratio);
int32_t height = static_cast<int32_t>(height_float);
int32_t width = static_cast<int32_t>(height_float * aspect_ratio);
if (width <= image->cols && height <= image->rows) {
int32_t x_max = (image->cols - width);
int32_t y_max = (image->rows - height);
int32_t x = x_max > 0 ? (NextRandomInt() % x_max) : x_max;
int32_t y = y_max > 0 ? (NextRandomInt() % y_max) : y_max;
*image = (*image)(cv::Rect(x, y, width, height));
return;
}
}
}
void ImagePreprocessImpl<PreprocessCase::kMirror>::DoPreprocess(
cv::Mat* image, const ImagePreprocess& preprocess_conf,
std::function<int32_t(void)> NextRandomInt) const {
......
......@@ -42,6 +42,14 @@ class ImagePreprocessImpl<PreprocessCase::kCrop> final : public ImagePreprocessI
std::function<int32_t(void)> NextRandomInt) const override;
};
template<>
class ImagePreprocessImpl<PreprocessCase::kCropWithRandomSize> final : public ImagePreprocessIf {
public:
private:
void DoPreprocess(cv::Mat* image, const ImagePreprocess& preprocess_conf,
std::function<int32_t(void)> NextRandomInt) const override;
};
template<>
class ImagePreprocessImpl<PreprocessCase::kMirror> final : public ImagePreprocessIf {
public:
......@@ -53,7 +61,8 @@ class ImagePreprocessImpl<PreprocessCase::kMirror> final : public ImagePreproces
#define PREPROCESS_CASE_SEQ \
OF_PP_MAKE_TUPLE_SEQ(PreprocessCase::kResize) \
OF_PP_MAKE_TUPLE_SEQ(PreprocessCase::kMirror) \
OF_PP_MAKE_TUPLE_SEQ(PreprocessCase::kCrop)
OF_PP_MAKE_TUPLE_SEQ(PreprocessCase::kCrop) \
OF_PP_MAKE_TUPLE_SEQ(PreprocessCase::kCropWithRandomSize)
ImagePreprocessIf* GetImagePreprocess(PreprocessCase);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册