in seamseg/models/resnet.py [0:0]
def __init__(self,
structure,
bottleneck,
norm_act=ABN,
classes=0,
dilation=1,
dropout=None,
caffe_mode=False):
super(ResNet, self).__init__()
self.structure = structure
self.bottleneck = bottleneck
self.dilation = dilation
self.dropout = dropout
self.caffe_mode = caffe_mode
if len(structure) != 4:
raise ValueError("Expected a structure with four values")
if dilation != 1 and len(dilation) != 4:
raise ValueError("If dilation is not 1 it must contain four values")
# Initial layers
layers = [
("conv1", nn.Conv2d(3, 64, 7, stride=2, padding=3, bias=caffe_mode)),
("bn1", try_index(norm_act, 0)(64))
]
if try_index(dilation, 0) == 1:
layers.append(("pool1", nn.MaxPool2d(3, stride=2, padding=1)))
self.mod1 = nn.Sequential(OrderedDict(layers))
# Groups of residual blocks
in_channels = 64
if self.bottleneck:
channels = (64, 64, 256)
else:
channels = (64, 64)
for mod_id, num in enumerate(structure):
mod_dropout = None
if self.dropout is not None:
if self.dropout[mod_id] is not None:
mod_dropout = partial(nn.Dropout, p=self.dropout[mod_id])
# Create blocks for module
blocks = []
for block_id in range(num):
stride, dil = self._stride_dilation(dilation, mod_id, block_id)
blocks.append((
"block%d" % (block_id + 1),
ResidualBlock(in_channels, channels, norm_act=try_index(norm_act, mod_id),
stride=stride, dilation=dil, dropout=mod_dropout)
))
# Update channels and p_keep
in_channels = channels[-1]
# Create module
self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks)))
# Double the number of channels for the next module
channels = [c * 2 for c in channels]
# Pooling and predictor
if classes != 0:
self.classifier = nn.Sequential(OrderedDict([
("avg_pool", GlobalAvgPool2d()),
("fc", nn.Linear(in_channels, classes))
]))