import torch
from torch import nn
import torchvision
vgg16=torchvision.models.vgg16(pretrained=False)
#如果需要预训练模型就让pretrained=True
train_data=torchvision.datasets.CIFAR10('./data',train=True,
transform=torchvision.transforms.ToTensor(),download=True)
print(vgg16)
此时输出vgg16结构
VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace=True)
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(6): ReLU(inplace=True)
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): ReLU(inplace=True)
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(13): ReLU(inplace=True)
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): ReLU(inplace=True)
(16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(18): ReLU(inplace=True)
(19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(20): ReLU(inplace=True)
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(22): ReLU(inplace=True)
(23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(25): ReLU(inplace=True)
(26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(27): ReLU(inplace=True)
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(29): ReLU(inplace=True)
(30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace=True)
(5): Dropout(p=0.5, inplace=False)
(6): Linear(in_features=4096, out_features=1000, bias=True)
)
)
添加新层,首先要注意层结构,这里有3个层结构VGG、features、classifier,需要修改哪个就对应添加哪个
比如在VGG后面加层:
vgg16.add_module('Linear',nn.Linear(1000,10))
#vgg16=VGG
比如在classifier后面加层:
vgg16.classifier.add_module('Linear',nn.Linear(1000,10))
比如在features中间加层:
vgg16.features[28].add_module('Conv2d',nn.Conv2d(512, 512, kernel_size=(3, 3),
stride=(1, 1), padding=(1, 1)))
note:PyTorch预训练模型修改、增删特定层
https://blog.csdn.net/lavinia_chen007/article/details/114833608
#获取中间层输出
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Conv2d(3, 9, 1, 1, 0, bias=False),
nn.BatchNorm2d(9),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d((1, 1)),
)
x = torch.rand([2, 3, 224, 224])
for i in range(len(model)):
x = model[i](x)
if i == 2:
ReLu_out = x
print('ReLu_out.shape:\n\t',ReLu_out.shape)
print('x.shape:\n\t',x.shape)