Example #1
0
    def get_model(self):
        model = models.mobilenet_v2(pretrained=True)

        # freeze all the layers, then make a new classifier layer to match # classes
        for param in model.parameters():
            param.requires_grad = False

        model.classifier = nn.Sequential(
            nn.Dropout(0.2), nn.Linear(model.last_channel, NUM_CLASSES))
        return model
def select_model(model_type: str, num_classes: int, pretrained: bool = False):
    if model_type == "alexnet":
        if pretrained:
            model = models.alexnet(pretrained=pretrained)
            num_ftrs = model.classifier[6].in_features
            model.classifier[6] = nn.Linear(num_ftrs, num_classes)
            return model
        else:
            return models.alexnet(num_classes=num_classes)
    elif model_type == "resnet":
        if pretrained:
            model = models.resnet50(pretrained=pretrained)
            num_ftrs = model.fc.in_features
            model.fc = nn.Linear(num_ftrs, num_classes)
            return model
        else:
            return models.resnet50(num_classes=num_classes)
    elif model_type == "densenet":
        if pretrained:
            model = models.densenet121(pretrained=pretrained)
            num_ftrs = model.classifier.in_features
            model.classifier = nn.Linear(num_ftrs, num_classes)
            return model
        else:
            return models.densenet121(num_classes=num_classes)
    elif model_type == "vgg":
        if pretrained:
            model = models.vgg16(pretrained=pretrained)
            num_ftrs = model.classifier[6].in_features
            model.classifier[6] = nn.Linear(num_ftrs, num_classes)
            return model
        else:
            return models.vgg16(num_classes=num_classes)
    elif model_type == "mobilenet":
        if pretrained:
            model = models.mobilenet_v2(pretrained=pretrained)
            num_ftrs = model.classifier[1].in_features
            model.classifier[1] = nn.Linear(num_ftrs, num_classes, bias=True)
            return model
        else:
            return models.mobilenet_v2(num_classes=num_classes)
    else:
        return None
Example #3
0
 def __init__(self):
     super(MobileNet_AVG, self).__init__()
     self.base = nn.Sequential(
         OrderedDict([
             *list(
                 models.mobilenet_v2(
                     pretrained=True).features.named_children())
         ]))
     self.pool = torch.nn.AdaptiveAvgPool2d((1, 1))
     self.norm = L2N()
Example #4
0
 def __init__(self):
     super(MobileNet_GeM, self).__init__()
     self.base = nn.Sequential(
         OrderedDict([
             *list(
                 models.mobilenet_v2(
                     pretrained=True).features.named_children())
         ]))
     self.pool = GeM()
     self.norm = L2N()
Example #5
0
def Mobilenetv2(num_classes, test=False):
    model = mobilenet_v2()
    state_dict = torch.hub.load_state_dict_from_url(
        'https://download.pytorch.org/models/mobilenet_v2-b0353104.pth',
        progress=True)
    model.load_state_dict(state_dict)
    fc_features = model.classifier[1].in_features
    model.classifier = nn.Linear(fc_features, num_classes)
    model = model.cuda()
    return model
Example #6
0
 def __init__(self):
     super(GazeEstimationMobile, self).__init__()
     mobilenet = models.mobilenet_v2(pretrained=True)
     # Remove linear layer
     modules = list(mobilenet.children())[:-1]
     self.features = nn.Sequential(*modules)
     self.pool = nn.AvgPool2d(kernel_size=7)
     # building last several layers
     self.fc_look_vec = nn.Linear(1280, 3)
     self.fc_pupil_size = nn.Linear(1280, 1)
Example #7
0
    def __init__(self):
        super().__init__()
        self.base_model = models.mobilenet_v2().features  # take the model without classifier
        last_channel = models.mobilenet_v2().last_channel # size of the layer before the classifier

        # the input for the classifier should be two-dimensional, but we will have
        # [<batch_size&gt;, <channels&gt;, <width&gt;, <height&gt;]
        # so, let's do the spatial averaging: reduce <width&gt; and <height&gt; to 1
        self.pool = nn.AdaptiveAvgPool2d((1, 1))

        # create separate classifiers for our outputs
        self.Type = nn.Sequential(
            nn.Dropout(p=0.2),
            nn.Linear(in_features=last_channel, out_features=2)
        )
        self.target = nn.Sequential(
            nn.Dropout(p=0.2),
            nn.Linear(in_features=last_channel, out_features=15)
        )
    def __init__(self):
        super().__init__()

        # pretrained encoder
        # TODO
        self.backbone = models.mobilenet_v2(pretrained=True)

        # do not train encoder
        for param in self.backbone.parameters():
            param.requires_grad = False
