Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make center_crop_and_resize support non-quadratics #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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