Beispiel #1
0
    def __init__(self, config):
        super(Faster_Rcnn, self).__init__()
        self.config = config
        self.Mean = torch.tensor(config.Mean, dtype=torch.float32)
        self.num_anchor = len(config.anchor_scales) * len(config.anchor_ratios)
        self.anchors = get_anchors(np.ceil(self.config.img_max / 16 + 1),
                                   self.config.anchor_scales,
                                   self.config.anchor_ratios)

        self.PC = ProposalCreator(nms_thresh=config.roi_nms_thresh,
                                  n_train_pre_nms=config.roi_train_pre_nms,
                                  n_train_post_nms=config.roi_train_post_nms,
                                  n_test_pre_nms=config.roi_test_pre_nms,
                                  n_test_post_nms=config.roi_test_post_nms,
                                  min_size=config.roi_min_size)

        self.features = vgg16().features[:-1]
        self.rpn = RPN_net(512, self.num_anchor)
        self.roialign = RoIAlign((7, 7), 1 / 16., 2)
        self.fast = Fast_net(config.num_cls, 512 * 7 * 7, 2048)
        self.fast_2 = Fast_net(config.num_cls, 512 * 7 * 7, 2048)
        self.fast_3 = Fast_net(config.num_cls, 512 * 7 * 7, 2048)

        self.loc_std1 = [1. / 10, 1. / 10, 1. / 5, 1. / 5]
        self.loc_std2 = [1. / 20, 1. / 20, 1. / 10, 1. / 10]
        self.loc_std3 = [1. / 30, 1. / 30, 1. / 15, 1. / 15]
        self.weights = [1.0, 1.0, 1.0]
    def __init__(self, config):
        super(Faster_Rcnn, self).__init__()
        self.config = config
        self.Mean = torch.tensor(config.Mean, dtype=torch.float32)
        self.num_anchor = len(config.anchor_scales) * len(config.anchor_ratios)
        self.anchors = get_anchors(np.ceil(self.config.img_max / 16 + 1),
                                   self.config.anchor_scales,
                                   self.config.anchor_ratios)
        self.ATC = AnchorTargetCreator(
            n_sample=config.rpn_n_sample,
            pos_iou_thresh=config.rpn_pos_iou_thresh,
            neg_iou_thresh=config.rpn_neg_iou_thresh,
            pos_ratio=config.rpn_pos_ratio)
        self.PC = ProposalCreator(nms_thresh=config.roi_nms_thresh,
                                  n_train_pre_nms=config.roi_train_pre_nms,
                                  n_train_post_nms=config.roi_train_post_nms,
                                  n_test_pre_nms=config.roi_test_pre_nms,
                                  n_test_post_nms=config.roi_test_post_nms,
                                  min_size=config.roi_min_size)
        self.PTC_1 = ProposalTargetCreator(
            n_sample=config.fast_n_sample,
            pos_ratio=config.fast_pos_ratio,
            pos_iou_thresh=config.fast_pos_iou_thresh,
            neg_iou_thresh_hi=config.fast_neg_iou_thresh_hi,
            neg_iou_thresh_lo=config.fast_neg_iou_thresh_lo)
        self.PTC_2 = ProposalTargetCreator(
            n_sample=config.fast_n_sample,
            pos_ratio=config.fast_pos_ratio,
            pos_iou_thresh=0.6,
            neg_iou_thresh_hi=0.6,
            neg_iou_thresh_lo=config.fast_neg_iou_thresh_lo)
        self.PTC_3 = ProposalTargetCreator(
            n_sample=config.fast_n_sample,
            pos_ratio=config.fast_pos_ratio,
            pos_iou_thresh=0.7,
            neg_iou_thresh_hi=0.7,
            neg_iou_thresh_lo=config.fast_neg_iou_thresh_lo)

        self.features = vgg16().features[:-1]
        self.rpn = RPN_net(512, self.num_anchor)
        self.roialign = RoIAlign((7, 7), 1 / 16., 2)
        self.fast = Fast_net(config.num_cls, 512 * 7 * 7, 4096)
        self.fast_2 = Fast_net(config.num_cls, 512 * 7 * 7, 4096)
        self.fast_3 = Fast_net(config.num_cls, 512 * 7 * 7, 4096)
        self.a = 0
        self.b = 0
        self.c = 0
        self.d = 0
        self.fast_num = 0
        self.fast_num_P = 0

        self.loc_std1 = [1. / 10, 1. / 10, 1. / 5, 1. / 5]
        self.loc_std2 = [1. / 20, 1. / 20, 1. / 10, 1. / 10]
        self.loc_std3 = [1. / 30, 1. / 30, 1. / 15, 1. / 15]
        self.loss_weights = [1.0, 0.5, 0.25]
    def __init__(self,
                 config,
                 block,
                 layers,
                 num_classes=1000,
                 zero_init_residual=False):
        super(ResNet, self).__init__()
        self.config = config
        self.Mean = torch.tensor(config.Mean, dtype=torch.float32)
        self.num_anchor = len(config.anchor_scales) * len(config.anchor_ratios)
        self.anchors = get_anchors(np.ceil(self.config.img_max / 16 + 1),
                                   self.config.anchor_scales,
                                   self.config.anchor_ratios)

        self.PC = ProposalCreator(nms_thresh=config.roi_nms_thresh,
                                  n_train_pre_nms=config.roi_train_pre_nms,
                                  n_train_post_nms=config.roi_train_post_nms,
                                  n_test_pre_nms=config.roi_test_pre_nms,
                                  n_test_post_nms=config.roi_test_post_nms,
                                  min_size=config.roi_min_size)

        self.inplanes = 64
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)

        self.maxpool = nn.MaxPool2d(kernel_size=3,
                                    stride=2,
                                    padding=0,
                                    ceil_mode=True)  # change
        for i in range(2, 5):
            print('layer%d' % i)
            getattr(self, 'layer%d' % i)[0].conv1.stride = (2, 2)
            getattr(self, 'layer%d' % i)[0].conv2.stride = (1, 1)

        self.rpn = RPN_net(1024, self.num_anchor)
        self.roialign = RoIAlign((14, 14), 1 / 16., 2)
        self.fast = Fast_net(config.num_cls)