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
投稿

猜你喜欢

  • python opencv根据颜色进行目标检测的方法示例

    2021-09-29 03:53:41
  • MySQL查询优化:连接查询排序limit(join、order by、limit语句)介绍

    2024-01-21 19:06:16
  • Python中字符串的基础介绍及常用操作总结

    2022-04-12 18:01:03
  • ADO.NET数据库访问技术

    2024-01-13 03:17:27
  • python 爬虫如何实现百度翻译

    2023-02-20 18:33:51
  • python 递归深度优先搜索与广度优先搜索算法模拟实现

    2022-10-03 12:51:59
  • Python动态导入模块和反射机制详解

    2023-07-16 14:02:10
  • python binascii 进制转换实例

    2021-03-25 10:56:25
  • 浅谈一下关于Python对XML的解析

    2023-08-15 21:33:36
  • Python运行错误异常代码含义对照表

    2023-11-14 09:20:09
  • Python简单格式化时间的方法【strftime函数】

    2023-03-29 11:30:16
  • python 详解如何使用GPU大幅提高效率

    2023-08-24 19:45:46
  • OpenCV实现图像平滑处理的方法汇总

    2023-07-12 14:44:33
  • mysql 5.7.18 winx64 免安装 配置方法

    2024-01-27 14:00:08
  • Python中矩阵库Numpy基本操作详解

    2021-07-09 18:13:05
  • MySQL主从原理及配置详解

    2024-01-26 08:33:42
  • 用WEB(ASP)方式实现SQL SERVER 数据库的备份和恢复

    2010-05-11 20:12:00
  • python偏函数partial用法

    2023-09-24 22:25:06
  • 用Oracle并行查询发挥多CPU的威力

    2010-07-23 12:52:00
  • Python prettytable模块应用详解

    2022-05-11 13:10:15
  • asp之家 网络编程 m.aspxhome.com