【发布时间】:2021-11-09 06:37:05
【问题描述】:
我正在尝试将 MaskRCNN 与 Tensorflow 2.x 一起使用。这是原始代码https://github.com/matterport/Mask_RCNN的github链接。 最初他们使用 Lambda 层在 Tensorflow 1.x 中创建变量:
anchors = self.get_anchors(config.IMAGE_SHAPE)
anchors = np.broadcast_to(anchors, (config.BATCH_SIZE,) + anchors.shape)
anchors = KL.Lambda(lambda x: tf.Variable(anchors), name="anchors")(input_image)
这是get_anchors的功能:
def get_anchors(self, image_shape):
"""Returns anchor pyramid for the given image size."""
backbone_shapes = compute_backbone_shapes(self.config, image_shape)
# Cache anchors and reuse if image shape is the same
if not hasattr(self, "_anchor_cache"):
self._anchor_cache = {}
if not tuple(image_shape) in self._anchor_cache:
# Generate Anchors
a = utils.generate_pyramid_anchors(
self.config.RPN_ANCHOR_SCALES,
self.config.RPN_ANCHOR_RATIOS,
backbone_shapes,
self.config.BACKBONE_STRIDES,
self.config.RPN_ANCHOR_STRIDE)
# Keep a copy of the latest anchors in pixel coordinates because
# it's used in inspect_model notebooks.
# TODO: Remove this after the notebook are refactored to not use it
self.anchors = a
# Normalize coordinates
self._anchor_cache[tuple(image_shape)] = utils.norm_boxes(a, image_shape[:2])
return self._anchor_cache[tuple(image_shape)]
但是,这在 Tensorflow 2.x 中是不可行的,所以我找到了一种解决方法来创建 Keras 层的子类:
anchors = self.get_anchors(config.IMAGE_SHAPE)
anchors = np.broadcast_to(anchors, (config.BATCH_SIZE,) + anchors.shape)
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
anchors = AnchorsLayer(anchors, name="anchors")(input_image)
我可以运行代码,但我注意到当我执行 model.summary() 时,原始层的参数为 0,而新层的参数很多。所以我的问题是,这些参数从何而来,这会影响模型架构和性能吗?如果它影响模型性能,我该如何解决?谢谢!!
【问题讨论】:
-
anchors有什么形状? -
你好,输出形状是(1, 261888, 4)
-
而AnchorsLayer的意义何在,它应该做什么?
-
嗨,对不起,但我也不确定,因为我还是深度学习的新手,还在学习 MaskRCNN。但我相信 AnchorsLayer 用于将变量锚点包装到一个层中。锚点是一组框(也许这就是为什么坐标为 4 的形状),具有预定义的位置和相对于图像的比例。这是基于我的理解
标签: python tensorflow deep-learning