PyTorch實現(xiàn)ResNet50、ResNet101和ResNet152示例
更新時間:2020年01月14日 09:13:30 作者:mingo_敏
今天小編就為大家分享一篇PyTorch實現(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實現(xiàn)ResNet50、ResNet101和ResNet152示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼
ttkbootstrap 是一個基于 tkinter 的界面美化庫,使用這個工具可以開發(fā)出類似前端 bootstrap 風格的 tkinter 桌面程序。本文重點給大家介紹Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼,感興趣的朋友一起看看吧2022-02-02
Python首次安裝后運行報錯(0xc000007b)的解決方法
最近在安裝完P(guān)ython后運行發(fā)現(xiàn)居然報錯了,錯誤代碼是0xc000007b,于是通過往上查找發(fā)現(xiàn)是因為首次安裝Python缺乏VC++庫的原因,下面通過這篇文章看看如何解決這個問題吧。2016-10-10
使用Python對mongo數(shù)據(jù)庫中字符串型正負數(shù)值比較大小
這篇文章主要介紹了使用Python對mongo數(shù)據(jù)庫中字符串型正負數(shù)值比較大小,2023-04-04
pytorch 實現(xiàn)計算 kl散度 F.kl_div()
這篇文章主要介紹了pytorch 實現(xiàn)計算 kl散度 F.kl_div(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
Python實現(xiàn)國外賭場熱門游戲Craps(雙骰子)
這篇文章主要介紹了Python實現(xiàn)國外賭場熱門游戲Craps(雙骰子)的源碼及運行方法,十分簡單,有需要的小伙伴可以參考下。2015-03-03
Python數(shù)據(jù)分析模塊pandas用法詳解
這篇文章主要介紹了Python數(shù)據(jù)分析模塊pandas用法,結(jié)合實例形式詳細分析了Python數(shù)據(jù)分析模塊pandas的功能、常見用法及相關(guān)操作注意事項,需要的朋友可以參考下2019-09-09

