vgg16代码实现pytorch
时间: 2023-10-16 20:21:26 浏览: 127
VGG pytorch代码
以下是VGG16的PyTorch代码实现:
```python
import torch
import torch.nn as nn
class VGG16(nn.Module):
def __init__(self, num_classes=1000):
super(VGG16, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
```
注释:
- `nn.Conv2d(in_channels, out_channels, kernel_size, padding)`:二维卷积层,其中`in_channels`表示输入通道数,`out_channels`表示输出通道数,`kernel_size`表示卷积核大小,`padding`表示填充大小。
- `nn.ReLU(inplace=True)`:ReLU激活函数,`inplace`为True表示原地操作,即直接修改输入的数据,节省内存。
- `nn.MaxPool2d(kernel_size, stride)`:最大值池化层,`kernel_size`表示池化核大小,`stride`表示步长。
- `nn.AdaptiveAvgPool2d(output_size)`:自适应平均值池化层,`output_size`表示输出的大小,可以是一个整数或一个元组。
- `nn.Linear(in_features, out_features)`:全连接层,其中`in_features`表示输入特征数,`out_features`表示输出特征数。
- `nn.Dropout(p=0.5)`:随机失活层,`p`表示失活概率。
- `torch.flatten(input, start_dim=0, end_dim=-1)`:展平输入张量。
- `forward(self, x)`:前向传播函数,其中`x`表示输入张量。
阅读全文