Skip to content

Commit

Permalink
Make center_crop_and_resize support non-quadratics
Browse files Browse the repository at this point in the history
Make the function center_crop_and_resize() support non-quadratic
output shapes and crop padding shapes. The arguments image_size and
crop_padding should be able to either be scalars (like they currently
are) or lists or tuples containing both a height and a width.
  • Loading branch information
krikru committed Aug 9, 2019
1 parent d740fc8 commit 356f107
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions efficientnet/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,37 @@ def center_crop_and_resize(image, image_size, crop_padding=32, interpolation="bi
assert image.ndim in {2, 3}
assert interpolation in MAP_INTERPOLATION_TO_ORDER.keys()

h, w = image.shape[:2]
in_h, in_w = image.shape[:2]

padded_center_crop_size = int(
(image_size / (image_size + crop_padding)) * min(h, w)
)
offset_height = ((h - padded_center_crop_size) + 1) // 2
offset_width = ((w - padded_center_crop_size) + 1) // 2
if isinstance(image_size, (int, float)):
out_h = out_w = image_size
else:
out_h, out_w = image_size

if isinstance(crop_padding, (int, float)):
crop_padding_h = crop_padding_w = crop_padding
else:
crop_padding_h, crop_padding_w = crop_padding

padded_center_crop_shape_post_scaling = (out_h + crop_padding_h,
out_w + crop_padding_w)

inv_scale = min(in_h / padded_center_crop_shape_post_scaling[0],
in_w / padded_center_crop_shape_post_scaling[1])

unpadded_center_crop_size_pre_scaling = (round(out_h * inv_scale),
round(out_w * inv_scale))

offset_h = ((in_h - unpadded_center_crop_size_pre_scaling[0]) + 1) // 2
offset_w = ((in_w - unpadded_center_crop_size_pre_scaling[1]) + 1) // 2

image_crop = image[
offset_height: padded_center_crop_size + offset_height,
offset_width: padded_center_crop_size + offset_width,
offset_h : unpadded_center_crop_size_pre_scaling[0] + offset_h,
offset_w : unpadded_center_crop_size_pre_scaling[1] + offset_w,
]
resized_image = resize(
image_crop,
(image_size, image_size),
(out_h, out_w),
order=MAP_INTERPOLATION_TO_ORDER[interpolation],
preserve_range=True,
)
Expand Down

0 comments on commit 356f107

Please sign in to comment.