AlexNet代码解读
生活随笔
收集整理的這篇文章主要介紹了
AlexNet代码解读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AlexNet代碼解讀
目錄
- AlexNet代碼解讀
- 概述
- 網絡結構圖
- AlexNet代碼細節分析
概述
AlexNet的網絡結構很簡單,是最初級版本的CNN,沒有使用什么技巧。
網絡分成兩個部分,分別是卷積、激活、池化構成的特征提取器,以及前向神經網絡的分類器。
網絡結構圖
AlexNet代碼細節分析
import numpy as np import torch import torch.nn as nn from typing import Any from torchsummary import summary __all__ = ['AlexNet','alexnet']mpdel_urls = {'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth', } class AlexNet(nn.Module): class AlexNet(nn.Module):def __init__(self, num_classes: int = 1000) -> None:super(AlexNet, self).__init__()# conv、relu、maxpool串聯結構來提取特征self.features = nn.Sequential(nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3, stride=2),nn.Conv2d(64, 192, kernel_size=5, padding=2),nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3, stride=2),nn.Conv2d(192, 384, kernel_size=3, padding=1),nn.ReLU(inplace=True),nn.Conv2d(384, 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=3, stride=2),)# 分類器(前面的卷積層已經全部寫好,提取出特征了)# AlexNet的卷積層比較簡單,層數不深,就直接寫在features函數里面了# 特征層操作:(卷積、激活、池化)*2、(卷積、激活)*2、卷積、激活、池化self.avgpool = nn.AdaptiveAvgPool2d(6)# 分類器操作:(dropout、全連接、激活)*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: torch.Tensor) -> torch.Tensor:x = self.features(x)x = self.avgpool(x)x = torch.flatten(x, 1)x = self.classifier(x)return x # 構建alexnet def alexnet(pretrained:bool = False, progress:bool = True, **kwargs:Any)->AlexNet:model = AlexNet(**kwargs)if pretrained:state_dict = load_state_dict_from_url(model_urls['alexnet'],progress = progress)model.load_state_dict(state_dict)return model現在輸入一個3x224x224的tensor,看它經過alexnet每一層之后會變成怎樣大小的tensor。
from torchsummary import summary device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') t = AlexNet().to(device) summary(t,(3,224,224))好了,輸出結果如下所示。
----------------------------------------------------------------Layer (type) Output Shape Param # ================================================================Conv2d-1 [-1, 64, 55, 55] 23,296ReLU-2 [-1, 64, 55, 55] 0MaxPool2d-3 [-1, 64, 27, 27] 0Conv2d-4 [-1, 192, 27, 27] 307,392ReLU-5 [-1, 192, 27, 27] 0MaxPool2d-6 [-1, 192, 13, 13] 0Conv2d-7 [-1, 384, 13, 13] 663,936ReLU-8 [-1, 384, 13, 13] 0Conv2d-9 [-1, 256, 13, 13] 884,992ReLU-10 [-1, 256, 13, 13] 0Conv2d-11 [-1, 256, 13, 13] 590,080ReLU-12 [-1, 256, 13, 13] 0MaxPool2d-13 [-1, 256, 6, 6] 0 AdaptiveAvgPool2d-14 [-1, 256, 6, 6] 0Dropout-15 [-1, 9216] 0Linear-16 [-1, 4096] 37,752,832752,832 0ReLU-17 [-1, 4096] 00 781,312Dropout-18 [-1, 4096] 00 097,000Linear-19 [-1, 4096] 16,========781,312ReLU-20 [-1, 4096]0Linear-21 [-1, 1000] 4,097,000 ================================================================ Total params: 61,100,840 Trainable params: 61,100,840 Non-trainable params: 0 ---------------------------------------------------------------- Input size (MB): 0.57 Forward/backward pass size (MB): 8.38 Params size (MB): 233.08 Estimated Total Size (MB): 242.03 ----------------------------------------------------------------總結
以上是生活随笔為你收集整理的AlexNet代码解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Resnet的pytorch官方实现代码
- 下一篇: 最简易上手的Numpy学习笔记一