Exemplo n.º 1
0
    def __init__(self, config):
        super(ResNet50Conv5ROIFeatureExtractor, self).__init__()

        resolution = config.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        scales = config.MODEL.ROI_BOX_HEAD.POOLER_SCALES
        sampling_ratio = config.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO
        pooler = Pooler(
            output_size=(resolution, resolution),
            scales=scales,
            sampling_ratio=sampling_ratio,
        )

        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.pooler = pooler
        self.head = head
Exemplo n.º 2
0
    def __init__(self, config, in_channels, RCNN_top=None):
        super(ResNet50Conv5ROIFeatureExtractorFlatten, self).__init__()

        resolution = config.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        scales = config.MODEL.ROI_BOX_HEAD.POOLER_SCALES
        sampling_ratio = config.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO
        pooler = Pooler(
            output_size=(resolution, resolution),
            scales=scales,
            sampling_ratio=sampling_ratio,
        )

        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.pooler = pooler
        self.head = head
        hidden_channels = head.out_channels
        use_gn = config.MODEL.ROI_BOX_HEAD.USE_GN
        self.out_channels = config.MODEL.ROI_BOX_HEAD.MLP_HEAD_DIM
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            make_fc(hidden_channels, self.out_channels, use_gn), nn.ReLU())
    def __init__(self, config, select_num=10, class_num=5, is_cat=True):
        super(Network, self).__init__()
        self.detector = build_detection_model(config)
        self.detector.eval()
        self.detector.load_state_dict(torch.load(config.MODEL.WEIGHT)['model'],
                                      strict=False)
        # output_dir = config.OUTPUT_DIR
        # checkpointer = DetectronCheckpointer(config, self.detector, save_dir=output_dir)
        # ckpt = config.MODEL.WEIGHT
        # _ = checkpointer.load(ckpt, use_latest=True)

        for count, child in enumerate(self.detector.children()):
            count += 1
            print(count)
            print(child)
            if count < 4:
                for param in child.parameters():
                    param.requires_grad = False
        self.selector = Selector()
        self.select_num = select_num
        self.sftmax = nn.Softmax(dim=0)
        self.is_cat = is_cat

        # ResNet Head
        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        self.head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.avgpool_glob = nn.AdaptiveAvgPool2d(output_size=14)
        # self.conv_glob = nn.Conv2d(1024, 2048, 2, stride=2)
        # self.relu_glob = nn.PReLU(2048)
        if self.is_cat:
            self.conv1 = nn.Conv2d(2048, 512, 1)
            # self.relu1 = nn.PReLU(512)
            self.relu1 = nn.ReLU(inplace=True)
            self.fc1 = nn.Linear(512 * (self.select_num + 1),
                                 512)  # select top 5
            # self.fc1_relu = nn.PReLU(512)
            self.fc1_relu = nn.ReLU(inplace=True)
            self.fc2 = nn.Linear(512, class_num)
        else:
            self.conv1 = nn.Conv2d(4096, 512, 1, 1)
            # self.relu1 = nn.PReLU(512)
            self.relu1 = nn.ReLU(inplace=True)
            self.fc1 = nn.Linear(512, 512)
            # self.fc1_relu = nn.PReLU(512)
            self.fc1_relu = nn.ReLU(inplace=True)
            self.fc2 = nn.Linear(512, class_num)
        self.avgpool = nn.AdaptiveAvgPool2d(output_size=1)
        self.dropout = nn.Dropout()
