Skip to content

Commit

Permalink
replace ReLU with Swish
Browse files Browse the repository at this point in the history
  • Loading branch information
tzing committed Oct 24, 2017
1 parent f4575bf commit 6b8f520
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
40 changes: 16 additions & 24 deletions resnet.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import torch.nn as nn
import math
import torch.utils.model_zoo as model_zoo

from swish import Swish


__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152']


model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}


def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
Expand All @@ -29,7 +21,7 @@ def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.act = Swish(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
Expand All @@ -40,7 +32,7 @@ def forward(self, x):

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.act(out)

out = self.conv2(out)
out = self.bn2(out)
Expand All @@ -49,7 +41,7 @@ def forward(self, x):
residual = self.downsample(x)

out += residual
out = self.relu(out)
out = self.act(out)

return out

Expand All @@ -66,7 +58,7 @@ def __init__(self, inplanes, planes, stride=1, downsample=None):
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.act = Swish(inplace=True)
self.downsample = downsample
self.stride = stride

Expand All @@ -75,11 +67,11 @@ def forward(self, x):

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.act(out)

out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.act(out)

out = self.conv3(out)
out = self.bn3(out)
Expand All @@ -88,7 +80,7 @@ def forward(self, x):
residual = self.downsample(x)

out += residual
out = self.relu(out)
out = self.act(out)

return out

Expand All @@ -101,7 +93,7 @@ def __init__(self, block, layers, num_classes=1000):
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.act = Swish(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
Expand Down Expand Up @@ -138,7 +130,7 @@ def _make_layer(self, block, planes, blocks, stride=1):
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.act(x)
x = self.maxpool(x)

x = self.layer1(x)
Expand All @@ -161,7 +153,7 @@ def resnet18(pretrained=False, **kwargs):
"""
model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
raise NotImplementedError()
return model


Expand All @@ -173,7 +165,7 @@ def resnet34(pretrained=False, **kwargs):
"""
model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
raise NotImplementedError()
return model


Expand All @@ -185,7 +177,7 @@ def resnet50(pretrained=False, **kwargs):
"""
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
raise NotImplementedError()
return model


Expand All @@ -197,7 +189,7 @@ def resnet101(pretrained=False, **kwargs):
"""
model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
raise NotImplementedError()
return model


Expand All @@ -209,5 +201,5 @@ def resnet152(pretrained=False, **kwargs):
"""
model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
raise NotImplementedError()
return model
16 changes: 16 additions & 0 deletions swish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import torch.nn as nn
import torch.nn.functional as F

class Swish(nn.Module):

def __init__(self, inplace=False):
super().__init__()

self.inplace = True

def forward(self, x):
if self.inplace:
x.mul_(F.sigmoid(x))
return x
else:
return x * F.sigmoid(x)

0 comments on commit 6b8f520

Please sign in to comment.