提示词:
给出{xxx}的网络结构表格,包含层名称、类型、输入大小(HWC),输出大小(HWC)、核尺寸、步长、参数数量
AlexNet
层名称 | 类型 | 输入大小(HWC) | 输出大小(HWC) | 核尺寸 | 步长 | 参数数量 |
---|
输入层 | 输入 | 227x227x3 | - | - | - | 0 |
Conv1 | 卷积层 | 227x227x3 | 55x55x96 | 11x11 | 4 | 961111*3 + 96 = 34944 |
MaxPool1 | 最大池化层 | 55x55x96 | 27x27x96 | 3x3 | 2 | 0 |
LRN1 | 局部响应归一化 | 27x27x96 | 27x27x96 | - | - | - |
Conv2 | 卷积层 | 27x27x96 | 27x27x256 | 5x5 | 1 | 25655*96 + 256 = 614656 |
MaxPool2 | 最大池化层 | 27x27x256 | 13x13x256 | 3x3 | 2 | 0 |
LRN2 | 局部响应归一化 | 13x13x256 | 13x13x256 | - | - | - |
Conv3 | 卷积层 | 13x13x256 | 13x13x384 | 3x3 | 1 | 38433*256 + 384 = 885120 |
Conv4 | 卷积层 | 13x13x384 | 13x13x384 | 3x3 | 1 | 38433*384 + 384 = 1327488 |
Conv5 | 卷积层 | 13x13x384 | 13x13x256 | 3x3 | 1 | 25633*384 + 256 = 884992 |
MaxPool3 | 最大池化层 | 13x13x256 | 6x6x256 | 3x3 | 2 | 0 |
FC6 | 全连接层 | 6x6x256 | 4096 | - | - | 66256*4096 + 4096 = 37752832 |
FC7 | 全连接层 | 4096 | 4096 | - | - | 4096*4096 + 4096 = 16781312 |
FC8 | 全连接层 | 4096 | 1000 | - | - | 4096*1000 + 1000 = 4194304 |
PyTorch 源码
import torch
import torch.nn as nn
import torch.nn.functional as F
class AlexNet(nn.Module):
def __init__(self, num_classes=1000):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75, k=2),
nn.Conv2d(96, 256, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.LocalResponseNorm(size=5, alpha=0.0001, beta=0.75, k=2),
nn.Conv2d(256, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
return x
model = AlexNet(num_classes=1000)
print(model)
LENET5
网络结构
层名称 | 类型 | 输入大小 (HWC) | 输出大小 (HWC) | 核尺寸 | 步长 | 参数数量 |
---|
输入层 | 输入 | 32x32x1 | 32x32x1 | - | - | 0 |
C1 | 卷积层 | 32x32x1 | 28x28x6 | 5x5 | 1 | (5x5x1+1)x6 = 156 |
S2 | 下采样层 | 28x28x6 | 14x14x6 | 2x2 | 2 | 0 |
C3 | 卷积层 | 14x14x6 | 10x10x16 | 5x5 | 1 | (5x5x6+1)x16 = 2416 |
S4 | 下采样层 | 10x10x16 | 5x5x16 | 2x2 | 2 | 0 |
C5 | 卷积层 | 5x5x16 | 1x1x120 | 5x5 | 1 | (5x5x16+1)x120 = 48120 |
F6 | 全连接层 | 1x1x120 | 1x1x84 | - | - | 120x84 + 84 = 10164 |
输出层 | 全连接层 | 1x1x84 | 1x1x10 | - | - | 84x10 + 10 = 850 |
PyTorch 代码
import torch
import torch.nn as nn
import torch.nn.functional as F
class LeNet5(nn.Module):
def __init__(self, num_classes=10):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2)
self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)
self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(in_channels=16, out_channels=120, kernel_size=5)
self.fc1 = nn.Linear(in_features=120, out_features=84)
self.fc2 = nn.Linear(in_features=84, out_features=num_classes)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.pool1(x)
x = self.conv2(x)
x = F.relu(x)
x = self.pool2(x)
x = self.conv3(x)
x = F.relu(x)
x = x.view(-1, self.num_flat_features(x))
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
return x
def num_flat_features(self, x):
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
model = LeNet5(num_classes=10)
print(model)
input_tensor = torch.randn(1, 1, 32, 32)
output = model(input_tensor)
print(output)
VGG16
层名称 | 类型 | 输入大小 (HWC) | 输出大小 (HWC) | 核尺寸 | 步长 | 参数数量 |
---|
Input | - | 224x224x3 | - | - | - | 0 |
Conv1_1 | Conv2D | 224x224x3 | 224x224x64 | 3x3 | 1 | 1792 |
Conv1_2 | Conv2D | 224x224x64 | 224x224x64 | 3x3 | 1 | 36928 |
MaxPool1 | MaxPooling2D | 224x224x64 | 112x112x64 | 2x2 | 2 | 0 |
Conv2_1 | Conv2D | 112x112x64 | 112x112x128 | 3x3 | 1 | 73856 |
Conv2_2 | Conv2D | 112x112x128 | 112x112x128 | 3x3 | 1 | 147584 |
MaxPool2 | MaxPooling2D | 112x112x128 | 56x56x128 | 2x2 | 2 | 0 |
Conv3_1 | Conv2D | 56x56x128 | 56x56x256 | 3x3 | 1 | 295168 |
Conv3_2 | Conv2D | 56x56x256 | 56x56x256 | 3x3 | 1 | 590080 |
Conv3_3 | Conv2D | 56x56x256 | 56x56x256 | 3x3 | 1 | 590080 |
MaxPool3 | MaxPooling2D | 56x56x256 | 28x28x256 | 2x2 | 2 | 0 |
Conv4_1 | Conv2D | 28x28x256 | 28x28x512 | 3x3 | 1 | 1180160 |
Conv4_2 | Conv2D | 28x28x512 | 28x28x512 | 3x3 | 1 | 2359808 |
Conv4_3 | Conv2D | 28x28x512 | 28x28x512 | 3x3 | 1 | 2359808 |
MaxPool4 | MaxPooling2D | 28x28x512 | 14x14x512 | 2x2 | 2 | 0 |
Conv5_1 | Conv2D | 14x14x512 | 14x14x512 | 3x3 | 1 | 2359808 |
Conv5_2 | Conv2D | 14x14x512 | 14x14x512 | 3x3 | 1 | 2359808 |
Conv5_3 | Conv2D | 14x14x512 | 14x14x512 | 3x3 | 1 | 2359808 |
MaxPool5 | MaxPooling2D | 14x14x512 | 7x7x512 | 2x2 | 2 | 0 |
Flatten | Flatten | 7x7x512 | 25088 | - | - | 0 |
FC6 | Dense | 25088 | 4096 | - | - | 102760448 |
FC7 | Dense | 4096 | 4096 | - | - | |
PyTorch 代码
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.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 = torch.flatten(x, 1)
x = self.classifier(x)
return x
model = VGG16(num_classes=1000)
print(model)
Inception
层名称 | 类型 | 输入大小(HWC) | 输出大小(HWC) | 核尺寸 | 步长 | 参数数量 |
---|
Conv2d_1a_3x3 | 卷积层 | 299x299x3 | 149x149x32 | 3x3 | 2 | 864 |
Conv2d_2a_3x3 | 卷积层 | 149x149x32 | 147x147x32 | 3x3 | 1 | 9216 |
Conv2d_2b_3x3 | 卷积层 | 147x147x32 | 147x147x64 | 3x3 | 1 | 18432 |
MaxPool_3a_3x3 | 最大池化层 | 147x147x64 | 73x73x64 | 3x3 | 2 | 0 |
Conv2d_3b_1x1 | 卷积层 | 73x73x64 | 73x73x80 | 1x1 | 1 | 5120 |
Conv2d_4a_3x3 | 卷积层 | 73x73x80 | 71x71x192 | 3x3 | 1 | 138240 |
MaxPool_5a_3x3 | 最大池化层 | 71x71x192 | 35x35x192 | 3x3 | 2 | 0 |
Mixed_5b | Inception模块 | 35x35x192 | 35x35x256 | - | - | - |
Mixed_5c | Inception模块 | 35x35x256 | 35x35x288 | - | - | - |
Mixed_5d | Inception模块 | 35x35x288 | 35x35x288 | - | - | - |
Mixed_6a | Inception模块 | 35x35x288 | 17x17x768 | - | 2 | - |
Mixed_6b | Inception模块 | 17x17x768 | 17x17x768 | - | - | - |
Mixed_6c | Inception模块 | 17x17x768 | 17x17x768 | - | - | - |
Mixed_6d | Inception模块 | 17x17x768 | 17x17x768 | - | - | - |
Mixed_6e | Inception模块 | 17x17x768 | 17x17x768 | - | - | - |
Mixed_7a | Inception模块 | 17x17x768 | 8x8x1280 | - | 2 | - |
Mixed_7b | Inception模块 | 8x8x1280 | 8x8x2048 | - | - | - |
Mixed_7c | Inception模块 | 8x8x2048 | 8x8x2048 | - | - | - |
以Mixed_5b为例,列出其内部结构。
层名称 | 类型 | 输入大小(HWC) | 输出大小(HWC) | 核尺寸 | 步长 | 参数数量 |
---|
Mixed_5b/1x1 | 卷积层 | 35x35x192 | 35x35x64 | 1x1 | 1 | 12288 |
Mixed_5b/3x3/1x1 | 卷积层 | 35x35x192 | 35x35x64 | 1x1 | 1 | 12288 |
Mixed_5b/3x3/3x3 | 卷积层 | 35x35x64 | 35x35x96 | 3x3 | 1 | 63360 |
Mixed_5b/5x5/1x1 | 卷积层 | 35x35x192 | 35x35x16 | 1x1 | 1 | 3072 |
Mixed_5b/5x5/5x5 | 卷积层 | 35x35x16 | 35x35x16 | 5x5 | 1 | 3072 |
Mixed_5b/pool | 池化层 | 35x35x192 | 35x35x32 | - | 1 | 0 |
Mixed_5b/output | Concatenate | - | 35x35x256 | - | - | - |
PyTorch 源码
以下是使用PyTorch构建InceptionV3模型的一部分源码。这个源码展示了如何定义Inception模块和一些辅助函数,但不包括整个网络的所有细节。完整的InceptionV3模型定义会更长,这里只提供了核心部分。
import torch
import torch.nn as nn
import torch.nn.functional as F
class InceptionA(nn.Module):
def __init__(self, in_channels):
super(InceptionA, self).__init__()
self.branch1x1 = BasicConv2d(in_channels, 64, kernel_size=1)
self.branch5x5_1 = BasicConv2d(in_channels, 48, kernel_size=1)
self.branch5x5_2 = BasicConv2d(48, 64, kernel_size=5, padding=2)
self.branch3x3dbl_1 = BasicConv2d(in_channels, 64, kernel_size=1)
self.branch3x3dbl_2 = BasicConv2d(64, 96, kernel_size=3, padding=1)
self.branch3x3dbl_3 = BasicConv2d(96, 96, kernel_size=3, padding=1)
self.branch_pool = BasicConv2d(in_channels, 32, kernel_size=1)
def forward(self, x):
branch1x1 = self.branch1x1(x)
branch5x5 = self.branch5x5_1(x)
branch5x5 = self.branch5x5_2(branch5x5)
branch3x3dbl = self.branch3x3dbl_1(x)
branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)
branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
branch_pool = self.branch_pool(branch_pool)
outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool]
return torch.cat(outputs, 1)
class BasicConv2d(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return F.relu(x, inplace=True)
class InceptionV3(nn.Module):
def __init__(self, num_classes=1000):
super(InceptionV3, self).__init__()
self.Conv2d_1a_3x3 = BasicConv2d(3, 32, kernel_size=3, stride=2)
self.Conv2d_2a_3x3 = BasicConv2d(32, 32, kernel_size=3)
self.Conv2d_2b_3x3 = BasicConv2d(32, 64, kernel_size=3, padding=1)
self.Mixed_5b = InceptionA(256)
self.Mixed_5c = InceptionA(288)
self.AuxLogits = None
self.Mixed_7c = InceptionA(768)
self.fc = nn.Linear(2048, num_classes)
def forward(self, x):
x = self.Conv2d_1a_3x3(x)
x = self.Conv2d_2a_3x3(x)
x = self.Conv2d_2b_3x3(x)
x = self.Mixed_5b(x)
x = self.Mixed_5c(x)
if self.AuxLogits is not None:
aux = self.AuxLogits(x)
else:
aux = None
x = self.Mixed_7c(x)
x = F.adaptive_avg_pool2d(x, (1, 1))
x = torch.flatten(x, 1)
x = self.fc(x)
return x, aux
Resnet18
层名称 | 类型 | 输入大小(HWC) | 输出大小(HWC) | 核尺寸 | 步长 | 参数数量 |
---|
Conv1 | 卷积层 | 224x224x3 | 112x112x64 | 7x7 | 2 | 9472 |
BatchNorm1 | 批归一化层 | 112x112x64 | 112x112x64 | - | - | 256 |
ReLU1 | 激活层 | 112x112x64 | 112x112x64 | - | - | 0 |
MaxPool1 | 最大池化层 | 112x112x64 | 56x56x64 | 3x3 | 2 | 0 |
ResidualBlock1_1 | 残差块 | 56x56x64 | 56x56x64 | - | - | 8448 |
ResidualBlock1_2 | 残差块 | 56x56x64 | 56x56x64 | - | - | 8448 |
ResidualBlock2_1 | 残差块 | 56x56x64 | 28x28x128 | - | 2 | 43008 |
ResidualBlock2_2 | 残差块 | 28x28x128 | 28x28x128 | - | - | 43008 |
ResidualBlock3_1 | 残差块 | 28x28x128 | 14x14x256 | - | 2 | 172448 |
ResidualBlock3_2 | 残差块 | 14x14x256 | 14x14x256 | - | - | 172448 |
AvgPool | 平均池化层 | 14x14x256 | 7x7x256 | 7x7 | 2 | 0 |
Flatten | 展平层 | 7x7x256 | 12544 | - | - | 0 |
FC | 全连接层 | 12544 | 1000 | - | - | 12545000 |
Softmax | Softmax层 | 1000 | 1000 | - | - | 0 |
每个残差块的结构:
阶段 | 残差块 | 层名称 | 类型 | 输入大小(HWC) | 输出大小(HWC) | 核尺寸 | 步长 | 参数数量 |
---|
1 | 1 | conv1 | 卷积 | 224x224x64 | 112x112x64 | 7x7 | 2 | 9408 |
| | conv2 | 卷积 | 112x112x64 | 112x112x64 | 3x3 | 1 | 18432 |
| | skip1 | 卷积 | 224x224x64 | 112x112x64 | 1x1 | 2 | 256 |
1 | 2 | conv1 | 卷积 | 112x112x64 | 112x112x64 | 3x3 | 1 | 18432 |
| | conv2 | 卷积 | 112x112x64 | 112x112x64 | 3x3 | 1 | 18432 |
2 | 1 | conv1 | 卷积 | 112x112x64 | 56x56x128 | 3x3 | 2 | 73984 |
| | conv2 | 卷积 | 56x56x128 | 56x56x128 | 3x3 | 1 | 147584 |
| | skip1 | 卷积 | 112x112x64 | 56x56x128 | 1x1 | 2 | 832 |
2 | 2 | conv1 | 卷积 | 56x56x128 | 56x56x128 | 3x3 | 1 | 147584 |
| | conv2 | 卷积 | 56x56x128 | 56x56x128 | 3x3 | 1 | 147584 |
3 | 1 | conv1 | 卷积 | 56x56x128 | 28x28x256 | 3x3 | 2 | 295168 |
| | conv2 | 卷积 | 28x28x256 | 28x28x256 | 3x3 | 1 | 589824 |
| | skip1 | 卷积 | 56x56x128 | 28x28x256 | 1x1 | 2 | 3328 |
3 | 2 | conv1 | 卷积 | 28x28x256 | 28x28x256 | 3x3 | 1 | 589824 |
| | conv2 | 卷积 | 28x28x256 | 28x28x256 | 3x3 | 1 | 589824 |
4 | 1 | conv1 | 卷积 | 28x28x256 | 14x14x512 | 3x3 | 2 | 1180928 |
| | conv2 | 卷积 | 14x14x512 | 14x14x512 | 3x3 | 1 | 2359296 |
| | | | | |
PyTorch 代码
import torch
import torch.nn as nn
import torch.nn.functional as F
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3,
stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = downsample
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000):
super(ResNet, self).__init__()
self.in_channels = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, out_channels, blocks, stride=1):
downsample = None
if stride != 1 or self.in_channels != out_channels * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.in_channels, out_channels * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channels * block.expansion),
)
layers = []
layers.append(block(self.in_channels, out_channels, stride, downsample))
self.in_channels = out_channels * block.expansion
for _ in range(1, blocks):
layers.append(block(self.in_channels, out_channels))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def resnet16(pretrained=False, **kwargs):
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
pass
return model
model = resnet16()
print(model)