ART classification

ART classification
  1. 今天没吃饭的网络

    • 1.1 网络代码
    • 1.2 网络结构
    • 1.3 数据处理
  2. 000wangbo的网络

    • 1.1 网络代码

    • 1.2 数据处理

内容
  1. 今天没吃饭的网络

    • 1.1 网络代码
    class SELayer(nn.Module):
        def __init__(self, channel, reduction=16):
            super(SELayer, self).__init__()
            self.avg_pool = nn.AdaptiveAvgPool2d(1)
            self.fc = nn.Sequential(
                nn.Linear(channel, channel // reduction, bias=False),
                nn.ReLU(inplace=True),
                nn.Linear(channel // reduction, channel, bias=False),
                nn.Sigmoid()
            )
    
        def forward(self, x):
            b, c, _, _ = x.size()
            y = self.avg_pool(x).view(b, c)
            y = self.fc(y).view(b, c, 1, 1)
            return y
    
    
    class AdaptiveConcatPool2d(nn.Module):
        def __init__(self, sz=(1,1)):
            super().__init__()
            self.ap = nn.AdaptiveAvgPool2d(sz)
            self.mp = nn.AdaptiveMaxPool2d(sz)
            
        def forward(self, x):
            return torch.cat([self.mp(x), self.ap(x)], 1)
    
    
    class GeneralizedMeanPooling(nn.Module):
        def __init__(self, norm=3, output_size=1, eps=1e-6):
            super().__init__()
            assert norm > 0
            self.p = float(norm)
            self.output_size = output_size
            self.eps = eps
    
        def forward(self, x):
            x = x.clamp(min=self.eps).pow(self.p)
            
            return torch.nn.functional.adaptive_avg_pool2d(x, self.output_size).pow(1. / self.p)
    
        def __repr__(self):
            return self.__class__.__name__ + '(' \
                   + str(self.p) + ', ' \
                   + 'output_size=' + str(self.output_size) + ')'
    
    
    
    class BaseModel(nn.Module):
        def __init__(self, model_name, num_classes=2, pretrained=True, pool_type='max', down=True, metric='linear'):
            super().__init__()
            self.model_name = model_name
            
            if model_name == 'eff-b3':
                backbone = EfficientNet.from_pretrained('efficientnet-b3')
                plane = 1536
            elif model_name == 'resnext50':
                backbone = nn.Sequential(*list(models.resnext50_32x4d(pretrained=pretrained).children())[:-2])
                plane = 2048
            else:
                backbone = None
                plane = None
    
            self.backbone = backbone
            
            if pool_type == 'avg':
                self.pool = nn.AdaptiveAvgPool2d((1, 1))
            elif pool_type == 'cat':
                self.pool = AdaptiveConcatPool2d()
                down = 1
            elif pool_type == 'max':
                self.pool = nn.AdaptiveMaxPool2d((1, 1))
            elif pool_type == 'gem':
                self.pool = GeneralizedMeanPooling()
            else:
                self.pool = None
            
            if down:
                if pool_type == 'cat':
                    self.down = nn.Sequential(
                        nn.Linear(plane * 2, plane),
                        nn.BatchNorm1d(plane),
                        nn.Dropout(0.2),
                        nn.ReLU(True)
                        )
                else:
                    self.down = nn.Sequential(
                        nn.Linear(plane, plane),
                        nn.BatchNorm1d(plane),
                        nn.Dropout(0.2),
                        nn.ReLU(True)
                    )
            else:
                self.down = nn.Identity()
            
            self.se = SELayer(plane)
            self.hidden = nn.Linear(plane, plane)
            self.relu = nn.ReLU(True)
            
            if metric == 'linear':
                self.metric = nn.Linear(plane, num_classes)
            elif metric == 'am':
                self.metric = AddMarginProduct(plane, num_classes)
            else:
                self.metric = None
    
        def forward(self, x):
            if self.model_name == 'eff-b3':
                feat = self.backbone.extract_features(x)
            else:
                feat = self.backbone(x)
            
            feat = self.pool(feat)
            se = self.se(feat).view(feat.size(0), -1)
            feat_flat = feat.view(feat.size(0), -1)
            feat_flat = self.relu(self.hidden(feat_flat) * se)
    
            out = self.metric(feat_flat)
            return out
    
    • 1.2 网络结构
        def forward(self, x):
            if self.model_name == 'eff-b3':
                feat = self.backbone.extract_features(x)
            else:
                feat = self.backbone(x)
            
            feat = self.pool(feat)
            se = self.se(feat).view(feat.size(0), -1)
            feat_flat = feat.view(feat.size(0), -1)
            feat_flat = self.relu(self.hidden(feat_flat) * se)
    
            out = self.metric(feat_flat)
            return out
    

    其中,backbone定义为

    if model_name == 'eff-b3':
                backbone = EfficientNet.from_pretrained('efficientnet-b3')
                plane = 1536
    elif model_name == 'resnext50':
                backbone = nn.Sequential(*list(models.resnext50_32x4d(pretrained=pretrained).children())[:-2])
                plane = 2048
    

    即backbone采用eff-b3结构或者resnext50结构

    SE为注意力提取模块,定义为,

    class SELayer(nn.Module):
        def __init__(self, channel, reduction=16):
            super(SELayer, self).__init__()
            self.avg_pool = nn.AdaptiveAvgPool2d(1)
            self.fc = nn.Sequential(
                nn.Linear(channel, channel // reduction, bias=False),
                nn.ReLU(inplace=True),
                nn.Linear(channel // reduction, channel, bias=False),
                nn.Sigmoid()
            )
    
        def forward(self, x):
            b, c, _, _ = x.size()
            y = self.avg_pool(x).view(b, c)
            y = self.fc(y).view(b, c, 1, 1)
            return y
    

    对于其中的 pool,有四种方案,分别是

    if pool_type == 'avg':
                self.pool = nn.AdaptiveAvgPool2d((1, 1))
    elif pool_type == 'cat':
                self.pool = AdaptiveConcatPool2d()
                down = 1
    elif pool_type == 'max':
                self.pool = nn.AdaptiveMaxPool2d((1, 1))
    elif pool_type == 'gem':
                self.pool = GeneralizedMeanPooling()
    

    即AdaptiveAvgPool2d, AdaptiveConcatPool2d,AdaptiveMaxPool2d与GeneralizedMeanPooling

    metric也有两种不同的方案,如下

    if metric == 'linear':
                self.metric = nn.Linear(plane, num_classes)
    elif metric == 'am':
                self.metric = AddMarginProduct(plane, num_classes)
    
    • 1.3 数据处理

      作者对最终结果进行了投票策略,对网络训练4次,最后采用投票机制选取最终结果,如下

      files = ['1.csv', '2.csv', '3.csv', '4.csv']
      weights = [1, 1, 1, 1]
      
      results = np.zeros((800, 49))
      for file, w in zip(files, weights):
          print(w)
          df = pd.read_csv(file, header=None).values
          for x, y in df:
              # print(x, y)
              results[x, y] += w
              # break
      
      print(results[0])
      
      submit = {
          'name': np.arange(800).tolist(),
          'pred': np.argmax(results, axis=1).tolist()
          }
      
      for k, v in submit.items():
          print(k, v)
      
      df = pd.DataFrame(submit)
      df.to_csv('vote.csv', header=False, index=False)
      
      

      主要是采用了argmax函数

  2. 000wangbo的网络

    • 1.1 网络代码

      主要是采用了resnest200,进行了pooling层的优化

          model =resnest200(pretrained=True)
          model.avgpool = torch.nn.AdaptiveAvgPool2d(output_size=1)
          model.fc = torch.nn.Linear(model.fc.in_features,49)
      
    • 1.2 数据处理

      同样也采用了投票机制。训练取得5组结果,最后进行投票,得到最后分数。

      val_score['preds_label'] = val_score['preds'].apply(lambda x:np.argmax(x))
      
posted @ 2020-11-04 11:56  沙夜  阅读(137)  评论(0)    收藏  举报