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示例就是小編分享給大家的全部內(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+Selenium自動(dòng)化測試——輸入,點(diǎn)擊操作
這篇文章主要介紹了python+Selenium自動(dòng)化測試——輸入,點(diǎn)擊操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python?ttkbootstrap?制作賬戶注冊(cè)信息界面的案例代碼
ttkbootstrap 是一個(gè)基于 tkinter 的界面美化庫,使用這個(gè)工具可以開發(fā)出類似前端 bootstrap 風(fēng)格的 tkinter 桌面程序。本文重點(diǎn)給大家介紹Python?ttkbootstrap?制作賬戶注冊(cè)信息界面的案例代碼,感興趣的朋友一起看看吧2022-02-02
python使用tornado實(shí)現(xiàn)登錄和登出
這篇文章主要為大家詳細(xì)介紹了python使用tornado實(shí)現(xiàn)登錄和登出,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Python首次安裝后運(yùn)行報(bào)錯(cuò)(0xc000007b)的解決方法
最近在安裝完P(guān)ython后運(yùn)行發(fā)現(xiàn)居然報(bào)錯(cuò)了,錯(cuò)誤代碼是0xc000007b,于是通過往上查找發(fā)現(xiàn)是因?yàn)槭状伟惭bPython缺乏VC++庫的原因,下面通過這篇文章看看如何解決這個(gè)問題吧。2016-10-10
Python中Scipy庫在信號(hào)處理中的應(yīng)用詳解
信號(hào)處理作為數(shù)字信號(hào)處理領(lǐng)域的關(guān)鍵技術(shù),涵蓋了從信號(hào)獲取、傳輸、存儲(chǔ)到最終應(yīng)用的一系列處理步驟,在這篇博客中,我們將深入探討Python中Scipy庫在信號(hào)處理領(lǐng)域的應(yīng)用,需要的朋友可以參考下2023-12-12
使用Python對(duì)mongo數(shù)據(jù)庫中字符串型正負(fù)數(shù)值比較大小
這篇文章主要介紹了使用Python對(duì)mongo數(shù)據(jù)庫中字符串型正負(fù)數(shù)值比較大小,2023-04-04
pytorch 實(shí)現(xiàn)計(jì)算 kl散度 F.kl_div()
這篇文章主要介紹了pytorch 實(shí)現(xiàn)計(jì)算 kl散度 F.kl_div(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python實(shí)現(xiàn)國外賭場熱門游戲Craps(雙骰子)
這篇文章主要介紹了Python實(shí)現(xiàn)國外賭場熱門游戲Craps(雙骰子)的源碼及運(yùn)行方法,十分簡單,有需要的小伙伴可以參考下。2015-03-03
Python數(shù)據(jù)分析模塊pandas用法詳解
這篇文章主要介紹了Python數(shù)據(jù)分析模塊pandas用法,結(jié)合實(shí)例形式詳細(xì)分析了Python數(shù)據(jù)分析模塊pandas的功能、常見用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09

