Python torchvision.models.vgg16() Examples

The following are 30 code examples of torchvision.models.vgg16(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torchvision.models , or try the search function .
Example #1
Source File: finetune.py    From transferlearning with MIT License 7 votes vote down vote up
def __init__(self):
        super(DANNet, self).__init__()
        model = models.vgg16(pretrained=True)  #False

        self.features = model.features
        for param in self.features.parameters(): #NOTE: prune:True  // finetune:False
            param.requires_grad = True

        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(25088, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
        )
        self.cls_fc = nn.Linear(4096, 31) 
Example #2
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 7 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_shared, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(812, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(448, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(1536, reddim, kernel_size=1, padding=0)
        elif model == 'resnet50':
            self.Feat_ext = resnet50_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(3584, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #3
Source File: test_attack_BlendedUniformNoiseAttack.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def test_untargeted_vgg16(image, label=None):
    import torch
    import torchvision.models as models
    from perceptron.models.classification import PyTorchModel
    mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    model_pyt = models.vgg16(pretrained=True).eval()
    if torch.cuda.is_available():
        model_pyt = model_pyt.cuda()
    model = PyTorchModel(
        model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
    print(np.argmax(model.predictions(image)))
    attack = Attack(model, criterion=Misclassification())
    adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
    distance = adversarial_obj.distance
    adversarial = adversarial_obj.image
    return distance, adversarial 
Example #4
Source File: fcn.py    From pytorch-semantic-segmentation with MIT License 6 votes vote down vote up
def __init__(self, num_classes):
        super().__init__()

        feats = list(models.vgg16(pretrained=True).features.children())

        self.feats = nn.Sequential(*feats[0:9])
        self.feat3 = nn.Sequential(*feats[10:16])
        self.feat4 = nn.Sequential(*feats[17:23])
        self.feat5 = nn.Sequential(*feats[24:30])

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                m.requires_grad = False

        self.fconn = nn.Sequential(
            nn.Conv2d(512, 4096, 7),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Conv2d(4096, 4096, 1),
            nn.ReLU(inplace=True),
            nn.Dropout(),
        )
        self.score_feat3 = nn.Conv2d(256, num_classes, 1)
        self.score_feat4 = nn.Conv2d(512, num_classes, 1)
        self.score_fconn = nn.Conv2d(4096, num_classes, 1) 
Example #5
Source File: test-ww.py    From AutoDL-Projects with MIT License 6 votes vote down vote up
def main():
  # model = models.vgg19_bn(pretrained=True)
  # _, summary = weight_watcher.analyze(model, alphas=False)
  # for key, value in summary.items():
  #   print('{:10s} : {:}'.format(key, value))

  _, summary = weight_watcher.analyze(models.vgg13(pretrained=True), alphas=False)
  print('vgg-13 : {:}'.format(summary['lognorm']))
  _, summary = weight_watcher.analyze(models.vgg13_bn(pretrained=True), alphas=False)
  print('vgg-13-BN : {:}'.format(summary['lognorm']))
  _, summary = weight_watcher.analyze(models.vgg16(pretrained=True), alphas=False)
  print('vgg-16 : {:}'.format(summary['lognorm']))
  _, summary = weight_watcher.analyze(models.vgg16_bn(pretrained=True), alphas=False)
  print('vgg-16-BN : {:}'.format(summary['lognorm']))
  _, summary = weight_watcher.analyze(models.vgg19(pretrained=True), alphas=False)
  print('vgg-19 : {:}'.format(summary['lognorm']))
  _, summary = weight_watcher.analyze(models.vgg19_bn(pretrained=True), alphas=False)
  print('vgg-19-BN : {:}'.format(summary['lognorm'])) 
Example #6
Source File: basenet.py    From MCD_DA with MIT License 6 votes vote down vote up
def __init__(self, num_classes=12):
        super(ClassifierMMD, self).__init__()
        model_ft = models.vgg16(pretrained=True)
        mod = list(model_ft.classifier.children())
        mod.pop()
        self.classifier1 = nn.Sequential(*mod)
        self.classifier2 = nn.Sequential(
            nn.Dropout(),
            nn.Linear(4096, 1000),
            nn.ReLU(inplace=True),
        )
        self.classifier3 = nn.Sequential(
            nn.BatchNorm1d(1000,affine=True),
            nn.Dropout(),
            nn.ReLU(inplace=True),
            )
        self.last = nn.Linear(1000, num_classes) 
Example #7
Source File: fcn.py    From pytorch-semantic-segmentation with MIT License 6 votes vote down vote up
def __init__(self, num_classes):
        super().__init__()

        feats = list(models.vgg16(pretrained=True).features.children())
        self.feats = nn.Sequential(*feats[0:16])
        self.feat4 = nn.Sequential(*feats[17:23])
        self.feat5 = nn.Sequential(*feats[24:30])
        self.fconn = nn.Sequential(
            nn.Conv2d(512, 4096, 7),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Conv2d(4096, 4096, 1),
            nn.ReLU(inplace=True),
            nn.Dropout(),
        )
        self.score_fconn = nn.Conv2d(4096, num_classes, 1)
        self.score_feat4 = nn.Conv2d(512, num_classes, 1) 
Example #8
Source File: network.py    From piwise with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, num_classes):
        super().__init__()

        feats = list(models.vgg16(pretrained=True).features.children())

        self.feats = nn.Sequential(*feats[0:9])
        self.feat3 = nn.Sequential(*feats[10:16])
        self.feat4 = nn.Sequential(*feats[17:23])
        self.feat5 = nn.Sequential(*feats[24:30])

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                m.requires_grad = False

        self.fconn = nn.Sequential(
            nn.Conv2d(512, 4096, 7),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Conv2d(4096, 4096, 1),
            nn.ReLU(inplace=True),
            nn.Dropout(),
        )
        self.score_feat3 = nn.Conv2d(256, num_classes, 1)
        self.score_feat4 = nn.Conv2d(512, num_classes, 1)
        self.score_fconn = nn.Conv2d(4096, num_classes, 1) 
Example #9
Source File: network.py    From piwise with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, num_classes):
        super().__init__()

        feats = list(models.vgg16(pretrained=True).features.children())
        self.feats = nn.Sequential(*feats[0:16])
        self.feat4 = nn.Sequential(*feats[17:23])
        self.feat5 = nn.Sequential(*feats[24:30])
        self.fconn = nn.Sequential(
            nn.Conv2d(512, 4096, 7),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Conv2d(4096, 4096, 1),
            nn.ReLU(inplace=True),
            nn.Dropout(),
        )
        self.score_fconn = nn.Conv2d(4096, num_classes, 1)
        self.score_feat4 = nn.Conv2d(512, num_classes, 1) 
Example #10
Source File: network.py    From piwise with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, classes):
        super().__init__()
        vgg16 = models.vgg16(pretrained=True)
        features = vgg16.features
        self.dec1 = features[0: 4]
        self.dec2 = features[5: 9]
        self.dec3 = features[10: 16]
        self.dec4 = features[17: 23]
        self.dec5 = features[24: -1]

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                m.requires_grad = False

        self.enc5 = SegNetEnc(512, 512, 1)
        self.enc4 = SegNetEnc(512, 256, 1)
        self.enc3 = SegNetEnc(256, 128, 1)
        self.enc2 = SegNetEnc(128, 64, 0)

        self.final = nn.Sequential(*[
            nn.Conv2d(64, classes, 3, padding=1),
            nn.BatchNorm2d(classes),
            nn.ReLU(inplace=True)
        ]) 
Example #11
Source File: vgg.py    From pytorch-multiple-style-transfer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, requires_grad=False):
        super(Vgg16, self).__init__()
        vgg_pretrained_features = models.vgg16(pretrained=True).features
        self.slice1 = torch.nn.Sequential()
        self.slice2 = torch.nn.Sequential()
        self.slice3 = torch.nn.Sequential()
        self.slice4 = torch.nn.Sequential()
        for x in range(4):
            self.slice1.add_module(str(x), vgg_pretrained_features[x])
        for x in range(4, 9):
            self.slice2.add_module(str(x), vgg_pretrained_features[x])
        for x in range(9, 16):
            self.slice3.add_module(str(x), vgg_pretrained_features[x])
        for x in range(16, 23):
            self.slice4.add_module(str(x), vgg_pretrained_features[x])
        if not requires_grad:
            for param in self.parameters():
                param.requires_grad = False 
Example #12
Source File: tools.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def get_image_format(framework_name, model_name):
    """Return the correct input range and shape for target framework and model"""
    special_shape = {'pytorch':{'inception_v3': (299, 299)},
                     'keras': {'xception': (299, 299),
                               'inception_v3':(299, 299),
                               'yolo_v3': (416, 416),
                               'ssd300': (300, 300)}}
    special_bound = {'keras':{'vgg16':(0, 255),
                              'vgg19':(0, 255),
                              'resnet50':(0, 255),
                              'ssd300': (0, 255)},
                     'cloud': {'aip_antiporn': (0, 255),
                               'google_safesearch': (0, 255),
                               'google_objectdetection': (0, 255)}}
    default_shape = (224, 224)
    default_bound = (0, 1)
    if special_shape.get(framework_name, None):
        if special_shape[framework_name].get(model_name, None):
            default_shape = special_shape[framework_name][model_name]
    if special_bound.get(framework_name, None):
        if special_bound[framework_name].get(model_name, None):
            default_bound = special_bound[framework_name][model_name]
    return {'shape': default_shape, 'bounds': default_bound} 
Example #13
Source File: test_attack_AdditiveUniformNoiseAttack.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def test_untargeted_vgg16(image, label=None):
    import torch
    import torchvision.models as models
    from perceptron.models.classification import PyTorchModel
    mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    model_pyt = models.vgg16(pretrained=True).eval()
    if torch.cuda.is_available():
        model_pyt = model_pyt.cuda()
    model = PyTorchModel(
        model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
    print(np.argmax(model.predictions(image)))
    attack = Attack(model, criterion=Misclassification())
    adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
    distance = adversarial_obj.distance
    adversarial = adversarial_obj.image
    return distance, adversarial 
Example #14
Source File: vgg.py    From examples with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, requires_grad=False):
        super(Vgg16, self).__init__()
        vgg_pretrained_features = models.vgg16(pretrained=True).features
        self.slice1 = torch.nn.Sequential()
        self.slice2 = torch.nn.Sequential()
        self.slice3 = torch.nn.Sequential()
        self.slice4 = torch.nn.Sequential()
        for x in range(4):
            self.slice1.add_module(str(x), vgg_pretrained_features[x])
        for x in range(4, 9):
            self.slice2.add_module(str(x), vgg_pretrained_features[x])
        for x in range(9, 16):
            self.slice3.add_module(str(x), vgg_pretrained_features[x])
        for x in range(16, 23):
            self.slice4.add_module(str(x), vgg_pretrained_features[x])
        if not requires_grad:
            for param in self.parameters():
                param.requires_grad = False 
Example #15
Source File: test_attack_SaltAndPepperNoiseAttack.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def test_untargeted_vgg16(image, label=None):
    import torch
    import torchvision.models as models
    from perceptron.models.classification import PyTorchModel
    mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    model_pyt = models.vgg16(pretrained=True).eval()
    if torch.cuda.is_available():
        model_pyt = model_pyt.cuda()
    model = PyTorchModel(
        model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
    print(np.argmax(model.predictions(image)))
    attack = Attack(model, criterion=Misclassification())
    adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
    distance = adversarial_obj.distance
    adversarial = adversarial_obj.image
    return distance, adversarial 
Example #16
Source File: test_attack_Gaussian_blur.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def test_untargeted_vgg16(image, label=None):
    import torch
    import torchvision.models as models
    from perceptron.models.classification import PyTorchModel
    mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    model_pyt = models.vgg16(pretrained=True).eval()
    if torch.cuda.is_available():
        model_pyt = model_pyt.cuda()
    model = PyTorchModel(
        model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
    print(np.argmax(model.predictions(image)))
    attack = Attack(model, criterion=Misclassification())
    adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
    distance = adversarial_obj.distance
    adversarial = adversarial_obj.image
    return distance, adversarial 
Example #17
Source File: test_attack_MotionBlurAttack.py    From perceptron-benchmark with Apache License 2.0 6 votes vote down vote up
def test_untargeted_vgg16(image, label=None):
    import torch
    import torchvision.models as models
    from perceptron.models.classification import PyTorchModel
    mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
    std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
    model_pyt = models.vgg16(pretrained=True).eval()
    if torch.cuda.is_available():
        model_pyt = model_pyt.cuda()
    model = PyTorchModel(
        model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
    print(np.argmax(model.predictions(image)))
    attack = Attack(model, criterion=Misclassification())
    adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
    distance = adversarial_obj.distance
    adversarial = adversarial_obj.image
    return distance, adversarial 
Example #18
Source File: cnn_benchmarks.py    From stacks-usecase with Apache License 2.0 6 votes vote down vote up
def select(self, model_name=None):
        """select models to be run"""
        logging.info("Run details")
        logging.info("=" * 71)
        models = [
            self.alexnet,
            self.resnet18,
            self.resnet50,
            self.vgg16,
            self.squeezenet,
        ]
        if model_name:
            self.models = [
                model for model in models for name in model_name if name == model.name
            ]
        logging.info("Selected model(s) :: ")
        for m in self.models:
            logging.info("%s ------------- Batchsize :: %s " % (m.name, m.batch))
        logging.info("=" * 71) 
Example #19
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_individual, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext1 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext2 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext3 = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(232, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext1 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext2 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext3 = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(96, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext1 = vgg_base(loadweight,downsample)
            self.Feat_ext2 = vgg_base(loadweight,downsample)
            self.Feat_ext3 = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(512, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #20
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_shared, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(812, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(448, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(1536, reddim, kernel_size=1, padding=0)
        elif model == 'resnet50':
            self.Feat_ext = resnet50_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(3584, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #21
Source File: vgg16.py    From NeuralBabyTalk with MIT License 6 votes vote down vote up
def __init__(self, opt, pretrained=True):
    super(vgg16, self).__init__()

    self.model_path = '%s/imagenet_weights/vgg16_caffe.pth' %(opt.data_path)
    self.pretrained = pretrained

    vgg = models.vgg16()
    vgg.classifier = nn.Sequential(*list(vgg.classifier._modules.values())[:-1])
    self.fc = vgg.classifier
    self.pooling = nn.AdaptiveAvgPool2d((7,7))
    if self.pretrained:
        print("Loading pretrained weights from %s" %(self.model_path))
        state_dict = torch.load(self.model_path)
        vgg.load_state_dict({k:v for k,v in state_dict.items() if k in vgg.state_dict()})

    # not using the last maxpool layer
    self.cnn_net = nn.Sequential(*list(vgg.features._modules.values())[:-1]) 
Example #22
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_individual, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext1 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext2 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext3 = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(232, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext1 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext2 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext3 = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(96, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext1 = vgg_base(loadweight,downsample)
            self.Feat_ext2 = vgg_base(loadweight,downsample)
            self.Feat_ext3 = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(512, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #23
Source File: fcn.py    From binseg_pytoch with Apache License 2.0 6 votes vote down vote up
def __init__(self, num_classes):
        super(FCN16, self).__init__()

        feats = list(models.vgg16(pretrained=True).features.children())
        self.feats = nn.Sequential(*feats[0:17])
        self.pool4 = nn.Sequential(*feats[17:24])
        self.pool5 = nn.Sequential(*feats[24:])

        self.fconn = nn.Sequential(nn.Conv2d(512, 4096, 7, padding=3),
                                   nn.ReLU(inplace=True),
                                   nn.Conv2d(4096, 4096, 1),
                                   nn.ReLU(inplace=True),
                                   nn.Conv2d(4096, num_classes, 1)
                                   )
        self.score_pool4 = nn.Conv2d(512, num_classes, 1)
        self.activation = nn.Sigmoid() 
Example #24
Source File: fcn.py    From binseg_pytoch with Apache License 2.0 6 votes vote down vote up
def __init__(self, num_classes):
        super(FCN8, self).__init__()

        feats = list(models.vgg16(pretrained=True).features.children())

        self.feats = nn.Sequential(*feats[0:10])
        self.pool3= nn.Sequential(*feats[10:17])
        self.pool4 = nn.Sequential(*feats[17:24])
        self.pool5 = nn.Sequential(*feats[24:])

        self.fconn = nn.Sequential(nn.Conv2d(512, 4096, 7, padding=3),
                                   nn.ReLU(inplace=True),
                                   nn.Conv2d(4096, 4096, 1),
                                   nn.ReLU(inplace=True),
                                   nn.Conv2d(4096, num_classes, 1)
                                   )

        self.score_pool3 = nn.Conv2d(256, num_classes, 1)
        self.score_pool4 = nn.Conv2d(512, num_classes, 1)
        self.activation = nn.Sigmoid() 
Example #25
Source File: segnet.py    From binseg_pytoch with Apache License 2.0 6 votes vote down vote up
def __init__(self, num_classes):
        super(SegNet, self).__init__()

        modules= list(models.vgg16(pretrained=True).features.children())

        self.conv1 = nn.Sequential(*modules[0:4])
        self.conv2 = nn.Sequential(*modules[5:9])
        self.conv3 = nn.Sequential(*modules[10:16])
        self.conv4 = nn.Sequential(*modules[17:23])
        self.conv5 = nn.Sequential(*modules[24:30])

        self.dec512 = DecodeBlock(512,512,3,1,num_layers=3)
        self.dec256 = DecodeBlock(512, 256, 3, 1, num_layers=3)
        self.dec128 = DecodeBlock(256, 128, 3, 1, num_layers=3)
        self.dec64 = DecodeBlock(128, 64, 3, 1, num_layers=2)

        self.final = nn.Sequential(nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
                                   nn.Conv2d(64, 1, kernel_size=3, padding=1))


        self.activation = nn.Sigmoid()

        initialize_weights(self.dec512,self.dec256,self.dec128,self.dec64, self.final) 
Example #26
Source File: models.py    From SPN.pytorch with MIT License 6 votes vote down vote up
def vgg16_sp(num_classes, pretrained=True, num_maps=1024):
    model = models.vgg16(pretrained=False)
    if pretrained:
        model_path = 'models/VGG16_ImageNet.pt'
        if os.path.isfile(model_path):
            state_dict = torch.load(model_path)
            model.load_state_dict(state_dict)
        else:
            print('Please download the pretrained VGG16 into ./models')

    num_features = model.features[28].out_channels
    pooling = nn.Sequential()
    pooling.add_module('adconv', nn.Conv2d(num_features, num_maps, kernel_size=3, stride=1, padding=1, groups=2, bias=True))
    pooling.add_module('maps', nn.ReLU())
    pooling.add_module('sp', SoftProposal())
    pooling.add_module('sum', SpatialSumOverMap())
    return SPNetWSL(model, num_classes, num_maps, pooling) 
Example #27
Source File: inference.py    From fine-tuning.pytorch with MIT License 6 votes vote down vote up
def getNetwork(args):
    if (args.net_type == 'alexnet'):
        net = models.alexnet(pretrained=args.finetune)
        file_name = 'alexnet'
    elif (args.net_type == 'vggnet'):
        if(args.depth == 16):
            net = models.vgg16(pretrained=args.finetune)
        file_name = 'vgg-%s' %(args.depth)
    elif (args.net_type == 'inception'):
        net = models.inception(pretrained=args.finetune)
        file_name = 'inceptino-v3'
    elif (args.net_type == 'resnet'):
        net = resnet(args.finetune, args.depth)
        file_name = 'resnet-%s' %(args.depth)
    else:
        print('Error : Network should be either [VGGNet / ResNet]')
        sys.exit(1)

    return net, file_name 
Example #28
Source File: faster_rcnn_vgg16.py    From FATE with Apache License 2.0 6 votes vote down vote up
def decom_vgg16():
    # the 30th layer of features is relu of conv5_3
    if opt.caffe_pretrain:
        model = vgg16(pretrained=False)
        if not opt.load_path:
            model.load_state_dict(t.load(opt.caffe_pretrain_path))
    else:
        model = vgg16(not opt.load_path)

    features = list(model.features)[:30]
    classifier = model.classifier

    classifier = list(classifier)
    del classifier[6]
    if not opt.use_drop:
        del classifier[5]
        del classifier[2]
    classifier = nn.Sequential(*classifier)

    # freeze top4 conv
    for layer in features[:10]:
        for p in layer.parameters():
            p.requires_grad = False

    return nn.Sequential(*features), classifier 
Example #29
Source File: vgg_fcn.py    From MCD_DA with MIT License 5 votes vote down vote up
def _initialize_weights(self):
        vgg16 = models.vgg16(pretrained=True)
        self.copy_params_from_vgg16(vgg16) 
Example #30
Source File: basenet.py    From MCD_DA with MIT License 5 votes vote down vote up
def __init__(self):
        super(BaseNet, self).__init__()
        model_ft = models.vgg16(pretrained=True)
        mod = list(model_ft.features.children())
        self.features = nn.Sequential(*mod)
        mod = list(model_ft.classifier.children())
        mod.pop()
        self.classifier = nn.Sequential(*mod)