Пример #1
0
 def load_inception(self, model_path):
     """加载Inception预训练模型;
     Inception model没有features;
     目前本函数不可用;"""
     model = models.Inception3()
     model.load_state_dict(torch.load(model_path))
     return model
Пример #2
0
    def test_inception_model_conv_below_conv(self):
        """ Test winnowing inception model conv below conv """
        model = models.Inception3()
        model.eval()
        input_shape = [1, 3, 299, 299]
        input_channels_to_prune = [1, 3, 5, 7, 9, 15, 32, 45]

        list_of_modules_to_winnow = [(model.Mixed_5b.branch3x3dbl_2.conv,
                                      input_channels_to_prune)]

        print(model.Mixed_5b.branch3x3dbl_1.conv.out_channels)

        new_model, _ = winnow_model(model,
                                    input_shape,
                                    list_of_modules_to_winnow,
                                    reshape=True,
                                    in_place=False,
                                    verbose=True)

        self.assertEqual(new_model.Mixed_5b.branch3x3dbl_1.conv.out_channels,
                         56)
        self.assertEqual(
            list(new_model.Mixed_5b.branch3x3dbl_1.conv.weight.shape),
            [56, 192, 1, 1])
        del model
        del new_model
Пример #3
0
    def test_inception_model_conv_below_avgpool(self):
        """ Test winnowing inception model with conv below avgpool """
        model = models.Inception3()
        model.eval()
        input_shape = [1, 3, 299, 299]
        input_channels_to_prune = [1, 3, 5, 7, 9, 15, 32, 45]
        list_of_modules_to_winnow = [(model.Mixed_5b.branch_pool.conv,
                                      input_channels_to_prune)]
        print(model.Mixed_5b.branch_pool.conv)
        print(model.Mixed_5b.branch_pool.conv.out_channels,
              model.Mixed_5b.branch_pool.conv.in_channels)

        # Call the Winnow API.
        new_model, _ = winnow_model(model,
                                    input_shape,
                                    list_of_modules_to_winnow,
                                    reshape=True,
                                    in_place=False,
                                    verbose=True)

        self.assertEqual(new_model.Mixed_5b.branch_pool.conv[1].out_channels,
                         32)
        self.assertEqual(
            list(new_model.Mixed_5b.branch_pool.conv[1].weight.shape),
            [32, 184, 1, 1])
        del model
        del new_model
Пример #4
0
    def test_inception_model_conv_below_split(self):
        """ Test winnowing inception model with conv below split """
        model = models.Inception3()
        model.eval()
        input_shape = [1, 3, 299, 299]
        input_channels_to_prune = [1, 3, 5, 7, 9, 15, 32, 45]

        list_of_modules_to_winnow = [(model.Mixed_5b.branch3x3dbl_1.conv,
                                      input_channels_to_prune)]

        print(model.Mixed_5b.branch3x3dbl_1.conv.out_channels)

        # Call the Winnow API.
        new_model, _ = winnow_model(model,
                                    input_shape,
                                    list_of_modules_to_winnow,
                                    reshape=True,
                                    in_place=False,
                                    verbose=True)

        del model
        del new_model

        model = models.Inception3()
        model.eval()
        input_shape = [1, 3, 299, 299]
        input_channels_to_prune = [1, 3, 5, 7, 9, 15, 32, 45]
        list_of_modules_to_winnow = [(model.Mixed_5b.branch1x1.conv,
                                      input_channels_to_prune)]
        print(model.Mixed_5b.branch3x3dbl_1.conv.out_channels)

        # Call the Winnow API.
        new_model, _ = winnow_model(model,
                                    input_shape,
                                    list_of_modules_to_winnow,
                                    reshape=True,
                                    in_place=False,
                                    verbose=True)

        del model
        del new_model
        self.assertEqual(0, 0)
Пример #5
0
 def test_inceptionv3_eval(self):
     # replacement for models.inception_v3(pretrained=True) that does not download weights
     kwargs = {}
     kwargs['transform_input'] = True
     kwargs['aux_logits'] = True
     kwargs['init_weights'] = False
     model = models.Inception3(**kwargs)
     model.aux_logits = False
     model.AuxLogits = None
     m = torch.jit.script(model.eval())
     self.checkModule(m, "inception_v3", torch.rand(1, 3, 299, 299))
Пример #6
0
def test_inception_v3_eval():
    # replacement for models.inception_v3(pretrained=True) that does not download weights
    kwargs = {}
    kwargs['transform_input'] = True
    kwargs['aux_logits'] = True
    kwargs['init_weights'] = False
    name = "inception_v3"
    model = models.Inception3(**kwargs)
    model.aux_logits = False
    model.AuxLogits = None
    model = model.eval()
    x = torch.rand(1, 3, 299, 299)
    _check_jit_scriptable(model, (x,), unwrapper=script_model_unwrapper.get(name, None))
