def __init__(self, in_channels): super(Classifier3, self).__init__() self.conv0 = nn.Conv2d(in_channels, in_channels // 6, kernel_size=3, stride=2) self.bn0 = nn.BatchNorm2d(in_channels // 6) # self.pool1 = nn.MaxPool2d(kernel_size=2) self.conv1 = nn.Conv2d(in_channels // 6, in_channels // 6, kernel_size=3, padding=1) self.bn1 = nn.BatchNorm2d(in_channels // 6) self.conv2 = nn.Conv2d(in_channels // 6, in_channels // 8, kernel_size=3, padding=1) self.bn2 = nn.BatchNorm2d(in_channels // 8) # self.conv3 = nn.Conv2d(in_channels//4, in_channels//2, kernel_size=1) # self.bn3 = nn.BatchNorm2d(in_channels//2) self.global_pool = SelectAdaptivePool2d(pool_type="avg") self.relu = nn.ReLU(inplace=True) self.linear0 = nn.Linear(in_channels // 8, 1792) # self.linear1 = nn.Linear(1792, 786) self.linear_final = nn.Linear(1792, 1)
def __init__(self, model_name, pretrained, input_channels, pool_type, num_classes, out_features, drop_rate, output_stride): super(ResNet, self).__init__() backbone = create_model(model_name=model_name, pretrained=pretrained, output_stride=output_stride) if input_channels > 3: if hasattr(backbone, "layer0"): old_conv = backbone.layer0.conv1 else: old_conv = backbone.conv1 new_conv = nn.Conv2d(input_channels, old_conv.out_channels, old_conv.kernel_size, old_conv.stride, old_conv.padding, old_conv.dilation, old_conv.groups, old_conv.bias) new_conv.weight.data[:, :3, ...] = old_conv.weight.data if hasattr(backbone, "layer0"): backbone.layer0.conv1 = new_conv else: backbone.conv1 = new_conv if hasattr(backbone, "layer0"): self.stem = backbone.layer0 else: layer0_modules = [('conv1', backbone.conv1), ('bn1', backbone.bn1), ('relu', backbone.act1), ('maxpool', backbone.maxpool)] self.stem = nn.Sequential(OrderedDict(layer0_modules)) current_stride = 4 # = stride 2 conv -> stride 2 max pool self._out_feature_strides = {"stem": current_stride} self._out_feature_channels = {"stem": 64} self.stages_and_names = [] for i in range(4): stage = getattr(backbone, f"layer{i + 1}") name = f"res{i + 2}" self.add_module(name, stage) self.stages_and_names.append((stage, name)) self._out_feature_strides[name] = current_stride = int( current_stride * np.prod([l.stride for l in stage])) try: self._out_feature_channels[name] = stage[-1].bn3.num_features except: self._out_feature_channels[name] = stage[ -1].conv2.attn.fc_select.out_channels if num_classes is not None: self.global_pool = SelectAdaptivePool2d(pool_type=pool_type) self.num_features = backbone.num_features * self.global_pool.feat_mult( ) self.linear = nn.Linear(self.num_features, num_classes) del backbone self.drop_rate = drop_rate self.num_classes = num_classes self._out_features = out_features
def __init__(self, encoder, num_classes, dropout=0): super().__init__() self.ela_bn = Normalize( mean=[3.26, 2.09, 2.71, 2.29, 1.24, 1.8, 1.7, 0.77, 1.27], std=[4.19, 2.77, 3.51, 3.17, 1.68, 2.49, 2.63, 1.12, 2.0], ) self.encoder = encoder self.pool = SelectAdaptivePool2d(pool_type="catavgmax", flatten=True) self.type_classifier = WeightNormClassifier(encoder.num_features * 2, num_classes, 128, dropout=dropout) self.flag_classifier = WeightNormClassifier(encoder.num_features * 2, 1, 128, dropout=dropout)
def __init__(self, model_name, pretrained, input_channels, pool_type, num_classes, out_features, drop_rate, drop_connect_rate): super(EfficientNet, self).__init__() backbone = timm.create_model(model_name=model_name, pretrained=pretrained, in_chans=input_channels, drop_connect_rate=drop_connect_rate) stem_modules = [('conv_stem', backbone.conv_stem), ('bn1', backbone.bn1), ('act1', backbone.act1)] self.stem = nn.Sequential(OrderedDict(stem_modules)) current_stride = backbone.conv_stem.stride[0] self._out_feature_strides = {"stem": current_stride} self._out_feature_channels = {"stem": backbone.bn1.num_features} self.blocks_and_names = [] for i in range(7): block = backbone.blocks[i] name = f"block{i}" self.add_module(name, block) self.blocks_and_names.append((block, name)) self._out_feature_strides[name] = current_stride = int( current_stride * block[0].conv_dw.stride[0]) if i == 0: self._out_feature_channels[name] = block[-1].bn2.num_features else: self._out_feature_channels[name] = block[-1].bn3.num_features head_modules = [('conv_head', backbone.conv_head), ('bn2', backbone.bn2), ('act2', backbone.act2)] self.head = nn.Sequential(OrderedDict(head_modules)) current_stride *= backbone.conv_head.stride[0] self._out_feature_strides["head"] = current_stride self._out_feature_channels["head"] = backbone.conv_head.out_channels if num_classes is not None: self.global_pool = SelectAdaptivePool2d(pool_type=pool_type) self.num_features = backbone.num_features * self.global_pool.feat_mult( ) self.linear = nn.Linear(self.num_features, num_classes) del backbone self.drop_rate = drop_rate self.num_classes = num_classes self._out_features = out_features
def __init__( self, encoder, num_classes, dropout=0, mean=[0.3914976, 0.44266784, 0.46043398], std=[0.17819773, 0.17319807, 0.18128773], ): super().__init__() self.encoder = encoder max_pixel_value = 255 self.rgb_bn = Normalize( np.array(mean) * max_pixel_value, np.array(std) * max_pixel_value) self.pool = SelectAdaptivePool2d(pool_type="catavgmax", flatten=True) self.type_classifier = WeightNormClassifier(encoder.num_features * 2, num_classes, 128, dropout=dropout) self.flag_classifier = WeightNormClassifier(encoder.num_features * 2, 1, 128, dropout=dropout)
def __init__(self, in_channels): super(ClassifierConv, self).__init__() self.sconv0 = SeparableConvBnAct(in_channels, in_channels // 4, act_layer=nn.ReLU) self.pool0 = nn.MaxPool2d(kernel_size=3, stride=1) self.dropout = nn.Dropout2d(p=0.4) self.sconv1 = SeparableConvBnAct(in_channels // 4, in_channels // 6, act_layer=nn.ReLU) self.pool1 = nn.MaxPool2d(kernel_size=3, stride=1) self.se0 = SEModule((in_channels // 6)) self.sconv2 = SeparableConvBnAct(in_channels // 6, in_channels // 8, act_layer=nn.ReLU) # self.pool2 = nn.MaxPool2d(kernel_size=3, stride=1) self.sconv3 = SeparableConvBnAct(in_channels // 8, in_channels // 8, act_layer=nn.ReLU) self.sconv4 = SeparableConvBnAct(in_channels // 8, in_channels // 16, act_layer=nn.ReLU) self.se1 = SEModule((in_channels // 16)) self.sconv5 = SeparableConvBnAct(in_channels // 16, in_channels // 32, act_layer=nn.ReLU) self.global_pool = SelectAdaptivePool2d(pool_type="avg") self.relu = nn.ReLU(inplace=True) self.linear = nn.Linear((in_channels // 32), 1)
def __init__(self, levels, channels, num_classes=1000, in_chans=3, cardinality=1, base_width=64, block=DlaBottle2neck, residual_root=False, linear_root=False, batch_norm=FrozenBatchNorm2d, drop_rate=0.0, global_pool='avg', feature_only=True, dcn_config=(False, )): super(DLA, self).__init__() self.channels = channels self.num_classes = num_classes self.cardinality = cardinality self.base_width = base_width self.drop_rate = drop_rate # check whether deformable conv config is right if len(dcn_config) != 6: raise ValueError("Deformable configuration is not correct, " "every level should specifcy a configuration.") self.base_layer = nn.Sequential( Conv2d(in_chans, channels[0], kernel_size=7, stride=1, padding=3, bias=False), batch_norm(channels[0]), nn.ReLU(inplace=True)) self.level0 = self._make_conv_level(channels[0], channels[0], levels[0], batch_norm=batch_norm) self.level1 = self._make_conv_level(channels[0], channels[1], levels[1], stride=2, batch_norm=batch_norm) cargs = dict(cardinality=cardinality, base_width=base_width, root_residual=residual_root, batch_norm=batch_norm) self.level2 = DlaTree(levels[2], block, channels[1], channels[2], 2, level_root=False, with_dcn=dcn_config[2], **cargs) self.level3 = DlaTree(levels[3], block, channels[2], channels[3], 2, level_root=True, with_dcn=dcn_config[3], **cargs) self.level4 = DlaTree(levels[4], block, channels[3], channels[4], 2, level_root=True, with_dcn=dcn_config[4], **cargs) self.level5 = DlaTree(levels[5], block, channels[4], channels[5], 2, level_root=True, with_dcn=dcn_config[5], **cargs) if not feature_only: self.num_features = channels[-1] self.global_pool = SelectAdaptivePool2d(pool_type=global_pool) self.fc = nn.Conv2d(self.num_features * self.global_pool.feat_mult(), num_classes, 1, bias=True)
def __init__(self, model_name, num_classes, aux=False): super(TIMMetricLearningMModels, self).__init__() self.model = create_model( model_name=model_name, pretrained=True, num_classes=num_classes, in_chans=3, ) # Deep-supervised learning self.aux = aux if self.aux: self.num_p2_features = self.model.layer2[-1].bn2.num_features self.num_p3_features = self.model.layer3[-1].bn2.num_features self.ds1 = nn.Sequential( # Fire(self.num_p2_features, 16, 64, 64), # Fire(128, 16, 64, 64), # Fire(128, 32, 128, 128), # nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), # Fire(256, 32, 128, 128), # Fire(256, 48, 192, 192), # Fire(384, 48, 192, 192), # Fire(384, 64, 256, 256), # nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), # Fire(512, 64, 256, 256), nn.Conv2d(self.num_p2_features, self.num_p2_features * 4, kernel_size=(1, 1), stride=(1, 1), bias=False), nn.BatchNorm2d(self.num_p2_features * 4), Swish(), SelectAdaptivePool2d(), Flatten(), nn.Linear(self.num_p2_features * 4, num_classes)) self.ds2 = nn.Sequential( # Fire(self.num_p3_features, 16, 64, 64), # Fire(128, 16, 64, 64), # Fire(128, 32, 128, 128), # nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), # Fire(256, 32, 128, 128), # Fire(256, 48, 192, 192), # Fire(384, 48, 192, 192), # Fire(384, 64, 256, 256), # nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), # Fire(512, 64, 256, 256), nn.Conv2d(self.num_p3_features, self.num_p3_features * 4, kernel_size=(1, 1), stride=(1, 1), bias=False), nn.BatchNorm2d(self.num_p3_features * 4), Swish(), SelectAdaptivePool2d(), Flatten(), nn.Linear(self.num_p3_features * 4, num_classes)) # Arcface features_num = self.model.num_features embedding_size = 512 self.neck = nn.Sequential( nn.BatchNorm1d(features_num), nn.Linear(features_num, embedding_size, bias=False), nn.ReLU(inplace=True), nn.BatchNorm1d(embedding_size), nn.Linear(embedding_size, embedding_size, bias=False), nn.BatchNorm1d(embedding_size), ) self.arc_margin_product = ArcMarginProduct(embedding_size, num_classes) self.arc_loss = ArcFaceLoss() self.head_arcface = nn.Linear(embedding_size, num_classes)
def reset_classifier(self, num_classes, global_pool='avg'): self.global_pool = SelectAdaptivePool2d(pool_type=global_pool) self.num_classes = num_classes self.classifier = nn.Linear( self.num_features * self.global_pool.feat_mult(), num_classes) if self.num_classes else None
def __init__(self, block_args, choices, num_classes=1000, in_chans=3, stem_size=16, num_features=1280, head_bias=True, channel_multiplier=1.0, pad_type='', act_layer=nn.ReLU, drop_rate=0., drop_path_rate=0., slice=4, se_kwargs=None, norm_layer=nn.BatchNorm2d, logger=None, norm_kwargs=None, global_pool='avg', resunit=False, dil_conv=False, verbose=False): super(SuperNet, self).__init__() self.num_classes = num_classes self.num_features = num_features self.drop_rate = drop_rate self._in_chs = in_chans self.logger = logger # Stem stem_size = round_channels(stem_size, channel_multiplier) self.conv_stem = create_conv2d(self._in_chs, stem_size, 3, stride=2, padding=pad_type) self.bn1 = norm_layer(stem_size, **norm_kwargs) self.act1 = act_layer(inplace=True) self._in_chs = stem_size # Middle stages (IR/ER/DS Blocks) builder = SuperNetBuilder(choices, channel_multiplier, 8, None, 32, pad_type, act_layer, se_kwargs, norm_layer, norm_kwargs, drop_path_rate, verbose=verbose, resunit=resunit, dil_conv=dil_conv, logger=self.logger) self.blocks = builder(self._in_chs, block_args) self._in_chs = builder.in_chs # Head + Pooling self.global_pool = SelectAdaptivePool2d(pool_type=global_pool) self.conv_head = create_conv2d(self._in_chs, self.num_features, 1, padding=pad_type, bias=head_bias) self.act2 = act_layer(inplace=True) # Classifier self.classifier = nn.Linear( self.num_features * self.global_pool.feat_mult(), self.num_classes) self.meta_layer = nn.Linear(self.num_classes * slice, 1) efficientnet_init_weights(self)
def __init__(self, block, layers, num_classes=1000, in_chans=3, cardinality=1, base_width=64, stem_width=64, stem_type='', block_reduce_first=1, down_kernel_size=1, avg_down=False, output_stride=32, act_layer=nn.ReLU, norm_layer=nn.BatchNorm2d, drop_rate=0.0, drop_path_rate=0., drop_block_rate=0., global_pool='avg', zero_init_last_bn=True, block_args=None): block_args = block_args or dict() self.num_classes = num_classes deep_stem = 'deep' in stem_type 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 super(ResNet, self).__init__() # Stem if deep_stem: stem_chs_1 = stem_chs_2 = stem_width if 'tiered' in stem_type: stem_chs_1 = 3 * (stem_width // 4) stem_chs_2 = stem_width if 'narrow' in stem_type else 6 * (stem_width // 4) self.conv1 = nn.Sequential(*[ nn.Conv2d(in_chans, stem_chs_1, 3, stride=2, padding=1, bias=False), norm_layer(stem_chs_1), act_layer(inplace=True), nn.Conv2d(stem_chs_1, stem_chs_2, 3, stride=1, padding=1, bias=False), norm_layer(stem_chs_2), act_layer(inplace=True), nn.Conv2d(stem_chs_2, self.inplanes, 3, stride=1, padding=1, bias=False)]) else: self.conv1 = nn.Conv2d(in_chans, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = norm_layer(self.inplanes) self.act1 = act_layer(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) # Feature Blocks dp = DropPath(drop_path_rate) if drop_path_rate else None db_3 = DropBlock2d(drop_block_rate, 7, 0.25) if drop_block_rate else None db_4 = DropBlock2d(drop_block_rate, 7, 1.00) if drop_block_rate else None channels, strides, dilations = [64, 128, 256, 512], [1, 2, 2, 2], [1] * 4 if output_stride == 16: strides[3] = 1 dilations[3] = 2 elif output_stride == 8: strides[2:4] = [1, 1] dilations[2:4] = [2, 4] else: assert output_stride == 32 layer_args = list(zip(channels, layers, strides, dilations)) layer_kwargs = dict( reduce_first=block_reduce_first, act_layer=act_layer, norm_layer=norm_layer, avg_down=avg_down, down_kernel_size=down_kernel_size, drop_path=dp, **block_args) self.layer1 = self._make_layer(block, *layer_args[0], **layer_kwargs) self.layer2 = self._make_layer(block, *layer_args[1], **layer_kwargs) self.layer3 = self._make_layer(block, drop_block=db_3, *layer_args[2], **layer_kwargs) self.layer4 = self._make_layer(block, drop_block=db_4, *layer_args[3], **layer_kwargs) # Head (Pooling and Classifier) 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 n, m in self.named_modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1.) nn.init.constant_(m.bias, 0.) if zero_init_last_bn: for m in self.modules(): if hasattr(m, 'zero_init_last_bn'): m.zero_init_last_bn()