Exemplo n.º 4
0
    def __init__(self,
                 config,
                 select_num=10,
                 class_num=4,
                 side=False,
                 is_cat=False):
        super(Predictor, self).__init__()
        self.selector = Selector()
        self.select_num = select_num
        self.sftmax = nn.Softmax(dim=0)
        self.side = side
        self.is_cat = is_cat

        # ResNet Head
        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        self.head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            dilation=config.MODEL.RESNETS.RES5_DILATION)
        self.head.load_state_dict(
            torch.load('/data6/SRIP19_SelfDriving/Outputs/layer4.pth')
        )  #TODO: how to avoid this? Maybe we can involve the weights into model_final.pth

        self.avgpool_glob = nn.AdaptiveAvgPool2d(output_size=14)
        if self.is_cat:
            self.avg1 = nn.AdaptiveAvgPool3d(output_size=1)
            self.avg2 = nn.AdaptiveAvgPool2d(output_size=1)
            self.fc1 = nn.Linear(4096, 100)
            self.relu1 = nn.ReLU(inplace=True)
            self.fc2 = nn.Linear(100, class_num)
            self.drop = nn.Dropout(p=0.25)
            if self.side:
                self.fc_side1 = nn.Linear(4096, 100)
                self.relu_side1 = nn.ReLU(inplace=True)
                self.fc_side2 = nn.Linear(100, 21)
        else:
            self.avg = nn.AdaptiveAvgPool2d(output_size=1)
            self.fc2 = nn.Linear(2048, class_num)
            self.drop = nn.Dropout(p=0.25)
            if self.side:
                self.fc_side = nn.Linear(2048, 21)
    def __init__(self, config, in_channels):
        super(ResNet50Conv5ROIFeatureExtractor, self).__init__()

        pooler = Pooler(config.MODEL.ROI_BOX_HEAD)

        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.pooler = pooler
        self.head = head
        self.out_channels = head.out_channels
    def __init__(self, config, in_channels):
        super(ResNet50Conv5ROIFeatureExtractor, self).__init__()

        resolution = config.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        scales = config.MODEL.ROI_BOX_HEAD.POOLER_SCALES
        sampling_ratio = config.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO
        pooler = Pooler(
            output_size=(resolution, resolution),
            scales=scales,
            sampling_ratio=sampling_ratio,
        )

        # ResNet50Conv5 input size is 1024, which is the default for backbone
        # without FPN. For FPN structure, the in_channels will be 256. So we
        # need to add a transit conv layer to make it compatible.
        # The corresponding predictor should be FastRCNNPredictor.
        if in_channels != 1024:
            self.trans_conv = nn.Conv2d(in_channels,
                                        1024,
                                        kernel_size=3,
                                        padding=1)
            torch.nn.init.normal_(self.trans_conv.weight, std=0.01)
            torch.nn.init.constant_(self.trans_conv.bias, 0)
        else:
            self.trans_conv = None

        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.pooler = pooler
        self.head = head
        self.out_channels = head.out_channels
    def __init__(self, cfg):
        super(ResNet50Conv5ROIFeatureExtractor, self).__init__()
        self.cfg = cfg
        self.in_channels = 2048
        self.out_channels = cfg.REID.OUT_CHANNELS
        self.avgpool = nn.AdaptiveAvgPool2d(1)
        self.fc = make_fc(self.in_channels, self.out_channels)

        if self.cfg.REID.USE_DIFF_FEAT:
            stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
            head = resnet.ResNetHead(
                block_module=cfg.MODEL.RESNETS.TRANS_FUNC,
                stages=(stage,),
                num_groups=cfg.MODEL.RESNETS.NUM_GROUPS,
                width_per_group=cfg.MODEL.RESNETS.WIDTH_PER_GROUP,
                stride_in_1x1=cfg.MODEL.RESNETS.STRIDE_IN_1X1,
                stride_init=None,
                res2_out_channels=cfg.MODEL.RESNETS.RES2_OUT_CHANNELS,
                dilation=cfg.MODEL.RESNETS.RES5_DILATION
            )

            self.head = head
    def __init__(self, config, select_num=5, class_num=5, is_cat=True):
        super(Predictor, self).__init__()
        self.selector = Selector()
        self.select_num = select_num
        self.sftmax = nn.Softmax(dim=0)
        self.is_cat = is_cat

        # ResNet Head
        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        self.head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        self.avgpool_glob = nn.AdaptiveAvgPool2d(output_size=14)
        # self.conv_glob = nn.Conv2d(1024, 2048, 2, stride=2)
        # self.relu_glob = nn.PReLU(2048)
        if self.is_cat:
            self.conv1 = nn.Conv2d(2048, 512, 1)
            self.relu1 = nn.ReLU(inplace=True)
            self.fc1 = nn.Linear(512 * (self.select_num + 1),
                                 512)  # select top 5
            self.fc1_relu = nn.ReLU(inplace=True)
            self.fc2 = nn.Linear(512, class_num)
        else:
            self.conv1 = nn.Conv2d(4096, 512, 1, 1)
            self.relu1 = nn.ReLU(inplace=True)
            self.fc1 = nn.Linear(512)
            self.fc1_relu = nn.ReLU(inplace=True)
            self.fc2 = nn.Linear(512, class_num)
        self.avgpool = nn.AdaptiveAvgPool2d(output_size=1)
        self.dropout = nn.Dropout()
    def __init__(self, config):
        super(MultiLabelCls, self).__init__()
        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        self.head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)
        self.pooler = nn.AdaptiveAvgPool2d(7)
        self.avgpool = nn.AdaptiveAvgPool2d(1)

        self.num_classes = config.MODEL.ROI_BOX_HEAD.NUM_CLASSES - 1  # 80
        # 1024 for both c4 and FPN architecture
        num_inputs = config.MODEL.RESNETS.BACKBONE_OUT_CHANNELS * 2
        self.classifier = nn.Linear(num_inputs, self.num_classes)

        nn.init.normal_(self.classifier.weight, mean=0, std=0.01)
        nn.init.constant_(self.classifier.bias, 0)

        self.softmax = nn.Softmax()
