赞
踩
AlexNet网络,是2012年ImageNet竞赛冠军,准确率达到57.1%,top 1-5 达到80.2%.
说明:
# encoding: utf-8 # @Author : NanG # create on 2021/8/19 11:34 下午 import torch from torch import nn from torch.nn import Conv2d, MaxPool2d, ReLU class Alexnet(nn.Module): def __init__(self, num_classes=1000): super().__init__() #input=3*227*227,ouput=96*55*55 (227-11)/4 + 1 = 55 self.conv1 = Conv2d(in_channels=3, out_channels=96, kernel_size=11,stride=4) self.relu1 = ReLU(inplace=True) #input 96*55*55 output=96*27*27 self.pool1 = MaxPool2d(kernel_size=3, stride=2) #input=96*27*27 output=256*27*27 self.conv2 = Conv2d(in_channels=96, out_channels=256, kernel_size=5, padding=2,stride=1) self.relu2 = ReLU(inplace=True) #input=256*27*27 output=256*13*13 self.pool2 = MaxPool2d(kernel_size=3, stride=2) #input=256*13*13 output=384*13*13 self.conv3 = Conv2d(in_channels=256, out_channels=384, kernel_size=3, padding=1, stride=1) self.relu3 = ReLU(inplace=True) #input=384*13*13 output=384*13*13 self.conv4 = Conv2d(in_channels=384, out_channels=384, kernel_size=3, padding=1,stride=1) self.relu4 = ReLU(inplace=True) #input=384*13*13 output= 256*13*13 self.conv5 = Conv2d(in_channels=384, out_channels=256, kernel_size=3, padding=1) self.relu5 = ReLU(inplace=True) #input=256*13*13 output=256*6*6 self.pool5 = MaxPool2d(kernel_size=3,stride=2) self.fc1 = nn.Linear(in_features=256*6*6, out_features=4096) self.fc2 = nn.Linear(in_features=4096, out_features=4096) self.fc3 = nn.Linear(in_features=4096, out_features=num_classes) def forward(self, x): x = self.conv1(x) x = self.relu1(x) x = self.pool1(x) x = self.conv2(x) x = self.relu2(x) x = self.pool2(x) x = self.conv3(x) x = self.relu3(x) x = self.conv4(x) x = self.relu4(x) x = self.conv5(x) x = self.relu5(x) x = self.pool5(x) x = x.view(-1, 256*6*6) x = self.fc1(x) x = self.fc2(x) x = self.fc3(x) return x if __name__ == '__main__': net = Alexnet(num_classes=1000) # print(net) x = torch.randn(20, 3, 227, 227) y3 = net(x) print("y3的维度是:{}".format(y3.size())) print(y3)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。