Example #9
0
    def __init__(self, embedding_size, momentum):

        super(EncoderCNN, self).__init__()
        mobile_net = models.mobilenet_v2(pretrained=True)
        modules = list(mobile_net.children())[:-1]
        self.mobilenet = nn.Sequential(*modules)
        self.classifier = nn.Sequential(
            nn.Dropout(0.2), nn.Linear(mobile_net.last_channel,
                                       embedding_size))
        self.batch_norm = nn.BatchNorm1d(embedding_size, momentum=momentum)
Example #10
0
def build_final_model():
    model_mn = models.mobilenet_v2(pretrained=False)
    model_mn.classifier[1] = nn.Linear(in_features=model_mn.last_channel,
                                       out_features=NUM_CLASSES)
    model = nn.Sequential(
        model_mn,
        nn.LogSoftmax(dim=-1),
    )

    return model
Example #11
0
    def _test_gradcam(self, name):

        # Get a pretrained model
        model = mobilenet_v2(pretrained=False)
        conv_layer = 'features'

        # Hook the corresponding layer in the model
        extractor = cams.__dict__[name](model, conv_layer)

        self._test_extractor(extractor, model)
Example #12
0
    def __init__(self, num_classes=3):
        super(mobilenet_mod, self).__init__()

        self.fusion = torch.nn.Conv2d(3, 3, 1, bias=False)
        self.mobilenet = mobilenet_v2(pretrained=True)
        '''
        for param in self.googlenet.parameters():
            param.requires_grad = False
        '''
        self.mobilenet.classifier[1] = torch.nn.Linear(1280, num_classes)
 def __init__(self):
     super(FaceAttributeModel, self).__init__()
     model = models.mobilenet_v2(pretrained=True)
     # Remove linear and pool layers (since we're not doing classification)
     modules = list(model.children())[:-1]
     self.resnet = nn.Sequential(*modules)
     self.pool = nn.AvgPool2d(kernel_size=7)
     self.fc = nn.Linear(1280, 17)
     self.sigmoid = nn.Sigmoid()
     self.softmax = nn.Softmax(dim=-1)
Example #14
0
def test_pretrained_mobilenet():
    fext = MobilenetV2FeatureExtractor(alpha=1.,
                                       pretrained=True,
                                       requires_grad=False)
    reference_model = torch_models.mobilenet_v2(pretrained=True)
    batch_cnt = 3
    x = torch.randn(batch_cnt, 3, 224, 224)
    ref_out = reference_model.features(x)
    out = fext(x)
    assert np.all(torch.eq(out, ref_out).numpy())
