rpn.py 3.43 KB
Newer Older
Kai Chen's avatar
Kai Chen committed
1
2
import mmcv

3
from mmdet.core import bbox_mapping, tensor2imgs
Kai Chen's avatar
Kai Chen committed
4
from .. import builder
Kai Chen's avatar
Kai Chen committed
5
from ..registry import DETECTORS
6
7
from .base import BaseDetector
from .test_mixins import RPNTestMixin
Kai Chen's avatar
Kai Chen committed
8
9


Kai Chen's avatar
Kai Chen committed
10
@DETECTORS.register_module
Kai Chen's avatar
Kai Chen committed
11
12
13
14
15
16
17
18
19
20
21
22
class RPN(BaseDetector, RPNTestMixin):

    def __init__(self,
                 backbone,
                 neck,
                 rpn_head,
                 train_cfg,
                 test_cfg,
                 pretrained=None):
        super(RPN, self).__init__()
        self.backbone = builder.build_backbone(backbone)
        self.neck = builder.build_neck(neck) if neck is not None else None
Kai Chen's avatar
Kai Chen committed
23
        self.rpn_head = builder.build_head(rpn_head)
Kai Chen's avatar
Kai Chen committed
24
25
26
27
28
        self.train_cfg = train_cfg
        self.test_cfg = test_cfg
        self.init_weights(pretrained=pretrained)

    def init_weights(self, pretrained=None):
29
        super(RPN, self).init_weights(pretrained)
Kai Chen's avatar
Kai Chen committed
30
        self.backbone.init_weights(pretrained=pretrained)
Kai Chen's avatar
Kai Chen committed
31
        if self.with_neck:
Kai Chen's avatar
Kai Chen committed
32
33
34
35
36
            self.neck.init_weights()
        self.rpn_head.init_weights()

    def extract_feat(self, img):
        x = self.backbone(img)
Kai Chen's avatar
Kai Chen committed
37
        if self.with_neck:
Kai Chen's avatar
Kai Chen committed
38
39
40
            x = self.neck(x)
        return x

Kai Chen's avatar
Kai Chen committed
41
42
43
44
45
    def forward_dummy(self, img):
        x = self.extract_feat(img)
        rpn_outs = self.rpn_head(x)
        return rpn_outs

46
47
48
49
50
    def forward_train(self,
                      img,
                      img_meta,
                      gt_bboxes=None,
                      gt_bboxes_ignore=None):
Kai Chen's avatar
Kai Chen committed
51
52
53
54
55
56
57
        if self.train_cfg.rpn.get('debug', False):
            self.rpn_head.debug_imgs = tensor2imgs(img)

        x = self.extract_feat(img)
        rpn_outs = self.rpn_head(x)

        rpn_loss_inputs = rpn_outs + (gt_bboxes, img_meta, self.train_cfg.rpn)
58
59
        losses = self.rpn_head.loss(
            *rpn_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore)
Kai Chen's avatar
Kai Chen committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        return losses

    def simple_test(self, img, img_meta, rescale=False):
        x = self.extract_feat(img)
        proposal_list = self.simple_test_rpn(x, img_meta, self.test_cfg.rpn)
        if rescale:
            for proposals, meta in zip(proposal_list, img_meta):
                proposals[:, :4] /= meta['scale_factor']
        # TODO: remove this restriction
        return proposal_list[0].cpu().numpy()

    def aug_test(self, imgs, img_metas, rescale=False):
        proposal_list = self.aug_test_rpn(
            self.extract_feats(imgs), img_metas, self.test_cfg.rpn)
        if not rescale:
            for proposals, img_meta in zip(proposal_list, img_metas[0]):
                img_shape = img_meta['img_shape']
                scale_factor = img_meta['scale_factor']
                flip = img_meta['flip']
                proposals[:, :4] = bbox_mapping(proposals[:, :4], img_shape,
                                                scale_factor, flip)
        # TODO: remove this restriction
        return proposal_list[0].cpu().numpy()

84
    def show_result(self, data, result, img_norm_cfg, dataset=None, top_k=20):
Kai Chen's avatar
Kai Chen committed
85
86
87
88
89
90
91
92
93
94
95
96
        """Show RPN proposals on the image.

        Although we assume batch size is 1, this method supports arbitrary
        batch size.
        """
        img_tensor = data['img'][0]
        img_metas = data['img_meta'][0].data[0]
        imgs = tensor2imgs(img_tensor, **img_norm_cfg)
        assert len(imgs) == len(img_metas)
        for img, img_meta in zip(imgs, img_metas):
            h, w, _ = img_meta['img_shape']
            img_show = img[:h, :w, :]
97
            mmcv.imshow_bboxes(img_show, result, top_k=top_k)