Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update model.py #3020

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions mrcnn/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
assert LooseVersion(keras.__version__) >= LooseVersion('2.0.8')


class AnchorsLayer(KL.Layer):
def __init__(self, anchors, name="anchors", **kwargs):
super(AnchorsLayer, self).__init__(name=name, **kwargs)
self.anchors = tf.Variable(anchors)

def call(self, dummy):
return self.anchors

def get_config(self):
config = super(AnchorsLayer, self).get_config()
return config

############################################################
# Utility Functions
############################################################
Expand Down Expand Up @@ -338,7 +350,7 @@ def compute_output_shape(self, input_shape):

def log2_graph(x):
"""Implementation of Log2. TF doesn't have a native implementation."""
return tf.log(x) / tf.log(2.0)
return tf.math.log(x) / tf.math.log(2.0)


class PyramidROIAlign(KE.Layer):
Expand Down Expand Up @@ -550,12 +562,12 @@ def detection_targets_graph(proposals, gt_class_ids, gt_boxes, gt_masks, config)
# Positive ROIs
positive_count = int(config.TRAIN_ROIS_PER_IMAGE *
config.ROI_POSITIVE_RATIO)
positive_indices = tf.random_shuffle(positive_indices)[:positive_count]
positive_indices = tf.random.shuffle(positive_indices)[:positive_count]
positive_count = tf.shape(positive_indices)[0]
# Negative ROIs. Add enough to maintain positive:negative ratio.
r = 1.0 / config.ROI_POSITIVE_RATIO
negative_count = tf.cast(r * tf.cast(positive_count, tf.float32), tf.int32) - positive_count
negative_indices = tf.random_shuffle(negative_indices)[:negative_count]
negative_indices = tf.random.shuffle(negative_indices)[:negative_count]
# Gather selected ROIs
positive_rois = tf.gather(proposals, positive_indices)
negative_rois = tf.gather(proposals, negative_indices)
Expand Down Expand Up @@ -1931,7 +1943,7 @@ def build(self, mode, config):
# TODO: can this be optimized to avoid duplicating the anchors?
anchors = np.broadcast_to(anchors, (config.BATCH_SIZE,) + anchors.shape)
# A hack to get around Keras's bad support for constants
anchors = KL.Lambda(lambda x: tf.Variable(anchors), name="anchors")(input_image)
anchors = AnchorsLayer(anchors, name="anchors")(input_image)
else:
anchors = input_anchors

Expand Down Expand Up @@ -2112,7 +2124,7 @@ def load_weights(self, filepath, by_name=False, exclude=None):

if h5py is None:
raise ImportError('`load_weights` requires h5py.')
f = h5py.File(filepath, mode='r')
f = h5py.save(filepath, mode='r')
if 'layer_names' not in f.attrs and 'model_weights' in f:
f = f['model_weights']

Expand Down
4 changes: 2 additions & 2 deletions mrcnn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def box_refinement_graph(box, gt_box):

dy = (gt_center_y - center_y) / height
dx = (gt_center_x - center_x) / width
dh = tf.log(gt_height / height)
dw = tf.log(gt_width / width)
dh = tf.math.log(gt_height / height)
dw = tf.math.log(gt_width / width)

result = tf.stack([dy, dx, dh, dw], axis=1)
return result
Expand Down