From bec48231ab889aa1470d2102e42bef6391dd4293 Mon Sep 17 00:00:00 2001 From: Waleed Abdulla Date: Mon, 2 Apr 2018 17:49:14 -0700 Subject: [PATCH] Clean up mage resizing code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn’t change the functionality, other than cleaning up the code to prepare for future changes to support detection on different image sizes. --- config.py | 13 ++++++++----- model.py | 6 +++--- utils.py | 18 +++++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/config.py b/config.py index 0a7b5f1..6b543ab 100644 --- a/config.py +++ b/config.py @@ -88,13 +88,16 @@ class Config(object): MINI_MASK_SHAPE = (56, 56) # (height, width) of the mini-mask # Input image resizing - # Images are resized such that the smallest side is >= IMAGE_MIN_DIM and - # the longest side is <= IMAGE_MAX_DIM. In case both conditions can't - # be satisfied together the IMAGE_MAX_DIM is enforced. + # 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 + IMAGE_RESIZE_MODE = "square" IMAGE_MIN_DIM = 800 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) MEAN_PIXEL = np.array([123.7, 116.8, 103.9]) diff --git a/model.py b/model.py index 6211c80..4feff10 100644 --- a/model.py +++ b/model.py @@ -1198,7 +1198,7 @@ def load_image_gt(dataset, config, image_id, augment=False, augmentation=None, image, min_dim=config.IMAGE_MIN_DIM, max_dim=config.IMAGE_MAX_DIM, - padding=config.IMAGE_PADDING) + mode=config.IMAGE_RESIZE_MODE) mask = utils.resize_mask(mask, scale, padding) # Random horizontal flips. @@ -2313,13 +2313,13 @@ class MaskRCNN(): image_metas = [] windows = [] for image in images: - # Resize image to fit the model expected size + # Resize image # TODO: move resizing to mold_image() molded_image, window, scale, padding = utils.resize_image( image, min_dim=self.config.IMAGE_MIN_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) # Build image_meta image_meta = compose_image_meta( diff --git a/utils.py b/utils.py index ffd753f..58ccf46 100644 --- a/utils.py +++ b/utils.py @@ -386,15 +386,17 @@ class Dataset(object): return mask, class_ids -def resize_image(image, min_dim=None, max_dim=None, padding=False): - """ - Resizes an image keeping the aspect ratio. +def resize_image(image, min_dim=None, max_dim=None, mode="square"): + """Resizes an image keeping the aspect ratio unchanged. min_dim: if provided, resizes the image such that it's smaller dimension == min_dim max_dim: if provided, ensures that the image longest side doesn't 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: image: the resized image @@ -409,6 +411,10 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False): h, w = image.shape[:2] window = (0, 0, h, w) scale = 1 + padding = [(0, 0), (0, 0), (0, 0)] + + if mode == "none": + return image, window, scale, padding # Scale? if min_dim: @@ -424,7 +430,7 @@ def resize_image(image, min_dim=None, max_dim=None, padding=False): image = scipy.misc.imresize( image, (round(h * scale), round(w * scale))) # Need padding? - if padding: + if mode == "square": # Get new height and width h, w = image.shape[:2] top_pad = (max_dim - h) // 2 @@ -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)] 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, window, scale, padding -- GitLab