Пример #1
0
    def __init__(self):
        nn.Module.__init__(self)

        from backbones.resnet import resnet18, deformable_resnet18
        #self.backbone = getattr(backbones, args['backbone'])(**args.get('backbone_args', {}))
        self.backbone = resnet18()
        from decoders.seg_detector import SegDetector
        self.decoder = SegDetector(adaptive=True,
                                   k=50,
                                   in_channels=[64, 128, 256, 512])
Пример #2
0
    def __init__(self, img_path, selected_layer):
        self.img_path = img_path
        self.selected_layer = selected_layer
        # self.pretrained_model = models.resnet18(pretrained=True)
        ############################

        from backbones.resnet import resnet18
        model_ft = resnet18()

        pretrained_dict = torch.load('save_model/finetune_model.pkl')
        model_dict = model_ft.state_dict()
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
        model_dict.update(pretrained_dict)
        model_ft.load_state_dict(model_dict)

        #############################

        self.pretrained_model = model_ft
    def load_backbone(self):
        def set_parameter_requires_grad(model, feature_extracting):
            if feature_extracting:
                for param in model.parameters():
                    param.requires_grad = False

        if self.config.backbone_type == 'resnet18':
            model_ft = resnet18(pretrained=self.config.use_pre_train)
            set_parameter_requires_grad(model_ft, self.config.feature_extract)
            num_ftrs = model_ft.fc.in_features
            model_ft.fc = nn.Linear(num_ftrs, self.config.class_num)
        elif self.config.backbone_type == 'se-resnext50':
            pretrained = None
            if self.config.use_pre_train:
                pretrained = 'imagenet'
            model_ft = se_resnext50_32x4d(pretrained=pretrained,
                                          input_3x3=self.config.input_3x3)
            set_parameter_requires_grad(model_ft, self.config.feature_extract)
            num_ftrs = model_ft.fc.in_features
            model_ft.fc = nn.Linear(num_ftrs, self.config.class_num)

        elif self.config.backbone_type == 'mobilenet':
            model_ft = mobilenet_v2(pretrained=self.config.use_pre_train)
            set_parameter_requires_grad(model_ft, self.config.feature_extract)
            num_ftrs = model_ft.last_channel
            model_ft.classifier = nn.Sequential(
                nn.Dropout(0.2),
                nn.Linear(num_ftrs, self.config.class_num),
            )

        elif self.config.backbone_type == 'shufflenet':
            model_ft = shufflenet_v2_x0_5(pretrained=self.config.use_pre_train)
            set_parameter_requires_grad(model_ft, self.config.feature_extract)
            num_ftrs = model_ft._stage_out_channels[-1]
            model_ft.fc = nn.Linear(num_ftrs, self.config.class_num)

        else:
            print('backbone not supported!')
            exit()

        return model_ft
Пример #4
0
def init_model(config_map,
               backbone_type_list=['resnet18', 'resnet50'],
               FCN_type_list=['FCN32s', 'FCN16s', 'FCN8s']):
    assert config_map[
        'backbone'] in backbone_type_list, 'backbone not supported!!!'
    assert config_map['FCN_type'] in FCN_type_list, 'backbone not supported!!!'
    if config_map['backbone'] == backbone_type_list[0]:
        backbone_net = resnet18(input_size=config_map['image_size'])
        resnet = models.resnet18(pretrained=True)
        new_state_dict = resnet.state_dict()
        dd = backbone_net.state_dict()
        for k in new_state_dict.keys():
            if k in dd.keys() and not k.startswith('fc'):
                dd[k] = new_state_dict[k]
        backbone_net.load_state_dict(dd)

    if config_map['backbone'] == backbone_type_list[1]:
        backbone_net = resnet50(input_size=config_map['image_size'])
        resnet = models.resnet50(pretrained=True)
        new_state_dict = resnet.state_dict()
        dd = backbone_net.state_dict()
        for k in new_state_dict.keys():
            if k in dd.keys() and not k.startswith('fc'):
                dd[k] = new_state_dict[k]
        backbone_net.load_state_dict(dd)

    if config_map['FCN_type'] == FCN_type_list[0]:
        fcn = FCN32(backbone_net, in_channel=config_map['in_channel'])

    if config_map['FCN_type'] == FCN_type_list[1]:
        fcn = FCN16(backbone_net, in_channel=config_map['in_channel'])

    if config_map['FCN_type'] == FCN_type_list[2]:
        fcn = FCN8(backbone_net, in_channel=config_map['in_channel'])

    return fcn
Пример #5
0
    random.seed(seed)
    torch.backends.cudnn.deterministic = True


# 设置随机数种子
setup_seed(168)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

##############################################################################################################
#注意到已经把backbone最后一层的linear注释掉,而使用margin层(可看成带要学习参数的特殊linear层)代替
#比如用于1000分类的resnet18最后使用linear(x, 1000), 把这层拿掉, 这里的x就是模型的输出维度,也就是下面的model_output_dimension

if args.backbone == 'resnet18':
    from backbones.resnet import resnet18
    model_ft = resnet18()
    pretrained_dict = models.resnet18(pretrained=True).state_dict()
    model_output_dimension = 512

elif args.backbone == 'densenet169':
    from backbones.densenet import densenet169
    model_ft = densenet169()
    pretrained_dict = models.densenet169(pretrained=True).state_dict()
    model_output_dimension = 1664

elif args.backbone == 'inception_v4':
    from backbones import inception_v4
    model_ft = inception_v4.inception_v4(num_classes=args.numclass,
                                         pretrained=False)
    pretrained_dict = inception_v4.inception_v4(
        num_classes=args.numclass).state_dict()
Пример #6
0
        # print(x3.shape)
        x3 = self.bn1(self.relu(self.Conv1x1_x3(x3)))
        x = self.dbn4(self.relu(self.DCN4(x)))
        # print(x3.shape)
        # print(x.shape)
        x = x + x3 

        x = self.dbn8(self.relu(self.DCN8(x)))

        
        return x 

if __name__ == "__main__":
    from torchvision import transforms, utils
    device = 'cuda:0'
    backbone = resnet18()
    model = FCN8(backbone, in_channel=512)
    summary(model.to(device), (3, 448, 448))


    
    tf = transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

    in_img = np.zeros((448, 448, 3), np.uint8)
    t_img = transforms.ToTensor()(in_img)
    t_img = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(t_img)