提交 bec48231 编写于 作者: W Waleed Abdulla

Clean up mage resizing code.

This doesn’t change the functionality, other than
cleaning up the code to prepare for future changes
to support detection on different image sizes.
上级 eb683957
...@@ -88,13 +88,16 @@ class Config(object): ...@@ -88,13 +88,16 @@ class Config(object):
MINI_MASK_SHAPE = (56, 56) # (height, width) of the mini-mask MINI_MASK_SHAPE = (56, 56) # (height, width) of the mini-mask
# Input image resizing # Input image resizing
# Images are resized such that the smallest side is >= IMAGE_MIN_DIM and # Images are resized such that the small side is IMAGE_MIN_DIM and
# the longest side is <= IMAGE_MAX_DIM. In case both conditions can't # the long side is <= IMAGE_MAX_DIM. If both conditions can't be
# be satisfied together the IMAGE_MAX_DIM is enforced. # 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
IMAGE_RESIZE_MODE = "square"
IMAGE_MIN_DIM = 800 IMAGE_MIN_DIM = 800
IMAGE_MAX_DIM = 1024 IMAGE_MAX_DIM = 1024
# If True, pad images with zeros such that they're (max_dim by max_dim)
IMAGE_PADDING = True # currently, the False option is not supported
# Image mean (RGB) # Image mean (RGB)
MEAN_PIXEL = np.array([123.7, 116.8, 103.9]) MEAN_PIXEL = np.array([123.7, 116.8, 103.9])
......
...@@ -1198,7 +1198,7 @@ def load_image_gt(dataset, config, image_id, augment=False, augmentation=None, ...@@ -1198,7 +1198,7 @@ def load_image_gt(dataset, config, image_id, augment=False, augmentation=None,
image, image,
min_dim=config.IMAGE_MIN_DIM, min_dim=config.IMAGE_MIN_DIM,
max_dim=config.IMAGE_MAX_DIM, max_dim=config.IMAGE_MAX_DIM,
padding=config.IMAGE_PADDING) mode=config.IMAGE_RESIZE_MODE)
mask = utils.resize_mask(mask, scale, padding) mask = utils.resize_mask(mask, scale, padding)
# Random horizontal flips. # Random horizontal flips.
...@@ -2313,13 +2313,13 @@ class MaskRCNN(): ...@@ -2313,13 +2313,13 @@ class MaskRCNN():
image_metas = [] image_metas = []
windows = [] windows = []
for image in images: for image in images:
# Resize image to fit the model expected size # Resize image
# TODO: move resizing to mold_image() # TODO: move resizing to mold_image()
molded_image, window, scale, padding = utils.resize_image( molded_image, window, scale, padding = utils.resize_image(
image, image,
min_dim=self.config.IMAGE_MIN_DIM, min_dim=self.config.IMAGE_MIN_DIM,
max_dim=self.config.IMAGE_MAX_DIM, max_dim=self.config.IMAGE_MAX_DIM,
padding=self.config.IMAGE_PADDING) mode=self.config.IMAGE_RESIZE_MODE)
molded_image = mold_image(molded_image, self.config) molded_image = mold_image(molded_image, self.config)
# Build image_meta # Build image_meta
image_meta = compose_image_meta( image_meta = compose_image_meta(
......
...@@ -386,15 +386,17 @@ class Dataset(object): ...@@ -386,15 +386,17 @@ class Dataset(object):
return mask, class_ids return mask, class_ids
def resize_image(image, min_dim=None, max_dim=None, padding=False): def resize_image(image, min_dim=None, max_dim=None, mode="square"):
""" """Resizes an image keeping the aspect ratio unchanged.
Resizes an image keeping the aspect ratio.
min_dim: if provided, resizes the image such that it's smaller min_dim: if provided, resizes the image such that it's smaller
dimension == min_dim dimension == min_dim
max_dim: if provided, ensures that the image longest side doesn't max_dim: if provided, ensures that the image longest side doesn't
exceed this value. exceed this value.
padding: If true, pads image with zeros so it's size is max_dim x max_dim mode: Resizing mode.
none: No resizing. Return the image unchanged
square: Resize and pad with zeros to get a square image
of size [max_dim, max_dim]
Returns: Returns:
image: the resized image image: the resized image
...@@ -409,6 +411,10 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False): ...@@ -409,6 +411,10 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False):
h, w = image.shape[:2] h, w = image.shape[:2]
window = (0, 0, h, w) window = (0, 0, h, w)
scale = 1 scale = 1
padding = [(0, 0), (0, 0), (0, 0)]
if mode == "none":
return image, window, scale, padding
# Scale? # Scale?
if min_dim: if min_dim:
...@@ -424,7 +430,7 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False): ...@@ -424,7 +430,7 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False):
image = scipy.misc.imresize( image = scipy.misc.imresize(
image, (round(h * scale), round(w * scale))) image, (round(h * scale), round(w * scale)))
# Need padding? # Need padding?
if padding: if mode == "square":
# Get new height and width # Get new height and width
h, w = image.shape[:2] h, w = image.shape[:2]
top_pad = (max_dim - h) // 2 top_pad = (max_dim - h) // 2
...@@ -434,6 +440,8 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False): ...@@ -434,6 +440,8 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False):
padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)] padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
image = np.pad(image, padding, mode='constant', constant_values=0) image = np.pad(image, padding, mode='constant', constant_values=0)
window = (top_pad, left_pad, h + top_pad, w + left_pad) window = (top_pad, left_pad, h + top_pad, w + left_pad)
else:
raise Exception("Mode {} not supported".format(mode))
return image, window, scale, padding return image, window, scale, padding
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册