神经网络----现有网络的下载和使用(vgg16)

时间:2024-06-10 18:57:19

 以下两种方法已经用不了

vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)

改为

vgg16_model0 = vgg16(weights=None)  #vgg16_false
vgg16_model1 = vgg16(weights=VGG16_Weights.DEFAULT)  # pretrained vgg16_true
import torchvision
from torch import nn
from torchvision.models import vgg16, VGG16_Weights

# 以下注释的两行用不了,现在更新了新方法
# vgg16_false = torchvision.models.vgg16(pretrained=False) 
# vgg16_true = torchvision.models.vgg16(pretrained=True)

vgg16_model0 = vgg16(weights=None)  #vgg16_false
vgg16_model1 = vgg16(weights=VGG16_Weights.DEFAULT)  # pretrained vgg16_true
# vgg16_model2 = vgg16(weights=VGG16_Weights.IMAGENET1K_V1)  # 代表使用IMAGENET1K_V1方法训练
# print(vgg16_model1)
train_data = torchvision.datasets.CIFAR10(root='./dataset', train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

print('---------------增加一层--------------------------------------------------    ----')
# vgg16_model1.add_module('add_liner', nn.Linear(1000, 10))
# print(vgg16_model1)
print('--------------在classifier里-增加一层--------------------------------- -----------')
vgg16_model1.classifier.add_module('add_liner', nn.Linear(1000, 10))
print(vgg16_model1)

print('-------------------未修改的vgg16--------------------------------------------------')
print(vgg16_model0)
print('-------------------将vgg16的classifier的第6层进行修改-------------------------------')
vgg16_model0.classifier[6] = nn.Linear(4096, 10)
print(vgg16_model0)