def __init__(self, inplanes, planes, stride=1, downsample=None, cardinality=1, base_width=64, use_se=False, reduce_first=1, dilation=1, previous_dilation=1, norm_layer=nn.BatchNorm2d, drop_connect_rate=0, harmonic=False): super(BasicBlock, self).__init__() assert cardinality == 1, 'BasicBlock only supports cardinality of 1' assert base_width == 64, 'BasicBlock doest not support changing base width' first_planes = planes // reduce_first outplanes = planes * self.expansion self.conv1 = Harm2d(inplanes, first_planes, kernel_size=3, stride=stride, padding=dilation, bias=False) if harmonic else nn.Conv2d( inplanes, first_planes, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, bias=False) self.bn1 = norm_layer(first_planes) self.relu = nn.ReLU(inplace=True) self.conv2 = Harm2d(first_planes, outplanes, kernel_size=3, padding=previous_dilation, bias=False) if harmonic else nn.Conv2d( first_planes, outplanes, kernel_size=3, padding=previous_dilation, dilation=previous_dilation, bias=False) self.bn2 = norm_layer(outplanes) self.se = SEModule(outplanes, planes // 4) if use_se else None self.downsample = downsample self.stride = stride self.dilation = dilation self.drop_connect_rate = drop_connect_rate self.harmonic = harmonic
def harm3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return Harm2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False, use_bn=False)
def harm3x3(in_planes, out_planes, stride=1, level=None): """3x3 harmonic convolution with padding""" return Harm2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False, use_bn=False, level=level)
def __init__(self, inplanes, planes, stride=1, downsample=None, cardinality=1, base_width=64, use_se=False, reduce_first=1, dilation=1, previous_dilation=1, norm_layer=nn.BatchNorm2d, drop_connect_rate=0, harmonic=False): super(Bottleneck, self).__init__() width = int(math.floor(planes * (base_width / 64)) * cardinality) first_planes = width // reduce_first outplanes = planes * self.expansion self.conv1 = nn.Conv2d(inplanes, first_planes, kernel_size=1, bias=False) self.bn1 = norm_layer(first_planes) self.conv2 = Harm2d(first_planes, width, kernel_size=3, stride=stride, padding=dilation, groups=cardinality, bias=False) if harmonic else nn.Conv2d( first_planes, width, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, groups=cardinality, bias=False) self.bn2 = norm_layer(width) self.conv3 = nn.Conv2d(width, outplanes, kernel_size=1, bias=False) self.bn3 = norm_layer(outplanes) self.se = SEModule(outplanes, planes // 4) if use_se else None self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride self.dilation = dilation self.drop_connect_rate = drop_connect_rate self.harmonic = harmonic
def __init__(self, depth, num_classes=1000, num_stages=4, strides=(1, 2, 2, 2), dilations=(1, 1, 1, 1), out_indices=(0, 1, 2, 3), style='pytorch', frozen_stages=-1, normalize=dict(type='BN', frozen=False), norm_eval=True, dcn=None, stage_with_dcn=(False, False, False, False), with_cp=False, zero_init_residual=True, root_pool=''): super(HarmResNet, self).__init__() self.inplanes = 64 #self.strides = strides #self.dilations = dilations self.out_indices = out_indices self.frozen_stages = frozen_stages self.norm_eval = norm_eval block, layers = self.arch_settings[depth] root_stride = 2 if root_pool in ('max', 'avg') else 4 self.harm1 = Harm2d(3, 64, kernel_size=7, stride=root_stride, padding=3, bias=False, use_bn=True) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) if root_pool == 'max': self.root_pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) elif root_pool == 'avg': self.root_pool = nn.AvgPool2d(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) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self._freeze_stages()
def __init__(self, block, layers, num_classes=1000, in_chans=3, use_se=False, cardinality=1, base_width=64, stem_width=64, deep_stem=False, block_reduce_first=1, down_kernel_size=1, avg_down=False, dilated=False, norm_layer=nn.BatchNorm2d, drop_rate=0.0, global_pool='avg', root_pool=None, zero_init_residual=False, drop_connect_rate=0, harmonic=False): self.num_classes = num_classes self.inplanes = stem_width * 2 if deep_stem else 64 self.cardinality = cardinality self.base_width = base_width self.drop_rate = drop_rate self.expansion = block.expansion self.dilated = dilated super(ResNet, self).__init__() root_stride = 2 if root_pool in ['avg', 'max'] else 4 if deep_stem: if harmonic: self.conv1 = nn.Sequential(*[ Harm2d(in_chans, stem_width, 3, stride=2, padding=1, bias=False), norm_layer(stem_width), nn.ReLU(inplace=True), Harm2d(stem_width, stem_width, 3, stride=1, padding=1, bias=False), norm_layer(stem_width), nn.ReLU(inplace=True), Harm2d(stem_width, self.inplanes, 3, stride=1, padding=1, bias=False) ]) else: self.conv1 = nn.Sequential(*[ nn.Conv2d(in_chans, stem_width, 3, stride=2, padding=1, bias=False), norm_layer(stem_width), nn.ReLU(inplace=True), nn.Conv2d(stem_width, stem_width, 3, stride=1, padding=1, bias=False), norm_layer(stem_width), nn.ReLU(inplace=True), nn.Conv2d(stem_width, self.inplanes, 3, stride=1, padding=1, bias=False) ]) else: self.conv1 = Harm2d(in_chans, stem_width, kernel_size=7, stride=root_stride, padding=3, bias=False, use_bn=True) if harmonic else nn.Conv2d( in_chans, stem_width, kernel_size=7, stride=root_stride, padding=3, bias=False) self.bn1 = norm_layer(self.inplanes) self.relu = nn.ReLU(inplace=True) if root_pool == 'max': self.root_pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) elif root_pool == 'avg': self.root_pool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1) stride_3_4 = 1 if self.dilated else 2 dilation_3 = 2 if self.dilated else 1 dilation_4 = 4 if self.dilated else 1 largs = dict(use_se=use_se, reduce_first=block_reduce_first, norm_layer=norm_layer, avg_down=avg_down, down_kernel_size=down_kernel_size, num_blocks=sum(layers), max_drop_connect_rate=drop_connect_rate, harmonic=harmonic) self.layer1 = self._make_layer(block, 64, layers[0], stride=1, idx=0, **largs) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, idx=layers[0], **largs) self.layer3 = self._make_layer(block, 256, layers[2], stride=stride_3_4, dilation=dilation_3, idx=sum(layers[:2]), **largs) self.layer4 = self._make_layer(block, 512, layers[3], stride=stride_3_4, dilation=dilation_4, idx=sum(layers[:3]), **largs) self.global_pool = SelectAdaptivePool2d(pool_type=global_pool) self.num_features = 512 * block.expansion self.fc = nn.Linear(self.num_features * self.global_pool.feat_mult(), num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d) and m.affine: nn.init.constant_(m.weight, 1.) nn.init.constant_(m.bias, 0.) if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0)
def __init__(self, block, layers, num_classes=1000, harm_root=True, harm_res_blocks=True, pool=None, levels=[None, None, None, None]): super(ResNet, self).__init__() self.inplanes = 64 root_stride = 2 if pool in ['avg', 'max'] else 4 if harm_root: self.harm1 = Harm2d(3, 64, kernel_size=7, stride=root_stride, padding=3, bias=False, use_bn=True) else: self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=root_stride, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) if pool == 'avg': self.pool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1) elif pool == 'max': self.pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], harm=harm_res_blocks, level=levels[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, harm=harm_res_blocks, level=levels[1]) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, harm=harm_res_blocks, level=levels[2]) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, harm=harm_res_blocks, level=levels[3]) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512 * block.expansion, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d) and m.affine: nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)