卷积神经网络(二)Alexnet Pytorch实现
程序员文章站
2022-03-17 14:29:09
...
卷积神经网络(二)Alexnet Pytorch实现
1.Alexnet 网络结构模型
相比于Lenet5,它的结构更深,还加入了**函数Relu()函数(仔细度代码就会发现)
2.网络模型搭建 Pytorch
import torch.nn as nn
import torch
class AlexNet(nn.Module):
def __init__(self, num_classes=1000, init_weights=False):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 48, kernel_size=11, stride=4, padding=2),
# input[3, 224, 224] output[48, 55, 55]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
# output[48, 27, 27]
nn.Conv2d(48, 128, kernel_size=5, padding=2),
# output[128, 27, 27]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
# output[128, 13, 13]
nn.Conv2d(128, 192, kernel_size=3, padding=1),
# output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 192, kernel_size=3, padding=1),
# output[192, 13, 13]
nn.ReLU(inplace=True),
nn.Conv2d(192, 128, kernel_size=3, padding=1),
# output[128, 13, 13]
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
# output[128, 6, 6]
)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(128 * 6 * 6, 2048),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(2048, 2048),
nn.ReLU(inplace=True),
nn.Linear(2048, num_classes),
)
if init_weights:
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
3.模型的初始化权重加快模型的训练
这个函数也可以加在前面介绍的Lenet()网络模型里面
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
**
4.打印模型
**
model=AlexNet()
print(model)
**
5.模型输出
**
AlexNet(
(features): Sequential(
(0): Conv2d(3, 48, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
(1): ReLU(inplace=True)
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(3): Conv2d(48, 128, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(4): ReLU(inplace=True)
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Conv2d(128, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(7): ReLU(inplace=True)
(8): Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(9): ReLU(inplace=True)
(10): Conv2d(192, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(classifier): Sequential(
(0): Dropout(p=0.5, inplace=False)
(1): Linear(in_features=4608, out_features=2048, bias=True)
(2): ReLU(inplace=True)
(3): Dropout(p=0.5, inplace=False)
(4): Linear(in_features=2048, out_features=2048, bias=True)
(5): ReLU(inplace=True)
(6): Linear(in_features=2048, out_features=1000, bias=True)
)
)
推荐阅读
-
PyTorch上搭建简单神经网络实现回归和分类的示例
-
利用PyTorch实现循环神经网络RNN
-
Python通过TensorFlow卷积神经网络实现猫狗识别
-
PyTorch上实现卷积神经网络CNN的方法
-
tensorflow2.0之keras实现卷积神经网络
-
TensorFlow 实战之实现卷积神经网络的实例讲解
-
深度之眼Pytorch打卡(十五):Pytorch卷积神经网络部件——转置卷积操作与转置卷积层(对转置卷积操作全网最细致分析,转置卷积的stride与padding,转置与反卷积名称论证)
-
二、计算机视觉与卷积神经网络
-
pytorch + python实现逻辑回归(二分类)
-
【Tensorflow】人脸128个关键点识别基于卷积神经网络实现