PyTorch实现的ResNet50、ResNet101和ResNet152

article/2025/10/6 13:44:17

PyTorch实现的ResNet50、ResNet101和ResNet152
PyTorch: https://github.com/shanglianlm0525/PyTorch-Networks
在这里插入图片描述

import torch
import torch.nn as nn
import torchvision
import numpy as npprint("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 = expansionself.downsampling = downsamplingself.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 = xout = self.bottleneck(x)if self.downsampling:residual = self.downsample(x)out += residualout = self.relu(out)return outclass ResNet(nn.Module):def __init__(self,blocks, num_classes=1000, expansion = 4):super(ResNet,self).__init__()self.expansion = expansionself.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 xdef 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)

http://chatgpt.dhexx.cn/article/X0FMxZsO.shtml

相关文章

Pytorch实现ResNet50网络结构,包含ResNet18,ResNet34,ResNet50,ResNet101,ResNet152

创建各版本的ResNet模型,ResNet18,ResNet34,ResNet50,ResNet101,ResNet152 原文地址: https://arxiv.org/pdf/1512.03385.pdf 论文就不解读了,大部分解读都是翻译,看的似懂非懂,自己…

mindspore-ResNet101使用GPU进行训练时报错

multiprocessing.context.TimeoutError RuntimeError: mindspore/ccsrc/backend/session/kernel_build_client.h:109 Response] Response is empty 1、修改resnet101_imagenet2012_config.yaml中的训练集路径,更改类数量以适应新数据集 2、在models/official/cv/r…

官方代码 Deeplab v3+ resnet101 做backbone

大年初一我居然在更博客。今年过年由于病毒横行,没有串门没有聚餐,整个人闲的没事干。。。医生真是不容易,忙得团团转还有生命危险,新希望他们平安。 本篇不属于初级教程。如果完全看不懂请自行谷歌或搜索作者博客。 deeplab官方…

基于pytorch+Resnet101加GPT搭建AI玩王者荣耀

本源码模型主要用了SamLynnEvans Transformer 的源码的解码部分。以及pytorch自带的预训练模型"resnet101-5d3b4d8f.pth" 本资源整理自网络,源地址:https://github.com/FengQuanLi/ResnetGPT 注意运行本代码需要注意以下几点 注意!…

resnet101网络_网络标准101

resnet101网络 让我告诉你一个故事。 一旦我为我们的设计系统构建了另一个日期选择器组件。 它由文本输入和带有日历的弹出窗口组成,单击可显示日历。 然后,可以在外部单击或选择日期来关闭弹出窗口。 外部点击逻辑的大多数实现都是通过将实际点击侦听器…

基于ResNet101实现猴痘病毒识别任务

前言 大家好,我是阿光。 本专栏整理了《PyTorch深度学习项目实战100例》,内包含了各种不同的深度学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集。 正在更新中~ ✨ 🚨 我的项目环境: 平台:Windows10语言环境:python3.7编译器:PyCharmPy…

使用ResNet101作为预训练模型训练Faster-RCNN-TensorFlow-Python3-master

使用VGG16作为预训练模型训练Faster-RCNN-TensorFlow-Python3-master的详细步骤→Windows10Faster-RCNN-TensorFlow-Python3-masterVOC2007数据集。 如果使用ResNet101作为预训练模型训练Faster-RCNN-TensorFlow-Python3-master,在之前使用VGG16作为预训练模型的训练…

TensorRT学习笔记--基于FCN-ResNet101推理引擎实现语义分割

目录 前言 1--Pytorch模型转换为Onnx模型 2--Onnx模型可视化及测试 2-1--可视化Onnx模型 2-2--测试Onnx模型 3--Onnx模型转换为Tensor RT推理模型 4--基于Tensor RT使用推理引擎实现语义分割 前言 基于Tensor RT的模型转换流程:Pytorch → Onnx → Tensor RT…

迁移学习之ResNet50和ResNet101(图像识别)

