Python torchvision.models 模块,vgg19() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用torchvision.models.vgg19()

项目:intel-cervical-cancer    作者:wangg12    | 项目源码 | 文件源码
def __init__(self, num_classes, pretrained=False,
              bn_after_act=False, bn_before_act=False):
    super(Vgg19, self).__init__()

    self.pretrained = pretrained
    self.bn_before_act = bn_before_act
    self.bn_after_act = bn_after_act

    model = models.vgg19(pretrained = pretrained)
    self.features = model.features


    self.fc17 = nn.Linear(512 * 7 * 7, 4096)
    self.bn17 = nn.BatchNorm1d(4096)
    self.fc18 = nn.Linear(4096, 4096)
    self.bn18 = nn.BatchNorm1d(4096)
    self.fc19 = nn.Linear(4096, num_classes)

    self._initialize_weights()
项目:PaintsPytorch    作者:orashi    | 项目源码 | 文件源码
def def_netF():
    vgg19 = M.vgg19()
    vgg19.load_state_dict(torch.load('vgg19.pth'))
    vgg19.classifier = nn.Sequential(
        *list(vgg19.classifier.children())[:2]
    )
    for param in vgg19.parameters():
        param.requires_grad = False
    return vgg19
项目:DeblurGAN    作者:KupynOrest    | 项目源码 | 文件源码
def contentFunc(self):
        conv_3_3_layer = 14
        cnn = models.vgg19(pretrained=True).features
        cnn = cnn.cuda()
        model = nn.Sequential()
        model = model.cuda()
        for i,layer in enumerate(list(cnn)):
            model.add_module(str(i),layer)
            if i == conv_3_3_layer:
                break
        return model
项目:kaggle-planet    作者:ZijunDeng    | 项目源码 | 文件源码
def get_vgg19(num_classes, pretrained):
    net = models.vgg19()
    if pretrained:
        net.load_state_dict(torch.load(pretrained_vgg19_path))
    net.classifier = nn.Sequential(
        nn.Linear(512 * 7 * 7, 4096),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(4096, 4096),
        nn.ReLU(True),
        nn.Dropout(),
        nn.Linear(4096, num_classes),
    )
    return net
项目:pytorch-tutorial    作者:yunjey    | 项目源码 | 文件源码
def __init__(self):
        """Select conv1_1 ~ conv5_1 activation maps."""
        super(VGGNet, self).__init__()
        self.select = ['0', '5', '10', '19', '28'] 
        self.vgg = models.vgg19(pretrained=True).features
项目:pytorch-explain-black-box    作者:jacobgil    | 项目源码 | 文件源码
def load_model():
    model = models.vgg19(pretrained=True)
    model.eval()
    if use_cuda:
        model.cuda()

    for p in model.features.parameters():
        p.requires_grad = False
    for p in model.classifier.parameters():
            p.requires_grad = False

    return model
项目:pytorch-model-zoo    作者:sdhnshu    | 项目源码 | 文件源码
def __init__(self):
        super(VGGNet, self).__init__()
        self.vgg = models.vgg19(pretrained=True)
项目:fine-tuning.pytorch    作者:meliketoy    | 项目源码 | 文件源码
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 == 11):
            net = models.vgg11(pretrained=args.finetune)
        elif(args.depth == 13):
            net = models.vgg13(pretrained=args.finetune)
        elif(args.depth == 16):
            net = models.vgg16(pretrained=args.finetune)
        elif(args.depth == 19):
            net = models.vgg19(pretrained=args.finetune)
        else:
            print('Error : VGGnet should have depth of either [11, 13, 16, 19]')
            sys.exit(1)
        file_name = 'vgg-%s' %(args.depth)
    elif (args.net_type == 'resnet'):
        net = resnet(args.finetune, args.depth)
        file_name = 'resnet-%s' %(args.depth)
    else:
        print('Error : Network should be either [alexnet / vggnet / resnet]')
        sys.exit(1)

    return net, file_name
项目:pretrained-models.pytorch    作者:Cadene    | 项目源码 | 文件源码
def vgg19(num_classes=1000, pretrained='imagenet'):
    """VGG 19-layer model (configuration "E")
    """
    model = models.vgg19(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['vgg19'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_vggs(model)
    return model
项目:pytorch-smoothgrad    作者:pkdn    | 项目源码 | 文件源码
def main():
    args = parse_args()

    if not os.path.exists(args.out_dir):
        os.makedirs(args.out_dir)

    target_layer_names = ['35']
    target_index = None

    # Prepare input image
    if args.img:
        img = cv2.imread(args.img, 1)
    else:
        img = misc.face()
    img = np.float32(cv2.resize(img, (224, 224))) / 255
    preprocessed_img = preprocess_image(img, args.cuda)

    model = vgg19(pretrained=True)
    if args.cuda:
        model.cuda()

    # Prediction
    output = model(preprocessed_img)
    pred_index = np.argmax(output.data.cpu().numpy())
    print('Prediction: {}'.format(IMAGENET_LABELS[pred_index]))

    # Prepare grad cam
    grad_cam = GradCam(
        pretrained_model=model,
        target_layer_names=target_layer_names,
        cuda=args.cuda)

        # Compute grad cam
    mask = grad_cam(preprocessed_img, target_index)

    save_cam_image(img, mask, os.path.join(args.out_dir, 'grad_cam.jpg'))
    print('Saved Grad-CAM image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img)

    # Compute guided backpropagation
    guided_backprop = GuidedBackpropGrad(
        pretrained_model=model, cuda=args.cuda)
    guided_backprop_saliency = guided_backprop(preprocessed_img, index=target_index)

    cam_mask = np.zeros(guided_backprop_saliency.shape)
    for i in range(guided_backprop_saliency.shape[0]):
        cam_mask[i, :, :] = mask

    cam_guided_backprop = np.multiply(cam_mask, guided_backprop_saliency)
    save_as_gray_image(
        cam_guided_backprop,
        os.path.join(args.out_dir, 'guided_grad_cam.jpg'))
    print('Saved Guided Grad-CAM image')