Пример #7
0
def test_inception_v3_eval():
    kwargs = {}
    kwargs["transform_input"] = True
    kwargs["aux_logits"] = True
    kwargs["init_weights"] = False
    name = "inception_v3"
    model = models.Inception3(**kwargs)
    model.aux_logits = False
    model.AuxLogits = None
    model = model.eval()
    x = torch.rand(1, 3, 299, 299)
    _check_jit_scriptable(model, (x,), unwrapper=script_model_unwrapper.get(name, None))
    _check_input_backprop(model, x)
Пример #8
0
    def __init__(self, k):
        super(LFGAAGoogleNet, self).__init__()
        self.k = k
    
        inception_v3 = th_models.Inception3(aux_logits=False, transform_input=False)
        state_dict = model_zoo.load_url(model_urls['inception_v3_google'])
        state_dict_rm_aux = {k: v for k, v in state_dict.items() if 'AuxLogits' not in k}
        inception_v3.load_state_dict(state_dict_rm_aux)

        layers = list(inception_v3.children())[:-1]
        layers.insert(3, torch.nn.MaxPool2d(3, 2))
        layers.insert(6, torch.nn.MaxPool2d(3, 2))
        layers.append(torch.nn.AvgPool2d(8))

        self.layers_1 = torch.nn.Sequential(*layers[:3]) # 64 x 147 x 147
        self.layers_2 = torch.nn.Sequential(*layers[3:6]) # 192 x 71 x 71
        self.layers_3 = torch.nn.Sequential(*layers[6:10]) # 288 x 35 x 35
        self.layers_4 = torch.nn.Sequential(*layers[10:15]) # 768 x 17 x 17
        self.tail_layers   = torch.nn.Sequential(*layers[15:])

        self.extract_1 = torch.nn.Sequential(
                torch.nn.MaxPool2d(kernel_size=8, stride=8, padding=-2),
                torch.nn.Conv2d(64, self.k, kernel_size=1, stride=1)
            )
        self.extract_2 = torch.nn.Sequential(
                torch.nn.MaxPool2d(kernel_size=4, stride=4),
                torch.nn.Conv2d(192, self.k, kernel_size=1, stride=1)
            )
        self.extract_3 = torch.nn.Sequential(
                torch.nn.MaxPool2d(kernel_size=2, stride=2),
                torch.nn.Conv2d(288, self.k, kernel_size=1, stride=1)
            )
        self.extract_4 = torch.nn.Sequential(
                torch.nn.Conv2d(768, self.k, kernel_size=1, stride=1)
            )
        
        self.fc1 = torch.nn.Linear(289, 1, bias=True)
        self.fc2 = torch.nn.Linear(289, 1, bias=True)
        self.fc3 = torch.nn.Linear(289, 1, bias=True)
        self.fc4 = torch.nn.Linear(289, 1, bias=True)

        self.fc5 = torch.nn.Linear(2048, 2 * k, bias=True)
        self.bn1 = torch.nn.BatchNorm1d(k)
        self.bn2 = torch.nn.BatchNorm1d(k)

        weight_init(self.fc1,
                    self.fc2,
                    self.fc3,
                    self.fc4,
                    self.fc5)
Пример #9
0
    def __init__(self, num_classes, aux_logits=True, transform_input=False,
                 pretrained=None, optimizer_name="Adam",
                 learning_rate=1e-3, loss_name="NLLLoss", metrics=None,
                 use_cuda=False, **kwargs):
        """ Class initilization.

        Parameters
        ----------
        num_classes: int
            number of classification classes.
        aux_logits: bool, default False
            auxiliary classifier for the training.
        transform_input: bool, default False
            normalize the data.
        pretrained: str, default None
            update the weights of the model using this state information.
        optimizer_name: str, default 'Adam'
            the name of the optimizer: see 'torch.optim' for a description
            of available optimizer.
        learning_rate: float, default 1e-3
            the optimizer learning rate.
        loss_name: str, default 'NLLLoss'
            the name of the loss: see 'torch.nn' for a description
            of available loss.
        metrics: list of str
            a list of extra metrics that will be computed.
        use_cuda: bool, default False
            wether to use GPU or CPU.
        kwargs: dict
            specify directly a custom 'optimizer' or 'loss'. Can also be used
            to set specific optimizer parameters.
        """
        self.model = models.Inception3(
            num_classes=num_classes,
            aux_logits=aux_logits,
            transform_input=transform_input)
        super().__init__(
            optimizer_name=optimizer_name,
            learning_rate=learning_rate,
            loss_name=loss_name,
            metrics=metrics,
            use_cuda=use_cuda,
            pretrained=pretrained,
            **kwargs)
