from torch import nn
__all__ = ["ResNet50", "ResNet101", "ResNet152"]
class Bottleneck(nn.Module):
def __init__(self, in_planes, planes, 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_planes, out_channels=planes, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=planes, out_channels=planes, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(planes),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=planes, out_channels=planes*self.expansion, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(planes*self.expansion)
)
if self.downsampling:
self.downsample = nn.Sequential(
nn.Conv2d(in_channels=in_planes, out_channels=planes * self.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes*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.pre_process = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
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.pre_process(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 = ResNet50().cuda()
x = torch.randn(2, 3, 224, 224).cuda()
out = model(x)
print(out.shape)
Pytorch實(shí)現(xiàn)ResNet
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巷燥,“玉大人赡盘,你說我怎么就攤上這事$志荆” “怎么了陨享?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)邀跃。 經(jīng)常有香客問我霉咨,道長(zhǎng),這世上最難降的妖魔是什么拍屑? 我笑而不...
- 正文 為了忘掉前任途戒,我火速辦了婚禮,結(jié)果婚禮上僵驰,老公的妹妹穿的比我還像新娘喷斋。我一直安慰自己,他們只是感情好蒜茴,可當(dāng)我...
- 文/花漫 我一把揭開白布星爪。 她就那樣靜靜地躺著,像睡著了一般粉私。 火紅的嫁衣襯著肌膚如雪顽腾。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼管毙,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了桌硫?” 一聲冷哼從身側(cè)響起夭咬,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铆隘,沒想到半個(gè)月后卓舵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡咖驮,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年边器,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了训枢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站际长,受9級(jí)特大地震影響耸采,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜工育,卻給世界環(huán)境...
- 文/蒙蒙 一虾宇、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧如绸,春花似錦嘱朽、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扼脐,卻和暖如春岸军,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瓦侮。 一陣腳步聲響...
- 正文 我出身青樓猖毫,卻偏偏與公主長(zhǎng)得像台谍,于是被迫代替她去往敵國和親须喂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...