def __init__(self, structure, norm_act=ABN, classes=0): """Wider ResNet with pre-activation (identity mapping) blocks Parameters ---------- structure : list of int Number of residual blocks in each of the six modules of the network. norm_act : callable Function to create normalization / activation Module. classes : int If not `0` also include global average pooling and a fully-connected layer with `classes` outputs at the end of the network. """ super(WiderResNet, self).__init__() self.structure = structure if len(structure) != 6: raise ValueError("Expected a structure with six values") # Initial layers self.mod1 = nn.Sequential( OrderedDict([("conv1", nn.Conv2d(3, 64, 3, stride=1, padding=1, bias=False))])) # Groups of residual blocks in_channels = 64 channels = [(128, 128), (256, 256), (512, 512), (512, 1024), (512, 1024, 2048), (1024, 2048, 4096)] for mod_id, num in enumerate(structure): # Create blocks for module blocks = [] for block_id in range(num): blocks.append(("block%d" % (block_id + 1), IdentityResidualBlock(in_channels, channels[mod_id], norm_act=norm_act))) # Update channels and p_keep in_channels = channels[mod_id][-1] # Create module if mod_id <= 4: self.add_module("pool%d" % (mod_id + 2), nn.MaxPool2d(3, stride=2, padding=1)) self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks))) # Pooling and predictor self.bn_out = norm_act(in_channels) if classes != 0: self.classifier = nn.Sequential( OrderedDict([("avg_pool", GlobalAvgPool2d()), ("fc", nn.Linear(in_channels, classes))]))
def __init__(self, structure, groups=64, norm_act=ABN, input_3x3=False, classes=0, dilation=1, base_channels=(128, 128, 256)): """Pre-activation (identity mapping) ResNeXt model Parameters ---------- structure : list of int Number of residual blocks in each of the four modules of the network. groups : int Number of groups in each ResNeXt block norm_act : callable Function to create normalization / activation Module. input_3x3 : bool If `True` use three `3x3` convolutions in the input module instead of a single `7x7` one. classes : int If not `0` also include global average pooling and a fully-connected layer with `classes` outputs at the end of the network. dilation : list of list of int or list of int or int List of dilation factors, or `1` to ignore dilation. For each module, if a single value is given it is used for all its blocks, otherwise this expects a value for each block. base_channels : list of int Channels in the blocks of the first residual module. Each following module will multiply these values by 2. """ super(ResNeXt, self).__init__() self.structure = structure 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 if input_3x3: layers = [ ("conv1", nn.Conv2d(3, 64, 3, stride=2, padding=1, bias=False)), ("bn1", norm_act(64)), ("conv2", nn.Conv2d(64, 64, 3, stride=1, padding=1, bias=False)), ("bn2", norm_act(64)), ("conv3", nn.Conv2d(64, 64, 3, stride=1, padding=1, bias=False)), ("pool", nn.MaxPool2d(3, stride=2, padding=1)) ] else: layers = [("conv1", nn.Conv2d(3, 64, 7, stride=2, padding=3, bias=False)), ("pool", nn.MaxPool2d(3, stride=2, padding=1))] self.mod1 = nn.Sequential(OrderedDict(layers)) # Groups of residual blocks in_channels = 64 channels = base_channels for mod_id, num in enumerate(structure): # Create blocks for module blocks = [] for block_id in range(num): s, d = self._stride_dilation(mod_id, block_id, dilation) blocks.append(("block%d" % (block_id + 1), IdentityResidualBlock(in_channels, channels, stride=s, norm_act=norm_act, groups=groups, dilation=d))) # Update channels in_channels = channels[-1] # Create and add module self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks))) channels = [c * 2 for c in channels] # Pooling and predictor self.bn_out = norm_act(in_channels) if classes != 0: self.classifier = nn.Sequential( OrderedDict([("avg_pool", GlobalAvgPool2d()), ("fc", nn.Linear(in_channels, classes))]))
def __init__(self, structure, norm_act=ABN, classes=0, dilation=False): """Wider ResNet with pre-activation (identity mapping) blocks This variant uses down-sampling by max-pooling in the first two blocks and by strided convolution in the others. Parameters ---------- structure : list of int Number of residual blocks in each of the six modules of the network. norm_act : callable Function to create normalization / activation Module. classes : int If not `0` also include global average pooling and a fully-connected layer with `classes` outputs at the end of the network. dilation : bool If `True` apply dilation to the last three modules and change the down-sampling factor from 32 to 8. """ super(WiderResNetA2, self).__init__() self.structure = structure self.dilation = dilation if len(structure) != 6: raise ValueError("Expected a structure with six values") # Initial layers self.mod1 = nn.Sequential(OrderedDict([ ("conv1", nn.Conv2d(3, 64, 3, stride=1, padding=1, bias=False)) ])) # Groups of residual blocks in_channels = 64 channels = [(128, 128), (256, 256), (512, 512), (512, 1024), (512, 1024, 2048), (1024, 2048, 4096)] for mod_id, num in enumerate(structure): # Create blocks for module blocks = [] for block_id in range(num): if not dilation: dil = 1 stride = 2 if block_id == 0 and 2 <= mod_id <= 4 else 1 else: if mod_id == 3: dil = 2 elif mod_id > 3: dil = 4 else: dil = 1 stride = 2 if block_id == 0 and mod_id == 2 else 1 if mod_id == 4: drop = partial(nn.Dropout2d, p=0.3) elif mod_id == 5: drop = partial(nn.Dropout2d, p=0.5) else: drop = None blocks.append(( "block%d" % (block_id + 1), IdentityResidualBlock(in_channels, channels[mod_id], norm_act=norm_act, stride=stride, dilation=dil, dropout=drop) )) # Update channels and p_keep in_channels = channels[mod_id][-1] # Create module if mod_id < 2: self.add_module("pool%d" % (mod_id + 2), nn.MaxPool2d(3, stride=2, padding=1)) self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks))) # Pooling and predictor self.bn_out = norm_act(in_channels) if classes != 0: self.classifier = nn.Sequential(OrderedDict([ ("avg_pool", GlobalAvgPool2d()), ("fc", nn.Linear(in_channels, classes)) ]))
def __init__(self, structure, norm_act=ABN, classes=0, dilation=True, use_se=True, in_size=(64, 64), aspp_out=512, fusion_out=64, aspp_sec=(12, 24, 36)): """ Wider ResNet with pre-activation (identity mapping) and Squeeze & Excitation(SE) blocks :param structure: (list of int) Number of residual blocks in each of the six modules of the network. :param norm_act: (callable) Function to create normalization / activation Module. :param classes: (int) Not `0` for segmentation task :param dilation: (bool) `True` for segmentation task :param use_se: (bool) Use Squeeze & Excitation (SE) or not :param in_size: (tuple of int) Size of the input image :param out_sec: (tuple of int) Number of channels of the ASPP output :param aspp_sec: (tuple of int) Dilation rate used in ASPP """ super(SEWiderResNetV2, self).__init__() self.structure = structure self.dilation = dilation self.classes = classes self.Sig = nn.Sigmoid() if len(structure) != 6: raise ValueError("Expected a structure with six values") # Initial layers self.mod1 = nn.Sequential( OrderedDict([("conv1", nn.Conv2d(512, 64, 3, stride=1, padding=1, bias=False))])) # Groups of residual blocks in_channels = 64 channels = [(128, 128), (256, 256), (512, 512), (512, 1024), (512, 1024, 2048), (1024, 2048, 4096)] for mod_id, num in enumerate(structure): # Create blocks for module blocks = [] for block_id in range(num): if not dilation: dil = 1 stride = 2 if block_id == 0 and 2 <= mod_id <= 4 else 1 else: if mod_id == 3: dil = 2 elif mod_id == 4: dil = 4 elif mod_id == 5: dil = 8 else: dil = 1 stride = 2 if block_id == 0 and mod_id == 2 else 1 if mod_id == 4: drop = partial(nn.Dropout2d, p=0.2) elif mod_id == 5: drop = partial(nn.Dropout2d, p=0.3) else: drop = None blocks.append(("block%d" % (block_id + 1), IdentityResidualBlock(in_channels, channels[mod_id], norm_act=norm_act, stride=stride, dilation=dil, dropout=drop, use_se=use_se))) # Update channels and p_keep in_channels = channels[mod_id][-1] # Create module if mod_id < 2: self.add_module("pool%d" % (mod_id + 2), nn.MaxPool2d(3, stride=2, padding=1)) self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks))) # Pooling and predictor # self.feat_out = nn.Sequential(OrderedDict([("out_norm", norm_act(in_channels)), # ("out_down", nn.Conv2d(in_channels, 1024, # kernel_size=1, stride=1, # padding=0, bias=True))])) self.bn_out = norm_act(in_channels) if classes != 0: self.stg3_fusion = nn.Conv2d(channels[1][1], fusion_out, kernel_size=1, stride=1, padding=0, bias=False) self.aspp = nn.Sequential( OrderedDict([ ("aspp", ASPPInPlaceABNBlock(channels[5][2], aspp_out, feat_res=(int(in_size[0] / 8), int(in_size[1] / 8)), up_ratio=2, aspp_sec=aspp_sec)) ])) self.score = nn.Sequential( OrderedDict([("conv", nn.Conv2d(aspp_out + fusion_out, classes, kernel_size=3, stride=1, padding=1, bias=True)), ("up", nn.Upsample(size=in_size, mode='bilinear'))]))
def __init__(self, structure = [3, 3, 6, 3, 1, 1], norm_act=partial(InPlaceABN, activation="leaky_relu", slope=.01), # PUT THIS INSIDE?????? n_classes=0, dilation=(1, 2, 4, 4), in_channels_head = 4096, # THIS AND BELOW ARGS FOR HEAD, VALS TAKEN FROM TEST FILE out_channels_head = 256, hidden_channels=256, dilations_head=(12, 24, 36), pooling_size=(84, 84)): """Wider ResNet with pre-activation (identity mapping) blocks. With the DeeplabV3 head. This variant uses down-sampling by max-pooling in the first two blocks and by strided convolution in the others. Parameters ---------- structure : list of int Number of residual blocks in each of the six modules of the network. norm_act : callable Function to create normalization / activation Module. classes : int If not `0` also include global average pooling and a fully-connected layer with `classes` outputs at the end of the network. dilation : bool If `True` apply dilation to the last three modules and change the down-sampling factor from 32 to 8. """ super(abn, self).__init__() self.structure = structure self.dilation = dilation if len(structure) != 6: raise ValueError("Expected a structure with six values") # Initial layers self.mod1 = nn.Sequential(OrderedDict([ ("conv1", nn.Conv2d(3, 64, 3, stride=1, padding=1, bias=False)) ])) # Groups of residual blocks in_channels = 64 channels = [(128, 128), (256, 256), (512, 512), (512, 1024), (512, 1024, 2048), (1024, 2048, 4096)] for mod_id, num in enumerate(structure): # Create blocks for module blocks = [] for block_id in range(num): if not dilation: dil = 1 stride = 2 if block_id == 0 and 2 <= mod_id <= 4 else 1 else: if mod_id == 3: dil = 2 elif mod_id > 3: dil = 4 else: dil = 1 stride = 2 if block_id == 0 and mod_id == 2 else 1 if mod_id == 4: drop = partial(nn.Dropout2d, p=0.3) elif mod_id == 5: drop = partial(nn.Dropout2d, p=0.5) else: drop = None blocks.append(( "block%d" % (block_id + 1), IdentityResidualBlock(in_channels, channels[mod_id], norm_act=norm_act, stride=stride, dilation=dil, dropout=drop) )) # Update channels and p_keep in_channels = channels[mod_id][-1] # Create module if mod_id < 2: self.add_module("pool%d" % (mod_id + 2), nn.MaxPool2d(3, stride=2, padding=1)) self.add_module("mod%d" % (mod_id + 2), nn.Sequential(OrderedDict(blocks))) # Pooling and predictor self.bn_out = norm_act(in_channels) # if n_classes != 0: # self.classifier = nn.Sequential(OrderedDict([ # ("avg_pool", GlobalAvgPool2d()), # ("fc", nn.Linear(in_channels, n_classes)) # ])) ####### HEAD self.pooling_size = pooling_size # IN THE PAPER THEY USE 9 INSTEAD OF 3 HERE. BUT IN THE GIT TEST FILE THEY USE 3 AS IT USES THESE IN DEEPLAB.PY. SUGGESTS THEIR BEST RESULT IS WITH 3 self.map_convs = nn.ModuleList([ nn.Conv2d(in_channels_head, hidden_channels, 1, bias=False), nn.Conv2d(in_channels_head, hidden_channels, 3, bias=False, dilation=dilations_head[0], padding=dilations_head[0]), nn.Conv2d(in_channels_head, hidden_channels, 3, bias=False, dilation=dilations_head[1], padding=dilations_head[1]), nn.Conv2d(in_channels_head, hidden_channels, 3, bias=False, dilation=dilations_head[2], padding=dilations_head[2]) ]) self.map_bn = norm_act(hidden_channels * 4) self.global_pooling_conv = nn.Conv2d(in_channels_head, hidden_channels, 1, bias=False) self.global_pooling_bn = norm_act(hidden_channels) self.red_conv = nn.Conv2d(hidden_channels * 4, out_channels_head, 1, bias=False) self.pool_red_conv = nn.Conv2d(hidden_channels, out_channels_head, 1, bias=False) self.red_bn = norm_act(out_channels_head) self.reset_parameters(self.map_bn.activation, self.map_bn.slope) self.cls = nn.Conv2d(out_channels_head, n_classes, 1)