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


相关推荐

  • 【技术分享】pip切换镜像源及pip常用命令

    【技术分享】pip切换镜像源及pip常用命令pip切换镜像源国内比较出色的镜像源清华:https://pypi.tuna.tsinghua.edu.cn/simple阿里云:http://mirrors.aliyun.com/pypi/simple中国科技大学https://pypi.mirrors.ustc.edu.cn/simple方法一:切换镜像源#全局设置镜像源地址pipconfigsetglobal.index-urlhttps://pypi.tuna.tsinghua.edu.cn/simple方法二:从指定

    2022年5月11日
    42
  • 【Spring Cloud】教你十分钟学会Gateway~

    【Spring Cloud】教你十分钟学会Gateway~SpringCloudGateway是SpringCloud的一个全新项目,该项目是基于Spring5.0,SpringBoot2.0和ProjectReactor(响应式编程)等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。………

    2025年6月26日
    5
  • Codeblocks的断点调试步骤

    Codeblocks的断点调试步骤首先,新建一个C/C++的codeblocks项目。具体步骤如下:1.新建一个工程(project),注意路径中不要包含中文,否则后面断点调试时会出现问题2.直接选择空工程3.选择C或者C++项目即可4.点开工作空间(workspace)中的Source文件夹,即可对文件进行编辑然后,在.c或者.cpp文件中编写程序,设置断点进行调试。下面以一个…

    2022年5月11日
    99
  • &0xffffffff(0x08)

    答案是-2为什么呢这里涉及到有符号整型数的补码,正数的补码与原码相同。负数的补码,将其原码除符号位外的所有位取反后加101111111+01111111=11111110符号位溢出了取反10000001再加110000010结果就得-2在网上看别人代码的时候,经常会看到他们把INF设为0x7fffffff,奇怪为什么设一个这么奇怪的十六进制数,一…

    2022年4月12日
    42
  • java的英文文献综述_java英文参考文献.doc[通俗易懂]

    java的英文文献综述_java英文参考文献.doc[通俗易懂]java英文参考文献环境风水学论文参考文献(一)摘要:中国传统人居环境文化,俗称风水,是中国人居环境的艺术。中国传统人居环境文化历经千年而不衰,归功于其坚实的理论基础和强大的现实适用性。风水作为一种东方文化特有的思维方式,不仅体现在中国城市、庙宇、乡村、道路、住宅的选址和规划布局中,而且渗透、积淀为中国人心理层面上的审美文化取向,成为中国人的基因。丁文剑《现代建筑与古代风水》上海东华大学出版社…

    2022年9月30日
    3
  • css css样式表 选择器 声明「建议收藏」

    css css样式表 选择器 声明「建议收藏」css部分css指层叠样式表(cascadingstylesheets),它们控制网页内容的外观。使用css设置页面样式时,可以将内容与表现形式分开。网页内容(HTML代码)驻留在HTML文件自身中,而css驻留在另一个文件中(外部样式表*.css)或HTML文档的另一部分(通常为文件头部分)中。写页面时要做到结构(HTML)、样式(css)、行为(js)相分离,

    2022年7月14日
    27

发表回复

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

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