提交 48a25fd4 编写于 作者: W Waleed Abdulla

Add pad64 mode to resize_image()

This is useful to run prediction on images of
variable sizes without resizing them to a fixed
size. While using mode=“none” is another option,
it requires that you pre-process your images to
ensure the width and height are multiples of 64.
This mode handles that automatically.
上级 f5edc82d
......@@ -88,13 +88,21 @@ class Config(object):
MINI_MASK_SHAPE = (56, 56) # (height, width) of the mini-mask
# Input image resizing
# Images are resized such that the small side is IMAGE_MIN_DIM and
# the long side is <= IMAGE_MAX_DIM. If both conditions can't be
# satisfied at the same time then IMAGE_MAX_DIM is enforced.
# Resizing modes:
# none: No resizing
# square: Pad with zeros to make it a square (MAX_DIM, MAX_DIM)
# TODO: currently, only 'square' mode is supported
# Generally, use the "square" resizing mode for training and inferencing
# and it should work well in most cases. In this mode, images are scaled
# up such that the small side is = IMAGE_MIN_DIM, but ensuring that the
# scaling doesn't make the long side > IMAGE_MAX_DIM. Then the image is
# padded with zeros to make it a square so multiple images can be put
# in one batch.
# Available resizing modes:
# none: No resizing or padding. Return the image unchanged.
# square: Resize and pad with zeros to get a square image
# of size [max_dim, max_dim].
# pad64: Pads width and height with zeros to make them multiples of 64.
# If IMAGE_MIN_DIM is not None, then scale the small side to
# that size before padding. IMAGE_MAX_DIM is ignored in this mode.
# The multiple of 64 is needed to ensure smooth scaling of feature
# maps up and down the 6 levels of the FPN pyramid (2**6=64).
IMAGE_RESIZE_MODE = "square"
IMAGE_MIN_DIM = 800
IMAGE_MAX_DIM = 1024
......
......@@ -394,9 +394,14 @@ def resize_image(image, min_dim=None, max_dim=None, mode="square"):
max_dim: if provided, ensures that the image longest side doesn't
exceed this value.
mode: Resizing mode.
none: No resizing. Return the image unchanged
none: No resizing. Return the image unchanged.
square: Resize and pad with zeros to get a square image
of size [max_dim, max_dim]
of size [max_dim, max_dim].
pad64: Pads width and height with zeros to make them multiples of 64.
If min_dim is provided, it scales the small side to >= min_dim
before padding. max_dim is ignored in this mode.
The multiple of 64 is needed to ensure smooth scaling of feature
maps up and down the 6 levels of the FPN pyramid (2**6=64).
Returns:
image: the resized image
......@@ -423,10 +428,11 @@ def resize_image(image, min_dim=None, max_dim=None, mode="square"):
# Scale up but not down
scale = max(1, min_dim / min(h, w))
# Does it exceed max dim?
if max_dim:
if max_dim and mode == "square":
image_max = max(h, w)
if round(image_max * scale) > max_dim:
scale = max_dim / image_max
# Resize image using bilinear interpolation
if scale != 1:
image = skimage.transform.resize(
......@@ -443,6 +449,27 @@ def resize_image(image, min_dim=None, max_dim=None, mode="square"):
padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
image = np.pad(image, padding, mode='constant', constant_values=0)
window = (top_pad, left_pad, h + top_pad, w + left_pad)
elif mode == "pad64":
h, w = image.shape[:2]
# Both sides must be divisible by 64
assert min_dim % 64 == 0, "Minimum dimension must be a multiple of 64"
# Height
if h % 64 > 0:
max_h = h - (h % 64) + 64
top_pad = (max_h - h) // 2
bottom_pad = max_h - h - top_pad
else:
top_pad = bottom_pad = 0
# Width
if w % 64 > 0:
max_w = w - (w % 64) + 64
left_pad = (max_w - w) // 2
right_pad = max_w - w - left_pad
else:
left_pad = right_pad = 0
padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
image = np.pad(image, padding, mode='constant', constant_values=0)
window = (top_pad, left_pad, h + top_pad, w + left_pad)
else:
raise Exception("Mode {} not supported".format(mode))
return image.astype(image_dtype), window, scale, padding
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册