mirror of
https://github.com/huggingface/pytorch-image-models.git
synced 2025-06-03 15:01:08 +08:00
Fixup Resnext, remove alternate shortcut types
This commit is contained in:
parent
45cde6f0c7
commit
5cb1a35c6b
@ -115,9 +115,9 @@ class RandomErasingTorch:
|
|||||||
h = int(round(math.sqrt(target_area * aspect_ratio)))
|
h = int(round(math.sqrt(target_area * aspect_ratio)))
|
||||||
w = int(round(math.sqrt(target_area / aspect_ratio)))
|
w = int(round(math.sqrt(target_area / aspect_ratio)))
|
||||||
if self.rand_color:
|
if self.rand_color:
|
||||||
c = torch.empty(chan, dtype=batch.dtype, device=self.device).normal_()
|
c = torch.empty((chan, 1, 1), dtype=batch.dtype, device=self.device).normal_()
|
||||||
elif not self.per_pixel:
|
elif not self.per_pixel:
|
||||||
c = torch.zeros(chan, dtype=batch.dtype, device=self.device)
|
c = torch.zeros((chan, 1, 1), dtype=batch.dtype, device=self.device)
|
||||||
if w < img_w and h < img_h:
|
if w < img_w and h < img_h:
|
||||||
top = random.randint(0, img_h - h)
|
top = random.randint(0, img_h - h)
|
||||||
left = random.randint(0, img_w - w)
|
left = random.randint(0, img_w - w)
|
||||||
|
@ -19,7 +19,7 @@ class ResNeXtBottleneckC(nn.Module):
|
|||||||
def __init__(self, inplanes, planes, stride=1, downsample=None, cardinality=32, base_width=4):
|
def __init__(self, inplanes, planes, stride=1, downsample=None, cardinality=32, base_width=4):
|
||||||
super(ResNeXtBottleneckC, self).__init__()
|
super(ResNeXtBottleneckC, self).__init__()
|
||||||
|
|
||||||
width = math.floor(planes / 64 * cardinality * base_width)
|
width = math.floor(planes * (base_width / 64)) * cardinality
|
||||||
|
|
||||||
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False)
|
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False)
|
||||||
self.bn1 = nn.BatchNorm2d(width)
|
self.bn1 = nn.BatchNorm2d(width)
|
||||||
@ -57,13 +57,12 @@ class ResNeXtBottleneckC(nn.Module):
|
|||||||
|
|
||||||
class ResNeXt(nn.Module):
|
class ResNeXt(nn.Module):
|
||||||
|
|
||||||
def __init__(self, block, layers, num_classes=1000, cardinality=32, base_width=4, shortcut='C',
|
def __init__(self, block, layers, num_classes=1000, cardinality=32, base_width=4,
|
||||||
drop_rate=0., global_pool='avg'):
|
drop_rate=0., global_pool='avg'):
|
||||||
self.num_classes = num_classes
|
self.num_classes = num_classes
|
||||||
self.inplanes = 64
|
self.inplanes = 64
|
||||||
self.cardinality = cardinality
|
self.cardinality = cardinality
|
||||||
self.base_width = base_width
|
self.base_width = base_width
|
||||||
self.shortcut = shortcut
|
|
||||||
self.drop_rate = drop_rate
|
self.drop_rate = drop_rate
|
||||||
super(ResNeXt, self).__init__()
|
super(ResNeXt, self).__init__()
|
||||||
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
||||||
@ -80,39 +79,24 @@ class ResNeXt(nn.Module):
|
|||||||
|
|
||||||
for m in self.modules():
|
for m in self.modules():
|
||||||
if isinstance(m, nn.Conv2d):
|
if isinstance(m, nn.Conv2d):
|
||||||
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
|
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
||||||
m.weight.data.normal_(0, math.sqrt(2. / n))
|
|
||||||
elif isinstance(m, nn.BatchNorm2d):
|
elif isinstance(m, nn.BatchNorm2d):
|
||||||
m.weight.data.fill_(1)
|
m.weight.data.fill_(1)
|
||||||
m.bias.data.zero_()
|
m.bias.data.zero_()
|
||||||
|
|
||||||
def _make_layer(self, block, planes, blocks, stride=1):
|
def _make_layer(self, block, planes, blocks, stride=1):
|
||||||
downsample = None
|
downsample = None
|
||||||
reshape = stride != 1 or self.inplanes != planes * block.expansion
|
if stride != 1 or self.inplanes != planes * block.expansion:
|
||||||
use_conv = (self.shortcut == 'C') or (self.shortcut == 'B' and reshape)
|
|
||||||
|
|
||||||
if use_conv:
|
|
||||||
downsample = nn.Sequential(
|
downsample = nn.Sequential(
|
||||||
nn.Conv2d(
|
nn.Conv2d(
|
||||||
self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
|
self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
|
||||||
nn.BatchNorm2d(planes * block.expansion),
|
nn.BatchNorm2d(planes * block.expansion),
|
||||||
)
|
)
|
||||||
elif reshape:
|
|
||||||
downsample = nn.AvgPool2d(3, stride=stride)
|
|
||||||
|
|
||||||
layers = [block(self.inplanes, planes, stride, downsample, self.cardinality, self.base_width)]
|
layers = [block(self.inplanes, planes, stride, downsample, self.cardinality, self.base_width)]
|
||||||
self.inplanes = planes * block.expansion
|
self.inplanes = planes * block.expansion
|
||||||
|
|
||||||
if self.shortcut == 'C':
|
|
||||||
shortcut = nn.Sequential(
|
|
||||||
nn.Conv2d(
|
|
||||||
self.inplanes, planes * block.expansion, kernel_size=1, stride=1, bias=False),
|
|
||||||
nn.BatchNorm2d(planes * block.expansion),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
shortcut = None
|
|
||||||
for i in range(1, blocks):
|
for i in range(1, blocks):
|
||||||
layers.append(block(self.inplanes, planes, 1, shortcut, self.cardinality, self.base_width))
|
layers.append(block(self.inplanes, planes, 1, None, self.cardinality, self.base_width))
|
||||||
|
|
||||||
return nn.Sequential(*layers)
|
return nn.Sequential(*layers)
|
||||||
|
|
||||||
@ -152,7 +136,7 @@ class ResNeXt(nn.Module):
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
def resnext50(cardinality=32, base_width=4, shortcut='C', pretrained=False, **kwargs):
|
def resnext50(cardinality=32, base_width=4, pretrained=False, **kwargs):
|
||||||
"""Constructs a ResNeXt-50 model.
|
"""Constructs a ResNeXt-50 model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -161,12 +145,11 @@ def resnext50(cardinality=32, base_width=4, shortcut='C', pretrained=False, **kw
|
|||||||
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
||||||
"""
|
"""
|
||||||
model = ResNeXt(
|
model = ResNeXt(
|
||||||
ResNeXtBottleneckC, [3, 4, 6, 3], cardinality=cardinality,
|
ResNeXtBottleneckC, [3, 4, 6, 3], cardinality=cardinality, base_width=base_width, **kwargs)
|
||||||
base_width=base_width, shortcut=shortcut, **kwargs)
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
def resnext101(cardinality=32, base_width=4, shortcut='C', pretrained=False, **kwargs):
|
def resnext101(cardinality=32, base_width=4, pretrained=False, **kwargs):
|
||||||
"""Constructs a ResNeXt-101 model.
|
"""Constructs a ResNeXt-101 model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -175,12 +158,11 @@ def resnext101(cardinality=32, base_width=4, shortcut='C', pretrained=False, **k
|
|||||||
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
||||||
"""
|
"""
|
||||||
model = ResNeXt(
|
model = ResNeXt(
|
||||||
ResNeXtBottleneckC, [3, 4, 23, 3], cardinality=cardinality,
|
ResNeXtBottleneckC, [3, 4, 23, 3], cardinality=cardinality, base_width=base_width, **kwargs)
|
||||||
base_width=base_width, shortcut=shortcut, **kwargs)
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
|
||||||
def resnext152(cardinality=32, base_width=4, shortcut='C', pretrained=False, **kwargs):
|
def resnext152(cardinality=32, base_width=4, pretrained=False, **kwargs):
|
||||||
"""Constructs a ResNeXt-152 model.
|
"""Constructs a ResNeXt-152 model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -189,6 +171,5 @@ def resnext152(cardinality=32, base_width=4, shortcut='C', pretrained=False, **k
|
|||||||
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
shortcut ('A'|'B'|'C'): 'B' use 1x1 conv to downsample, 'C' use 1x1 conv on every residual connection
|
||||||
"""
|
"""
|
||||||
model = ResNeXt(
|
model = ResNeXt(
|
||||||
ResNeXtBottleneckC, [3, 8, 36, 3], cardinality=cardinality,
|
ResNeXtBottleneckC, [3, 8, 36, 3], cardinality=cardinality, base_width=base_width, **kwargs)
|
||||||
base_width=base_width, shortcut=shortcut, **kwargs)
|
|
||||||
return model
|
return model
|
||||||
|
Loading…
x
Reference in New Issue
Block a user