def initialize_model(model_name,
                     num_classes,
                     feature_extract,
                     use_pretrained=True):

    model_ft = None
    input_size = 0

    if model_name == "inception":
        """ Inception V3
        """
        model_ft = models.Inception3()
        set_parameter_requires_grad(model_ft, feature_extract)
        num_ftrs = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        input_size = 224
        model_ft.aux_logits = False

    return model_ft, input_size
Пример #11
0
    def __init__(self, model_name, use_gpu=False):
        self.model_name = model_name
        self.x = process_img('./pandas.jpg')
        self.use_gpu = use_gpu

        if self.model_name in 'inception':
            self.model_name = 'inception'
            self.path = "./model_weight/inception_v3/inception_v3_google-1a9a5a14.pth"

            model = models.Inception3(aux_logits=False,
                                      transform_input=False,
                                      init_weights=False)
            model.eval()
            self.model = model
            self.depth = 2
        elif self.model_name in 'alexnet':
            self.model_name = 'alexnet'
            self.path = "./model_weight/alexnet/alexnet-owt-4df8aa71.pth"

            model = models.alexnet(False)
            model.eval()
            self.model = model
            self.depth = -1
        elif self.model_name in 'resnet':
            self.model_name = 'resnet'
            self.path = './model_weight/resnet/resnet18-f37072fd.pth'
            model = models.resnet18(False)
            model.eval()
            self.model = model
            self.depth = 2
        else:
            print("Wrong model name")

        if self.use_gpu:
            self.model = self.model.to(0)
            # self.x = self.x.cuda()
            self.x = self.x.to(0)
Пример #12
0
def _get_untrained_model(model_name, num_classes):
    """
    Primarily, this method exists to return an untrained / vanilla version of a specified (pretrained) model.
    This is on best-attempt basis only and may be out of sync with actual model definitions. The code is manually maintained.

    :param model_name: Lower-case model names are pretrained by convention.
    :param num_classes: Number of classes to initialize the vanilla model with.

    :return: default model for the model_name with custom number of classes
    """

    if model_name.startswith('bninception'):
        return classification.BNInception(num_classes=num_classes)
    elif model_name.startswith('densenet'):
        return torch_models.DenseNet(num_classes=num_classes)
    elif model_name.startswith('dpn'):
        return classification.DPN(num_classes=num_classes)
    elif model_name.startswith('inceptionresnetv2'):
        return classification.InceptionResNetV2(num_classes=num_classes)
    elif model_name.startswith('inception_v3'):
        return torch_models.Inception3(num_classes=num_classes)
    elif model_name.startswith('inceptionv4'):
        return classification.InceptionV4(num_classes=num_classes)
    elif model_name.startswith('nasnetalarge'):
        return classification.NASNetALarge(num_classes=num_classes)
    elif model_name.startswith('nasnetamobile'):
        return classification.NASNetAMobile(num_classes=num_classes)
    elif model_name.startswith('pnasnet5large'):
        return classification.PNASNet5Large(num_classes=num_classes)
    elif model_name.startswith('polynet'):
        return classification.PolyNet(num_classes=num_classes)
    elif model_name.startswith('pyresnet'):
        return classification.PyResNet(num_classes=num_classes)
    elif model_name.startswith('resnet'):
        return torch_models.ResNet(num_classes=num_classes)
    elif model_name.startswith('resnext101_32x4d'):
        return classification.ResNeXt101_32x4d(num_classes=num_classes)
    elif model_name.startswith('resnext101_64x4d'):
        return classification.ResNeXt101_64x4d(num_classes=num_classes)
    elif model_name.startswith('se_inception'):
        return classification.SEInception3(num_classes=num_classes)
    elif model_name.startswith('se_resnext50_32x4d'):
        return classification.se_resnext50_32x4d(num_classes=num_classes,
                                                 pretrained=None)
    elif model_name.startswith('se_resnext101_32x4d'):
        return classification.se_resnext101_32x4d(num_classes=num_classes,
                                                  pretrained=None)
    elif model_name.startswith('senet154'):
        return classification.senet154(num_classes=num_classes,
                                       pretrained=None)
    elif model_name.startswith('se_resnet50'):
        return classification.se_resnet50(num_classes=num_classes,
                                          pretrained=None)
    elif model_name.startswith('se_resnet101'):
        return classification.se_resnet101(num_classes=num_classes,
                                           pretrained=None)
    elif model_name.startswith('se_resnet152'):
        return classification.se_resnet152(num_classes=num_classes,
                                           pretrained=None)
    elif model_name.startswith('squeezenet1_0'):
        return torch_models.squeezenet1_0(num_classes=num_classes,
                                          pretrained=False)
    elif model_name.startswith('squeezenet1_1'):
        return torch_models.squeezenet1_1(num_classes=num_classes,
                                          pretrained=False)
    elif model_name.startswith('xception'):
        return classification.Xception(num_classes=num_classes)
    else:
        raise ValueError(
            'No vanilla model found for model name: {}'.format(model_name))
