def __init__(self, in_channels, nclasses, out_channels=32, global_pooling='avg', activation=nn.ReLU(), norm_layer=nn.BatchNorm2d, norm_kwargs={}): super(OneHotConv, self).__init__() self.in_channels = in_channels self.activation = activation self.norm_layer = norm_layer self.nclasses = nclasses self.out_channels = out_channels self.global_pooling = SelectAdaptivePool2d(pool_type=global_pooling, flatten=True) self.fc = nn.Linear(self.out_channels, self.nclasses, bias=True) self.layer1 = OneHotConv_layer1(self.in_channels, 2 * self.out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs) self.layer2 = SRNet_layer1(2 * self.out_channels, self.out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs)
def __init__(self, cfg): super(EfficientNet, self).__init__() self.cfg = cfg backbone = timm.create_model( model_name=self.cfg.MODEL.NAME, pretrained=self.cfg.MODEL.PRETRAINED, in_chans=self.cfg.DATA.INP_CHANNEL, drop_path_rate=self.cfg.MODEL.DROPPATH, ) self.conv_stem = backbone.conv_stem self.bn1 = backbone.bn1 self.act1 = backbone.act1 ### Original blocks ### for i in range(len((backbone.blocks))): setattr(self, "block{}".format(str(i)), backbone.blocks[i]) self.conv_head = backbone.conv_head self.bn2 = backbone.bn2 self.act2 = backbone.act2 if self.cfg.MODEL.POOL == "adaptive_pooling": self.global_pool = SelectAdaptivePool2d(pool_type="avg") self.num_features = backbone.num_features if self.cfg.MODEL.HYPER: self.num_features = backbone.num_features + self.block4[-1].bn3.num_features + \ self.block5[-1].bn3.num_features ### Baseline head ### if self.cfg.MODEL.CLS_HEAD == "linear": self.fc = nn.Linear(self.num_features, self.cfg.MODEL.NUM_CLASSES) elif self.cfg.MODEL.CLS_HEAD == "norm": self.fc = NormSoftmax(self.num_features, self.cfg.MODEL.NUM_CLASSES) self.second_fc = nn.Linear(self.cfg.MODEL.NUM_CLASSES, 1) del backbone
def __init__(self, cfg): super(ResNet, self).__init__() model_name = cfg.TRAIN.MODEL pretrained = cfg.TRAIN.PRETRAINED input_channels = cfg.DATA.INP_CHANNEL pool_type = cfg.TRAIN.POOL_TYPE self.drop_rate = cfg.TRAIN.DROPOUT num_classes = cfg.TRAIN.NUM_CLASSES backbone = timm.create_model(model_name=model_name, pretrained=pretrained, in_chans=input_channels) self.conv1 = backbone.conv1 self.bn1 = backbone.bn1 self.act1 = backbone.act1 self.maxpool = backbone.maxpool ### Original blocks ### self.block1 = backbone.layer1 self.block2 = backbone.layer2 self.block3 = backbone.layer3 self.block4 = backbone.layer4 self.global_pool = SelectAdaptivePool2d(pool_type=pool_type) self.num_features = backbone.num_features * self.global_pool.feat_mult( ) ### Baseline head ### self.alaska_fc = nn.Linear(self.num_features, num_classes) # Replace with Mish activation if cfg.TRAIN.ACTIVATION == "mish": convert_relu_to_mish(self) del backbone
def __init__(self): super(EmbeddingNet, self).__init__() self.fw = nn.Sequential( nn.Conv2d(1, 32, 7, 1, 3), nn.Dropout(p=0.2), nn.ReLU(inplace=True), nn.Conv2d(32, 64, 5, 1, 2), nn.Dropout(p=0.2), nn.ReLU(inplace=True), nn.Conv2d(64, 128, 3, 1, 1), nn.Dropout(p=0.2), nn.ReLU(inplace=True), nn.Conv2d(128, 256, 3, 1, 1), nn.Dropout(p=0.2), nn.ReLU(inplace=True), SelectAdaptivePool2d(pool_type="avg"), nn.Flatten(), nn.Linear(256, 7) ) self.conv_out = nn.Linear(7, 1) self.lstm1 = nn.LSTM(7, 128, bidirectional=True, batch_first=True) self.lstm2 = nn.LSTM(128 * 2, 128, bidirectional=True, batch_first=True) self.linear1 = nn.Linear(128*2, 128*2) self.linear2 = nn.Linear(128*2, 128*2) self.linear = nn.Linear(128*2, 7) self.lstm_out = nn.Linear(7, 1)
def __init__(self, cfg): super(EfficientNet, self).__init__() model_name = cfg.TRAIN.MODEL pretrained = cfg.TRAIN.PRETRAINED input_channels = cfg.DATA.INP_CHANNEL pool_type = cfg.TRAIN.POOL_TYPE drop_connect_rate = cfg.MODEL.DROP_CONNECT self.drop_rate = cfg.TRAIN.DROPOUT num_classes = cfg.TRAIN.NUM_CLASSES backbone = timm.create_model( model_name=model_name, pretrained=pretrained, in_chans=input_channels, drop_connect_rate=drop_connect_rate, ) self.conv_stem = backbone.conv_stem self.bn1 = backbone.bn1 self.act1 = backbone.act1 ### Original blocks ### for i in range(len((backbone.blocks))): setattr(self, "block{}".format(str(i)), backbone.blocks[i]) self.conv_head = backbone.conv_head self.bn2 = backbone.bn2 self.act2 = backbone.act2 self.global_pool = SelectAdaptivePool2d(pool_type=pool_type) self.num_features = backbone.num_features * self.global_pool.feat_mult( ) ### Baseline head ### self.alaska_fc = nn.Linear(self.num_features, num_classes) # Replace with Mish activation if cfg.TRAIN.ACTIVATION == "mish": convert_swish_to_mish(self) del backbone
def __init__(self, in_chs, num_classes, pool_type='avg', drop_rate=0.): super(ClassifierHead, self).__init__() self.drop_rate = drop_rate self.global_pool = SelectAdaptivePool2d(pool_type=pool_type) if num_classes > 0: self.fc = nn.Linear(in_chs * self.global_pool.feat_mult(), num_classes, bias=True) else: self.fc = nn.Identity()
def __init__(self, in_channels=3, encoder_checkpoint="", freeze_encoder=False, num_classes=1): super(SRM_Classifer, self).__init__() self.in_channels = in_channels self.num_classes = num_classes self.srm_conv = setup_srm_layer(self.in_channels) self.bayer_conv = nn.Conv2d(self.in_channels, out_channels=3, kernel_size=5, padding=2, bias=False) nn.init.xavier_uniform_(self.bayer_conv.weight) self.rgb_conv = nn.Sequential( nn.Conv2d(self.in_channels, out_channels=16, kernel_size=3, padding=1, bias=False), nn.Conv2d(16, out_channels=16, kernel_size=3, padding=1, bias=False), nn.ReLU(inplace=True) ) nn.init.xavier_uniform_(self.rgb_conv[0].weight) nn.init.xavier_uniform_(self.rgb_conv[1].weight) self.ela_net = nn.Sequential( nn.Conv2d(3, 32, kernel_size=3, padding=1, bias=False), nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False), nn.ReLU(inplace=True) ) nn.init.xavier_uniform_(self.ela_net[0].weight) nn.init.xavier_uniform_(self.ela_net[1].weight) base_model = EfficientNet(in_channels=54) self.encoder = base_model.encoder self.reducer = nn.Sequential( SelectAdaptivePool2d(pool_type="avg", flatten=True), nn.Dropout(0.3), nn.Linear(1792, 448), nn.ReLU(inplace=True), nn.Linear(448, 256), nn.ReLU(inplace=True), ) nn.init.xavier_uniform_(self.reducer[2].weight) nn.init.xavier_uniform_(self.reducer[4].weight) self.classifier = nn.Linear(256, self.num_classes) nn.init.xavier_uniform_(self.classifier.weight) del base_model gc.collect() if freeze_encoder: self.freeze() if encoder_checkpoint: self.load_weights(encoder_checkpoint)
def __init__(self, encoder_attention, in_channels=3, num_classes=1, model_name='tf_efficientnet_b4_ns', freeze=False, checkpoint=None): super(Mani_FeatX, self).__init__() self.in_channels = in_channels self.srm_conv = setup_srm_layer(self.in_channels) self.bayer_conv = nn.Conv2d(self.in_channels, out_channels=3, kernel_size=5, padding=2, bias=False) nn.init.xavier_uniform_(self.bayer_conv.weight) self.rgb_conv = nn.Sequential( nn.Conv2d(self.in_channels, out_channels=16, kernel_size=3, padding=1, bias=False), nn.Conv2d(16, out_channels=16, kernel_size=3, padding=1, bias=False), nn.ReLU(inplace=True) ) nn.init.xavier_uniform_(self.rgb_conv[0].weight) nn.init.xavier_uniform_(self.rgb_conv[1].weight) self.ela_net = nn.Sequential( nn.Conv2d(3, 32, kernel_size=3, padding=1, bias=False), nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=False), nn.ReLU(inplace=True) ) nn.init.xavier_uniform_(self.ela_net[0].weight) nn.init.xavier_uniform_(self.ela_net[1].weight) self.encoder = TimmEfficientNetBaseEncoder(in_channels=54, model_name=model_name, pretrained=True, encoder_attention=encoder_attention) self.encoder_params = timm_efficientnet_encoder_params[model_name.rsplit('_', 1)[0]]['params'] self.reducer = nn.Sequential( SelectAdaptivePool2d(pool_type="avg", flatten=True), nn.Dropout(0.3), nn.Linear(self.encoder_params['head_channel'], self.encoder_params['head_channel']//4), nn.ReLU(inplace=True), nn.Linear(self.encoder_params['head_channel']//4, 256), nn.ReLU(inplace=True), ) nn.init.xavier_uniform_(self.reducer[2].weight) nn.init.xavier_uniform_(self.reducer[4].weight) self.classifier = nn.Linear(256, num_classes) nn.init.xavier_uniform_(self.classifier.weight) if freeze: self.freeze() if checkpoint: self.load_weights(checkpoint)
def __init__(self, name): super(EfficientNet, self).__init__() backbone = timm.create_model( model_name=name, pretrained=False, in_chans=3, ) self.conv_stem = backbone.conv_stem self.bn1 = backbone.bn1 self.act1 = backbone.act1 ### Original blocks ### for i in range(len((backbone.blocks))): setattr(self, "block{}".format(str(i)), backbone.blocks[i]) self.conv_head = backbone.conv_head self.bn2 = backbone.bn2 self.act2 = backbone.act2 self.global_pool = SelectAdaptivePool2d(pool_type="avg") self.num_features = backbone.num_features ### Baseline head ### self.fc = nn.Linear(self.num_features, 7) del backbone
def __init__(self, in_channels, nclasses, global_pooling='avg', activation=nn.ReLU(), norm_layer=nn.BatchNorm2d, norm_kwargs={}): super(SRNet, self).__init__() self.in_channels = in_channels self.activation = activation self.norm_layer = norm_layer self.nclasses = nclasses self.global_pooling = SelectAdaptivePool2d(pool_type=global_pooling, flatten=True) self.layer_1_specs = [64, 16] self.layer_2_specs = [16, 16, 16, 16, 16] self.layer_3_specs = [16, 64, 128, 256] self.layer_4_specs = [512] in_channels = self.in_channels block1 = [] for out_channels in self.layer_1_specs: block1.append( SRNet_layer1(in_channels, out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs)) in_channels = out_channels block2 = [] for out_channels in self.layer_2_specs: block2.append( SRNet_layer2(in_channels, out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs)) in_channels = out_channels block3 = [] for out_channels in self.layer_3_specs: block3.append( SRNet_layer3(in_channels, out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs)) in_channels = out_channels block4 = [] for out_channels in self.layer_4_specs: block4.append( SRNet_layer4(in_channels, out_channels, activation=self.activation, norm_layer=self.norm_layer, norm_kwargs=norm_kwargs)) in_channels = out_channels self.block1 = nn.Sequential(*block1) self.block2 = nn.Sequential(*block2) self.block3 = nn.Sequential(*block3) self.block4 = nn.Sequential(*block4) self.fc = nn.Linear(in_channels, self.nclasses, bias=True)
def __init__(self, model_name='resnet200d', out_dim=4, pretrained=False, dropout=0.5, pool='AdaptiveAvgPool2d'): super().__init__() self.model = timm.create_model(model_name, pretrained=pretrained) self.model_name = model_name if pool == 'AdaptiveAvgPool2d': self.pooling = nn.AdaptiveAvgPool2d(1) elif pool == 'gem': self.pooling = GeM() else: raise NotImplementedError( f"pooling type {pool} has not implemented!") self.global_pool = SelectAdaptivePool2d(pool_type="avg") if 'resne' in model_name: #resnet n_features = self.model.fc.in_features self.model.global_pool = nn.Identity() self.model.fc = nn.Identity() # print(self.model.conv1[-1].out_channels) # print(self.model.layer1[-1].bn3.num_features) # print(self.model.layer2[-1].bn3.num_features) # print(self.model.layer3[-1].bn3.num_features) # print(self.model.layer4[-1].bn3.num_features) feats_list = [ n_features, self.model.layer3[-1].bn3.num_features, self.model.layer2[-1].bn3.num_features, self.model.layer1[-1].bn3.num_features, self.model.conv1[-1].out_channels ] # self.bottleneck_b5 = nn.Identity() #Bottleneck(inplanes=1024, planes=int(1024 / 4)) # self.fc_b5 = nn.Linear(1024, out_dim) elif "efficientnet" in model_name: self.conv_stem = self.model.conv_stem self.bn1 = self.model.bn1 self.act1 = self.model.act1 ### Original blocks ### for i in range(len((self.model.blocks))): setattr(self, "block{}".format(str(i)), self.model.blocks[i]) self.conv_head = self.model.conv_head self.bn2 = self.model.bn2 self.act2 = self.model.act2 n_features = self.model.num_features self.bottleneck_b4 = Bottleneck( inplanes=self.block4[-1].bn3.num_features, planes=int(self.block4[-1].bn3.num_features / 4)) self.bottleneck_b5 = Bottleneck( inplanes=self.block5[-1].bn3.num_features, planes=int(self.block5[-1].bn3.num_features / 4)) # self.fc_b4 = nn.Linear(self.block4[-1].bn3.num_features, out_dim) # self.fc_b5 = nn.Linear(self.block5[-1].bn3.num_features, out_dim) feats_list = [ n_features, self.block4[-1].bn3.num_features, self.block2[-1].bn3.num_features, self.block1[-1].bn3.num_features, self.block0[-1].bn2.num_features ] del self.model elif "nfnet" in model_name: self.model.head = nn.Identity() n_features = self.model.final_conv.out_channels self.bottleneck_b5 = nn.Identity() self.fc_b5 = nn.Linear( self.model.stages[-2][-1].conv3.out_channels, out_dim) feats_list = [ n_features, self.model.stages[-2][-1].conv3.out_channels, self.model.stages[-3][-1].conv3.out_channels, self.model.stages[-4][-1].conv3.out_channels, self.model.stem[-1].out_channels ] else: raise NotImplementedError( f"model type {model_name} has not implemented!") self.out_shape = { 'C3_size': feats_list[2], 'C4_size': feats_list[1], 'C5_size': feats_list[0] } print("backbone output channel: C3 {}, C4 {}, C5 {}".format( feats_list[2] * 2, feats_list[1] * 2, feats_list[0] * 2)) self.fc = nn.Linear(n_features, out_dim) self.dropout = nn.Dropout(dropout)