PyTorch實(shí)現(xiàn)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)
以上這篇PyTorch實(shí)現(xiàn)ResNet50、ResNet101和ResNet152示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解利用Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之評(píng)估訓(xùn)練模型
- pytorch常用函數(shù)定義及resnet模型修改實(shí)例
- Pytorch深度學(xué)習(xí)經(jīng)典卷積神經(jīng)網(wǎng)絡(luò)resnet模塊訓(xùn)練
- 人工智能學(xué)習(xí)pyTorch的ResNet殘差模塊示例詳解
- pytorch實(shí)現(xiàn)ResNet結(jié)構(gòu)的實(shí)例代碼
- Pytorch實(shí)現(xiàn)ResNet網(wǎng)絡(luò)之Residual Block殘差塊
相關(guān)文章
Python?創(chuàng)建或讀取?Excel?文件的操作代碼
Excel是一種常用的電子表格軟件,廣泛應(yīng)用于金融、商業(yè)和教育等領(lǐng)域,本文介紹Python?創(chuàng)建或讀取?Excel?文件的操作代碼,感興趣的朋友一起看看吧2023-09-09
Python web框架fastapi中間件的使用及CORS跨域問(wèn)題
fastapi "中間件"是一個(gè)函數(shù),它在每個(gè)請(qǐng)求被特定的路徑操作處理之前,以及在每個(gè)響應(yīng)之后工作,它接收你的應(yīng)用程序的每一個(gè)請(qǐng)求,下面通過(guò)本文給大家介紹Python web框架fastapi中間件的使用及CORS跨域問(wèn)題,感興趣的朋友一起看看吧2024-03-03
Python字符串操作實(shí)戰(zhàn)之如何提取子字符串
這篇文章主要給大家介紹了關(guān)于Python字符串操作實(shí)戰(zhàn)之如何提取子字符串的相關(guān)資料,字符串是Python中最常用的數(shù)據(jù)類型,大家應(yīng)該都不陌生,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
python實(shí)現(xiàn)比較兩段文本不同之處的方法
這篇文章主要介紹了python實(shí)現(xiàn)比較兩段文本不同之處的方法,涉及Python針對(duì)文本與字符串的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
基于Python實(shí)現(xiàn)敲擊木魚積累功德效果
最近大家都很流行用手機(jī)敲擊電子木魚積累功德,這在很多短視頻中也常常見到。本文將用Python語(yǔ)言實(shí)現(xiàn)這一效果,感興趣的小伙伴開業(yè)了解一下2022-11-11
Python實(shí)現(xiàn)日期判斷和加減操作詳解
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)日期的判斷,以及對(duì)日期的加減操作,文中的示例代碼對(duì)我們學(xué)習(xí)或工作有一定的價(jià)值,需要的可以參考一下2022-01-01
jupyter?notebook加載和運(yùn)行.py文件方式
這篇文章主要介紹了jupyter?notebook加載和運(yùn)行.py文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
python-jwt用戶認(rèn)證食用教學(xué)的實(shí)現(xiàn)方法
這篇文章主要介紹了python-jwt用戶認(rèn)證食用教學(xué)的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
python中response.text 和response.content的區(qū)別詳解
這篇文章主要介紹了python中response.text 和response.content的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