Пример #13
0
    print('Testing complete in {:.1f}m {:.4f}s'.format(time_elapsed // 60,
                                                       time_elapsed % 60))

    print('-' * 10)
    print('Number of parameters in the model %d ' % (get_num_params(model)))

    return mean_test_loss  # , mean_test_loss, std_test_acc


# ### Inception network model

# #### Pretrained Inception  model

# In[14]:

model = models.Inception3()
fc = nn.Linear(in_features=2048, out_features=2)
model.fc = fc
model = model.cuda()

optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
optimizer.scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, 'min')

# In[15]:

get_num_params(model)

# In[16]:

testing = test_model(model, test_loader)
Пример #14
0
 def forward(self, x):
     x = models.Inception3(1, False)
     x = self.sigmoid(x)
     return x
Пример #15
0
    def __init__(self,
                 num_classes=3,
                 num_level=2,
                 pool_type='max_pool',
                 use_spp=False):
        super(InceptionV3, self).__init__()
        self.inception = models.Inception3(num_classes=3, aux_logits=False)
        del self.inception.fc
        # print(self.inception._modules.keys())
        # print(self.inception)
        origin_dicts = torch.load(
            'pth/inception_v3_google-1a9a5a14.pth')  # 预训练模型中inception_v3网络的参数
        model_dicts = self.inception.state_dict()  # 自定义的去掉后面几层的网络的参数列表
        pretrained_dicts = {
            k: v
            for k, v in origin_dicts.items() if k in model_dicts
        }  # 预训练模型参数在自定义模型中有的参数列表
        model_dicts.update(pretrained_dicts)  # 更新自定义的模型参数
        self.inception.load_state_dict(model_dicts)
        self.inception_1 = models.Inception3(num_classes=3, aux_logits=False)
        del self.inception_1.fc
        self.inception_1.Conv2d_1a_3x3.conv = nn.Conv2d(1,
                                                        32,
                                                        kernel_size=(3, 3),
                                                        stride=(2, 2),
                                                        bias=False)
        model_dicts = self.inception_1.state_dict()  # 自定义的去掉后面几层的网络的参数列表
        pretrained_dicts = {
            k: v
            for k, v in origin_dicts.items() if k in model_dicts
        }  # 预训练模型参数在自定义模型中有的参数列表
        layer1 = pretrained_dicts['Conv2d_1a_3x3.conv.weight']
        new = torch.zeros(32, 1, 3, 3)
        for i, output_channel in enumerate(layer1):
            new[i] = 0.299 * output_channel[0] + 0.587 * output_channel[
                1] + 0.114 * output_channel[2]
        pretrained_dicts['Conv2d_1a_3x3.conv.weight'] = new
        model_dicts.update(pretrained_dicts)  # 更新自定义的模型参数
        self.inception_1.load_state_dict(model_dicts)

        self.conv1_fusion = nn.Conv2d(6144,
                                      256,
                                      kernel_size=3,
                                      stride=1,
                                      padding=1,
                                      bias=False)
        self.bn1 = nn.BatchNorm2d(256)
        self.relu1_fusion = nn.ReLU(inplace=True)

        self.conv2_fusion = nn.Conv2d(256,
                                      128,
                                      kernel_size=3,
                                      stride=1,
                                      padding=1,
                                      bias=False)
        self.bn2 = nn.BatchNorm2d(128)
        self.relu2_fusion = nn.ReLU(inplace=True)

        self.downsample = nn.Sequential(
            nn.Conv2d(6144,
                      128,
                      kernel_size=1,
                      stride=1,
                      padding=0,
                      bias=False),
            nn.BatchNorm2d(128),
        )
        self.conv_fusion = nn.Sequential(
            nn.Conv2d(128, 64, kernel_size=1, stride=1, padding=0, bias=False),
            nn.ReLU(inplace=True),
        )

        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(64 * 2 * 2, 512),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(512, 256),
            nn.ReLU(inplace=True),
            nn.Linear(256, num_classes),
        )

        self.use_spp = use_spp
        if use_spp:
            self.num_level = num_level
            self.pool_type = pool_type
            self.num_grid = self._cal_num_grids(num_level)
            self.spp_layer = SpatialPyramidPooling2d(num_level)

            self.classifier_spp = nn.Sequential(
                nn.Dropout(),
                nn.Linear(64 * self.num_grid, 512),
                nn.ReLU(inplace=True),
                nn.Dropout(),
                nn.Linear(512, 256),
                nn.ReLU(inplace=True),
                nn.Linear(256, num_classes),
            )