文章目录 1.实现的效果:2.主文件TransorResNet.py: 1.实现的效果: 实际的图片: (1)可以看到ResNet50预测的前三个结果中第一个结果为:whippet(小灵狗) (2)Re…

Mask-RCNN(2)Resnet101

1. 对应着图像中的CNN部分,其对输入进来的图片有尺寸要求,需要可以整除2的6次方。在进行特征提取后,利用长宽压缩了两次、三次、四次、五次的特征层来进行特征金字塔结构的构造。Mask-RCNN使用Resnet101作为主干特征提取网络 2.ResNet101有…

Pytorch-预训练网络

预训练网络 我们可以把预训练的神经网络看作一个接收输入并生成输出的程序,该程序的行为是由神经网络的结构以及它在训练过程中所看到的样本所决定的,即期望的输入-输出对,或者期望输出应该满足的特性。我们可以在Pytorch中加载和运行这些预…

基于ResNet-101深度学习网络的图像目标识别算法matlab仿真

目录 1.算法理论概述 1.1、ResNet-101的基本原理 1.2、基于深度学习框架的ResNet-101实现 1.3网络训练与测试 2.部分核心程序 3.算法运行软件版本 4.算法运行效果图预览 5.算法完整程序工程 1.算法理论概述 介绍ResNet-101的基本原理和数学模型,并解释其在图…

【深度学习】ResNet网络详解

文章目录 ResNet参考结构概况conv1与池化层残差结构Batch Normalization总结 ResNet 参考 ResNet论文: https://arxiv.org/abs/1512.03385 本文主要参考视频:https://www.bilibili.com/video/BV1T7411T7wa https://www.bilibili.com/video/BV14E411H7U…

【使用Pytorch实现ResNet网络模型:ResNet50、ResNet101和ResNet152】

使用Pytorch实现Resnet网络模型:ResNet50、ResNet101和ResNet152 介绍什么是 ResNet?ResNet 的架构使用Pytorch构建 ResNet网络 介绍 在深度学习和计算机视觉领域取得了一系列突破。尤其是随着非常深的卷积神经网络的引入,这些模型有助于在图…

使用PyTorch搭建ResNet101、ResNet152网络

ResNet18的搭建请移步:使用PyTorch搭建ResNet18网络并使用CIFAR10数据集训练测试 ResNet34的搭建请移步:使用PyTorch搭建ResNet34网络 ResNet34的搭建请移步:使用PyTorch搭建ResNet50网络 参照我的ResNet50的搭建,由于50层以上几…

Java中的数组

数组 1.什么是数组 数组就是存储相同数据类型的一组数据,且长度固定 基本数据类型4类8种:byte/char/short/int/long/float/double/boolean 数组,是由同一种数据类型按照一定的顺序排序的集合,给这个数组起一个名字。是一种数据类型&#…

java输出数组(java输出数组)

多维数组在Java里如何创建多维数组? 这从第四个例子可以看出,它向我们演示了用花括号收集多个new表达式的能力: Integer[][] a4 { { new Integer (1), new Integer (2)}, { new Integer (3), new Integer (4)}, { new Integer (5), new…

java怎么输出数组(Java怎么给数组赋值)

Java中数组输出的三种方式。第一种方式,传统的for循环方式,第二种方式,for each循环,  第三种方式,利用Array类中的toString方法. 定义一个int类型数组,用于输出 int[] array={1,2,3,4,5}; 第一种方式,传统的for循环方式 for(int i=0;i {System.out.println(a[i]); } 第…

数组的输入与输出

前言: 我们知道对一个字符数组进行输入与输出时会用到: 输入:scanf,getchar,gets 输出:printf,putchar,puts 然而可能还有很多的朋友对这些还不是很了解,今天让我们共同学习数组的输入与输出吧。 %c格式是用于输入…

Java二维数组的输出

1. Java二维数组的输出<1> (1) 输出结果右对齐"%5d" public class HelloWorld {public static void main(String[] args){int myArray[ ][ ] { {1,2}, {7,2}, {3,4} };for(int i0; i<3; i){for (int j0; j<2; j)System.out.printf("%5d",my…