Example #15
0
def get_model(model_name):
    """
    Get specific modified pre-trained models by name
    """
    model = None
    if model_name == "AlexNet":
        model = models.alexnet(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "VGG":
        model = models.vgg16(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "ResNet":
        model = models.resnet18(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "SqueezNet":
        model = models.squeezenet1_1(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "DenseNet":
        model = models.densenet121(pretrained=True)
        in_features = model.classifier.in_features
        model.classifier = nn.Linear(in_features, 26)
    elif model_name == "Inception":
        model = models.inception_v3(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "GoogleNet":
        model = models.googlenet(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "ShuffleNet":
        model = models.shufflenet_v2_x1_0(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "MobileNet":
        model = models.mobilenet_v2(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "ResNext":
        model = models.resnext101_32x8d(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "WResNet":
        model = models.wide_resnet101_2(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)
    elif model_name == "MNASNet":
        model = models.mnasnet1_0(pretrained=True)
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, 26)

    return model
Example #16
0
 def __init__(self, num_classes):
     super(MobileNet_GDConv_56, self).__init__()
     self.pretrain_net = models.mobilenet_v2(pretrained=False)
     self.base_net = nn.Sequential(*list(self.pretrain_net.children())[:-1])
     self.linear7 = ConvBlock(1280,
                              1280, (2, 2),
                              1,
                              0,
                              dw=True,
                              linear=True)
     self.linear1 = ConvBlock(1280, num_classes, 1, 1, 0, linear=True)
Example #17
0
 def create(cls, cfg):
     pretrained = cfg.pretrained
     if cfg.backbone == "resnet18":
         backbone = resnet18(pretrained)
     elif cfg.backbone == "resnet50":
         backbone = resnet50(pretrained)
     elif cfg.backbone == "mobilenet_v2":
         backbone = mobilenet_v2(pretrained)
     else:
         raise ValueError(f"{cfg.backbone} is not a valid backbone")
     return cls(backbone)
Example #18
0
def torch_vision_mobilenet_v2(pretrained):
    from torchvision.models import mobilenet_v2
    imagenet_pretrained = pretrained == 'imagenet'
    net = mobilenet_v2(imagenet_pretrained)
    splits = [0, 2, 4, 7, 14]
    layers = [net.features[i:j] for i, j in zip(splits, splits[1:] + [len(net.features)-1])]
    for l in layers:
        l.out_channels = l[-1].conv[-2].out_channels

    n_pretrained = len(layers) if pretrained else 0
    return layers, True, n_pretrained
Example #19
0
    def __init__(self, opt):
        super(Model, self).__init__()
        model_ft = models.mobilenet_v2()
        num_ftrs = model_ft.classifier[1].in_features
        model_ft.classifier[1] = nn.Identity()
        if opt.pretrained:
            model_ft.load_state_dict(torch.load(opt.net))

        self.model_ft = model_ft.to(opt.device)
        self.cifar_layer = nn.Linear(num_ftrs, 10).to(opt.device)
        self.imagenet_layer = nn.Linear(num_ftrs, 100).to(opt.device)
Example #20
0
 def __init__(self):
     super(SpectrogramEncoderNet, self).__init__()
     self.encoder_size = 128
     self.encoder = models.mobilenet_v2(
         pretrained=False)  # base model (transfer learning)
     # self.encoder = models.densenet121(pretrained=False)  # base model (transfer learning)
     self.encoder.classifier = nn.Sequential(
         nn.Linear(1280, self.encoder_size
                   ),  # encoding layer, mobile_netV2 output: 1280
         # nn.Linear(1024, self.encoder_size),  # encoding layer, densenet121 output: 1024
     )
Example #21
0
def create_basenet(name, pretrained, drop_last=0, activation=None, frozen_batchnorm=False, in_channels=3, **kwargs):
    """
    Parameters
    ----------
    name: model name
    pretrained: dataset name

    Returns
    -------
    list of modules, is_batchnorm, num_of_pretrained_module
    """
    if name.startswith('vgg'):
        layers, bn, n_pretrained = vgg(name, pretrained)
    elif name.lower() in ('resnext101_32x4d', 'resnext101_64x4d'):
        layers, bn, n_pretrained = resnext(name, pretrained)
    elif name.lower().startswith('resnet') or name.lower().startswith('resnext'):
        layers, bn, n_pretrained = resnet(name, pretrained, **kwargs)
    elif name.lower().startswith('se'):
        layers, bn, n_pretrained = se_net(name, pretrained)
    elif name.lower().startswith('densenet'):
        layers, bn, n_pretrained = densenet(name, pretrained, **kwargs)
    elif name.lower().startswith('efficientnet'):
        layers, bn, n_pretrained = efficientnet(name, pretrained, in_channels=in_channels, memory_efficient=True)
    elif name == 'darknet':
        layers, bn, n_pretrained = darknet(pretrained)
    elif name == 'mobilenet_v2':
        layers, bn, n_pretrained = mobilenet_v2(pretrained)
    elif name == 'Mobilenet_v2':
        layers, bn, n_pretrained = torch_vision_mobilenet_v2(pretrained)
    elif name == 'mobilenet_v3':
        layers, bn, n_pretrained = mobilenet_v3(pretrained)
    elif name.startswith('shufflenet_v2'):
        layers, bn, n_pretrained = shufflenet_v2(name, pretrained)
    elif name.startswith('squeezenet1'):
        layers, bn, n_pretrained = squeezenet1(name, pretrained)
    else:
        raise NotImplemented(name)

    if pretrained in ('voc', 'coco', 'oid'):
        load_pretrained_weights(layers, name, pretrained)
        n_pretrained = len(layers)

    if drop_last > 0:
        layers = layers[:-drop_last]
        n_pretrained = max(0, n_pretrained - drop_last)

    if activation:
        layers = [convert_activation(activation, l) for l in layers]

    if frozen_batchnorm:
        from .batch_norm import FrozenBatchNorm2d
        layers = [FrozenBatchNorm2d.convert_frozen_batchnorm(l) for l in layers]

    return layers, bn, n_pretrained
Example #22
0
    def test_smooth_gradcampp(self):

        # Get a pretrained model
        model = mobilenet_v2(pretrained=False)
        conv_layer = 'features'
        input_layer = 'features'

        # Hook the corresponding layer in the model
        extractor = cams.SmoothGradCAMpp(model, conv_layer, input_layer)

        self._test_extractor(extractor, model)
def test_mobilenet_v2_model(input_var):
    original_model = torchvision_models.mobilenet_v2(pretrained=True)
    finetune_model = make_model(
        'mobilenet_v2',
        num_classes=1000,
        pool=default,
        pretrained=True,
    )
    copy_module_weights(original_model.classifier[-1],
                        finetune_model._classifier)
    assert_equal_model_outputs(input_var, original_model, finetune_model)
def flops_counter(args):
    # model speed up
    torch.manual_seed(0)
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    train_loader, val_loader, criterion = get_data(args)

    if args.pruner != 'AutoCompressPruner':
        if args.model == 'LeNet':
            model = LeNet().to(device)
        elif args.model == 'vgg16':
            model = VGG(depth=16).to(device)
        elif args.model == 'resnet18':
            model = models.resnet18(pretrained=False,
                                    num_classes=10).to(device)
        elif args.model == 'mobilenet_v2':
            model = models.mobilenet_v2(pretrained=False).to(device)

        def evaluator(model):
            return test(model, device, criterion, val_loader)

        model.load_state_dict(
            torch.load(
                os.path.join(args.experiment_data_dir,
                             'model_fine_tuned.pth')))
        masks_file = os.path.join(args.experiment_data_dir, 'mask.pth')

        dummy_input = get_dummy_input(args, device)

        m_speedup = ModelSpeedup(model, dummy_input, masks_file, device)
        m_speedup.speedup_model()
        evaluation_result = evaluator(model)
        print('Evaluation result (speed up model): %s' % evaluation_result)

        with open(os.path.join(args.experiment_data_dir,
                               'performance.json')) as f:
            result = json.load(f)

        result['speedup'] = evaluation_result
        with open(os.path.join(args.experiment_data_dir, 'performance.json'),
                  'w+') as f:
            json.dump(result, f)

        torch.save(
            model.state_dict(),
            os.path.join(args.experiment_data_dir, 'model_speed_up.pth'))
        print('Speed up model saved to %s', args.experiment_data_dir)
    else:
        model = torch.load(
            os.path.join(args.experiment_data_dir, 'model_fine_tuned.pth'))
        model.eval()
        flops, params = count_flops_params(model, (1, 3, 32, 32))
        with open(os.path.join(args.experiment_data_dir, 'flops.json'),
                  'w+') as f:
            json.dump({'FLOPS': int(flops), 'params': int(params)}, f)
Example #25
0
 def __init__(self):
     super(FaceNet, self).__init__()
     self.sub_net = nn.Sequential(models.mobilenet_v2(), )
     # print(models.mobilenet_v2())
     self.feature_net = nn.Sequential(
         nn.BatchNorm1d(1000),
         nn.LeakyReLU(0.1),
         # nn.PReLU(),
         nn.Linear(1000, 512, bias=False),
     )
     self.arc_softmax = Arcsoftmax(512, 108)
Example #26
0
def get_net(net_name, weight_path=None):
    """
    根据网络名称获取模型
    :param net_name: 网络名称
    :param weight_path: 与训练权重路径
    :return:
    """
    pretrain = weight_path is None  # 没有指定权重路径,则加载默认的预训练权重
    if net_name in ['vgg16']:
        net = models.vgg16(pretrained=pretrain)
    elif net_name == 'vgg19':
        net = models.vgg19(pretrained=pretrain)
    elif net_name in ['resnet18']:
        net = models.resnet18(pretrained=pretrain)
    elif net_name in ['frcnn']:
        net = models.frcnn(pretrained=pretrain)
    elif net_name in ['resnet50']:
        net = models.resnet50(pretrained=pretrain)
    elif net_name == 'resnet101':
        net = models.resnet101(pretrained=pretrain)
    elif net_name in ['densenet121']:
        net = models.densenet121(pretrained=pretrain)
    elif net_name in ['densenet169']:
        net = models.densenet169(pretrained=pretrain)
    elif net_name in ['inception']:
        net = models.inception_v3(pretrained=pretrain)
    elif net_name in ['mobilenet_v2']:
        net = models.mobilenet_v2(pretrained=pretrain)
    elif net_name in ['shufflenet_v2']:
        net = models.shufflenet_v2_x1_0(pretrained=pretrain)
    elif net_name == 'efficientnet':
        net = EfficientNet.from_name('efficientnet-b0')
        feature = net._fc.in_features
        net._fc = nn.Linear(in_features=feature, out_features=2,
                            bias=True)  # 修改分类层结构
        net.load_state_dict(torch.load("efficientNet-b0.pt"))  # 加载CT集上训练参数
    else:
        raise ValueError('invalid network name:{}'.format(net_name))
    # 加载指定路径的权重参数
    if weight_path is not None and net_name.startswith('densenet'):
        pattern = re.compile(
            r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$'
        )
        state_dict = torch.load(weight_path)
        for key in list(state_dict.keys()):
            res = pattern.match(key)
            if res:
                new_key = res.group(1) + res.group(2)
                state_dict[new_key] = state_dict[key]
                del state_dict[key]
        net.load_state_dict(state_dict)
    elif weight_path is not None:
        net.load_state_dict(torch.load(weight_path))
    return net
Example #27
0
 def __init__(self):
     super(CNN, self).__init__()
     self.mobile_model = models.mobilenet_v2(pretrained=True)
     n = 0
     for child in self.mobile_model.children():
         n += 1
         if n < 2:
             for param in child.parameters():
                 param.requires_grad = False
     self.features = nn.Sequential(*list(self.mobile_model.children())[:-1])
     self.linear = nn.Linear(62720, 149)
 def __init__(self, num_attributes=40):
     super(FaceAttrMobileNetV2, self).__init__()
     self.name = 'FaceAttrMobileNetV2_50'
     pt_model = tvmodels.mobilenet_v2(pretrained=True, progress=False)
     pt_out = pt_model.classifier[-1].out_features
     self.pretrained = pt_model
     self.num_attributes = num_attributes
     for i in range(num_attributes):
         setattr(self, 'classifier' + str(i).zfill(2),
                 nn.Sequential(FC_Block(pt_out, pt_out // 2),
                               nn.Linear(pt_out // 2, 2)))
Example #29
0
    def __init__(self, device, freeze=True):
        super(MobileNet, self).__init__()
        self.device = device
        self.mobilenet = mobilenet_v2(pretrained=True, progress=True).to(device)
        if freeze:
            for m in self.mobilenet.parameters():
                m.requires_grad = False

        self.layer6 = LayerActivation(self.mobilenet.features, 6)
        self.layer13 = LayerActivation(self.mobilenet.features, 13)
        self.layer18 = LayerActivation(self.mobilenet.features, 18)
  def __init__(self):
    super(mobilenetv2Model,self).__init__()

    model = models.mobilenet_v2(pretrained=True)
    for param in model.parameters():
        param.requires_grad = False
    num_ftrs = 1280 #cannot get from classifier component
    model.classifier = nn.Sequential(nn.Dropout(p=0.2,inplace=False),
                                          nn.Linear(in_features = num_ftrs,out_features = 256),
                                          nn.Dropout(p=0.5),
                                          nn.Linear(256,1))
    self.pretrainedModel = model