resnet pytorch代码_resnet pytorch

resnet pytorch代码_resnet pytorchPyTorch:https://github.com/shanglianlm0525/PyTorch-Networksimporttorchimporttorch.nnasnnimporttorchvisionimportnumpyasnpprint(“PyTorchVersion:”,torch.__version__)print(“TorchvisionVersion:…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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

resnet pytorch代码_resnet pytorch

import torch

import torch.nn as nn

import torchvision

import numpy as np

print(“PyTorch Version: “,torch.__version__)

print(“Torchvision Version: “,torchvision.__version__)

__all__ = [‘ResNet50’, ‘ResNet101′,’ResNet152’]

def Conv1(in_planes, places, stride=2):

return nn.Sequential(

nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=7,stride=stride,padding=3, bias=False),

nn.BatchNorm2d(places),

nn.ReLU(inplace=True),

nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

)

class Bottleneck(nn.Module):

def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):

super(Bottleneck,self).__init__()

self.expansion = expansion

self.downsampling = downsampling

self.bottleneck = nn.Sequential(

nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),

nn.BatchNorm2d(places),

nn.ReLU(inplace=True),

nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),

nn.BatchNorm2d(places),

nn.ReLU(inplace=True),

nn.Conv2d(in_channels=places, out_channels=places*self.expansion, kernel_size=1, stride=1, bias=False),

nn.BatchNorm2d(places*self.expansion),

)

if self.downsampling:

self.downsample = nn.Sequential(

nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),

nn.BatchNorm2d(places*self.expansion)

)

self.relu = nn.ReLU(inplace=True)

def forward(self, x):

residual = x

out = self.bottleneck(x)

if self.downsampling:

residual = self.downsample(x)

out += residual

out = self.relu(out)

return out

class ResNet(nn.Module):

def __init__(self,blocks, num_classes=1000, expansion = 4):

super(ResNet,self).__init__()

self.expansion = expansion

self.conv1 = Conv1(in_planes = 3, places= 64)

self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)

self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)

self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)

self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)

self.avgpool = nn.AvgPool2d(7, stride=1)

self.fc = nn.Linear(2048,num_classes)

for m in self.modules():

if isinstance(m, nn.Conv2d):

nn.init.kaiming_normal_(m.weight, mode=’fan_out’, nonlinearity=’relu’)

elif isinstance(m, nn.BatchNorm2d):

nn.init.constant_(m.weight, 1)

nn.init.constant_(m.bias, 0)

def make_layer(self, in_places, places, block, stride):

layers = []

layers.append(Bottleneck(in_places, places,stride, downsampling =True))

for i in range(1, block):

layers.append(Bottleneck(places*self.expansion, places))

return nn.Sequential(*layers)

def forward(self, x):

x = self.conv1(x)

x = self.layer1(x)

x = self.layer2(x)

x = self.layer3(x)

x = self.layer4(x)

x = self.avgpool(x)

x = x.view(x.size(0), -1)

x = self.fc(x)

return x

def ResNet50():

return ResNet([3, 4, 6, 3])

def ResNet101():

return ResNet([3, 4, 23, 3])

def ResNet152():

return ResNet([3, 8, 36, 3])

if __name__==’__main__’:

#model = torchvision.models.resnet50()

model = ResNet50()

print(model)

input = torch.randn(1, 3, 224, 224)

out = model(input)

print(out.shape)

以上这篇PyTorch实现ResNet50、ResNet101和ResNet152示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

本文标题: PyTorch实现ResNet50、ResNet101和ResNet152示例

本文地址: http://www.cppcns.com/jiaoben/python/298348.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185199.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 8000401a错误解决方案(Excel)「建议收藏」

    8000401a错误解决方案(Excel)「建议收藏」前一阵子做开发需要用到Excel和Word编程,本人用的是Vista系统,开发环境是VS2005和Office2007,测试无任何问题,可是到部署的时候出现了一些令人很头痛的问题,老是会出现例如:检索COM类工厂中CLSID为{000209FF-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误:8000401a。的错误,在网上查询了许多资

    2022年7月25日
    7
  • Java自定义注解Annotation详解[通俗易懂]

    简介开发中经常使用到注解,在项目中也偶尔会见到过自定义注解,今天就来探讨一下这个注解是什么鬼,以及注解的作用和自定义注解。列举开发中常见的注解@Override:当重写父类的方法时一般都会在方法上标注上此注解(我们最经常看到的toString()方法上总能看到这货)@Deprecated:用于标记某个方法已经过期,请使用新的方法来替代已经废弃的方法@SuppressWarnings:让编译器或

    2022年4月13日
    66
  • oracle sysdate毫秒,Oracle sysdate常用「建议收藏」

    oracle sysdate毫秒,Oracle sysdate常用「建议收藏」SELECT*FROM(SELECT*FROMcalenderDetailAORDERBYa.calenderdesc)WHERErownum<3;//计算时间大于当前时间的最近2条记录取当前小时,以及相隔一小时v_begin_date:=to_date(to_char(sysdate,‘yyyy-mm-ddhh24‘),‘yyyy-mm-ddhh24‘);…

    2025年8月7日
    2
  • request对象[通俗易懂]

    request对象[通俗易懂]request对象request对象封装了由客户端生成的HTTP请求的所有细节,主要包括HTTP头信息、系统信息、请求方式和请求参数等。通过request对象提供的相应方法可以处理客户端浏览器提交的

    2022年7月2日
    33
  • 深度学习、机器学习方向计算机毕业设计题目大全(算法应用实践类)

    深度学习、机器学习方向计算机毕业设计题目大全(算法应用实践类)(吐血整理)手动整理了1500多个深度学习及机器学习相关算法在实际应用中的项目,完全可以作为本科生当前较新的毕业设计题目选择方向。讲道理有些题目,比如“用户评分的隐式成分信息的研究”这种题目取的就比较广,有点科学研究的味道,如果真的去做,还是比较有技术含量的。因为其下一步的应用是具有广阔前景的。还有部分项目可能需要大量的数据集,收集的收集和整理比较麻烦。另外对自己电脑的要求也比较高。大家在选择…

    2022年5月7日
    108
  • 初识lunix_centos ubuntu

    初识lunix_centos ubuntuLinux常用快捷键    先安装rz指令,再使用rz进行导入文件    ls显示当前目录下的文件  ls-thal显示当前目录下的文件及详细信息  cd切换目录  mkdir新建目录  cp-r旧目录/新目录拷贝文件  rm-r目录删除文件  su账号名使用指定用户登录系统  tar压缩/解压命令    …

    2022年9月28日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号