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)
上一篇 2022年10月6日 下午7:16
下一篇 2022年10月6日 下午7:36


相关推荐

  • 互联网公司职位简介

    互联网公司职位简介互联网公司职位简介

    2022年4月23日
    92
  • Excel宏编程,给出2列进行去重合并

    Excel宏编程,给出2列进行去重合并Sub去重合并()arr=Worksheets(“sheet1”).Range(“D1”).Clear’选择D列作为存储列,保存最后去重合并的值arr=Worksheets(“sheet1”).UsedRangeDimARowAsLong’A列的行数DimBRowAsLong’B列的行数ARow=Sheet1.Range(“A”&Rows.Count).End(xlUp).Row’不带空格的行数BRow=Sheet1.Range..

    2022年5月25日
    37
  • IE主页被https://hao.360.cn/?a1004劫持,如何解决

    IE主页被https://hao.360.cn/?a1004劫持,如何解决最近IE主页被https://hao.360.cn/?a1004劫持了,不管如何杀毒,更换主页地址,都是不行,包括306、火绒等工具,它就是那么的顽强,不让你更改。但是发现一个现象,那就是如果你在C:\ProgramFiles(x86)\InternetExplorer里找到iexplore.exe,直接打开,会跳转到自己设置的主页,如果你把ie“固定到开始屏幕”,然后在开始屏幕里打开,还是被劫持,那如何解决呢?解决方案:1、IE的主页里先设置自己需要的地址;2、卸载360安全卫士;

    2022年7月26日
    6
  • Java课程设计——学生成绩管理系统

    Java课程设计——学生成绩管理系统Java课程设计题目:学生成绩管理系统摘要在现今信息时代,生活速度的加快,使得人们越来越向信息化、数字化发展。随着学校的规模不断扩大,学生数量急剧增加,有关学生的各种信息量也成倍增长,尤其是学生的考试成绩数据。面对庞大的学生的成绩,需要有学生成绩管理系统来提高学生管理工作的效率。介于此提出了学生成绩管理系统,学生管理系统是计算机对学生档案信息进行管理,具有手工管理无可比拟的优点,如索检迅速、查找方便、可靠性高、存储量大等有点。现在我国的大中专院校的学生成绩管理水平正在不断提高,停留在纸介质

    2022年7月17日
    16
  • bit rate / frame rate /sample rate等等

    bit rate / frame rate /sample rate等等原文地址码率:Bit Rate,指视频或音频文件在单位时间内使用的数据流量,该参数的单位通常是Kbps,也就是千比特每秒。通常2000kbps~3000kbps就已经足以将画质效果表现到极致了。码率参数与视频文件最终体积大小有直接性的关系。 (编码码率—软件)  混合码率:Overall Bit Rate,指视频文件中视频和音频混合后的整体平均码率。一般描述一个视频文件的码率都是指

    2022年10月17日
    4
  • 树莓派4b支持5gwifi吗_树莓派4和4b的区别

    树莓派4b支持5gwifi吗_树莓派4和4b的区别树莓派4b与Manjaro,安装、配置、修复WiFi频段5G和CountryCode安装Manjaro到树莓派4b下载Manjaro烧录系统到SD卡并启动修复无线网络5G频段更新软件仓库安装缺失的功能安装Manjaro到树莓派4bManjaroLinux(或简称Manjaro)是基于ArchLinux的Linux发行版,使用Xfce、GNOME和KDEPlasma作为默认桌面环境,和Arch一样,采用滚动更新。其目标是为PC提供易于使用的自由的操作系统。Manjaro

    2022年10月20日
    5

发表回复

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

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