PyTorch实现ResNet50、ResNet101和ResNet152示例
作者:mingo_敏 发布时间:2023-10-16 05:44:39
标签:PyTorch,ResNet50,ResNet101,ResNet152
PyTorch: https://github.com/shanglianlm0525/PyTorch-Networks
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)
来源:https://blog.csdn.net/shanglianlm/article/details/86376627


猜你喜欢
- 删除链表中重复的结点: 定义两个指针pre和current两个指针同时往后移动,current指针如果与后一个结点值相同,就独自往前走直到没
- 一、使用docker部署mysql主从 实现主从复制此次使用的是windows版本docker,mysql版本是5.71、使用docker获
- Python 直接连接mongodb数据库进行查询操作1、安装所需模块使用到的是pymongo模块,安装方法:pip instal
- Python Dash开发Web应用的控件基础本文主要是通过Dash的Checklist组件,简单介绍使用Dash开发的Web应用展示效果如
- 不用库,写了很久,一直出bug,到网上一搜,可以直接输入之后,eval(str)即可得到结果!eval程序如下:s=input("
- 在python中调用fortran代码,要用到f2py这个程序。它的项目主页在此。现在该项目已经合并到numpy中了,先安装python再装
- 实例如下:String.prototype.trim = function (char, type) { if (char) {
- 最近学了一个关于省市级联简单的小例子,贴出来与大家分享一下:<!DOCTYPE html><html lang="
- drop_duplicates为我们提供了数据去重的方法,那怎么得到哪些数据有重复呢?实现步骤:1、采用drop_duplicates对数据
- OVER的定义OVER用于为行定义一个窗口,它对一组值进行操作,不需要使用GROUP BY子句对数据进行分组,能够在同一行中同时返回基础行的
- 节点类型主要有三种:元素节点,属性节点和文本节点。而对DOM的主要也就是围绕元素节点和属性节点的增删改查。下面就分别从对元素节点的操作和对属
- 本文实例为大家分享了python实现网页自动签到功能的具体代码,供大家参考,具体内容如下第1步、环境准备(用的chrome浏览器)1.安装s
- 前言在前端开发过程中,关于JS逻辑相关的使用相比都不陌生,尤其是在日常开发中使用到的常用的逻辑内容,如倒计时的使用、点击时间放重复点击、生成
- 用过MySQL之后,不论容量的话,发现比其他两个(sql server 、oracle)好用的多,一下子就喜欢上了。下面给那些还不知道怎么弄
- 对于php,个人感觉能够熟练操作数组和字符串,基本上已经是入门了,php本身有很多操作数组和字符串的函数,今天在做一个功能时,需要用Js动态
- 一、 概念: ① 数据库同步 (主从同步 --- 主数据库写的同时 往从服务器写数据)② 数据库同步 (主主同步 -
- 如何删除表中的数据Mysql删除表中的数据有三种方法,分别是deletedroptruncate一、delete删除表中的数据delete好
- 在 Python 的项目中,如何管理所用的全部依赖库呢?最主流的做法是维护一份“requirements.txt”,记录下依赖库的名字及其版
- 问题引入作为一名Golang开发者,线上环境遇到过好几次连接数暴增问题(mysql/redis/kafka等)。纠其原因,Golang作为常
- 1.最小开发框架代码import sys import pygamepygame.init() size=w,h = (