现有网络模型的使用和修改
- 官网 [https://pytorch.org/](https://pytorch.org/)
- torchvison 相关model
- 1. 图像常用vgg16模型 【vgg19也常用】
- 2. ImageNet数据集太大 无法代码下载 kaggle网址下载
- 3. 代码
- 4. 执行结果
官网 https://pytorch.org/
torchvison 相关model
1. 图像常用vgg16模型 【vgg19也常用】
https://pytorch.org/vision/stable/models/generated/torchvision.models.vgg16.html#torchvision.models.vgg16
2. ImageNet数据集太大 无法代码下载 kaggle网址下载
https://image-net.org/challenges/LSVRC/2017/index.php
3. 代码
import torch
import torch.nn
import torchvision.transforms
from torch.nn import Linear
from torchvision.models import vgg16
from torchvision import datasets
# imagenet_set = datasets.ImageNet(root='./imagenet_set', split='val', download=True,
# transform=torchvision.transforms.ToTensor())
train_data = torchvision.datasets.CIFAR10('./dataset', train=True, transform=torchvision.transforms.ToTensor(), download=True)
vgg16_pre_true = vgg16(pretrained=True)
vgg16_pre_false = vgg16(pretrained=False, progress=False)
print(vgg16_pre_false)
print(dir(vgg16_pre_false))
# 1. .add_module
# vgg16_pre_false.add_module('add_linear', Linear(in_features=1000, out_features=10))
# print(vgg16_pre_false)
# 2. .classifier.add_module
vgg16_pre_false.classifier.add_module('add_linear', Linear(in_features=1000, out_features=10))
print(vgg16_pre_false)
# 3. .classifier[6]
# print(dir(vgg16_pre_false.classifier[6]))
vgg16_pre_true.classifier[6] = Linear(4096, 10)
print(vgg16_pre_true)
4. 执行结果