Pytorch – 使用pytorch自带的Resnet作为网络的backbone
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Pytorch – 使用pytorch自带的Resnet作为网络的backbone
原文链接:https://www.stubbornhuang.com/2468/
发布于:2023年01月06日 13:31:19
修改于:2023年01月09日 15:04:01

在使用Pytorch搭建自己的神经网络框架时,经常需要使用Pytorch中内置的torchvision.models
中的模型作为特征提取的Backbone,然后再在这个基础上进行更加复杂的网络搭建。
在这里以使用Pytorch中内置的Resnet18为例,如何作为Backbone层进行使用,看以下示例代码
# -*- coding: utf-8 -*-
import torch.nn as nn
import torchvision
class Resnet18Backbone(nn.Module):
def __init__(self):
super(Resnet18Backbone, self).__init__()
self.model = torchvision.models.resnet18(pretrained=True)
self.model.fc = nn.Sequential()
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
return x
使用上述代码,如果输入Tensor的维度为[1,3,244,244],fowward
输出的Tensor的维度为[1,512,1,1],如果我们需要输出的Tensor维度为[1,512],需要squeeze
相应的维度,修改后的代码如下
# -*- coding: utf-8 -*-
import torch.nn as nn
import torchvision
class Resnet18Backbone(nn.Module):
def __init__(self):
super(Resnet18Backbone, self).__init__()
self.model = torchvision.models.resnet18(pretrained=True)
self.model.fc = nn.Sequential()
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = x.squeeze(2).squeeze(2)
return x
好了,上述代码的Resnet18Backbone
可以作为网络中的一层进行使用,这里都是以ResNet的Adaptive Average Pooling层作为backbone的输出层,如果我们仅仅需要前面的卷积层作为输出层,可以参考以下代码。
比如,如果我们要使用ResNet18的Adaptive Average Pooling作为backbone的输出层,我们可以这样写,
# backbone
if backbone_name == 'resnet_18':
resnet_net = torchvision.models.resnet18(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 512
elif backbone_name == 'resnet_34':
resnet_net = torchvision.models.resnet34(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 512
elif backbone_name == 'resnet_50':
resnet_net = torchvision.models.resnet50(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 2048
elif backbone_name == 'resnet_101':
resnet_net = torchvision.models.resnet101(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 2048
elif backbone_name == 'resnet_152':
resnet_net = torchvision.models.resnet152(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 2048
elif backbone_name == 'resnet_50_modified_stride_1':
resnet_net = resnet50(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 2048
elif backbone_name == 'resnext101_32x8d':
resnet_net = torchvision.models.resnext101_32x8d(pretrained=True)
modules = list(resnet_net.children())[:-1]
backbone = nn.Sequential(*modules)
backbone.out_channels = 2048
如果我们仅仅只是需要前面的卷积层作为backbone,我们可以这样写
# backbone
if backbone_name == 'resnet_18':
resnet_net = torchvision.models.resnet18(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnet_34':
resnet_net = torchvision.models.resnet34(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnet_50':
resnet_net = torchvision.models.resnet50(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnet_101':
resnet_net = torchvision.models.resnet101(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnet_152':
resnet_net = torchvision.models.resnet152(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnet_50_modified_stride_1':
resnet_net = resnet50(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
elif backbone_name == 'resnext101_32x8d':
resnet_net = torchvision.models.resnext101_32x8d(pretrained=True)
modules = list(resnet_net.children())[:-2]
backbone = nn.Sequential(*modules)
参考链接
当前分类随机文章推荐
- 深度学习 - 我的深度学习项目代码文件组织结构 阅读822次,点赞3次
- Pytorch - reshape和view的用法和区别 阅读85次,点赞0次
- Pytorch - 使用torchsummary/torchsummaryX/torchinfo库打印模型结构、输出维度和参数信息 阅读993次,点赞0次
- Pytorch - torch.nn.Conv1d参数详解与使用 阅读1153次,点赞0次
- Pytorch - 使用pytorch自带的Resnet作为网络的backbone 阅读85次,点赞0次
- Pytorch - 检测CUDA、cuDNN以及GPU版本的Pytorch是否安装成功、GPU显存测试 阅读2802次,点赞1次
- Pytorch - torch.nn.Conv2d参数详解与使用 阅读117次,点赞0次
- Pytorch - torch.nn.Module的parameters()和named_parameters() 阅读351次,点赞0次
- Pytorch - 手动调整学习率以及使用torch.optim.lr_scheduler调整学习率 阅读364次,点赞0次
- Pytorch - 使用torch.matmul()替换torch.einsum('nctw,cd->ndtw',(a,b))算子模式 阅读1458次,点赞0次
全站随机文章推荐
- 资源分享 - Calculus for Computer Graphics , First Edition 英文高清PDF下载 阅读912次,点赞0次
- 资源分享 - Non-Photorealistic Computer Graphics - Modeling, Rendering, and Animation 英文高清PDF下载 阅读1612次,点赞0次
- Pytorch - 使用pytorch自带的Resnet作为网络的backbone 阅读85次,点赞0次
- 资源分享 - Jim Blinn's Corner - A Trip Down the Graphics Pipeline 英文高清PDF下载 阅读2542次,点赞3次
- WordPress - 用户修改密码/邮箱时禁止向管理员/用户发送通知邮件 阅读661次,点赞0次
- C++11/std::shared_ptr - 循环引用问题 阅读3856次,点赞0次
- 资源分享 - Color Imaging - Fundamentals and Applications 英文高清PDF下载 阅读1101次,点赞0次
- 资源分享 - AI Game Engine Programming , Second Edition 英文高清PDF下载 阅读786次,点赞0次
- 资源分享 - Video Game Optimization 英文高清PDF下载 阅读1129次,点赞0次
- Sigmoid激活函数的快速替代函数以及相应的C++实现 阅读1093次,点赞0次
评论
164