Ejemplo n.º 1
0
    def call(self, inputs, **kwargs):
        """
        应用边框回归,并使用nms生成最后的边框
        :param inputs:
        inputs[0]: deltas, [batch_size,N,(dy,dx,dh,dw)]   N是所有的anchors数量
        inputs[1]: class logits [batch_size,N,num_classes]
        inputs[2]: anchors [batch_size,N,(y1,x1,y2,x2)]
        inputs[3]: anchors是否有效 bool类型 [batch_size, N]
        inputs[4]: image metas是否有效 bool类型 [batch_size, 12]
        :param kwargs:
        :return:
        """
        deltas, class_logits, anchors, anchors_tag, metas = inputs

        # 转为分类评分
        class_scores = tf.nn.softmax(logits=class_logits,
                                     axis=-1)  # [N,num_classes]
        fg_scores = tf.reduce_max(class_scores[..., 1:],
                                  axis=-1)  # 第一类为背景 (N,)

        # 应用边框回归
        # proposals = tf.map_fn(lambda x: apply_regress(*x),
        #                       elems=[deltas, anchors],
        #                       dtype=tf.float32)
        # # 非极大抑制
        # options = {"max_output_size": self.output_box_num,
        #            "iou_threshold": self.iou_threshold,
        #            "score_threshold": self.score_threshold,
        #            "name": "proposals_nms"}
        #
        # outputs = tf.map_fn(lambda x: nms(*x, **options),
        #                     elems=[proposals, fg_scores, class_logits],
        #                     dtype=[tf.float32] * 3)
        # 应用边框回归
        proposals = tf_utils.batch_slice([deltas, anchors],
                                         lambda x, y: apply_regress(x, y),
                                         self.batch_size)
        # 裁剪边框到图像窗口内
        windows = metas[:, 7:11]
        proposals = tf_utils.batch_slice(
            [proposals, windows], lambda x, y: tf_utils.clip_boxes(x, y),
            self.batch_size)

        # 非极大抑制
        scales = metas[:, -1]
        outputs = tf_utils.batch_slice(
            [proposals, fg_scores, class_logits, anchors_tag, scales],
            lambda x, y, z, r, t: nms(x,
                                      y,
                                      z,
                                      r,
                                      t,
                                      max_output_size=self.output_box_num,
                                      iou_threshold=self.iou_threshold,
                                      score_threshold=self.score_threshold),
            self.batch_size)
        return outputs
Ejemplo n.º 2
0
    def call(self, inputs, **kwargs):
        """
        计算检测分类和回归目标
        :param inputs:
        inputs[0]: GT 边框坐标 [batch_size, MAX_GT_BOXs,(y1,x1,y2,x2,tag)] ,tag=0 为padding
        inputs[1]: GT 边框 类别 [batch_size, MAX_GT_BOXs,1+1] ;最后一位为tag, tag=0 为padding
        inputs[2]: proposals [batch_size, N,(y1,x1,y2,x2,tag)]
        :param kwargs:
        :return: [deltas,class_ids,rois]
        """
        gt_boxes = inputs[0]
        gt_class_ids = inputs[1]
        proposals = inputs[2]

        # options = {"train_rois_per_image": self.train_rois_per_image,
        #           "roi_positive_ratio": self.roi_positive_ratio}
        # outputs = tf.map_fn(lambda x: detect_targets_graph(*x, **options),
        #                    elems=[gt_boxes, gt_class_ids, proposals],
        #                    dtype=[tf.float32] * 4)
        outputs = tf_utils.batch_slice(
            [gt_boxes, gt_class_ids, proposals],
            lambda x, y, z: detect_targets_graph(
                x, y, z, self.train_rois_per_image, self.roi_positive_ratio),
            self.batch_size)
        return outputs
Ejemplo n.º 3
0
    def call(self, inputs, **kwargs):
        """
        计算分类和回归目标
        :param inputs:
        inputs[0]: GT 边框坐标 [batch_size, MAX_GT_BOXs,(y1,x1,y2,x2,tag)] ,tag=-1 为padding
        inputs[1]: GT 类别 [batch_size, MAX_GT_BOXs,num_class+1] ;最后一位为tag, tag=-1 为padding
        inputs[2]: Anchors [batch_size, anchor_num,(y1,x1,y2,x2)]
        inputs[3]: Anchors是否有效 bool类型 [batch_size, anchor_num]
        :param kwargs:
        :return:
        """
        gt_boxes = inputs[0]
        gt_cls_ids = inputs[1]
        anchors = inputs[2]
        anchors_tag = inputs[3]

        # options = {"rpn_train_anchors": self.train_anchors_per_image}
        # outputs = tf.map_fn(lambda x: rpn_targets_graph(*x, **options),
        #                    elems=[gt_boxes, gt_cls_ids, anchors],
        #                    dtype=[tf.float32] * 2 + [tf.int64] + [tf.float32] * 4)

        outputs = tf_utils.batch_slice(
            [gt_boxes, gt_cls_ids, anchors, anchors_tag],
            lambda x, y, z, t: rpn_targets_graph(
                x, y, z, t, self.train_anchors_per_image), self.batch_size)

        return outputs
Ejemplo n.º 4
0
    def call(self, inputs, **kwargs):
        """
        应用边框回归,并使用nms生成最后的边框
        :param inputs:
        inputs[0]: deltas, [batch_size,N,(dy,dx,dh,dw)]   N是所有的anchors数量
        inputs[1]: class logits [batch_size,N,num_classes]
        inputs[2]: anchors [batch_size,N,(y1,x1,y2,x2)]
        :param kwargs:
        :return:
        """
        deltas = inputs[0]
        class_logits = inputs[1]
        anchors = inputs[2]
        # 转为分类评分
        class_scores = tf.nn.softmax(logits=class_logits,
                                     axis=-1)  # [N,num_classes]
        fg_scores = tf.reduce_max(class_scores[..., 1:],
                                  axis=-1)  # 第一类为背景 (N,)

        # 应用边框回归
        # proposals = tf.map_fn(lambda x: apply_regress(*x),
        #                       elems=[deltas, anchors],
        #                       dtype=tf.float32)
        # # 非极大抑制
        # options = {"max_output_size": self.output_box_num,
        #            "iou_threshold": self.iou_threshold,
        #            "score_threshold": self.score_threshold,
        #            "name": "proposals_nms"}
        #
        # outputs = tf.map_fn(lambda x: nms(*x, **options),
        #                     elems=[proposals, fg_scores, class_logits],
        #                     dtype=[tf.float32] * 3)
        # # 非极大抑制
        proposals = tf_utils.batch_slice([deltas, anchors],
                                         lambda x, y: apply_regress(x, y),
                                         self.batch_size)

        outputs = tf_utils.batch_slice(
            [proposals, fg_scores, class_logits],
            lambda x, y, z: nms(x,
                                y,
                                z,
                                max_output_size=self.output_box_num,
                                iou_threshold=self.iou_threshold,
                                score_threshold=self.score_threshold),
            self.batch_size)
        return outputs