图像分类:AlexNet代码(pytorch)之model.py解读

附上代码:

import torch.nn as nn
import torch


class AlexNet(nn.Module):
    def __init__(self, num_classes=1000, init_weights=True):
        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)#进行展平处理,索引从1开始即是从channel开始,因为pytorch的tensor通道排序为:[batch,channel,height,weight]
        x = self.classifier(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():#self.modules继承自nn.Module()
            if isinstance(m, nn.Conv2d):#isinstance判断在self.modules()中遍历的模块m是否为nn.Cond2d
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')#如果是,就采用kaiming_normal_初始化方法,对卷积权重m.weight进行初始化
                if m.bias is not None:#如果偏执不为空
                    nn.init.constant_(m.bias, 0)#就用0进行初始化
            elif isinstance(m, nn.Linear):#如果m是全连接层
                nn.init.normal_(m.weight, 0, 0.01)#采用normal_(正态分布)进行初始化,其中0为均值,0.01为方差
                nn.init.constant_(m.bias, 0)#对偏执进行初始化为0


# import torch
# input1=torch.rand([32,3,224,224])
# model=AlexNet()
# print(model)
# output=model(input1)

3.2 使用pytorch搭建AlexNet并训练花分类数据集_哔哩哔哩_bilibili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值