提交 64020bd5 编写于 作者: W Waleed

A wrapper for skimage resize() to avoid warnings

skimage generates different warnings depending on the version. This wrapper function calls skimage.tranform.resize() with the right parameter for each version.
上级 a5d80c73
...@@ -16,7 +16,6 @@ import logging ...@@ -16,7 +16,6 @@ import logging
from collections import OrderedDict from collections import OrderedDict
import multiprocessing import multiprocessing
import numpy as np import numpy as np
import skimage.transform
import tensorflow as tf import tensorflow as tf
import keras import keras
import keras.backend as K import keras.backend as K
...@@ -1071,8 +1070,7 @@ def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox): ...@@ -1071,8 +1070,7 @@ def rpn_bbox_loss_graph(config, target_bbox, rpn_match, rpn_bbox):
target_bbox = batch_pack_graph(target_bbox, batch_counts, target_bbox = batch_pack_graph(target_bbox, batch_counts,
config.IMAGES_PER_GPU) config.IMAGES_PER_GPU)
#use smooth_l1_loss() rather than reimplementing here to reduce code duplication loss = smooth_l1_loss(target_bbox, rpn_bbox)
loss = smooth_l1_loss(target_bbox,rpn_bbox)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0)) loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss return loss
...@@ -1434,15 +1432,14 @@ def build_detection_targets(rpn_rois, gt_class_ids, gt_boxes, gt_masks, config): ...@@ -1434,15 +1432,14 @@ def build_detection_targets(rpn_rois, gt_class_ids, gt_boxes, gt_masks, config):
gt_h = gt_y2 - gt_y1 gt_h = gt_y2 - gt_y1
# Resize mini mask to size of GT box # Resize mini mask to size of GT box
placeholder[gt_y1:gt_y2, gt_x1:gt_x2] = \ placeholder[gt_y1:gt_y2, gt_x1:gt_x2] = \
np.round(skimage.transform.resize( np.round(utils.resize(class_mask, (gt_h, gt_w))).astype(bool)
class_mask, (gt_h, gt_w), order=1, mode="constant")).astype(bool)
# Place the mini batch in the placeholder # Place the mini batch in the placeholder
class_mask = placeholder class_mask = placeholder
# Pick part of the mask and resize it # Pick part of the mask and resize it
y1, x1, y2, x2 = rois[i].astype(np.int32) y1, x1, y2, x2 = rois[i].astype(np.int32)
m = class_mask[y1:y2, x1:x2] m = class_mask[y1:y2, x1:x2]
mask = skimage.transform.resize(m, config.MASK_SHAPE, order=1, mode="constant") mask = utils.resize(m, config.MASK_SHAPE)
masks[i, :, :, class_id] = mask masks[i, :, :, class_id] = mask
return rois, roi_gt_class_ids, bboxes, masks return rois, roi_gt_class_ids, bboxes, masks
......
...@@ -20,6 +20,7 @@ import skimage.transform ...@@ -20,6 +20,7 @@ import skimage.transform
import urllib.request import urllib.request
import shutil import shutil
import warnings import warnings
from distutils.version import LooseVersion
# URL from which to download the latest COCO trained weights # URL from which to download the latest COCO trained weights
COCO_MODEL_URL = "https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5" COCO_MODEL_URL = "https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5"
...@@ -452,9 +453,8 @@ def resize_image(image, min_dim=None, max_dim=None, min_scale=None, mode="square ...@@ -452,9 +453,8 @@ def resize_image(image, min_dim=None, max_dim=None, min_scale=None, mode="square
# Resize image using bilinear interpolation # Resize image using bilinear interpolation
if scale != 1: if scale != 1:
image = skimage.transform.resize( image = resize(image, (round(h * scale), round(w * scale)),
image, (round(h * scale), round(w * scale)), preserve_range=True)
order=1, mode="constant", preserve_range=True)
# Need padding or cropping? # Need padding or cropping?
if mode == "square": if mode == "square":
...@@ -538,7 +538,7 @@ def minimize_mask(bbox, mask, mini_shape): ...@@ -538,7 +538,7 @@ def minimize_mask(bbox, mask, mini_shape):
if m.size == 0: if m.size == 0:
raise Exception("Invalid bounding box with area of zero") raise Exception("Invalid bounding box with area of zero")
# Resize with bilinear interpolation # Resize with bilinear interpolation
m = skimage.transform.resize(m, mini_shape, order=1, mode="constant") m = resize(m, mini_shape)
mini_mask[:, :, i] = np.around(m).astype(np.bool) mini_mask[:, :, i] = np.around(m).astype(np.bool)
return mini_mask return mini_mask
...@@ -556,7 +556,7 @@ def expand_mask(bbox, mini_mask, image_shape): ...@@ -556,7 +556,7 @@ def expand_mask(bbox, mini_mask, image_shape):
h = y2 - y1 h = y2 - y1
w = x2 - x1 w = x2 - x1
# Resize with bilinear interpolation # Resize with bilinear interpolation
m = skimage.transform.resize(m, (h, w), order=1, mode="constant") m = resize(m, (h, w))
mask[y1:y2, x1:x2, i] = np.around(m).astype(np.bool) mask[y1:y2, x1:x2, i] = np.around(m).astype(np.bool)
return mask return mask
...@@ -576,7 +576,7 @@ def unmold_mask(mask, bbox, image_shape): ...@@ -576,7 +576,7 @@ def unmold_mask(mask, bbox, image_shape):
""" """
threshold = 0.5 threshold = 0.5
y1, x1, y2, x2 = bbox y1, x1, y2, x2 = bbox
mask = skimage.transform.resize(mask, (y2 - y1, x2 - x1), order=1, mode="constant") mask = resize(mask, (y2 - y1, x2 - x1))
mask = np.where(mask >= threshold, 1, 0).astype(np.bool) mask = np.where(mask >= threshold, 1, 0).astype(np.bool)
# Put the mask in the right location. # Put the mask in the right location.
...@@ -891,3 +891,27 @@ def denorm_boxes(boxes, shape): ...@@ -891,3 +891,27 @@ def denorm_boxes(boxes, shape):
scale = np.array([h - 1, w - 1, h - 1, w - 1]) scale = np.array([h - 1, w - 1, h - 1, w - 1])
shift = np.array([0, 0, 1, 1]) shift = np.array([0, 0, 1, 1])
return np.around(np.multiply(boxes, scale) + shift).astype(np.int32) return np.around(np.multiply(boxes, scale) + shift).astype(np.int32)
def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True,
preserve_range=False, anti_aliasing=False, anti_aliasing_sigma=None):
"""A wrapper for Scikit-Image resize().
Scikit-Image generates warnings on every call to resize() if it doesn't
receive the right parameters. The right parameters depend on the version
of skimage. This solves the problem by using different parameters per
version. And it provides a central place to control resizing defaults.
"""
if LooseVersion(skimage.__version__) >= LooseVersion("0.14"):
# New in 0.14: anti_aliasing. Default it to False for backward
# compatibility with skimage 0.13.
return skimage.transform.resize(
image, output_shape,
order=order, mode=mode, cval=cval, clip=clip,
preserve_range=preserve_range, anti_aliasing=anti_aliasing,
anti_aliasing_sigma=anti_aliasing_sigma)
else:
return skimage.transform.resize(
image, output_shape,
order=order, mode=mode, cval=cval, clip=clip,
preserve_range=preserve_range)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册