PyTorch实现AlexNet示例

yipeiwu_com5年前Python基础

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

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)

以上这篇PyTorch实现AlexNet示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

基于Pycharm加载多个项目过程图解

基于Pycharm加载多个项目过程图解

这篇文章主要介绍了基于Pycharm加载多个项目过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 今天在使用Pycharm工具练...

解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题

解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题

官方说明链接: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-...

python opencv捕获摄像头并显示内容的实现

1、捕获摄像头和实时显示 import cv2 import numpy as np import pickle import matplotlib.pyplot as plt...

PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上

PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上

Django2.1 + Python3.6 + nginx + uwsgi 部署到Ubuntu18.04 材料准备 准备一个Django项目准备一台Ubuntu18.04的主机 ssh连...

Django unittest 设置跳过某些case的方法

按理说unittest 中是不应该测试那种外部依赖很强的用例,但是呢,有时候有些接口总是调试好之后怕忘了,就写了一些简单的测试case,想要通过在settings中增加一些配置来开启和关...