Changed the random_cropper so that it samples background patches uniformly

across scales regardless of the input image size.  Previously, if you gave
really large images or really small images it had a bias towards giving only
large patches or small patches respectively.
This commit is contained in:
Davis King 2017-08-18 05:30:11 -04:00
parent 4541a1b90a
commit 98cf44e8e3
1 changed files with 8 additions and 6 deletions

View File

@ -235,7 +235,7 @@ namespace dlib
} }
else else
{ {
crop_rect = make_random_cropping_rect_resnet(img); crop_rect = make_random_cropping_rect(img);
} }
should_flip_crop = randomly_flip && rnd.get_random_double() > 0.5; should_flip_crop = randomly_flip && rnd.get_random_double() > 0.5;
const double angle = rnd.get_double_in_range(-max_rotation_degrees, max_rotation_degrees)*pi/180; const double angle = rnd.get_double_in_range(-max_rotation_degrees, max_rotation_degrees)*pi/180;
@ -266,16 +266,18 @@ namespace dlib
} }
template <typename image_type> template <typename image_type>
rectangle make_random_cropping_rect_resnet( rectangle make_random_cropping_rect(
const image_type& img_ const image_type& img_
) )
{ {
const_image_view<image_type> img(img_); const_image_view<image_type> img(img_);
// figure out what rectangle we want to crop from the image // Figure out what rectangle we want to crop from the image. We are going to
double mins = 0.1, maxs = 0.95; // crop out an image of size this->dims, so we pick a random scale factor that
// lets this random box be either as big as it can be while still fitting in
// the image or as small as a 3x zoomed in box randomly somewhere in the image.
double mins = 1.0/3.0, maxs = std::min(img.nr()/(double)dims.rows, img.nc()/(double)dims.cols);
auto scale = rnd.get_double_in_range(mins, maxs); auto scale = rnd.get_double_in_range(mins, maxs);
auto size = scale*std::min(img.nr(), img.nc()); rectangle rect(scale*dims.cols, scale*dims.rows);
rectangle rect(size, size);
// randomly shift the box around // randomly shift the box around
point offset(rnd.get_random_32bit_number()%(img.nc()-rect.width()), point offset(rnd.get_random_32bit_number()%(img.nc()-rect.width()),
rnd.get_random_32bit_number()%(img.nr()-rect.height())); rnd.get_random_32bit_number()%(img.nr()-rect.height()));