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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql的sql语句优化5种方式_MySQL数据库优化

    mysql的sql语句优化5种方式_MySQL数据库优化1、建表语句+联合索引CREATETABLE`student`(`id`int(10)NOTNULL,`name`varchar(20)NOTNULL,`age`int(10)NOTNULL,`sex`int(11)DEFAULTNULL,`address`varchar(100)DEFAULTNULL,`phone`varchar(100)DEFAULTNULL,`create_time`timestamp.

    2022年8月20日
    4
  • pycharm安装不上包_pycharm调用不了已安装的包

    pycharm安装不上包_pycharm调用不了已安装的包如下图方式安装不成功,显示pip版本需要升级的问题:cmd方式安装bs4仍然失败:cmd以管理员身份运行,输入python-mpipinstall–upgradepip,将pip升级到最新版完成后可输入:piplist查看此时输入:pipinstallbs4成功输入python;importbs4,没有报错则成功;exit()退出但此时发现在pycharm中仍然无法导入bs4:原因:安装的bs4不在该项…

    2022年8月27日
    7
  • Python中字符串String去除出换行符(\n,\r)和空格的问题「建议收藏」

    Python中字符串String去除出换行符(\n,\r)和空格的问题「建议收藏」Python中字符串String去除出换行符和空格的问题(\n,\r)在Python的编写过程中,获取到的字符串进场存在不明原因的换行和空格,如何整合成一个单句,成为问题。方法:一、去除空格“·”代表的为空格  strip()"···xyz···".strip()#returns"xyz""···xyz···".lstrip()…

    2022年5月27日
    144
  • jupyter notebook和spyder区别_命令行打开spyder

    jupyter notebook和spyder区别_命令行打开spyderPycharm、jupyterlab、jupyternotebook、python的区别Pycharm是开发工具,开发人员修改bug特别方便jupyterlab是jupyternotebook的升级版,特别友好,里面的很多插件对于你做数据分析很是方便jupyternotebook是数据分析师经常用的工具,小白学习数据分析时练习numpy、pandas就在这里进行python这个软件我没有用过,我大学学习python装的是Anaconda里面的spyderspyder刚开始接触pyt

    2022年8月28日
    2
  • 计算机网络的分类_计算机网络是怎样分类的

    计算机网络的分类_计算机网络是怎样分类的计算机网络的分类:按照覆盖范围分,计算机网络可以分为局域网(LAN)、城域网(MAN)、和广域网(WAN)。局域网(LAN)是一个高速数据通信系统,它在较小的区域内将若干独立的数据设备连接起来,使

    2022年8月2日
    2
  • 词法分析程序 LEX和VC6整合使用的一个简单例子

    词法分析的理论知识不少,包括了正规式、正规文法、它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等。。。要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if

    2021年12月25日
    48

发表回复

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

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