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


相关推荐

  • WebGame开发过程中的一些思考和总结

    WebGame开发过程中的一些思考和总结WebGame如今已经很火,市场也很大,盛大和腾讯都已经看中这一块市场。我自己也在做这方面的研发,总结和思考一些问题。

    2022年5月29日
    29
  • JavaScript 数组排序——快速排序[通俗易懂]

    JavaScript 数组排序——快速排序[通俗易懂]数组中的快速排序就是取原始数组中的一个元素最为基点,小于基点的放在一个数组中,大于基点的放在一个数组中,无限循环,知道将数组分解到长度(length<1)停止vararr=[12,3,569,78,0,-56,-56,-56,1223,11,16,13,1];functionquickSort(arr){if(arr.length<1)returnarr;分解数组,长度小于1的时候停止执行varmiddle=pa…

    2022年5月1日
    39
  • SIGABRT错误的调试办法[通俗易懂]

    iOS经常会遇到一个头疼的error就是在main函数上显示“Thread1:signalSIGABRT”这个错误,终于在stackoverflow上找到了调试的办法: 原文链接:http://stackoverflow.com/questions/9782621/i-have-an-error-in-main-m-thread-1-signal-sigabrt-how

    2022年4月16日
    132
  • 越权漏洞详解

    越权漏洞详解OverPermission越权风险问题越权访问(BrokenAccessControl,简称BAC)是Web应用程序中一种常见的漏洞越权访问漏洞的产生比如,某个订单系统,用户可以查询自己的订单信息。A用户查询订单时,发送的HTTP请求中包含参数“orderid=A”,订单系统取得orderid后最终会查询数据库,查询语句类似于“select*fromtablenamewhereorderid=A”。B用户查询订单时,发送的HTTP请求中包含参数“orderid=B”,系统查询数

    2022年6月16日
    36
  • tinyint 范围「建议收藏」

    tinyint 范围「建议收藏」最进做项目要记日志日志表同事建的关联任务id用的tinyint一开始测试没问题后来日志记录里数据全是127纳闷看了127的也没人使用然后才看到“`lang=sqlTINYINT型的字段如果不设置UNSIGNED类型,存储-128到127的整数。“`改了就好了抠鼻.jpg…

    2022年9月21日
    3
  • [系统审计]SAP HANA 中的系统审计策略管理

    [系统审计]SAP HANA 中的系统审计策略管理

    2022年3月7日
    35

发表回复

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

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