Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the original implementation with GroupNorm in resnet_sge.py file #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions classification/models/imagenet/resnet_sge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,37 @@ def __init__(self, groups = 64):
self.weight = Parameter(torch.zeros(1, groups, 1, 1))
self.bias = Parameter(torch.ones(1, groups, 1, 1))
self.sig = nn.Sigmoid()
self.gn = nn.GroupNorm(1, 1)

# By GroupNorm
def forward(self, x): # (b, c, h, w)
b, c, h, w = x.size()
x = x.view(b * self.groups, -1, h, w)
xn = x * self.avg_pool(x)
xn = xn.sum(dim=1, keepdim=True)
t = xn.view(b * self.groups, -1)
t = t - t.mean(dim=1, keepdim=True)
std = t.std(dim=1, keepdim=True) + 1e-5
t = t / std
t = t.view(b, self.groups, h, w)
t = t * self.weight + self.bias
t = t.view(b * self.groups, 1, h, w)

xn = xn.view(b * self.groups, -1, h, w)
t = self.gn.forward(x_pool)
x = x * self.sig(t)
x = x.view(b, c, h, w)
return x

# def forward(self, x): # (b, c, h, w)
# b, c, h, w = x.size()
# x = x.view(b * self.groups, -1, h, w)
# xn = x * self.avg_pool(x)
# xn = xn.sum(dim=1, keepdim=True)
# t = xn.view(b * self.groups, -1)
# t = t - t.mean(dim=1, keepdim=True)
# std = t.std(dim=1, keepdim=True) + 1e-5
# t = t / std
# t = t.view(b, self.groups, h, w)
# t = t * self.weight + self.bias
# t = t.view(b * self.groups, 1, h, w)
# x = x * self.sig(t)
# x = x.view(b, c, h, w)
# return x

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 Down