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

时间:2019-04-15 01:34:56

标签: tensorflow tensorflow-estimator

我正在建立一个网络,该网络将重新使用来自物质端口Mask R-CNN存储库(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.

有什么提示可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我找到了引起问题的原因。

估算者先进行训练,然后进行评估。在训练过程中,我们加载预训练的权重,然后在训练后创建一个检查点。然后从检查点加载权重以进行评估,此时权重已锁定(有充分的理由)。但是后来我试图将权重加载到冻结模型的顶部,因此出现了问题。

TL; DR:我只在火车阶段才装载了砝码。