在评估预训练的RPN期间获取“图表已完成且无法修改”

问题描述 投票:0回答:1

我正在建立一个网络,重新使用来自物联网掩码R-CNN repo(mask_rcnn_coco.h5)的区域提议网络权重,并且只使用RPN。代码与原始存储库中的代码非常相似:

    def rpn_graph(self, feature_map, anchors_per_location=3, anchor_stride=1):
        """Builds the computation graph of Region Proposal Network.
        feature_map: backbone features [frames, height, width, depth]
        anchors_per_location: number of anchors per pixel in the feature map
        anchor_stride: Controls the density of anchors. Typically 1 (anchors for
                       every pixel in the feature map), or 2 (every other pixel).
        Returns:
            rpn_class_logits: [frames, H * W * anchors_per_location, 2] Anchor classifier logits (before softmax)
            rpn_probs: [frames, H * W * anchors_per_location, 2] Anchor classifier probabilities.
            rpn_bbox: [frames, H * W * anchors_per_location, (dy, dx, log(dh), log(dw))] Deltas to be
                      applied to anchors.
        """
        # Shared convolutional base of the RPN
        shared = KL.Conv2D(512, (3, 3), padding='same', activation='relu',
                           strides=anchor_stride, name='rpn_conv_shared', trainable=False)(feature_map)

        # Anchor Score. [batch, height, width, anchors per location * 2].
        x = KL.Conv2D(2 * anchors_per_location, (1, 1), padding='valid',
                      activation='linear', name='rpn_class_raw', trainable=False)(shared)

        # Reshape to [batch, anchors, 2]
        rpn_class_logits = KL.Lambda(lambda t: tf.reshape(t, [tf.shape(t)[0], -1, 2]))(x)

        # Softmax on last dimension of BG/FG.
        rpn_probs = KL.Activation("softmax", name="rpn_class_xxx")(rpn_class_logits)

        # Bounding box refinement. [batch, H, W, anchors per location * depth]
        # where depth is [x, y, log(w), log(h)]
        x = KL.Conv2D(anchors_per_location * 4, (1, 1), padding="valid", activation='linear',
                      name='rpn_bbox_pred', trainable=False)(shared)

        # Reshape to [batch, anchors, 4]
        rpn_bbox = KL.Lambda(lambda t: tf.reshape(t, [tf.shape(t)[0], -1, 4]))(x)
        return [rpn_class_logits, rpn_probs, rpn_bbox]

    def build_rpn_model(self):
        """Builds a Keras model of the Region Proposal Network.
        It wraps the RPN graph so it can be used multiple times with shared
        weights.

        Returns a Keras Model object. The model outputs, when called, are:
        rpn_class_logits: [batch, H * W * anchors_per_location, 2] Anchor classifier logits (before softmax)
        rpn_probs: [batch, H * W * anchors_per_location, 2] Anchor classifier probabilities.
        rpn_bbox: [batch, H * W * anchors_per_location, (dy, dx, log(dh), log(dw))] Deltas to be
                    applied to anchors.
        """
        input_feature_map = KL.Input(shape=[None, None, self.DEPTH], name="input_rpn_feature_map")
        outputs = self.rpn_graph(
            input_feature_map,
            self.ANCHORS_PER_LOCATION,
            self.ANCHOR_STRIDE
        )

        model = KM.Model([input_feature_map], outputs, name="rpn_model")
        self.load_pre_trained_rpn_weights(model)

        return model

    def load_pre_trained_rpn_weights(self, model):
        """
        We load the weights for the region proposal network from the pre-trained mask-rcnn model

        :param model: Keras model for the region proposal network
        :return:
        """

        weights = h5py.File(self.PATH_TO_MASK_RCNN_WEIGHTS, mode='r')
        saving.load_weights_from_hdf5_group_by_name(weights, [model])

        weights.close()

然后我在init中初始化RPN以便稍后使用:

    ...
    self.region_proposal_network = self.build_rpn_model()
    ...

训练运行良好,我可以看到检查点已保存。但是,当我加载模型进行评估时(使用tf.estimator.train_and_evaluate

    RuntimeError: Graph is finalized and cannot be modified.

任何可以帮助我的提示?

tensorflow tensorflow-estimator
1个回答
0
投票

我找到了造成这个问题的原因。

估算员进行培训然后进行评估。在训练期间,我们加载预训练的重量,然后在训练之后创建检查点。然后从检查点加载权重以进行评估,此时权重被锁定(有充分理由)。但后来我试图在冷冻模型的顶部加载权重,因此问题。

TL; DR:我最后只在火车阶段加载了重量。

© www.soinside.com 2019 - 2024. All rights reserved.