Exemplo n.º 10
0
    def __init__(self, config, in_channels):
        super(ResNet50Conv5ROIFeatureExtractor, self).__init__()

        resolution = config.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        scales = config.MODEL.ROI_BOX_HEAD.POOLER_SCALES
        sampling_ratio = config.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO

        pooler = Pooler(
            output_size=(resolution, resolution),
            scales=scales,
            sampling_ratio=sampling_ratio,
        )

        stage = resnet.StageSpec(index=4, block_count=3, return_features=False)
        head = resnet.ResNetHead(
            block_module=config.MODEL.RESNETS.TRANS_FUNC,
            stages=(stage, ),
            num_groups=config.MODEL.RESNETS.NUM_GROUPS,
            width_per_group=config.MODEL.RESNETS.WIDTH_PER_GROUP,
            stride_in_1x1=config.MODEL.RESNETS.STRIDE_IN_1X1,
            stride_init=None,
            res2_out_channels=config.MODEL.RESNETS.RES2_OUT_CHANNELS,
            dilation=config.MODEL.RESNETS.RES5_DILATION)

        model_parameters = head.state_dict()

        if '.pkl' in cfg.MODEL.VG.RESNET_PARAMS_FILE:
            with open(cfg.MODEL.VG.RESNET_PARAMS_FILE, 'rb') as f:
                if torch._six.PY3:
                    pretrained_paras = pickle.load(f,
                                                   encoding="latin1")['blobs']
                else:
                    pretrained_paras = pickle.load(f)['blobs']

            stages = ["1.2", "2.3", "3.5", "4.2"]
            pretrained_paras = _rename_weights_for_resnet(
                pretrained_paras, stages)
            pretrained_dict = {}
            model_parameters.keys()
            for k, v in pretrained_paras.items():
                if k in list(model_parameters.keys()):
                    print(k)
                    pretrained_dict[k] = v

        else:

            pretrained_paras = torch.load(
                cfg.MODEL.VG.RESNET_PARAMS_FILE)['model']
            pretrained_new = {}

            keys_list = list(pretrained_paras.keys())
            for keys in keys_list:
                pretrained_new[keys] = pretrained_paras[keys].cpu()
            pretrained_paras = pretrained_new

            pretrained_dict = {}
            model_parameters.keys()

            print('loading pascal pretrained weights')
            for key in key_words:
                new_key = "module.roi_heads.box.feature_extractor.head." + key
                if new_key in list(pretrained_paras.keys()):
                    pretrained_dict[key] = pretrained_paras[new_key]
                    print(key, '------>', new_key)

        model_parameters.update(pretrained_dict)
        head.load_state_dict(model_parameters)
        print('loading bottom up attention feature done')

        self.pooler = pooler
        self.head = head
        self.out_channels = head.out_channels

        spatial_dim = cfg.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        if cfg.MODEL.VG.SPATIAL_FEAT:
            self.spatial_transform = nn.Sequential(
                nn.Linear(2 * spatial_dim * spatial_dim, 256), nn.ReLU())