当前位置:网站首页>AlexNet网络详解及复现
AlexNet网络详解及复现
2022-08-03 05:29:00 【WGS.】
详细请看:
''' 这里将卷积核个数设为原文的一半 '''
class AlexNet(nn.Module):
def __init__(self, num_classes=1000, init_weights=False):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
# pytorch tensor通道顺序:[batch_size, channel, height, width],通道数、高、宽,以下注释忽略batch_size
# 这里为了方便padding直接为2了,结果为小数的话会舍弃掉小数点后面的
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) # dim=1是channel的维度
x = self.classifier(x)
return x
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)
边栏推荐
- VS Project Configuration Manager
- 【onnx 输入尺寸】修改pytorch生成的onnx模型的输入尺寸
- 超全!9种PCB表面处理工艺大对比
- 【multi_scale】多尺度训练——目标检测训练trick
- Redis哨兵模式+过期策略、淘汰策略、读写策略
- TFS (Azure conversation) prohibit people checked out at the same time
- ClickHouse删除数据之delete问题详解
- MySQL的安装教程(嗷嗷详细,包教包会~)
- 【DIoU CIoU】DIoU和CIoU损失函数理解及代码实现
- Use of Alibaba Cloud SMS Service (create, test notes)
猜你喜欢
随机推荐
流式低代码编程,拖拽节点画流程图并运行
MySQL 数据库基础知识(系统化一篇入门)
SQLServer2019安装(Windows)
Zabbix历史数据清理(保留以往每个项目每天一条数据)
Scala 高阶(八):集合内容汇总(下篇)
C # to switch input method
国内首款PCB资料分析软件,华秋DFM使用介绍
RADIUS计费认证如何配置?这篇文章一步一步教你完成
ClickHouse 数据插入、更新与删除操作 SQL
C # program with administrator rights to open by default
Oracle 数据库集群常用巡检命令
C#操作FTP上传文件后检查上传正确性
【EA Price strategy OC1】以实时价格为依据的EA,首月翻仓!】
Oracle数据文件收缩_最佳实践_超简单方法
C#通过WebBrowser对网页截图
Redis-记一次docker下使用redis
Redis的应用详解
AQS、CAS、Synchronized小理解
MySQL的主从复制
Podman can learn in one piece








