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


相关推荐

  • 如何将sql文件导入数据库中[通俗易懂]

    如何将sql文件导入数据库中[通俗易懂]1、首先创建一个数据库,并设置好它的名称。2、在所要操作的数据库上鼠标右键3、执行的sql脚本4、选择你要执行的文件5、点击执行直到文件导入为止即可!

    2022年10月2日
    0
  • python之有趣的二维码—–二维码表白记

    python之有趣的二维码—–二维码表白记

    2021年9月18日
    63
  • video operation_open vino

    video operation_open vino转载注明出处:http://zjbintsystem.blog.51cto.com/964211/713240从盛夏走到深秋,我们继续DAVINCIDM365-DM368的开发。说来惭愧,人家51CTO热情支持本博客,而本人却一直没有像其他博客之星一样频繁更新博客,心里确实说不过去。管理公司确实很累,有更急的客户的项目要做,我们成功先推出了DM6446-810MHz的核心板( htt

    2022年8月13日
    2
  • 【Autoit】Autoit 使用[通俗易懂]

    【Autoit】Autoit 使用[通俗易懂]一、Autoit上传文件、1.常用语法-WinActivate("title")聚焦到指定活动窗口 -ControlFocus("title&q

    2022年7月3日
    62
  • java actioncontext_关于struts2中ActionContext的实现原理

    java actioncontext_关于struts2中ActionContext的实现原理北京,雾霾天气阻止了今天的马拉松之行,蜗居一天。为一个问题“struts2如何保证ActionContext每次取的都是本次请求所对应的实例?”,给一个网友解释了半天。首先,我们知道,struts2和struts1的一个重要区别就是它进行了Action类和Servlet的解耦。而又提供了获取ServletAPI的其它通道,就是ActionContext(别跟我说还有个ServletActionC…

    2022年9月9日
    0
  • Java开发已经烂大街,没前途了?假的

    Java开发已经烂大街,没前途了?假的经过了多年的发展,Java早已由一门单纯的计算机编程语言,演变为了一套强大的技术体系。在程序员中,Java开发工程师就占据20%的比例,不可否认,Java语言有着广泛的行业发展前景,它在行业中的影响力已是不可避免的。所以许多人想通过学习Java转行、找好工作,所以报班学习Java开发的人越来越多。很多人说Java开发已经烂大街了,现在学Java没前途。真的是这样的吗?现在学Java还有前途吗?本文,给大家详细解答一下。1、Java人才缺口大根据职友集数据显示,Java开发的全国招聘量123805条。从

    2022年7月8日
    248

发表回复

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

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