From dffcb6110184707c28e80879ac8c70f7ebf6dcfe Mon Sep 17 00:00:00 2001 From: Waleed Abdulla Date: Fri, 30 Mar 2018 01:16:07 -0700 Subject: [PATCH] Fix mask resizing when mask is all FG and no BG. If the mask is a rectangle, its mini-mask pixels become all 1s, and it has no zeros. Scipy imresize fails on that and returns 0s instead. This update fixes the problem. --- model.py | 10 +++++----- train_shapes.ipynb | 2 +- utils.py | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/model.py b/model.py index 3636f85..8ae51e3 100644 --- a/model.py +++ b/model.py @@ -20,7 +20,7 @@ import logging from collections import OrderedDict import multiprocessing import numpy as np -import scipy.misc +import skimage.transform import tensorflow as tf import keras import keras.backend as K @@ -1408,16 +1408,16 @@ def build_detection_targets(rpn_rois, gt_class_ids, gt_boxes, gt_masks, config): gt_h = gt_y2 - gt_y1 # Resize mini mask to size of GT box placeholder[gt_y1:gt_y2, gt_x1:gt_x2] = \ - np.round(scipy.misc.imresize(class_mask.astype(float), (gt_h, gt_w), - interp='nearest') / 255.0).astype(bool) + np.round(skimage.transform.resize( + class_mask, (gt_h, gt_w), order=1, mode="reflect")).astype(bool) # Place the mini batch in the placeholder class_mask = placeholder # Pick part of the mask and resize it y1, x1, y2, x2 = rois[i].astype(np.int32) m = class_mask[y1:y2, x1:x2] - mask = scipy.misc.imresize( - m.astype(float), config.MASK_SHAPE, interp='nearest') / 255.0 + mask = skimage.transform.resize(m, config.MASK_SHAPE, + order=1, mode="reflect") masks[i, :, :, class_id] = mask return rois, roi_gt_class_ids, bboxes, masks diff --git a/train_shapes.ipynb b/train_shapes.ipynb index b32110d..be44d62 100644 --- a/train_shapes.ipynb +++ b/train_shapes.ipynb @@ -276,7 +276,7 @@ " occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))\n", " # Map class names to class IDs.\n", " class_ids = np.array([self.class_names.index(s[0]) for s in shapes])\n", - " return mask, class_ids.astype(np.int32)\n", + " return mask.astype(np.bool), class_ids.astype(np.int32)\n", "\n", " def draw_shape(self, image, shape, dims, color):\n", " \"\"\"Draws a shape from the given specs.\"\"\"\n", diff --git a/utils.py b/utils.py index 404b695..71eb7b8 100644 --- a/utils.py +++ b/utils.py @@ -16,6 +16,7 @@ import tensorflow as tf import scipy.misc import skimage.color import skimage.io +import skimage.transform import urllib.request import shutil import warnings @@ -445,7 +446,6 @@ def resize_mask(mask, scale, padding): padding: Padding to add to the mask in the form [(top, bottom), (left, right), (0, 0)] """ - h, w = mask.shape[:2] # Suppress warning from scipy 0.13.0, the output shape of zoom() is # calculated with round() instead of int() with warnings.catch_warnings(): @@ -468,8 +468,8 @@ def minimize_mask(bbox, mask, mini_shape): m = m[y1:y2, x1:x2] if m.size == 0: raise Exception("Invalid bounding box with area of zero") - m = scipy.misc.imresize(m.astype(float), mini_shape, interp='bilinear') - mini_mask[:, :, i] = np.where(m >= 128, 1, 0) + m = skimage.transform.resize(m, mini_shape, order=1, mode="reflect") + mini_mask[:, :, i] = np.around(m).astype(np.bool) return mini_mask @@ -485,8 +485,8 @@ def expand_mask(bbox, mini_mask, image_shape): y1, x1, y2, x2 = bbox[i][:4] h = y2 - y1 w = x2 - x1 - m = scipy.misc.imresize(m.astype(float), (h, w), interp='bilinear') - mask[y1:y2, x1:x2, i] = np.where(m >= 128, 1, 0) + m = skimage.transform.resize(m, (h, w), order=1, mode="reflect") + mask[y1:y2, x1:x2, i] = np.around(m).astype(np.bool) return mask @@ -505,8 +505,8 @@ def unmold_mask(mask, bbox, image_shape): """ threshold = 0.5 y1, x1, y2, x2 = bbox - mask = scipy.misc.imresize( - mask, (y2 - y1, x2 - x1), interp='bilinear').astype(np.float32) / 255.0 + mask = skimage.transform.resize(mask, (y2 - y1, x2 - x1), + order=1, mode="reflect") mask = np.where(mask >= threshold, 1, 0).astype(np.uint8) # Put the mask in the right location. @@ -618,7 +618,7 @@ def compute_ap(gt_boxes, gt_class_ids, gt_masks, # Compute IoU overlaps [pred_masks, gt_masks] overlaps = compute_overlaps_masks(pred_masks, gt_masks) - # Loop through ground truth boxes and find matching predictions + # Loop through predictions and find matching ground truth boxes match_count = 0 pred_match = np.zeros([pred_boxes.shape[0]]) gt_match = np.zeros([gt_boxes.shape[0]]) -- GitLab