PyTorch实现AlexNet示例

作者:mingo_敏 时间:2021-08-31 20:15:44 

PyTorch: https://github.com/shanglianlm0525/PyTorch-Networks

PyTorch实现AlexNet示例


import torch
import torch.nn as nn
import torchvision

class AlexNet(nn.Module):
 def __init__(self,num_classes=1000):
   super(AlexNet,self).__init__()
   self.feature_extraction = nn.Sequential(
     nn.Conv2d(in_channels=3,out_channels=96,kernel_size=11,stride=4,padding=2,bias=False),
     nn.ReLU(inplace=True),
     nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
     nn.Conv2d(in_channels=96,out_channels=192,kernel_size=5,stride=1,padding=2,bias=False),
     nn.ReLU(inplace=True),
     nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
     nn.Conv2d(in_channels=192,out_channels=384,kernel_size=3,stride=1,padding=1,bias=False),
     nn.ReLU(inplace=True),
     nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
     nn.ReLU(inplace=True),
     nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
     nn.ReLU(inplace=True),
     nn.MaxPool2d(kernel_size=3, stride=2, padding=0),
   )
   self.classifier = nn.Sequential(
     nn.Dropout(p=0.5),
     nn.Linear(in_features=256*6*6,out_features=4096),
     nn.ReLU(inplace=True),
     nn.Dropout(p=0.5),
     nn.Linear(in_features=4096, out_features=4096),
     nn.ReLU(inplace=True),
     nn.Linear(in_features=4096, out_features=num_classes),
   )
 def forward(self,x):
   x = self.feature_extraction(x)
   x = x.view(x.size(0),256*6*6)
   x = self.classifier(x)
   return x

if __name__ =='__main__':
 # model = torchvision.models.AlexNet()
 model = AlexNet()
 print(model)

input = torch.randn(8,3,224,224)
 out = model(input)
 print(out.shape)

来源:https://blog.csdn.net/shanglianlm/article/details/86424857

标签:PyTorch,AlexNet
0
投稿

猜你喜欢

  • 页面重构中的组件制作要点

    2009-10-25 13:06:00
  • Python项目 基于Scapy实现SYN泛洪攻击的方法

    2023-01-15 01:34:30
  • 十分钟搞定pandas(入门教程)

    2023-08-09 01:00:15
  • Python实现京东秒杀功能代码

    2021-08-14 15:41:27
  • PHP chunk_split()函数讲解

    2023-06-09 13:54:44
  • Python调用Prometheus监控数据并计算

    2023-12-01 02:18:39
  • python定时任务apscheduler的详细使用教程

    2023-03-30 07:48:05
  • PHP5.6读写excel表格文件操作示例

    2023-11-21 15:03:21
  • python利用 pytesseract快速识别提取图片中的文字((图片识别)

    2023-06-16 08:00:18
  • 模型训练时GPU利用率太低的原因及解决

    2021-02-05 22:22:07
  • Python 使用多属性来进行排序

    2023-11-10 21:15:07
  • python使用PIL剪切和拼接图片

    2022-06-26 07:41:55
  • MSSQL存储过程解秘过程全析

    2010-07-05 08:49:00
  • Python 中的Selenium异常处理实例代码

    2021-03-11 12:42:11
  • Python scikit-learn 做线性回归的示例代码

    2022-05-03 11:00:54
  • Python操作注册表详细步骤介绍

    2023-09-16 01:05:53
  • 怎么样才能抓住用户?

    2008-10-20 12:10:00
  • FCKeditor编辑器实战技巧

    2007-10-08 21:13:00
  • [ASP]提高数据显示效率--缓存探幽

    2008-05-18 13:51:00
  • OpenCV-Python实现人脸美白算法的实例

    2023-03-27 00:33:39
  • asp之家 网络编程 m.aspxhome.com