Browse Source

Reformat all codes (#38)

LutaoChu 3 years ago
parent
commit
166b28fdcd
100 changed files with 716 additions and 730 deletions
  1. 1 1
      paddlers/custom_models/cd/__init__.py
  2. 1 1
      paddlers/custom_models/cd/models/__init__.py
  3. 8 3
      paddlers/custom_models/cd/models/backbones/resnet.py
  4. 101 77
      paddlers/custom_models/cd/models/bit.py
  5. 5 5
      paddlers/custom_models/cd/models/cdnet.py
  6. 21 35
      paddlers/custom_models/cd/models/changestar.py
  7. 15 12
      paddlers/custom_models/cd/models/dsamnet.py
  8. 30 33
      paddlers/custom_models/cd/models/dsifn.py
  9. 5 5
      paddlers/custom_models/cd/models/layers/attention.py
  10. 89 40
      paddlers/custom_models/cd/models/layers/blocks.py
  11. 2 2
      paddlers/custom_models/cd/models/param_init.py
  12. 51 31
      paddlers/custom_models/cd/models/snunet.py
  13. 62 54
      paddlers/custom_models/cd/models/stanet.py
  14. 10 31
      paddlers/custom_models/cd/models/unet_ef.py
  15. 23 40
      paddlers/custom_models/cd/models/unet_siamconc.py
  16. 35 36
      paddlers/custom_models/cd/models/unet_siamdiff.py
  17. 1 1
      paddlers/custom_models/cls/__init__.py
  18. 1 1
      paddlers/custom_models/det/__init__.py
  19. 1 1
      paddlers/custom_models/gan/__init__.py
  20. 1 1
      paddlers/custom_models/seg/__init__.py
  21. 1 1
      paddlers/custom_models/seg/models/__init__.py
  22. 1 1
      paddlers/custom_models/seg/models/farseg/__init__.py
  23. 55 36
      paddlers/custom_models/seg/models/farseg/farseg.py
  24. 8 7
      paddlers/custom_models/seg/models/farseg/fpn.py
  25. 1 1
      paddlers/custom_models/seg/models/utils/__init__.py
  26. 4 3
      paddlers/custom_models/seg/models/utils/layers_lib.py
  27. 1 1
      paddlers/custom_models/seg/models/utils/param_init.py
  28. 1 1
      paddlers/custom_models/seg/models/utils/torch_nn.py
  29. 3 2
      paddlers/datasets/clas_dataset.py
  30. 10 17
      paddlers/datasets/voc.py
  31. 1 1
      paddlers/models/ppcls/arch/backbone/__init__.py
  32. 6 10
      paddlers/models/ppcls/arch/backbone/base/theseus_layer.py
  33. 1 1
      paddlers/models/ppcls/arch/backbone/legendary_models/esnet.py
  34. 4 8
      paddlers/models/ppcls/arch/backbone/legendary_models/hrnet.py
  35. 2 6
      paddlers/models/ppcls/arch/backbone/legendary_models/inception_v3.py
  36. 4 8
      paddlers/models/ppcls/arch/backbone/legendary_models/mobilenet_v1.py
  37. 3 4
      paddlers/models/ppcls/arch/backbone/legendary_models/mobilenet_v3.py
  38. 9 9
      paddlers/models/ppcls/arch/backbone/legendary_models/pp_lcnet.py
  39. 2 2
      paddlers/models/ppcls/arch/backbone/legendary_models/resnet.py
  40. 1 1
      paddlers/models/ppcls/arch/backbone/legendary_models/vgg.py
  41. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/alexnet.py
  42. 3 7
      paddlers/models/ppcls/arch/backbone/model_zoo/cspnet.py
  43. 2 3
      paddlers/models/ppcls/arch/backbone/model_zoo/darknet.py
  44. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/densenet.py
  45. 5 9
      paddlers/models/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py
  46. 2 3
      paddlers/models/ppcls/arch/backbone/model_zoo/dla.py
  47. 2 4
      paddlers/models/ppcls/arch/backbone/model_zoo/dpn.py
  48. 4 7
      paddlers/models/ppcls/arch/backbone/model_zoo/efficientnet.py
  49. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/ghostnet.py
  50. 4 5
      paddlers/models/ppcls/arch/backbone/model_zoo/gvt.py
  51. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/hardnet.py
  52. 3 11
      paddlers/models/ppcls/arch/backbone/model_zoo/inception_v4.py
  53. 3 3
      paddlers/models/ppcls/arch/backbone/model_zoo/levit.py
  54. 2 3
      paddlers/models/ppcls/arch/backbone/model_zoo/mixnet.py
  55. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/mobilenet_v2.py
  56. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/pvt_v2.py
  57. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/rednet.py
  58. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/regnet.py
  59. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/repvgg.py
  60. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/res2net.py
  61. 2 5
      paddlers/models/ppcls/arch/backbone/model_zoo/res2net_vd.py
  62. 2 3
      paddlers/models/ppcls/arch/backbone/model_zoo/resnest.py
  63. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/resnet_vc.py
  64. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/resnext.py
  65. 5 17
      paddlers/models/ppcls/arch/backbone/model_zoo/resnext_vd.py
  66. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/rexnet.py
  67. 3 9
      paddlers/models/ppcls/arch/backbone/model_zoo/se_resnext.py
  68. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/se_resnext_vd.py
  69. 3 11
      paddlers/models/ppcls/arch/backbone/model_zoo/shufflenet_v2.py
  70. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/squeezenet.py
  71. 4 5
      paddlers/models/ppcls/arch/backbone/model_zoo/swin_transformer.py
  72. 3 5
      paddlers/models/ppcls/arch/backbone/model_zoo/tnt.py
  73. 1 1
      paddlers/models/ppcls/arch/backbone/model_zoo/vision_transformer.py
  74. 2 3
      paddlers/models/ppcls/arch/backbone/model_zoo/xception_deeplab.py
  75. 2 2
      paddlers/models/ppcls/arch/gears/__init__.py
  76. 0 1
      paddlers/models/ppcls/arch/slim/prune.py
  77. 1 1
      paddlers/models/ppcls/arch/utils.py
  78. 1 2
      paddlers/models/ppcls/data/dataloader/dali.py
  79. 2 2
      paddlers/models/ppcls/data/dataloader/pk_sampler.py
  80. 1 1
      paddlers/models/ppcls/data/postprocess/topk.py
  81. 1 2
      paddlers/models/ppcls/data/preprocess/ops/autoaugment.py
  82. 16 13
      paddlers/models/ppcls/data/preprocess/ops/functional.py
  83. 2 2
      paddlers/models/ppcls/data/preprocess/ops/grid.py
  84. 2 4
      paddlers/models/ppcls/data/preprocess/ops/operators.py
  85. 1 2
      paddlers/models/ppcls/data/preprocess/ops/timm_autoaugment.py
  86. 4 4
      paddlers/models/ppcls/engine/engine.py
  87. 2 4
      paddlers/models/ppcls/engine/evaluation/retrieval.py
  88. 1 1
      paddlers/models/ppcls/loss/celoss.py
  89. 1 1
      paddlers/models/ppcls/loss/dmlloss.py
  90. 1 1
      paddlers/models/ppcls/loss/rkdloss.py
  91. 3 5
      paddlers/models/ppcls/metric/metrics.py
  92. 1 1
      paddlers/models/ppcls/optimizer/learning_rate.py
  93. 4 3
      paddlers/models/ppcls/optimizer/optimizer.py
  94. 2 3
      paddlers/models/ppcls/static/program.py
  95. 3 3
      paddlers/models/ppcls/static/train.py
  96. 2 3
      paddlers/models/ppcls/utils/config.py
  97. 1 2
      paddlers/models/ppcls/utils/feature_maps_visualization/fm_vis.py
  98. 2 2
      paddlers/models/ppcls/utils/feature_maps_visualization/resnet.py
  99. 1 2
      paddlers/models/ppcls/utils/feature_maps_visualization/utils.py
  100. 8 4
      paddlers/models/ppcls/utils/gallery2fc.py

+ 1 - 1
paddlers/custom_models/cd/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import models
+from . import models

+ 1 - 1
paddlers/custom_models/cd/models/__init__.py

@@ -21,4 +21,4 @@ from .dsamnet import DSAMNet
 from .changestar import ChangeStar
 from .unet_ef import UNetEarlyFusion
 from .unet_siamconc import UNetSiamConc
-from .unet_siamdiff import UNetSiamDiff
+from .unet_siamdiff import UNetSiamDiff

+ 8 - 3
paddlers/custom_models/cd/models/backbones/resnet.py

@@ -36,7 +36,6 @@ import paddle.nn as nn
 
 from paddle.utils.download import get_weights_path_from_url
 
-
 __all__ = []
 
 model_urls = {
@@ -180,7 +179,13 @@ class ResNet(nn.Layer):
             resnet18 = ResNet(BasicBlock, 18)
     """
 
-    def __init__(self, block, depth, num_classes=1000, with_pool=True, strides=(1,1,2,2,2), norm_layer=None):
+    def __init__(self,
+                 block,
+                 depth,
+                 num_classes=1000,
+                 with_pool=True,
+                 strides=(1, 1, 2, 2, 2),
+                 norm_layer=None):
         super(ResNet, self).__init__()
         layer_cfg = {
             18: [2, 2, 2, 2],
@@ -356,4 +361,4 @@ def resnet152(pretrained=False, **kwargs):
             # build model and load imagenet pretrained weight
             # model = resnet152(pretrained=True)
     """
-    return _resnet('resnet152', BottleneckBlock, 152, pretrained, **kwargs)
+    return _resnet('resnet152', BottleneckBlock, 152, pretrained, **kwargs)

+ 101 - 77
paddlers/custom_models/cd/models/bit.py

@@ -55,25 +55,35 @@ class BIT(nn.Layer):
     Raises:
         ValueError: When an unsupported backbone type is specified, or the number of backbone stages is not 3, 4, or 5.
     """
-    
-    def __init__(
-        self, in_channels, num_classes,
-        backbone='resnet18', n_stages=4, 
-        use_tokenizer=True, token_len=4, 
-        pool_mode='max', pool_size=2,
-        enc_with_pos=True, 
-        enc_depth=1, enc_head_dim=64, 
-        dec_depth=8, dec_head_dim=8,
-        **backbone_kwargs
-    ):
+
+    def __init__(self,
+                 in_channels,
+                 num_classes,
+                 backbone='resnet18',
+                 n_stages=4,
+                 use_tokenizer=True,
+                 token_len=4,
+                 pool_mode='max',
+                 pool_size=2,
+                 enc_with_pos=True,
+                 enc_depth=1,
+                 enc_head_dim=64,
+                 dec_depth=8,
+                 dec_head_dim=8,
+                 **backbone_kwargs):
         super().__init__()
 
         # TODO: reduce hard-coded parameters
         DIM = 32
-        MLP_DIM = 2*DIM
+        MLP_DIM = 2 * DIM
         EBD_DIM = DIM
 
-        self.backbone = Backbone(in_channels, EBD_DIM, arch=backbone, n_stages=n_stages, **backbone_kwargs)
+        self.backbone = Backbone(
+            in_channels,
+            EBD_DIM,
+            arch=backbone,
+            n_stages=n_stages,
+            **backbone_kwargs)
 
         self.use_tokenizer = use_tokenizer
         if not use_tokenizer:
@@ -88,46 +98,43 @@ class BIT(nn.Layer):
         self.enc_with_pos = enc_with_pos
         if enc_with_pos:
             self.enc_pos_embedding = self.create_parameter(
-                shape=(1,self.token_len*2,EBD_DIM), 
-                default_initializer=Normal()
-            )
+                shape=(1, self.token_len * 2, EBD_DIM),
+                default_initializer=Normal())
 
         self.enc_depth = enc_depth
         self.dec_depth = dec_depth
         self.enc_head_dim = enc_head_dim
         self.dec_head_dim = dec_head_dim
-        
+
         self.encoder = TransformerEncoder(
-            dim=DIM, 
-            depth=enc_depth, 
+            dim=DIM,
+            depth=enc_depth,
             n_heads=8,
             head_dim=enc_head_dim,
             mlp_dim=MLP_DIM,
-            dropout_rate=0.
-        )
+            dropout_rate=0.)
         self.decoder = TransformerDecoder(
-            dim=DIM, 
+            dim=DIM,
             depth=dec_depth,
-            n_heads=8, 
-            head_dim=dec_head_dim, 
-            mlp_dim=MLP_DIM, 
+            n_heads=8,
+            head_dim=dec_head_dim,
+            mlp_dim=MLP_DIM,
             dropout_rate=0.,
-            apply_softmax=True
-        )
+            apply_softmax=True)
 
         self.upsample = nn.Upsample(scale_factor=4, mode='bilinear')
         self.conv_out = nn.Sequential(
-            Conv3x3(EBD_DIM, EBD_DIM, norm=True, act=True),
-            Conv3x3(EBD_DIM, num_classes)
-        )
+            Conv3x3(
+                EBD_DIM, EBD_DIM, norm=True, act=True),
+            Conv3x3(EBD_DIM, num_classes))
 
     def _get_semantic_tokens(self, x):
         b, c = paddle.shape(x)[:2]
         att_map = self.conv_att(x)
-        att_map = att_map.reshape((b,self.token_len,1,-1))
+        att_map = att_map.reshape((b, self.token_len, 1, -1))
         att_map = F.softmax(att_map, axis=-1)
-        x = x.reshape((b,1,c,-1))
-        tokens = (x*att_map).sum(-1)
+        x = x.reshape((b, 1, c, -1))
+        tokens = (x * att_map).sum(-1)
         return tokens
 
     def _get_reshaped_tokens(self, x):
@@ -137,7 +144,7 @@ class BIT(nn.Layer):
             x = F.adaptive_avg_pool2d(x, (self.pool_size, self.pool_size))
         else:
             x = x
-        tokens = x.transpose((0,2,3,1)).flatten(1,2)
+        tokens = x.transpose((0, 2, 3, 1)).flatten(1, 2)
         return tokens
 
     def encode(self, x):
@@ -148,9 +155,9 @@ class BIT(nn.Layer):
 
     def decode(self, x, m):
         b, c, h, w = paddle.shape(x)
-        x = x.transpose((0,2,3,1)).flatten(1,2)
+        x = x.transpose((0, 2, 3, 1)).flatten(1, 2)
         x = self.decoder(x, m)
-        x = x.transpose((0,2,1)).reshape((b,c,h,w))
+        x = x.transpose((0, 2, 1)).reshape((b, c, h, w))
         return x
 
     def forward(self, t1, t2):
@@ -232,29 +239,30 @@ class FeedForward(nn.Sequential):
             nn.Linear(dim, hidden_dim),
             nn.GELU(),
             nn.Dropout(dropout_rate),
-            nn.Linear(hidden_dim, dim),
-            nn.Dropout(dropout_rate)
-        )
+            nn.Linear(hidden_dim, dim), nn.Dropout(dropout_rate))
 
 
 class CrossAttention(nn.Layer):
-    def __init__(self, dim, n_heads=8, head_dim=64, dropout_rate=0., apply_softmax=True):
+    def __init__(self,
+                 dim,
+                 n_heads=8,
+                 head_dim=64,
+                 dropout_rate=0.,
+                 apply_softmax=True):
         super().__init__()
 
         inner_dim = head_dim * n_heads
         self.n_heads = n_heads
-        self.scale = dim ** -0.5
+        self.scale = dim**-0.5
 
         self.apply_softmax = apply_softmax
-        
+
         self.fc_q = nn.Linear(dim, inner_dim, bias_attr=False)
         self.fc_k = nn.Linear(dim, inner_dim, bias_attr=False)
         self.fc_v = nn.Linear(dim, inner_dim, bias_attr=False)
 
         self.fc_out = nn.Sequential(
-            nn.Linear(inner_dim, dim),
-            nn.Dropout(dropout_rate)
-        )
+            nn.Linear(inner_dim, dim), nn.Dropout(dropout_rate))
 
     def forward(self, x, ref):
         b, n = paddle.shape(x)[:2]
@@ -264,9 +272,9 @@ class CrossAttention(nn.Layer):
         k = self.fc_k(ref)
         v = self.fc_v(ref)
 
-        q = q.reshape((b,n,h,-1)).transpose((0,2,1,3))
-        k = k.reshape((b,paddle.shape(ref)[1],h,-1)).transpose((0,2,1,3))
-        v = v.reshape((b,paddle.shape(ref)[1],h,-1)).transpose((0,2,1,3))
+        q = q.reshape((b, n, h, -1)).transpose((0, 2, 1, 3))
+        k = k.reshape((b, paddle.shape(ref)[1], h, -1)).transpose((0, 2, 1, 3))
+        v = v.reshape((b, paddle.shape(ref)[1], h, -1)).transpose((0, 2, 1, 3))
 
         mult = paddle.matmul(q, k, transpose_y=True) * self.scale
 
@@ -274,7 +282,7 @@ class CrossAttention(nn.Layer):
             mult = F.softmax(mult, axis=-1)
 
         out = paddle.matmul(mult, v)
-        out = out.transpose((0,2,1,3)).flatten(2)
+        out = out.transpose((0, 2, 1, 3)).flatten(2)
         return self.fc_out(out)
 
 
@@ -288,10 +296,15 @@ class TransformerEncoder(nn.Layer):
         super().__init__()
         self.layers = nn.LayerList([])
         for _ in range(depth):
-            self.layers.append(nn.LayerList([
-                Residual(PreNorm(dim, SelfAttention(dim, n_heads, head_dim, dropout_rate))),
-                Residual(PreNorm(dim, FeedForward(dim, mlp_dim, dropout_rate)))
-            ]))
+            self.layers.append(
+                nn.LayerList([
+                    Residual(
+                        PreNorm(dim,
+                                SelfAttention(dim, n_heads, head_dim,
+                                              dropout_rate))),
+                    Residual(
+                        PreNorm(dim, FeedForward(dim, mlp_dim, dropout_rate)))
+                ]))
 
     def forward(self, x):
         for att, ff in self.layers:
@@ -301,14 +314,26 @@ class TransformerEncoder(nn.Layer):
 
 
 class TransformerDecoder(nn.Layer):
-    def __init__(self, dim, depth, n_heads, head_dim, mlp_dim, dropout_rate, apply_softmax=True):
+    def __init__(self,
+                 dim,
+                 depth,
+                 n_heads,
+                 head_dim,
+                 mlp_dim,
+                 dropout_rate,
+                 apply_softmax=True):
         super().__init__()
         self.layers = nn.LayerList([])
         for _ in range(depth):
-            self.layers.append(nn.LayerList([
-                Residual2(PreNorm2(dim, CrossAttention(dim, n_heads, head_dim, dropout_rate, apply_softmax))),
-                Residual(PreNorm(dim, FeedForward(dim, mlp_dim, dropout_rate)))
-            ]))
+            self.layers.append(
+                nn.LayerList([
+                    Residual2(
+                        PreNorm2(dim,
+                                 CrossAttention(dim, n_heads, head_dim,
+                                                dropout_rate, apply_softmax))),
+                    Residual(
+                        PreNorm(dim, FeedForward(dim, mlp_dim, dropout_rate)))
+                ]))
 
     def forward(self, x, m):
         for att, ff in self.layers:
@@ -318,21 +343,26 @@ class TransformerDecoder(nn.Layer):
 
 
 class Backbone(nn.Layer, KaimingInitMixin):
-    def __init__(
-        self, 
-        in_ch, out_ch=32,
-        arch='resnet18',
-        pretrained=True,
-        n_stages=5
-    ):
+    def __init__(self,
+                 in_ch,
+                 out_ch=32,
+                 arch='resnet18',
+                 pretrained=True,
+                 n_stages=5):
         super().__init__()
 
         expand = 1
-        strides = (2,1,2,1,1)
+        strides = (2, 1, 2, 1, 1)
         if arch == 'resnet18':
-            self.resnet = resnet.resnet18(pretrained=pretrained, strides=strides, norm_layer=get_norm_layer())
+            self.resnet = resnet.resnet18(
+                pretrained=pretrained,
+                strides=strides,
+                norm_layer=get_norm_layer())
         elif arch == 'resnet34':
-            self.resnet = resnet.resnet34(pretrained=pretrained, strides=strides, norm_layer=get_norm_layer())
+            self.resnet = resnet.resnet34(
+                pretrained=pretrained,
+                strides=strides,
+                norm_layer=get_norm_layer())
         else:
             raise ValueError
 
@@ -346,7 +376,7 @@ class Backbone(nn.Layer, KaimingInitMixin):
             itm_ch = 128 * expand
         else:
             raise ValueError
-            
+
         self.upsample = nn.Upsample(scale_factor=2)
         self.conv_out = Conv3x3(itm_ch, out_ch)
 
@@ -354,13 +384,7 @@ class Backbone(nn.Layer, KaimingInitMixin):
 
         if in_ch != 3:
             self.resnet.conv1 = nn.Conv2D(
-                in_ch, 
-                64,
-                kernel_size=7,
-                stride=2,
-                padding=3,
-                bias_attr=False
-            )
+                in_ch, 64, kernel_size=7, stride=2, padding=3, bias_attr=False)
 
         if not pretrained:
             self.init_weight()
@@ -391,4 +415,4 @@ class Backbone(nn.Layer, KaimingInitMixin):
             self.resnet.layer3 = Identity()
 
         self.resnet.avgpool = Identity()
-        self.resnet.fc = Identity()
+        self.resnet.fc = Identity()

+ 5 - 5
paddlers/custom_models/cd/models/cdnet.py

@@ -36,7 +36,7 @@ class CDNet(nn.Layer):
         self.conv8 = Conv7x7(64, 64, norm=True, act=True)
         self.upool1 = nn.MaxUnPool2D(2, 2)
         self.conv_out = Conv7x7(64, num_classes, norm=False, act=False)
-    
+
     def forward(self, t1, t2):
         x = paddle.concat([t1, t2], axis=1)
         x, ind1 = self.pool1(self.conv1(x))
@@ -54,15 +54,15 @@ class Conv7x7(nn.Layer):
     def __init__(self, in_ch, out_ch, norm=False, act=False):
         super(Conv7x7, self).__init__()
         layers = [
-            nn.Pad2D(3),
-            nn.Conv2D(in_ch, out_ch, 7, bias_attr=(False if norm else None))
+            nn.Pad2D(3), nn.Conv2D(
+                in_ch, out_ch, 7, bias_attr=(False if norm else None))
         ]
         if norm:
             layers.append(nn.BatchNorm2D(out_ch))
         if act:
             layers.append(nn.ReLU())
         self.layers = nn.Sequential(*layers)
-    
+
     def forward(self, x):
         return self.layers(x)
 
@@ -72,4 +72,4 @@ if __name__ == "__main__":
     t2 = paddle.randn((1, 3, 512, 512), dtype="float32")
     model = CDNet(6, 2)
     pred = model(t1, t2)[0]
-    print(pred.shape)
+    print(pred.shape)

+ 21 - 35
paddlers/custom_models/cd/models/changestar.py

@@ -24,36 +24,22 @@ from .layers import Conv3x3, Identity
 class _ChangeStarBase(nn.Layer):
 
     USE_MULTITASK_DECODER = True
-    OUT_TYPES = (
-        MaskType.CD, 
-        MaskType.CD, 
-        MaskType.SEG_T1, 
-        MaskType.SEG_T2
-    )
+    OUT_TYPES = (MaskType.CD, MaskType.CD, MaskType.SEG_T1, MaskType.SEG_T2)
 
-    def __init__(
-        self,
-        seg_model,
-        num_classes,
-        mid_channels,
-        inner_channels,
-        num_convs,
-        scale_factor
-    ):
+    def __init__(self, seg_model, num_classes, mid_channels, inner_channels,
+                 num_convs, scale_factor):
         super().__init__()
 
         self.extract = seg_model
         self.detect = ChangeMixin(
-            in_ch=mid_channels*2,
+            in_ch=mid_channels * 2,
             out_ch=num_classes,
             mid_ch=inner_channels,
             num_convs=num_convs,
-            scale_factor=scale_factor
-        )
+            scale_factor=scale_factor)
         self.segment = nn.Sequential(
             Conv3x3(mid_channels, 2),
-            nn.UpsamplingBilinear2D(scale_factor=scale_factor)
-        )
+            nn.UpsamplingBilinear2D(scale_factor=scale_factor))
 
         self.init_weight()
 
@@ -80,14 +66,14 @@ class ChangeMixin(nn.Layer):
         super().__init__()
         convs = [Conv3x3(in_ch, mid_ch, norm=True, act=True)]
         convs += [
-            Conv3x3(mid_ch, mid_ch, norm=True, act=True) 
-            for _ in range(num_convs-1)
+            Conv3x3(
+                mid_ch, mid_ch, norm=True, act=True)
+            for _ in range(num_convs - 1)
         ]
         self.detect = nn.Sequential(
             *convs,
             Conv3x3(mid_ch, out_ch),
-            nn.UpsamplingBilinear2D(scale_factor=scale_factor)
-        )
+            nn.UpsamplingBilinear2D(scale_factor=scale_factor))
 
     def forward(self, x1, x2):
         pred12 = self.detect(paddle.concat([x1, x2], axis=1))
@@ -117,13 +103,12 @@ class ChangeStar_FarSeg(_ChangeStarBase):
     """
 
     def __init__(
-        self, 
-        num_classes,
-        mid_channels=256,
-        inner_channels=16,
-        num_convs=4,
-        scale_factor=4.0,
-    ):
+            self,
+            num_classes,
+            mid_channels=256,
+            inner_channels=16,
+            num_convs=4,
+            scale_factor=4.0, ):
         # TODO: Configurable FarSeg model
         class _FarSegWrapper(nn.Layer):
             def __init__(self, seg_model):
@@ -137,7 +122,8 @@ class ChangeStar_FarSeg(_ChangeStarBase):
                 if self._seg_model.scene_relation:
                     c5 = feat_list[-1]
                     c6 = self._seg_model.gap(c5)
-                    refined_fpn_feat_list = self._seg_model.sr(c6, fpn_feat_list)
+                    refined_fpn_feat_list = self._seg_model.sr(c6,
+                                                               fpn_feat_list)
                 else:
                     refined_fpn_feat_list = fpn_feat_list
                 final_feat = self._seg_model.decoder(refined_fpn_feat_list)
@@ -151,8 +137,8 @@ class ChangeStar_FarSeg(_ChangeStarBase):
             mid_channels=mid_channels,
             inner_channels=inner_channels,
             num_convs=num_convs,
-            scale_factor=scale_factor
-        )
+            scale_factor=scale_factor)
+
 
 # NOTE: Currently, ChangeStar = FarSeg + ChangeMixin + SegHead
-ChangeStar = ChangeStar_FarSeg
+ChangeStar = ChangeStar_FarSeg

+ 15 - 12
paddlers/custom_models/cd/models/dsamnet.py

@@ -39,13 +39,14 @@ class DSAMNet(nn.Layer):
         ca_ratio (int, optional): The channel reduction ratio for the channel attention module. Default: 8.
         sa_kernel (int, optional): The size of the convolutional kernel used in the spatial attention module. Default: 7.
     """
-    
+
     def __init__(self, in_channels, num_classes, ca_ratio=8, sa_kernel=7):
         super().__init__()
 
         WIDTH = 64
 
-        self.backbone = Backbone(in_ch=in_channels, arch='resnet18', strides=(1,1,2,2,1))
+        self.backbone = Backbone(
+            in_ch=in_channels, arch='resnet18', strides=(1, 1, 2, 2, 1))
         self.decoder = Decoder(WIDTH)
 
         self.cbam1 = CBAM(64, ratio=ca_ratio, kernel_size=sa_kernel)
@@ -55,9 +56,9 @@ class DSAMNet(nn.Layer):
         self.dsl3 = DSLayer(128, num_classes, 32, stride=4, output_padding=3)
 
         self.conv_out = nn.Sequential(
-            Conv3x3(WIDTH, WIDTH, norm=True, act=True),
-            Conv3x3(WIDTH, num_classes)
-        )
+            Conv3x3(
+                WIDTH, WIDTH, norm=True, act=True),
+            Conv3x3(WIDTH, num_classes))
 
         self.init_weight()
 
@@ -71,15 +72,16 @@ class DSAMNet(nn.Layer):
         y1 = self.cbam1(y1)
         y2 = self.cbam2(y2)
 
-        out = paddle.abs(y1-y2)
-        out = F.interpolate(out, size=paddle.shape(t1)[2:], mode='bilinear', align_corners=True)
+        out = paddle.abs(y1 - y2)
+        out = F.interpolate(
+            out, size=paddle.shape(t1)[2:], mode='bilinear', align_corners=True)
         pred = self.conv_out(out)
 
         if not self.training:
             return [pred]
         else:
-            ds2 = self.dsl2(paddle.abs(f1[0]-f2[0]))
-            ds3 = self.dsl3(paddle.abs(f1[1]-f2[1]))
+            ds2 = self.dsl2(paddle.abs(f1[0] - f2[0]))
+            ds3 = self.dsl3(paddle.abs(f1[1] - f2[1]))
             return [pred, ds2, ds3]
 
     def init_weight(self):
@@ -89,9 +91,10 @@ class DSAMNet(nn.Layer):
 class DSLayer(nn.Sequential):
     def __init__(self, in_ch, out_ch, itm_ch, **convd_kwargs):
         super().__init__(
-            nn.Conv2DTranspose(in_ch, itm_ch, kernel_size=3, padding=1, **convd_kwargs),
+            nn.Conv2DTranspose(
+                in_ch, itm_ch, kernel_size=3, padding=1, **convd_kwargs),
             make_norm(itm_ch),
             nn.ReLU(),
             nn.Dropout2D(p=0.2),
-            nn.Conv2DTranspose(itm_ch, out_ch, kernel_size=3, padding=1)
-        )
+            nn.Conv2DTranspose(
+                itm_ch, out_ch, kernel_size=3, padding=1))

+ 30 - 33
paddlers/custom_models/cd/models/dsifn.py

@@ -43,7 +43,7 @@ class DSIFN(nn.Layer):
         self.encoder1 = self.encoder2 = VGG16FeaturePicker()
 
         self.sa1 = SpatialAttention()
-        self.sa2= SpatialAttention()
+        self.sa2 = SpatialAttention()
         self.sa3 = SpatialAttention()
         self.sa4 = SpatialAttention()
         self.sa5 = SpatialAttention()
@@ -98,7 +98,7 @@ class DSIFN(nn.Layer):
             t2_feats = self.encoder2(t2)
 
         t1_f_l3, t1_f_l8, t1_f_l15, t1_f_l22, t1_f_l29 = t1_feats
-        t2_f_l3, t2_f_l8, t2_f_l15, t2_f_l22, t2_f_l29,= t2_feats
+        t2_f_l3, t2_f_l8, t2_f_l15, t2_f_l22, t2_f_l29, = t2_feats
 
         aux_x = []
 
@@ -113,44 +113,44 @@ class DSIFN(nn.Layer):
 
         x = self.trans_conv1(x)
         x = paddle.concat([x, t1_f_l22, t2_f_l22], axis=1)
-        x = self.ca2(x)*x
+        x = self.ca2(x) * x
         x = self.o2_conv1(x)
         x = self.o2_conv2(x)
         x = self.o2_conv3(x)
-        x = self.sa2(x) *x
+        x = self.sa2(x) * x
         x = self.bn_sa2(x)
         if self.training:
             aux_x.append(x)
 
         x = self.trans_conv2(x)
         x = paddle.concat([x, t1_f_l15, t2_f_l15], axis=1)
-        x = self.ca3(x)*x
+        x = self.ca3(x) * x
         x = self.o3_conv1(x)
         x = self.o3_conv2(x)
         x = self.o3_conv3(x)
-        x = self.sa3(x) *x
+        x = self.sa3(x) * x
         x = self.bn_sa3(x)
         if self.training:
             aux_x.append(x)
 
         x = self.trans_conv3(x)
         x = paddle.concat([x, t1_f_l8, t2_f_l8], axis=1)
-        x = self.ca4(x)*x
+        x = self.ca4(x) * x
         x = self.o4_conv1(x)
         x = self.o4_conv2(x)
         x = self.o4_conv3(x)
-        x = self.sa4(x) *x
+        x = self.sa4(x) * x
         x = self.bn_sa4(x)
         if self.training:
             aux_x.append(x)
 
         x = self.trans_conv4(x)
         x = paddle.concat([x, t1_f_l3, t2_f_l3], axis=1)
-        x = self.ca5(x)*x
+        x = self.ca5(x) * x
         x = self.o5_conv1(x)
         x = self.o5_conv2(x)
         x = self.o5_conv3(x)
-        x = self.sa5(x) *x
+        x = self.sa5(x) * x
         x = self.bn_sa5(x)
 
         out5 = self.o5_conv4(x)
@@ -160,29 +160,25 @@ class DSIFN(nn.Layer):
         else:
             size = paddle.shape(t1)[2:]
             out1 = F.interpolate(
-                self.o1_conv3(aux_x[0]), 
-                size=size, 
-                mode='bilinear', 
-                align_corners=True
-            )
+                self.o1_conv3(aux_x[0]),
+                size=size,
+                mode='bilinear',
+                align_corners=True)
             out2 = F.interpolate(
-                self.o2_conv4(aux_x[1]), 
-                size=size, 
-                mode='bilinear', 
-                align_corners=True
-            )
+                self.o2_conv4(aux_x[1]),
+                size=size,
+                mode='bilinear',
+                align_corners=True)
             out3 = F.interpolate(
-                self.o3_conv4(aux_x[2]), 
-                size=size, 
-                mode='bilinear', 
-                align_corners=True
-            )
+                self.o3_conv4(aux_x[2]),
+                size=size,
+                mode='bilinear',
+                align_corners=True)
             out4 = F.interpolate(
-                self.o4_conv4(aux_x[3]), 
-                size=size, 
-                mode='bilinear', 
-                align_corners=True
-            )
+                self.o4_conv4(aux_x[3]),
+                size=size,
+                mode='bilinear',
+                align_corners=True)
             return [out5, out4, out3, out2, out1]
 
     def init_weight(self):
@@ -191,7 +187,7 @@ class DSIFN(nn.Layer):
 
 
 class VGG16FeaturePicker(nn.Layer):
-    def __init__(self, indices=(3,8,15,22,29)):
+    def __init__(self, indices=(3, 8, 15, 22, 29)):
         super().__init__()
         features = list(vgg16(pretrained=True).features)[:30]
         self.features = nn.LayerList(features)
@@ -209,10 +205,11 @@ class VGG16FeaturePicker(nn.Layer):
 
 def conv2d_bn(in_ch, out_ch, with_dropout=True):
     lst = [
-        nn.Conv2D(in_ch, out_ch, kernel_size=3, stride=1, padding=1),
+        nn.Conv2D(
+            in_ch, out_ch, kernel_size=3, stride=1, padding=1),
         nn.PReLU(),
         make_norm(out_ch),
     ]
     if with_dropout:
         lst.append(nn.Dropout(p=0.6))
-    return nn.Sequential(*lst)
+    return nn.Sequential(*lst)

+ 5 - 5
paddlers/custom_models/cd/models/layers/attention.py

@@ -36,10 +36,10 @@ class ChannelAttention(nn.Layer):
         super().__init__()
         self.avg_pool = nn.AdaptiveAvgPool2D(1)
         self.max_pool = nn.AdaptiveMaxPool2D(1)
-        self.fc1 = Conv1x1(in_ch, in_ch//ratio, bias=False, act=True)
-        self.fc2 = Conv1x1(in_ch//ratio, in_ch, bias=False)
+        self.fc1 = Conv1x1(in_ch, in_ch // ratio, bias=False, act=True)
+        self.fc2 = Conv1x1(in_ch // ratio, in_ch, bias=False)
 
-    def forward(self,x):
+    def forward(self, x):
         avg_out = self.fc2(self.fc1(self.avg_pool(x)))
         max_out = self.fc2(self.fc1(self.max_pool(x)))
         out = avg_out + max_out
@@ -83,7 +83,7 @@ class CBAM(nn.Layer):
         ratio (int, optional): The channel reduction ratio for the channel attention module. Default: 8.
         kernel_size (int, optional): The size of the convolutional kernel used in the spatial attention module. Default: 7.
     """
-    
+
     def __init__(self, in_ch, ratio=8, kernel_size=7):
         super().__init__()
         self.ca = ChannelAttention(in_ch, ratio=ratio)
@@ -92,4 +92,4 @@ class CBAM(nn.Layer):
     def forward(self, x):
         y = self.ca(x) * x
         y = self.sa(y) * y
-        return y
+        return y

+ 89 - 40
paddlers/custom_models/cd/models/layers/blocks.py

@@ -14,13 +14,9 @@
 
 import paddle.nn as nn
 
-
 __all__ = [
-    'BasicConv', 'Conv1x1', 'Conv3x3', 'Conv7x7', 
-    'MaxPool2x2', 'MaxUnPool2x2', 
-    'ConvTransposed3x3',
-    'Identity',
-    'get_norm_layer', 'get_act_layer', 
+    'BasicConv', 'Conv1x1', 'Conv3x3', 'Conv7x7', 'MaxPool2x2', 'MaxUnPool2x2',
+    'ConvTransposed3x3', 'Identity', 'get_norm_layer', 'get_act_layer',
     'make_norm', 'make_act'
 ]
 
@@ -46,24 +42,28 @@ def make_act(*args, **kwargs):
 
 
 class BasicConv(nn.Layer):
-    def __init__(
-        self, in_ch, out_ch, 
-        kernel_size, pad_mode='constant', 
-        bias='auto', norm=False, act=False, 
-        **kwargs
-    ):
+    def __init__(self,
+                 in_ch,
+                 out_ch,
+                 kernel_size,
+                 pad_mode='constant',
+                 bias='auto',
+                 norm=False,
+                 act=False,
+                 **kwargs):
         super().__init__()
         seq = []
         if kernel_size >= 2:
-            seq.append(nn.Pad2D(kernel_size//2, mode=pad_mode))
+            seq.append(nn.Pad2D(kernel_size // 2, mode=pad_mode))
         seq.append(
             nn.Conv2D(
-                in_ch, out_ch, kernel_size,
-                stride=1, padding=0,
-                bias_attr=(False if norm else None) if bias=='auto' else bias,
-                **kwargs
-            )
-        )
+                in_ch,
+                out_ch,
+                kernel_size,
+                stride=1,
+                padding=0,
+                bias_attr=(False if norm else None) if bias == 'auto' else bias,
+                **kwargs))
         if norm:
             if norm is True:
                 norm = make_norm(out_ch)
@@ -79,46 +79,94 @@ class BasicConv(nn.Layer):
 
 
 class Conv1x1(BasicConv):
-    def __init__(self, in_ch, out_ch, pad_mode='constant', bias='auto', norm=False, act=False, **kwargs):
-        super().__init__(in_ch, out_ch, 1, pad_mode=pad_mode, bias=bias, norm=norm, act=act, **kwargs)
+    def __init__(self,
+                 in_ch,
+                 out_ch,
+                 pad_mode='constant',
+                 bias='auto',
+                 norm=False,
+                 act=False,
+                 **kwargs):
+        super().__init__(
+            in_ch,
+            out_ch,
+            1,
+            pad_mode=pad_mode,
+            bias=bias,
+            norm=norm,
+            act=act,
+            **kwargs)
 
 
 class Conv3x3(BasicConv):
-    def __init__(self, in_ch, out_ch, pad_mode='constant', bias='auto', norm=False, act=False, **kwargs):
-        super().__init__(in_ch, out_ch, 3, pad_mode=pad_mode, bias=bias, norm=norm, act=act, **kwargs)
+    def __init__(self,
+                 in_ch,
+                 out_ch,
+                 pad_mode='constant',
+                 bias='auto',
+                 norm=False,
+                 act=False,
+                 **kwargs):
+        super().__init__(
+            in_ch,
+            out_ch,
+            3,
+            pad_mode=pad_mode,
+            bias=bias,
+            norm=norm,
+            act=act,
+            **kwargs)
 
 
 class Conv7x7(BasicConv):
-    def __init__(self, in_ch, out_ch, pad_mode='constant', bias='auto', norm=False, act=False, **kwargs):
-        super().__init__(in_ch, out_ch, 7, pad_mode=pad_mode, bias=bias, norm=norm, act=act, **kwargs)
+    def __init__(self,
+                 in_ch,
+                 out_ch,
+                 pad_mode='constant',
+                 bias='auto',
+                 norm=False,
+                 act=False,
+                 **kwargs):
+        super().__init__(
+            in_ch,
+            out_ch,
+            7,
+            pad_mode=pad_mode,
+            bias=bias,
+            norm=norm,
+            act=act,
+            **kwargs)
 
 
 class MaxPool2x2(nn.MaxPool2D):
     def __init__(self, **kwargs):
-        super().__init__(kernel_size=2, stride=(2,2), padding=(0,0), **kwargs)
+        super().__init__(kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
 
 
 class MaxUnPool2x2(nn.MaxUnPool2D):
     def __init__(self, **kwargs):
-        super().__init__(kernel_size=2, stride=(2,2), padding=(0,0), **kwargs)
+        super().__init__(kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
 
 
 class ConvTransposed3x3(nn.Layer):
-    def __init__(
-        self, in_ch, out_ch,
-        bias='auto', norm=False, act=False, 
-        **kwargs
-    ):
+    def __init__(self,
+                 in_ch,
+                 out_ch,
+                 bias='auto',
+                 norm=False,
+                 act=False,
+                 **kwargs):
         super().__init__()
         seq = []
         seq.append(
             nn.Conv2DTranspose(
-                in_ch, out_ch, 3,
-                stride=2, padding=1,
-                bias_attr=(False if norm else None) if bias=='auto' else bias,
-                **kwargs
-            )
-        )
+                in_ch,
+                out_ch,
+                3,
+                stride=2,
+                padding=1,
+                bias_attr=(False if norm else None) if bias == 'auto' else bias,
+                **kwargs))
         if norm:
             if norm is True:
                 norm = make_norm(out_ch)
@@ -135,8 +183,9 @@ class ConvTransposed3x3(nn.Layer):
 
 class Identity(nn.Layer):
     """A placeholder identity operator that accepts exactly one argument."""
+
     def __init__(self, *args, **kwargs):
         super().__init__()
-    
+
     def forward(self, x):
-        return x
+        return x

+ 2 - 2
paddlers/custom_models/cd/models/param_init.py

@@ -26,7 +26,7 @@ def normal_init(param, *args, **kwargs):
     Returns:
         The initialized parameters.
     """
-    
+
     return nn.initializer.Normal(*args, **kwargs)(param)
 
 
@@ -83,4 +83,4 @@ class KaimingInitMixin:
                 kaiming_normal_init(layer.weight)
             elif isinstance(layer, (nn.BatchNorm, nn.SyncBatchNorm)):
                 constant_init(layer.weight, value=1)
-                constant_init(layer.bias, value=0)
+                constant_init(layer.bias, value=0)

+ 51 - 31
paddlers/custom_models/cd/models/snunet.py

@@ -35,11 +35,11 @@ class SNUNet(nn.Layer, KaimingInitMixin):
         num_classes (int): The number of target classes.
         width (int, optional): The output channels of the first convolutional layer. Default: 32.
     """
-    
+
     def __init__(self, in_channels, num_classes, width=32):
         super().__init__()
 
-        filters = (width, width*2, width*4, width*8, width*16)
+        filters = (width, width * 2, width * 4, width * 8, width * 16)
 
         self.conv0_0 = ConvBlockNested(in_channels, filters[0], filters[0])
         self.conv1_0 = ConvBlockNested(filters[0], filters[1], filters[1])
@@ -55,30 +55,40 @@ class SNUNet(nn.Layer, KaimingInitMixin):
         self.up3_0 = Up(filters[3])
         self.up4_0 = Up(filters[4])
 
-        self.conv0_1 = ConvBlockNested(filters[0]*2+filters[1], filters[0], filters[0])
-        self.conv1_1 = ConvBlockNested(filters[1]*2+filters[2], filters[1], filters[1])
-        self.conv2_1 = ConvBlockNested(filters[2]*2+filters[3], filters[2], filters[2])
-        self.conv3_1 = ConvBlockNested(filters[3]*2+filters[4], filters[3], filters[3])
+        self.conv0_1 = ConvBlockNested(filters[0] * 2 + filters[1], filters[0],
+                                       filters[0])
+        self.conv1_1 = ConvBlockNested(filters[1] * 2 + filters[2], filters[1],
+                                       filters[1])
+        self.conv2_1 = ConvBlockNested(filters[2] * 2 + filters[3], filters[2],
+                                       filters[2])
+        self.conv3_1 = ConvBlockNested(filters[3] * 2 + filters[4], filters[3],
+                                       filters[3])
         self.up1_1 = Up(filters[1])
         self.up2_1 = Up(filters[2])
         self.up3_1 = Up(filters[3])
 
-        self.conv0_2 = ConvBlockNested(filters[0]*3+filters[1], filters[0], filters[0])
-        self.conv1_2 = ConvBlockNested(filters[1]*3+filters[2], filters[1], filters[1])
-        self.conv2_2 = ConvBlockNested(filters[2]*3+filters[3], filters[2], filters[2])
+        self.conv0_2 = ConvBlockNested(filters[0] * 3 + filters[1], filters[0],
+                                       filters[0])
+        self.conv1_2 = ConvBlockNested(filters[1] * 3 + filters[2], filters[1],
+                                       filters[1])
+        self.conv2_2 = ConvBlockNested(filters[2] * 3 + filters[3], filters[2],
+                                       filters[2])
         self.up1_2 = Up(filters[1])
         self.up2_2 = Up(filters[2])
 
-        self.conv0_3 = ConvBlockNested(filters[0]*4+filters[1], filters[0], filters[0])
-        self.conv1_3 = ConvBlockNested(filters[1]*4+filters[2], filters[1], filters[1])
+        self.conv0_3 = ConvBlockNested(filters[0] * 4 + filters[1], filters[0],
+                                       filters[0])
+        self.conv1_3 = ConvBlockNested(filters[1] * 4 + filters[2], filters[1],
+                                       filters[1])
         self.up1_3 = Up(filters[1])
 
-        self.conv0_4 = ConvBlockNested(filters[0]*5+filters[1], filters[0], filters[0])
+        self.conv0_4 = ConvBlockNested(filters[0] * 5 + filters[1], filters[0],
+                                       filters[0])
 
         self.ca_intra = ChannelAttention(filters[0], ratio=4)
-        self.ca_inter = ChannelAttention(filters[0]*4, ratio=16)
+        self.ca_inter = ChannelAttention(filters[0] * 4, ratio=16)
 
-        self.conv_out = Conv1x1(filters[0]*4, num_classes)
+        self.conv_out = Conv1x1(filters[0] * 4, num_classes)
 
         self.init_weight()
 
@@ -94,24 +104,35 @@ class SNUNet(nn.Layer, KaimingInitMixin):
         x3_0_t2 = self.conv3_0(self.down3(x2_0_t2))
         x4_0_t2 = self.conv4_0(self.down4(x3_0_t2))
 
-        x0_1 = self.conv0_1(paddle.concat([x0_0_t1, x0_0_t2, self.up1_0(x1_0_t2)], 1))
-        x1_1 = self.conv1_1(paddle.concat([x1_0_t1, x1_0_t2, self.up2_0(x2_0_t2)], 1))
-        x0_2 = self.conv0_2(paddle.concat([x0_0_t1, x0_0_t2, x0_1, self.up1_1(x1_1)], 1))
-
-        x2_1 = self.conv2_1(paddle.concat([x2_0_t1, x2_0_t2, self.up3_0(x3_0_t2)], 1))
-        x1_2 = self.conv1_2(paddle.concat([x1_0_t1, x1_0_t2, x1_1, self.up2_1(x2_1)], 1))
-        x0_3 = self.conv0_3(paddle.concat([x0_0_t1, x0_0_t2, x0_1, x0_2, self.up1_2(x1_2)], 1))
-
-        x3_1 = self.conv3_1(paddle.concat([x3_0_t1, x3_0_t2, self.up4_0(x4_0_t2)], 1))
-        x2_2 = self.conv2_2(paddle.concat([x2_0_t1, x2_0_t2, x2_1, self.up3_1(x3_1)], 1))
-        x1_3 = self.conv1_3(paddle.concat([x1_0_t1, x1_0_t2, x1_1, x1_2, self.up2_2(x2_2)], 1))
-        x0_4 = self.conv0_4(paddle.concat([x0_0_t1, x0_0_t2, x0_1, x0_2, x0_3, self.up1_3(x1_3)], 1))
+        x0_1 = self.conv0_1(
+            paddle.concat([x0_0_t1, x0_0_t2, self.up1_0(x1_0_t2)], 1))
+        x1_1 = self.conv1_1(
+            paddle.concat([x1_0_t1, x1_0_t2, self.up2_0(x2_0_t2)], 1))
+        x0_2 = self.conv0_2(
+            paddle.concat([x0_0_t1, x0_0_t2, x0_1, self.up1_1(x1_1)], 1))
+
+        x2_1 = self.conv2_1(
+            paddle.concat([x2_0_t1, x2_0_t2, self.up3_0(x3_0_t2)], 1))
+        x1_2 = self.conv1_2(
+            paddle.concat([x1_0_t1, x1_0_t2, x1_1, self.up2_1(x2_1)], 1))
+        x0_3 = self.conv0_3(
+            paddle.concat([x0_0_t1, x0_0_t2, x0_1, x0_2, self.up1_2(x1_2)], 1))
+
+        x3_1 = self.conv3_1(
+            paddle.concat([x3_0_t1, x3_0_t2, self.up4_0(x4_0_t2)], 1))
+        x2_2 = self.conv2_2(
+            paddle.concat([x2_0_t1, x2_0_t2, x2_1, self.up3_1(x3_1)], 1))
+        x1_3 = self.conv1_3(
+            paddle.concat([x1_0_t1, x1_0_t2, x1_1, x1_2, self.up2_2(x2_2)], 1))
+        x0_4 = self.conv0_4(
+            paddle.concat(
+                [x0_0_t1, x0_0_t2, x0_1, x0_2, x0_3, self.up1_3(x1_3)], 1))
 
         out = paddle.concat([x0_1, x0_2, x0_3, x0_4], 1)
 
         intra = paddle.sum(paddle.stack([x0_1, x0_2, x0_3, x0_4]), axis=0)
         m_intra = self.ca_intra(intra)
-        out = self.ca_inter(out) * (out + paddle.tile(m_intra, (1,4,1,1)))
+        out = self.ca_inter(out) * (out + paddle.tile(m_intra, (1, 4, 1, 1)))
 
         pred = self.conv_out(out)
         return [pred]
@@ -144,10 +165,9 @@ class Up(nn.Layer):
         if use_conv:
             self.up = nn.Conv2DTranspose(in_ch, in_ch, 2, stride=2)
         else:
-            self.up = nn.Upsample(scale_factor=2,
-                                  mode='bilinear',
-                                  align_corners=True)
+            self.up = nn.Upsample(
+                scale_factor=2, mode='bilinear', align_corners=True)
 
     def forward(self, x):
         x = self.up(x)
-        return x
+        return x

+ 62 - 54
paddlers/custom_models/cd/models/stanet.py

@@ -44,24 +44,19 @@ class STANet(nn.Layer):
     Raises:
         ValueError: When `att_type` has an illeagal value (unsupported attention type).
     """
-    
-    def __init__(
-        self, 
-        in_channels, 
-        num_classes, 
-        att_type='BAM', 
-        ds_factor=1
-    ):
+
+    def __init__(self, in_channels, num_classes, att_type='BAM', ds_factor=1):
         super().__init__()
 
         WIDTH = 64
 
         self.extract = build_feat_extractor(in_ch=in_channels, width=WIDTH)
-        self.attend = build_sta_module(in_ch=WIDTH, att_type=att_type, ds=ds_factor)
+        self.attend = build_sta_module(
+            in_ch=WIDTH, att_type=att_type, ds=ds_factor)
         self.conv_out = nn.Sequential(
-            Conv3x3(WIDTH, WIDTH, norm=True, act=True),
-            Conv3x3(WIDTH, num_classes)
-        )
+            Conv3x3(
+                WIDTH, WIDTH, norm=True, act=True),
+            Conv3x3(WIDTH, num_classes))
 
         self.init_weight()
 
@@ -71,8 +66,9 @@ class STANet(nn.Layer):
 
         f1, f2 = self.attend(f1, f2)
 
-        y = paddle.abs(f1- f2)
-        y = F.interpolate(y, size=paddle.shape(t1)[2:], mode='bilinear', align_corners=True)
+        y = paddle.abs(f1 - f2)
+        y = F.interpolate(
+            y, size=paddle.shape(t1)[2:], mode='bilinear', align_corners=True)
 
         pred = self.conv_out(y)
         return [pred]
@@ -84,10 +80,7 @@ class STANet(nn.Layer):
 
 
 def build_feat_extractor(in_ch, width):
-    return nn.Sequential(
-        Backbone(in_ch, 'resnet18'),
-        Decoder(width)
-    )
+    return nn.Sequential(Backbone(in_ch, 'resnet18'), Decoder(width))
 
 
 def build_sta_module(in_ch, att_type, ds):
@@ -100,15 +93,24 @@ def build_sta_module(in_ch, att_type, ds):
 
 
 class Backbone(nn.Layer, KaimingInitMixin):
-    def __init__(self, in_ch, arch, pretrained=True, strides=(2,1,2,2,2)):
+    def __init__(self, in_ch, arch, pretrained=True, strides=(2, 1, 2, 2, 2)):
         super().__init__()
 
         if arch == 'resnet18':
-            self.resnet = resnet.resnet18(pretrained=pretrained, strides=strides, norm_layer=get_norm_layer())
+            self.resnet = resnet.resnet18(
+                pretrained=pretrained,
+                strides=strides,
+                norm_layer=get_norm_layer())
         elif arch == 'resnet34':
-            self.resnet = resnet.resnet34(pretrained=pretrained, strides=strides, norm_layer=get_norm_layer())
+            self.resnet = resnet.resnet34(
+                pretrained=pretrained,
+                strides=strides,
+                norm_layer=get_norm_layer())
         elif arch == 'resnet50':
-            self.resnet = resnet.resnet50(pretrained=pretrained, strides=strides, norm_layer=get_norm_layer())
+            self.resnet = resnet.resnet50(
+                pretrained=pretrained,
+                strides=strides,
+                norm_layer=get_norm_layer())
         else:
             raise ValueError
 
@@ -116,13 +118,12 @@ class Backbone(nn.Layer, KaimingInitMixin):
 
         if in_ch != 3:
             self.resnet.conv1 = nn.Conv2D(
-                in_ch, 
+                in_ch,
                 64,
                 kernel_size=7,
                 stride=strides[0],
                 padding=3,
-                bias_attr=False
-            )
+                bias_attr=False)
 
         if not pretrained:
             self.init_weight()
@@ -153,10 +154,11 @@ class Decoder(nn.Layer, KaimingInitMixin):
         self.dr3 = Conv1x1(256, 96, norm=True, act=True)
         self.dr4 = Conv1x1(512, 96, norm=True, act=True)
         self.conv_out = nn.Sequential(
-            Conv3x3(384, 256, norm=True, act=True),
+            Conv3x3(
+                384, 256, norm=True, act=True),
             nn.Dropout(0.5),
-            Conv1x1(256, f_ch, norm=True, act=True)
-        )
+            Conv1x1(
+                256, f_ch, norm=True, act=True))
 
         self.init_weight()
 
@@ -166,9 +168,12 @@ class Decoder(nn.Layer, KaimingInitMixin):
         f3 = self.dr3(feats[2])
         f4 = self.dr4(feats[3])
 
-        f2 = F.interpolate(f2, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
-        f3 = F.interpolate(f3, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
-        f4 = F.interpolate(f4, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
+        f2 = F.interpolate(
+            f2, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
+        f3 = F.interpolate(
+            f3, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
+        f4 = F.interpolate(
+            f4, size=paddle.shape(f1)[2:], mode='bilinear', align_corners=True)
 
         x = paddle.concat([f1, f2, f3, f4], axis=1)
         y = self.conv_out(x)
@@ -194,23 +199,23 @@ class BAM(nn.Layer):
     def forward(self, x):
         x = x.flatten(-2)
         x_rs = self.pool(x)
-        
+
         b, c, h, w = paddle.shape(x_rs)
-        query = self.conv_q(x_rs).reshape((b,-1,h*w)).transpose((0,2,1))
-        key = self.conv_k(x_rs).reshape((b,-1,h*w))
+        query = self.conv_q(x_rs).reshape((b, -1, h * w)).transpose((0, 2, 1))
+        key = self.conv_k(x_rs).reshape((b, -1, h * w))
         energy = paddle.bmm(query, key)
         energy = (self.key_ch**(-0.5)) * energy
 
         attention = self.softmax(energy)
 
-        value = self.conv_v(x_rs).reshape((b,-1,w*h))
+        value = self.conv_v(x_rs).reshape((b, -1, w * h))
 
-        out = paddle.bmm(value, attention.transpose((0,2,1)))
-        out = out.reshape((b,c,h,w))
+        out = paddle.bmm(value, attention.transpose((0, 2, 1)))
+        out = out.reshape((b, c, h, w))
 
         out = F.interpolate(out, scale_factor=self.ds)
         out = out + x
-        return out.reshape(out.shape[:-1]+[out.shape[-1]//2, 2])
+        return out.reshape(out.shape[:-1] + [out.shape[-1] // 2, 2])
 
 
 class PAMBlock(nn.Layer):
@@ -234,13 +239,13 @@ class PAMBlock(nn.Layer):
         query = self.conv_q(x_rs)
         key = self.conv_k(x_rs)
         value = self.conv_v(x_rs)
-        
+
         # Split the whole image into subregions.
         b, c, h, w = paddle.shape(x_rs)
         query = self._split_subregions(query)
         key = self._split_subregions(key)
         value = self._split_subregions(value)
-        
+
         # Perform subregion-wise attention.
         out = self._attend(query, key, value)
 
@@ -250,40 +255,43 @@ class PAMBlock(nn.Layer):
         return out
 
     def _attend(self, query, key, value):
-        energy = paddle.bmm(query.transpose((0,2,1)), key)  # batch matrix multiplication
+        energy = paddle.bmm(query.transpose((0, 2, 1)),
+                            key)  # batch matrix multiplication
         energy = (self.key_ch**(-0.5)) * energy
         attention = F.softmax(energy, axis=-1)
-        out = paddle.bmm(value, attention.transpose((0,2,1)))
+        out = paddle.bmm(value, attention.transpose((0, 2, 1)))
         return out
 
     def _split_subregions(self, x):
         b, c, h, w = paddle.shape(x)
         assert h % self.scale == 0 and w % self.scale == 0
-        x = x.reshape((b, c, self.scale, h//self.scale, self.scale, w//self.scale))
-        x = x.transpose((0,2,4,1,3,5)).reshape((b*self.scale*self.scale, c, -1))
+        x = x.reshape(
+            (b, c, self.scale, h // self.scale, self.scale, w // self.scale))
+        x = x.transpose((0, 2, 4, 1, 3, 5)).reshape(
+            (b * self.scale * self.scale, c, -1))
         return x
 
     def _recons_whole(self, x, b, c, h, w):
-        x = x.reshape((b, self.scale, self.scale, c, h//self.scale, w//self.scale))
-        x = x.transpose((0,3,1,4,2,5)).reshape((b, c, h, w))
+        x = x.reshape(
+            (b, self.scale, self.scale, c, h // self.scale, w // self.scale))
+        x = x.transpose((0, 3, 1, 4, 2, 5)).reshape((b, c, h, w))
         return x
 
 
 class PAM(nn.Layer):
-    def __init__(self, in_ch, ds, scales=(1,2,4,8)):
+    def __init__(self, in_ch, ds, scales=(1, 2, 4, 8)):
         super().__init__()
 
-        self.stages = nn.LayerList([
-            PAMBlock(in_ch, scale=s, ds=ds)
-            for s in scales
-        ])
-        self.conv_out = Conv1x1(in_ch*len(scales), in_ch, bias=False)
+        self.stages = nn.LayerList(
+            [PAMBlock(
+                in_ch, scale=s, ds=ds) for s in scales])
+        self.conv_out = Conv1x1(in_ch * len(scales), in_ch, bias=False)
 
     def forward(self, x):
         x = x.flatten(-2)
         res = [stage(x) for stage in self.stages]
         out = self.conv_out(paddle.concat(res, axis=1))
-        return out.reshape(out.shape[:-1]+[out.shape[-1]//2, 2])
+        return out.reshape(out.shape[:-1] + [out.shape[-1] // 2, 2])
 
 
 class Attention(nn.Layer):
@@ -294,4 +302,4 @@ class Attention(nn.Layer):
     def forward(self, x1, x2):
         x = paddle.stack([x1, x2], axis=-1)
         y = self.att(x)
-        return y[...,0], y[...,1]
+        return y[..., 0], y[..., 1]

+ 10 - 31
paddlers/custom_models/cd/models/unet_ef.py

@@ -35,12 +35,7 @@ class UNetEarlyFusion(nn.Layer):
             on a relatively small dataset, the dropout layers help prevent overfitting. Default: False.
     """
 
-    def __init__(
-        self, 
-        in_channels, 
-        num_classes, 
-        use_dropout=False
-    ):
+    def __init__(self, in_channels, num_classes, use_dropout=False):
         super().__init__()
 
         C1, C2, C3, C4, C5 = 16, 32, 64, 128, 256
@@ -135,12 +130,8 @@ class UNetEarlyFusion(nn.Layer):
 
         # Stage 4d
         x4d = self.upconv4(x4p)
-        pad4 = (
-            0, 
-            paddle.shape(x43)[3]-paddle.shape(x4d)[3], 
-            0, 
-            paddle.shape(x43)[2]-paddle.shape(x4d)[2]
-        )
+        pad4 = (0, paddle.shape(x43)[3] - paddle.shape(x4d)[3], 0,
+                paddle.shape(x43)[2] - paddle.shape(x4d)[2])
         x4d = paddle.concat([F.pad(x4d, pad=pad4, mode='replicate'), x43], 1)
         x43d = self.do43d(self.conv43d(x4d))
         x42d = self.do42d(self.conv42d(x43d))
@@ -148,12 +139,8 @@ class UNetEarlyFusion(nn.Layer):
 
         # Stage 3d
         x3d = self.upconv3(x41d)
-        pad3 = (
-            0, 
-            paddle.shape(x33)[3]-paddle.shape(x3d)[3], 
-            0, 
-            paddle.shape(x33)[2]-paddle.shape(x3d)[2]
-        )
+        pad3 = (0, paddle.shape(x33)[3] - paddle.shape(x3d)[3], 0,
+                paddle.shape(x33)[2] - paddle.shape(x3d)[2])
         x3d = paddle.concat([F.pad(x3d, pad=pad3, mode='replicate'), x33], 1)
         x33d = self.do33d(self.conv33d(x3d))
         x32d = self.do32d(self.conv32d(x33d))
@@ -161,24 +148,16 @@ class UNetEarlyFusion(nn.Layer):
 
         # Stage 2d
         x2d = self.upconv2(x31d)
-        pad2 = (
-            0, 
-            paddle.shape(x22)[3]-paddle.shape(x2d)[3], 
-            0, 
-            paddle.shape(x22)[2]-paddle.shape(x2d)[2]
-        )
+        pad2 = (0, paddle.shape(x22)[3] - paddle.shape(x2d)[3], 0,
+                paddle.shape(x22)[2] - paddle.shape(x2d)[2])
         x2d = paddle.concat([F.pad(x2d, pad=pad2, mode='replicate'), x22], 1)
         x22d = self.do22d(self.conv22d(x2d))
         x21d = self.do21d(self.conv21d(x22d))
 
         # Stage 1d
         x1d = self.upconv1(x21d)
-        pad1 = (
-            0, 
-            paddle.shape(x12)[3]-paddle.shape(x1d)[3], 
-            0, 
-            paddle.shape(x12)[2]-paddle.shape(x1d)[2]
-        )
+        pad1 = (0, paddle.shape(x12)[3] - paddle.shape(x1d)[3], 0,
+                paddle.shape(x12)[2] - paddle.shape(x1d)[2])
         x1d = paddle.concat([F.pad(x1d, pad=pad1, mode='replicate'), x12], 1)
         x12d = self.do12d(self.conv12d(x1d))
         x11d = self.conv11d(x12d)
@@ -197,4 +176,4 @@ class UNetEarlyFusion(nn.Layer):
         if self.use_dropout:
             return nn.Dropout2D(p=0.2)
         else:
-            return Identity()
+            return Identity()

+ 23 - 40
paddlers/custom_models/cd/models/unet_siamconc.py

@@ -34,13 +34,8 @@ class UNetSiamConc(nn.Layer):
         use_dropout (bool, optional): A bool value that indicates whether to use dropout layers. When the model is trained 
             on a relatively small dataset, the dropout layers help prevent overfitting. Default: False.
     """
-    
-    def __init__(
-        self, 
-        in_channels, 
-        num_classes, 
-        use_dropout=False
-    ):
+
+    def __init__(self, in_channels, num_classes, use_dropout=False):
         super().__init__()
 
         C1, C2, C3, C4, C5 = 16, 32, 64, 128, 256
@@ -77,7 +72,7 @@ class UNetSiamConc(nn.Layer):
 
         self.upconv4 = ConvTransposed3x3(C4, C4, output_padding=1)
 
-        self.conv43d = Conv3x3(C5+C4, C4, norm=True, act=True)
+        self.conv43d = Conv3x3(C5 + C4, C4, norm=True, act=True)
         self.do43d = self._make_dropout()
         self.conv42d = Conv3x3(C4, C4, norm=True, act=True)
         self.do42d = self._make_dropout()
@@ -86,7 +81,7 @@ class UNetSiamConc(nn.Layer):
 
         self.upconv3 = ConvTransposed3x3(C3, C3, output_padding=1)
 
-        self.conv33d = Conv3x3(C4+C3, C3, norm=True, act=True)
+        self.conv33d = Conv3x3(C4 + C3, C3, norm=True, act=True)
         self.do33d = self._make_dropout()
         self.conv32d = Conv3x3(C3, C3, norm=True, act=True)
         self.do32d = self._make_dropout()
@@ -95,14 +90,14 @@ class UNetSiamConc(nn.Layer):
 
         self.upconv2 = ConvTransposed3x3(C2, C2, output_padding=1)
 
-        self.conv22d = Conv3x3(C3+C2, C2, norm=True, act=True)
+        self.conv22d = Conv3x3(C3 + C2, C2, norm=True, act=True)
         self.do22d = self._make_dropout()
         self.conv21d = Conv3x3(C2, C1, norm=True, act=True)
         self.do21d = self._make_dropout()
 
         self.upconv1 = ConvTransposed3x3(C1, C1, output_padding=1)
 
-        self.conv12d = Conv3x3(C2+C1, C1, norm=True, act=True)
+        self.conv12d = Conv3x3(C2 + C1, C1, norm=True, act=True)
         self.do12d = self._make_dropout()
         self.conv11d = Conv3x3(C1, num_classes)
 
@@ -158,51 +153,39 @@ class UNetSiamConc(nn.Layer):
         # Decode
         # Stage 4d
         x4d = self.upconv4(x4p)
-        pad4 = (
-            0, 
-            paddle.shape(x43_1)[3]-paddle.shape(x4d)[3], 
-            0, 
-            paddle.shape(x43_1)[2]-paddle.shape(x4d)[2]
-        )
-        x4d = paddle.concat([F.pad(x4d, pad=pad4, mode='replicate'), x43_1, x43_2], 1)
+        pad4 = (0, paddle.shape(x43_1)[3] - paddle.shape(x4d)[3], 0,
+                paddle.shape(x43_1)[2] - paddle.shape(x4d)[2])
+        x4d = paddle.concat(
+            [F.pad(x4d, pad=pad4, mode='replicate'), x43_1, x43_2], 1)
         x43d = self.do43d(self.conv43d(x4d))
         x42d = self.do42d(self.conv42d(x43d))
         x41d = self.do41d(self.conv41d(x42d))
 
         # Stage 3d
         x3d = self.upconv3(x41d)
-        pad3 = (
-            0, 
-            paddle.shape(x33_1)[3]-paddle.shape(x3d)[3], 
-            0, 
-            paddle.shape(x33_1)[2]-paddle.shape(x3d)[2]
-        )
-        x3d = paddle.concat([F.pad(x3d, pad=pad3, mode='replicate'), x33_1, x33_2], 1)
+        pad3 = (0, paddle.shape(x33_1)[3] - paddle.shape(x3d)[3], 0,
+                paddle.shape(x33_1)[2] - paddle.shape(x3d)[2])
+        x3d = paddle.concat(
+            [F.pad(x3d, pad=pad3, mode='replicate'), x33_1, x33_2], 1)
         x33d = self.do33d(self.conv33d(x3d))
         x32d = self.do32d(self.conv32d(x33d))
         x31d = self.do31d(self.conv31d(x32d))
 
         # Stage 2d
         x2d = self.upconv2(x31d)
-        pad2 = (
-            0, 
-            paddle.shape(x22_1)[3]-paddle.shape(x2d)[3], 
-            0, 
-            paddle.shape(x22_1)[2]-paddle.shape(x2d)[2]
-        )
-        x2d = paddle.concat([F.pad(x2d, pad=pad2, mode='replicate'), x22_1, x22_2], 1)
+        pad2 = (0, paddle.shape(x22_1)[3] - paddle.shape(x2d)[3], 0,
+                paddle.shape(x22_1)[2] - paddle.shape(x2d)[2])
+        x2d = paddle.concat(
+            [F.pad(x2d, pad=pad2, mode='replicate'), x22_1, x22_2], 1)
         x22d = self.do22d(self.conv22d(x2d))
         x21d = self.do21d(self.conv21d(x22d))
 
         # Stage 1d
         x1d = self.upconv1(x21d)
-        pad1 = (
-            0, 
-            paddle.shape(x12_1)[3]-paddle.shape(x1d)[3], 
-            0, 
-            paddle.shape(x12_1)[2]-paddle.shape(x1d)[2]
-        )
-        x1d = paddle.concat([F.pad(x1d, pad=pad1, mode='replicate'), x12_1, x12_2], 1)
+        pad1 = (0, paddle.shape(x12_1)[3] - paddle.shape(x1d)[3], 0,
+                paddle.shape(x12_1)[2] - paddle.shape(x1d)[2])
+        x1d = paddle.concat(
+            [F.pad(x1d, pad=pad1, mode='replicate'), x12_1, x12_2], 1)
         x12d = self.do12d(self.conv12d(x1d))
         x11d = self.conv11d(x12d)
 
@@ -220,4 +203,4 @@ class UNetSiamConc(nn.Layer):
         if self.use_dropout:
             return nn.Dropout2D(p=0.2)
         else:
-            return Identity()
+            return Identity()

+ 35 - 36
paddlers/custom_models/cd/models/unet_siamdiff.py

@@ -34,13 +34,8 @@ class UNetSiamDiff(nn.Layer):
         use_dropout (bool, optional): A bool value that indicates whether to use dropout layers. When the model is trained 
             on a relatively small dataset, the dropout layers help prevent overfitting. Default: False.
     """
-    
-    def __init__(
-        self, 
-        in_channels, 
-        num_classes, 
-        use_dropout=False
-    ):
+
+    def __init__(self, in_channels, num_classes, use_dropout=False):
         super().__init__()
 
         C1, C2, C3, C4, C5 = 16, 32, 64, 128, 256
@@ -158,51 +153,55 @@ class UNetSiamDiff(nn.Layer):
         # Decode
         # Stage 4d
         x4d = self.upconv4(x4p)
-        pad4 = (
-            0, 
-            paddle.shape(x43_1)[3]-paddle.shape(x4d)[3], 
-            0, 
-            paddle.shape(x43_1)[2]-paddle.shape(x4d)[2]
-        )
-        x4d = paddle.concat([F.pad(x4d, pad=pad4, mode='replicate'), paddle.abs(x43_1-x43_2)], 1)
+        pad4 = (0, paddle.shape(x43_1)[3] - paddle.shape(x4d)[3], 0,
+                paddle.shape(x43_1)[2] - paddle.shape(x4d)[2])
+        x4d = paddle.concat(
+            [
+                F.pad(x4d, pad=pad4, mode='replicate'),
+                paddle.abs(x43_1 - x43_2)
+            ],
+            1)
         x43d = self.do43d(self.conv43d(x4d))
         x42d = self.do42d(self.conv42d(x43d))
         x41d = self.do41d(self.conv41d(x42d))
 
         # Stage 3d
         x3d = self.upconv3(x41d)
-        pad3 = (
-            0, 
-            paddle.shape(x33_1)[3]-paddle.shape(x3d)[3], 
-            0, 
-            paddle.shape(x33_1)[2]-paddle.shape(x3d)[2]
-        )
-        x3d = paddle.concat([F.pad(x3d, pad=pad3, mode='replicate'), paddle.abs(x33_1-x33_2)], 1)
+        pad3 = (0, paddle.shape(x33_1)[3] - paddle.shape(x3d)[3], 0,
+                paddle.shape(x33_1)[2] - paddle.shape(x3d)[2])
+        x3d = paddle.concat(
+            [
+                F.pad(x3d, pad=pad3, mode='replicate'),
+                paddle.abs(x33_1 - x33_2)
+            ],
+            1)
         x33d = self.do33d(self.conv33d(x3d))
         x32d = self.do32d(self.conv32d(x33d))
         x31d = self.do31d(self.conv31d(x32d))
 
         # Stage 2d
         x2d = self.upconv2(x31d)
-        pad2 = (
-            0, 
-            paddle.shape(x22_1)[3]-paddle.shape(x2d)[3], 
-            0, 
-            paddle.shape(x22_1)[2]-paddle.shape(x2d)[2]
-        )
-        x2d = paddle.concat([F.pad(x2d, pad=pad2, mode='replicate'), paddle.abs(x22_1-x22_2)], 1)
+        pad2 = (0, paddle.shape(x22_1)[3] - paddle.shape(x2d)[3], 0,
+                paddle.shape(x22_1)[2] - paddle.shape(x2d)[2])
+        x2d = paddle.concat(
+            [
+                F.pad(x2d, pad=pad2, mode='replicate'),
+                paddle.abs(x22_1 - x22_2)
+            ],
+            1)
         x22d = self.do22d(self.conv22d(x2d))
         x21d = self.do21d(self.conv21d(x22d))
 
         # Stage 1d
         x1d = self.upconv1(x21d)
-        pad1 = (
-            0, 
-            paddle.shape(x12_1)[3]-paddle.shape(x1d)[3], 
-            0, 
-            paddle.shape(x12_1)[2]-paddle.shape(x1d)[2]
-        )
-        x1d = paddle.concat([F.pad(x1d, pad=pad1, mode='replicate'), paddle.abs(x12_1-x12_2)], 1)
+        pad1 = (0, paddle.shape(x12_1)[3] - paddle.shape(x1d)[3], 0,
+                paddle.shape(x12_1)[2] - paddle.shape(x1d)[2])
+        x1d = paddle.concat(
+            [
+                F.pad(x1d, pad=pad1, mode='replicate'),
+                paddle.abs(x12_1 - x12_2)
+            ],
+            1)
         x12d = self.do12d(self.conv12d(x1d))
         x11d = self.conv11d(x12d)
 
@@ -220,4 +219,4 @@ class UNetSiamDiff(nn.Layer):
         if self.use_dropout:
             return nn.Dropout2D(p=0.2)
         else:
-            return Identity()
+            return Identity()

+ 1 - 1
paddlers/custom_models/cls/__init__.py

@@ -10,4 +10,4 @@
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
-# limitations under the License.
+# limitations under the License.

+ 1 - 1
paddlers/custom_models/det/__init__.py

@@ -10,4 +10,4 @@
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
-# limitations under the License.
+# limitations under the License.

+ 1 - 1
paddlers/custom_models/gan/__init__.py

@@ -10,4 +10,4 @@
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
-# limitations under the License.
+# limitations under the License.

+ 1 - 1
paddlers/custom_models/seg/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import models
+from . import models

+ 1 - 1
paddlers/custom_models/seg/models/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from .farseg import FarSeg
+from .farseg import FarSeg

+ 1 - 1
paddlers/custom_models/seg/models/farseg/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from .farseg import FarSeg
+from .farseg import FarSeg

+ 55 - 36
paddlers/custom_models/seg/models/farseg/farseg.py

@@ -29,44 +29,51 @@ class SceneRelation(nn.Layer):
         super(SceneRelation, self).__init__()
         self.scale_aware_proj = scale_aware_proj
         if scale_aware_proj:
-            self.scene_encoder = nn.LayerList([nn.Sequential(
-                nn.Conv2D(in_channels, out_channels, 1),
-                nn.ReLU(),
-                nn.Conv2D(out_channels, out_channels, 1)) for _ in range(len(channel_list))
+            self.scene_encoder = nn.LayerList([
+                nn.Sequential(
+                    nn.Conv2D(in_channels, out_channels, 1),
+                    nn.ReLU(), nn.Conv2D(out_channels, out_channels, 1))
+                for _ in range(len(channel_list))
             ])
         else:
             # 2mlp
             self.scene_encoder = nn.Sequential(
                 nn.Conv2D(in_channels, out_channels, 1),
                 nn.ReLU(),
-                nn.Conv2D(out_channels, out_channels, 1),
-            )
+                nn.Conv2D(out_channels, out_channels, 1), )
         self.content_encoders = nn.LayerList()
         self.feature_reencoders = nn.LayerList()
         for c in channel_list:
-            self.content_encoders.append(nn.Sequential(
-                nn.Conv2D(c, out_channels, 1),
-                nn.BatchNorm2D(out_channels),
-                nn.ReLU()
-            ))
-            self.feature_reencoders.append(nn.Sequential(
-                nn.Conv2D(c, out_channels, 1),
-                nn.BatchNorm2D(out_channels),
-                nn.ReLU()
-            ))
+            self.content_encoders.append(
+                nn.Sequential(
+                    nn.Conv2D(c, out_channels, 1),
+                    nn.BatchNorm2D(out_channels), nn.ReLU()))
+            self.feature_reencoders.append(
+                nn.Sequential(
+                    nn.Conv2D(c, out_channels, 1),
+                    nn.BatchNorm2D(out_channels), nn.ReLU()))
         self.normalizer = nn.Sigmoid()
 
     def forward(self, scene_feature, features: list):
-        content_feats = [c_en(p_feat) for c_en, p_feat in zip(self.content_encoders, features)]
+        content_feats = [
+            c_en(p_feat)
+            for c_en, p_feat in zip(self.content_encoders, features)
+        ]
         if self.scale_aware_proj:
             scene_feats = [op(scene_feature) for op in self.scene_encoder]
-            relations = [self.normalizer((sf * cf).sum(axis=1, keepdim=True)) 
-                         for sf, cf in zip(scene_feats, content_feats)]
+            relations = [
+                self.normalizer((sf * cf).sum(axis=1, keepdim=True))
+                for sf, cf in zip(scene_feats, content_feats)
+            ]
         else:
             scene_feat = self.scene_encoder(scene_feature)
-            relations = [self.normalizer((scene_feat * cf).sum(axis=1, keepdim=True)) 
-                         for cf in content_feats]
-        p_feats = [op(p_feat) for op, p_feat in zip(self.feature_reencoders, features)]
+            relations = [
+                self.normalizer((scene_feat * cf).sum(axis=1, keepdim=True))
+                for cf in content_feats
+            ]
+        p_feats = [
+            op(p_feat) for op, p_feat in zip(self.feature_reencoders, features)
+        ]
         refined_feats = [r * p for r, p in zip(relations, p_feats)]
         return refined_feats
 
@@ -84,22 +91,33 @@ class AssymetricDecoder(nn.Layer):
             norm_fn_args = dict(num_features=out_channels)
         elif norm_fn == nn.GroupNorm:
             if num_groups_gn is None:
-                raise ValueError('When norm_fn is nn.GroupNorm, num_groups_gn is needed.')
-            norm_fn_args = dict(num_groups=num_groups_gn, num_channels=out_channels)
+                raise ValueError(
+                    'When norm_fn is nn.GroupNorm, num_groups_gn is needed.')
+            norm_fn_args = dict(
+                num_groups=num_groups_gn, num_channels=out_channels)
         else:
             raise ValueError('Type of {} is not support.'.format(type(norm_fn)))
         self.blocks = nn.LayerList()
         for in_feat_os in in_feat_output_strides:
-            num_upsample = int(math.log2(int(in_feat_os))) - int(math.log2(int(out_feat_output_stride)))
+            num_upsample = int(math.log2(int(in_feat_os))) - int(
+                math.log2(int(out_feat_output_stride)))
             num_layers = num_upsample if num_upsample != 0 else 1
-            self.blocks.append(nn.Sequential(*[
-                nn.Sequential(
-                    nn.Conv2D(in_channels if idx == 0 else out_channels, out_channels, 3, 1, 1, bias_attr=False),
-                    norm_fn(**norm_fn_args) if norm_fn is not None else Identity(),
-                    nn.ReLU(),
-                    nn.UpsamplingBilinear2D(scale_factor=2) if num_upsample != 0 else Identity(),
-                ) for idx in range(num_layers)
-            ]))
+            self.blocks.append(
+                nn.Sequential(*[
+                    nn.Sequential(
+                        nn.Conv2D(
+                            in_channels if idx == 0 else out_channels,
+                            out_channels,
+                            3,
+                            1,
+                            1,
+                            bias_attr=False),
+                        norm_fn(**norm_fn_args)
+                        if norm_fn is not None else Identity(),
+                        nn.ReLU(),
+                        nn.UpsamplingBilinear2D(scale_factor=2) if num_upsample
+                        != 0 else Identity(), ) for idx in range(num_layers)
+                ]))
 
     def forward(self, feat_list: list):
         inner_feat_list = []
@@ -136,6 +154,7 @@ class FarSeg(nn.Layer):
         Zheng, Zhuo, et al. "Foreground-Aware Relation Network for Geospatial Object Segmentation in High Spatial Resolution Remote Sensing Imagery"
         (https://openaccess.thecvf.com/content_CVPR_2020/papers/Zheng_Foreground-Aware_Relation_Network_for_Geospatial_Object_Segmentation_in_High_Spatial_CVPR_2020_paper.pdf)
     '''
+
     def __init__(self,
                  num_classes=16,
                  fpn_ch_list=(256, 512, 1024, 2048),
@@ -145,9 +164,9 @@ class FarSeg(nn.Layer):
                  encoder_pretrained=True):
         super(FarSeg, self).__init__()
         self.en = ResNet50Encoder(encoder_pretrained)
-        self.fpn = FPN(in_channels_list=fpn_ch_list,
-                       out_channels=mid_ch)
-        self.decoder = AssymetricDecoder(in_channels=mid_ch, out_channels=out_ch)
+        self.fpn = FPN(in_channels_list=fpn_ch_list, out_channels=mid_ch)
+        self.decoder = AssymetricDecoder(
+            in_channels=mid_ch, out_channels=out_ch)
         self.cls_pred_conv = nn.Conv2D(out_ch, num_classes, 1)
         self.upsample4x_op = nn.UpsamplingBilinear2D(scale_factor=4)
         self.scene_relation = True if sr_ch_list is not None else False

+ 8 - 7
paddlers/custom_models/seg/models/farseg/fpn.py

@@ -14,9 +14,7 @@
 
 from paddle import nn
 import paddle.nn.functional as F
-from ..utils import (
-    ConvReLU, kaiming_normal_init, constant_init
-)
+from ..utils import (ConvReLU, kaiming_normal_init, constant_init)
 
 
 class FPN(nn.Layer):
@@ -25,12 +23,12 @@ class FPN(nn.Layer):
     The feature maps are currently supposed to be in increasing depth
     order, and must be consecutive
     """
+
     def __init__(self,
                  in_channels_list,
                  out_channels,
                  conv_block=ConvReLU,
-                 top_blocks=None
-                 ):
+                 top_blocks=None):
         super(FPN, self).__init__()
         self.inner_blocks = []
         self.layer_blocks = []
@@ -55,10 +53,12 @@ class FPN(nn.Layer):
         last_inner = getattr(self, self.inner_blocks[-1])(x[-1])
         results = [getattr(self, self.layer_blocks[-1])(last_inner)]
         for feature, inner_block, layer_block in zip(
-                x[:-1][::-1], self.inner_blocks[:-1][::-1], self.layer_blocks[:-1][::-1]):
+                x[:-1][::-1], self.inner_blocks[:-1][::-1],
+                self.layer_blocks[:-1][::-1]):
             if not inner_block:
                 continue
-            inner_top_down = F.interpolate(last_inner, scale_factor=2, mode="nearest")
+            inner_top_down = F.interpolate(
+                last_inner, scale_factor=2, mode="nearest")
             inner_lateral = getattr(self, inner_block)(feature)
             last_inner = inner_lateral + inner_top_down
             results.insert(0, getattr(self, layer_block)(last_inner))
@@ -80,6 +80,7 @@ class LastLevelP6P7(nn.Layer):
     """
     This module is used in RetinaNet to generate extra layers, P6 and P7.
     """
+
     def __init__(self, in_channels, out_channels):
         super(LastLevelP6P7, self).__init__()
         self.p6 = nn.Conv2D(in_channels, out_channels, 3, 2, 1)

+ 1 - 1
paddlers/custom_models/seg/models/utils/__init__.py

@@ -14,4 +14,4 @@
 
 from .torch_nn import *
 from .param_init import *
-from .layers_lib import *
+from .layers_lib import *

+ 4 - 3
paddlers/custom_models/seg/models/utils/layers_lib.py

@@ -117,6 +117,7 @@ class Activation(nn.Layer):
         # 'tanh', 'hardtanh', 'prelu', 'relu', 'relu6', 'selu', 'leakyrelu', 'sigmoid', 'softmax',
         # 'softplus', 'softshrink', 'softsign', 'tanhshrink', 'logsigmoid', 'logsoftmax', 'hsigmoid'])"
     """
+
     def __init__(self, act=None):
         super(Activation, self).__init__()
         self._act = act
@@ -126,8 +127,8 @@ class Activation(nn.Layer):
         if act is not None:
             if act in act_dict.keys():
                 act_name = act_dict[act]
-                self.act_func = eval(
-                    "nn.layer.activation.{}()".format(act_name))
+                self.act_func = eval("nn.layer.activation.{}()".format(
+                    act_name))
             else:
                 raise KeyError("{} does not exist in the current {}".format(
                     act, act_dict.keys()))
@@ -136,4 +137,4 @@ class Activation(nn.Layer):
         if self._act is not None:
             return self.act_func(x)
         else:
-            return x
+            return x

+ 1 - 1
paddlers/custom_models/seg/models/utils/param_init.py

@@ -27,4 +27,4 @@ def normal_init(param, **kwargs):
 
 def kaiming_normal_init(param, **kwargs):
     initializer = nn.initializer.KaimingNormal(**kwargs)
-    initializer(param, param.block)
+    initializer(param, param.block)

+ 1 - 1
paddlers/custom_models/seg/models/utils/torch_nn.py

@@ -20,4 +20,4 @@ class Identity(nn.Layer):
         super(Identity, self).__init__()
 
     def forward(self, input):
-        return input
+        return input

+ 3 - 2
paddlers/datasets/clas_dataset.py

@@ -72,8 +72,9 @@ class ClasDataset(Dataset):
                     raise IOError('Image file {} does not exist!'.format(
                         full_path_im))
                 if not label.isdigit():
-                    raise ValueError('Label {} does not convert to number(int)!'.format(
-                        label))
+                    raise ValueError(
+                        'Label {} does not convert to number(int)!'.format(
+                            label))
                 self.file_list.append({
                     'image': full_path_im,
                     'label': int(label)

+ 10 - 17
paddlers/datasets/voc.py

@@ -136,8 +136,7 @@ class VOCDetection(Dataset):
                     ct = int(tree.find('id').text)
                     im_id = np.asarray([int(tree.find('id').text)])
                 pattern = re.compile('<size>', re.IGNORECASE)
-                size_tag = pattern.findall(
-                    str(ET.tostringlist(tree.getroot())))
+                size_tag = pattern.findall(str(ET.tostringlist(tree.getroot())))
                 if len(size_tag) > 0:
                     size_tag = size_tag[0][1:-1]
                     size_element = tree.find(size_tag)
@@ -170,8 +169,8 @@ class VOCDetection(Dataset):
                 difficult = np.zeros((num_bbox, 1), dtype=np.int32)
                 for obj in objs:
                     pattern = re.compile('<name>', re.IGNORECASE)
-                    name_tag = pattern.findall(str(ET.tostringlist(obj)))[0][
-                        1:-1]
+                    name_tag = pattern.findall(str(ET.tostringlist(obj)))[0][1:
+                                                                             -1]
                     cname = obj.find(name_tag).text.strip()
                     pattern = re.compile('<difficult>', re.IGNORECASE)
                     diff_tag = pattern.findall(str(ET.tostringlist(obj)))
@@ -218,7 +217,8 @@ class VOCDetection(Dataset):
                     if not (x2 >= x1 and y2 >= y1):
                         logging.warning(
                             "Bounding box for object {} does not satisfy xmin {} <= xmax {} and ymin {} <= ymax {}, "
-                            "so this object is skipped. xml file: {}".format(i, x1, x2, y1, y2, xml_file))
+                            "so this object is skipped. xml file: {}".format(
+                                i, x1, x2, y1, y2, xml_file))
                         continue
 
                     gt_bbox[i, :] = [x1, y1, x2, y2]
@@ -287,8 +287,7 @@ class VOCDetection(Dataset):
                     self.num_max_boxes = max(self.num_max_boxes, len(objs))
 
         if not ct:
-            logging.error(
-                "No voc record found in %s' % (file_list)", exit=True)
+            logging.error("No voc record found in %s' % (file_list)", exit=True)
         self.pos_num = len(self.file_list)
         if self.allow_empty and neg_file_list:
             self.file_list += self._sample_empty(neg_file_list)
@@ -319,7 +318,8 @@ class VOCDetection(Dataset):
             if self.data_fields is not None:
                 sample_mix = {k: sample_mix[k] for k in self.data_fields}
             sample = self.mixup_op(sample=[
-                ImgDecoder(to_rgb=False)(sample), ImgDecoder(to_rgb=False)(sample_mix)
+                ImgDecoder(to_rgb=False)(sample),
+                ImgDecoder(to_rgb=False)(sample_mix)
             ])
         sample = self.transforms(sample)
         return sample
@@ -384,8 +384,7 @@ class VOCDetection(Dataset):
         if empty_ratio is not None:
             self.empty_ratio = empty_ratio
         image_list = os.listdir(image_dir)
-        max_img_id = max(
-            len(self.file_list) - 1, max(self.coco_gt.getImgIds()))
+        max_img_id = max(len(self.file_list) - 1, max(self.coco_gt.getImgIds()))
         neg_file_list = list()
         for image in image_list:
             if not is_pic(image):
@@ -416,13 +415,7 @@ class VOCDetection(Dataset):
             if 'gt_poly' in self.file_list[0]:
                 label_info['gt_poly'] = []
 
-            neg_file_list.append({
-                'image': im_fname,
-                **
-                im_info,
-                **
-                label_info
-            })
+            neg_file_list.append({'image': im_fname, ** im_info, ** label_info})
         if neg_file_list:
             self.allow_empty = True
             self.file_list += self._sample_empty(neg_file_list)

+ 1 - 1
paddlers/models/ppcls/arch/backbone/__init__.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 6 - 10
paddlers/models/ppcls/arch/backbone/base/theseus_layer.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -42,9 +42,7 @@ class TheseusLayer(nn.Layer):
             res_dict[res_key] = self.res_dict.pop(res_key)
         return res_dict
 
-    def init_res(self,
-                 stages_pattern,
-                 return_patterns=None,
+    def init_res(self, stages_pattern, return_patterns=None,
                  return_stages=None):
         if return_patterns and return_stages:
             msg = f"The 'return_patterns' would be ignored when 'return_stages' is set."
@@ -206,8 +204,7 @@ def save_sub_res_hook(layer, input, output):
     layer.res_dict[layer.res_name] = output
 
 
-def set_identity(parent_layer: nn.Layer,
-                 layer_name: str,
+def set_identity(parent_layer: nn.Layer, layer_name: str,
                  layer_index: str=None) -> bool:
     """set the layer specified by layer_name and layer_index to Indentity.
 
@@ -230,8 +227,7 @@ def set_identity(parent_layer: nn.Layer,
 
     if layer_index and stop_after:
         stop_after = False
-        for sub_layer_index in parent_layer._sub_layers[
-                layer_name]._sub_layers:
+        for sub_layer_index in parent_layer._sub_layers[layer_name]._sub_layers:
             if stop_after:
                 parent_layer._sub_layers[layer_name][
                     sub_layer_index] = Identity()
@@ -242,8 +238,8 @@ def set_identity(parent_layer: nn.Layer,
     return stop_after
 
 
-def parse_pattern_str(pattern: str, parent_layer: nn.Layer) -> Union[
-        None, List[Dict[str, Union[nn.Layer, str, None]]]]:
+def parse_pattern_str(pattern: str, parent_layer: nn.Layer) -> Union[None, List[
+        Dict[str, Union[nn.Layer, str, None]]]]:
     """parse the string type pattern.
 
     Args:

+ 1 - 1
paddlers/models/ppcls/arch/backbone/legendary_models/esnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 4 - 8
paddlers/models/ppcls/arch/backbone/legendary_models/hrnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -246,7 +246,7 @@ class HighResolutionModule(TheseusLayer):
 
         for i in range(len(num_filters)):
             self.basic_block_list.append(
-                nn.Sequential(* [
+                nn.Sequential(*[
                     BasicBlock(
                         num_channels=num_filters[i],
                         num_filters=num_filters[i],
@@ -389,11 +389,7 @@ class HRNet(TheseusLayer):
         ]
 
         self.conv_layer1_1 = ConvBNLayer(
-            num_channels=3,
-            num_filters=64,
-            filter_size=3,
-            stride=2,
-            act="relu")
+            num_channels=3, num_filters=64, filter_size=3, stride=2, act="relu")
 
         self.conv_layer1_2 = ConvBNLayer(
             num_channels=64,
@@ -402,7 +398,7 @@ class HRNet(TheseusLayer):
             stride=2,
             act="relu")
 
-        self.layer1 = nn.Sequential(* [
+        self.layer1 = nn.Sequential(*[
             BottleneckBlock(
                 num_channels=64 if i == 0 else 256,
                 num_filters=64,

+ 2 - 6
paddlers/models/ppcls/arch/backbone/legendary_models/inception_v3.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -86,11 +86,7 @@ class InceptionStem(TheseusLayer):
     def __init__(self):
         super().__init__()
         self.conv_1a_3x3 = ConvBNLayer(
-            num_channels=3,
-            num_filters=32,
-            filter_size=3,
-            stride=2,
-            act="relu")
+            num_channels=3, num_filters=32, filter_size=3, stride=2, act="relu")
         self.conv_2a_3x3 = ConvBNLayer(
             num_channels=32,
             num_filters=32,

+ 4 - 8
paddlers/models/ppcls/arch/backbone/legendary_models/mobilenet_v1.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -137,7 +137,7 @@ class MobileNet(TheseusLayer):
                     [int(512 * scale), 512, 1024, 512, 2],
                     [int(1024 * scale), 1024, 1024, 1024, 1]]
 
-        self.blocks = nn.Sequential(* [
+        self.blocks = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=params[0],
                 num_filters1=params[1],
@@ -212,9 +212,7 @@ def MobileNetV1_x0_5(pretrained=False, use_ssld=False, **kwargs):
         model: nn.Layer. Specific `MobileNetV1_x0_5` model depends on args.
     """
     model = MobileNet(
-        scale=0.5,
-        stages_pattern=MODEL_STAGES_PATTERN["MobileNetV1"],
-        **kwargs)
+        scale=0.5, stages_pattern=MODEL_STAGES_PATTERN["MobileNetV1"], **kwargs)
     _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1_x0_5"],
                      use_ssld)
     return model
@@ -250,8 +248,6 @@ def MobileNetV1(pretrained=False, use_ssld=False, **kwargs):
         model: nn.Layer. Specific `MobileNetV1` model depends on args.
     """
     model = MobileNet(
-        scale=1.0,
-        stages_pattern=MODEL_STAGES_PATTERN["MobileNetV1"],
-        **kwargs)
+        scale=1.0, stages_pattern=MODEL_STAGES_PATTERN["MobileNetV1"], **kwargs)
     _load_pretrained(pretrained, model, MODEL_URLS["MobileNetV1"], use_ssld)
     return model

+ 3 - 4
paddlers/models/ppcls/arch/backbone/legendary_models/mobilenet_v3.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -46,8 +46,7 @@ MODEL_URLS = {
 }
 
 MODEL_STAGES_PATTERN = {
-    "MobileNetV3_small":
-    ["blocks[0]", "blocks[2]", "blocks[7]", "blocks[10]"],
+    "MobileNetV3_small": ["blocks[0]", "blocks[2]", "blocks[7]", "blocks[10]"],
     "MobileNetV3_large":
     ["blocks[0]", "blocks[2]", "blocks[5]", "blocks[11]", "blocks[14]"]
 }
@@ -172,7 +171,7 @@ class MobileNetV3(TheseusLayer):
             if_act=True,
             act="hardswish")
 
-        self.blocks = nn.Sequential(* [
+        self.blocks = nn.Sequential(*[
             ResidualUnit(
                 in_c=_make_divisible(self.inplanes * self.scale if i == 0 else
                                      self.cfg[i - 1][2] * self.scale),

+ 9 - 9
paddlers/models/ppcls/arch/backbone/legendary_models/pp_lcnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -61,9 +61,9 @@ NET_CONFIG = {
     [[3, 16, 32, 1, False]],
     "blocks3": [[3, 32, 64, 2, False], [3, 64, 64, 1, False]],
     "blocks4": [[3, 64, 128, 2, False], [3, 128, 128, 1, False]],
-    "blocks5": [[3, 128, 256, 2, False], [5, 256, 256, 1, False],
-                [5, 256, 256, 1, False], [5, 256, 256, 1, False],
-                [5, 256, 256, 1, False], [5, 256, 256, 1, False]],
+    "blocks5":
+    [[3, 128, 256, 2, False], [5, 256, 256, 1, False], [5, 256, 256, 1, False],
+     [5, 256, 256, 1, False], [5, 256, 256, 1, False], [5, 256, 256, 1, False]],
     "blocks6": [[5, 256, 512, 2, True], [5, 512, 512, 1, True]]
 }
 
@@ -189,7 +189,7 @@ class PPLCNet(TheseusLayer):
             num_filters=make_divisible(16 * scale),
             stride=2)
 
-        self.blocks2 = nn.Sequential(* [
+        self.blocks2 = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=make_divisible(in_c * scale),
                 num_filters=make_divisible(out_c * scale),
@@ -199,7 +199,7 @@ class PPLCNet(TheseusLayer):
             for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks2"])
         ])
 
-        self.blocks3 = nn.Sequential(* [
+        self.blocks3 = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=make_divisible(in_c * scale),
                 num_filters=make_divisible(out_c * scale),
@@ -209,7 +209,7 @@ class PPLCNet(TheseusLayer):
             for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks3"])
         ])
 
-        self.blocks4 = nn.Sequential(* [
+        self.blocks4 = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=make_divisible(in_c * scale),
                 num_filters=make_divisible(out_c * scale),
@@ -219,7 +219,7 @@ class PPLCNet(TheseusLayer):
             for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks4"])
         ])
 
-        self.blocks5 = nn.Sequential(* [
+        self.blocks5 = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=make_divisible(in_c * scale),
                 num_filters=make_divisible(out_c * scale),
@@ -229,7 +229,7 @@ class PPLCNet(TheseusLayer):
             for i, (k, in_c, out_c, s, se) in enumerate(NET_CONFIG["blocks5"])
         ])
 
-        self.blocks6 = nn.Sequential(* [
+        self.blocks6 = nn.Sequential(*[
             DepthwiseSeparable(
                 num_channels=make_divisible(in_c * scale),
                 num_filters=make_divisible(out_c * scale),

+ 2 - 2
paddlers/models/ppcls/arch/backbone/legendary_models/resnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -309,7 +309,7 @@ class ResNet(TheseusLayer):
             [[input_image_channel, 32, 3, 2], [32, 32, 3, 1], [32, 64, 3, 1]]
         }
 
-        self.stem = nn.Sequential(* [
+        self.stem = nn.Sequential(*[
             ConvBNLayer(
                 num_channels=in_c,
                 num_filters=out_c,

+ 1 - 1
paddlers/models/ppcls/arch/backbone/legendary_models/vgg.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/alexnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 3 - 7
paddlers/models/ppcls/arch/backbone/model_zoo/cspnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -40,8 +40,7 @@ MODEL_CFGS = {
             down_growth=True, ))
 }
 
-__all__ = ['CSPDarkNet53'
-           ]  # model_registry will add each entrypoint fn to this
+__all__ = ['CSPDarkNet53']  # model_registry will add each entrypoint fn to this
 
 
 class ConvBnAct(nn.Layer):
@@ -341,10 +340,7 @@ class CSPNet(nn.Layer):
         self.pool = nn.AdaptiveAvgPool2D(1)
         self.flatten = nn.Flatten(1)
         self.fc = nn.Linear(
-            prev_chs,
-            class_num,
-            weight_attr=ParamAttr(),
-            bias_attr=ParamAttr())
+            prev_chs, class_num, weight_attr=ParamAttr(), bias_attr=ParamAttr())
 
     def forward(self, x):
         x = self.stem(x)

+ 2 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/darknet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -86,8 +86,7 @@ class DarkNet(nn.Layer):
 
         self.stages = [1, 2, 8, 8, 4]
         self._conv1 = ConvBNLayer(3, 32, 3, 1, 1, name="yolo_input")
-        self._conv2 = ConvBNLayer(
-            32, 64, 3, 2, 1, name="yolo_input.downsample")
+        self._conv2 = ConvBNLayer(32, 64, 3, 2, 1, name="yolo_input.downsample")
 
         self._basic_block_01 = BasicBlock(64, 32, name="stage.0.0")
         self._downsample_0 = ConvBNLayer(

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/densenet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 5 - 9
paddlers/models/ppcls/arch/backbone/model_zoo/distilled_vision_transformer.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -175,8 +175,7 @@ def DeiT_base_patch16_224(pretrained=False, use_ssld=False, **kwargs):
     return model
 
 
-def DeiT_tiny_distilled_patch16_224(pretrained=False, use_ssld=False,
-                                    **kwargs):
+def DeiT_tiny_distilled_patch16_224(pretrained=False, use_ssld=False, **kwargs):
     model = DistilledVisionTransformer(
         patch_size=16,
         embed_dim=192,
@@ -194,8 +193,7 @@ def DeiT_tiny_distilled_patch16_224(pretrained=False, use_ssld=False,
     return model
 
 
-def DeiT_small_distilled_patch16_224(pretrained=False,
-                                     use_ssld=False,
+def DeiT_small_distilled_patch16_224(pretrained=False, use_ssld=False,
                                      **kwargs):
     model = DistilledVisionTransformer(
         patch_size=16,
@@ -214,8 +212,7 @@ def DeiT_small_distilled_patch16_224(pretrained=False,
     return model
 
 
-def DeiT_base_distilled_patch16_224(pretrained=False, use_ssld=False,
-                                    **kwargs):
+def DeiT_base_distilled_patch16_224(pretrained=False, use_ssld=False, **kwargs):
     model = DistilledVisionTransformer(
         patch_size=16,
         embed_dim=768,
@@ -252,8 +249,7 @@ def DeiT_base_patch16_384(pretrained=False, use_ssld=False, **kwargs):
     return model
 
 
-def DeiT_base_distilled_patch16_384(pretrained=False, use_ssld=False,
-                                    **kwargs):
+def DeiT_base_distilled_patch16_384(pretrained=False, use_ssld=False, **kwargs):
     model = DistilledVisionTransformer(
         img_size=384,
         patch_size=16,

+ 2 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/dla.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -291,8 +291,7 @@ class DLA(nn.Layer):
             nn.BatchNorm2D(channels[0]),
             nn.ReLU())
 
-        self.level0 = self._make_conv_level(channels[0], channels[0],
-                                            levels[0])
+        self.level0 = self._make_conv_level(channels[0], channels[0], levels[0])
         self.level1 = self._make_conv_level(
             channels[0], channels[1], levels[1], stride=2)
 

+ 2 - 4
paddlers/models/ppcls/arch/backbone/model_zoo/dpn.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -295,9 +295,7 @@ class DPN(nn.Layer):
                             _type=_type2,
                             name="dpn" + str(num))))
 
-                num_channel_dpn = [
-                    num_channel_dpn[0], num_channel_dpn[1] + inc
-                ]
+                num_channel_dpn = [num_channel_dpn[0], num_channel_dpn[1] + inc]
 
         out_channel = sum(num_channel_dpn)
 

+ 4 - 7
paddlers/models/ppcls/arch/backbone/model_zoo/efficientnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -316,10 +316,8 @@ class Conv2ds(nn.Layer):
             model_name][cur_stage]
         self.need_crop = False
         if padding_type == "SAME":
-            top_padding, bottom_padding = cal_padding(inps, stride,
-                                                      filter_size)
-            left_padding, right_padding = cal_padding(inps, stride,
-                                                      filter_size)
+            top_padding, bottom_padding = cal_padding(inps, stride, filter_size)
+            left_padding, right_padding = cal_padding(inps, stride, filter_size)
             height_padding = bottom_padding
             width_padding = right_padding
             if top_padding != bottom_padding or left_padding != right_padding:
@@ -695,8 +693,7 @@ class ExtractFeatures(nn.Layer):
                                             _global_params),
                 output_filters=round_filters(block_args.output_filters,
                                              _global_params),
-                num_repeat=round_repeats(block_args.num_repeat,
-                                         _global_params))
+                num_repeat=round_repeats(block_args.num_repeat, _global_params))
 
             drop_connect_rate = self._global_params.drop_connect_rate
             if drop_connect_rate:

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/ghostnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 4 - 5
paddlers/models/ppcls/arch/backbone/model_zoo/gvt.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -125,8 +125,7 @@ class Attention(nn.Layer):
 
         self.sr_ratio = sr_ratio
         if sr_ratio > 1:
-            self.sr = nn.Conv2D(
-                dim, dim, kernel_size=sr_ratio, stride=sr_ratio)
+            self.sr = nn.Conv2D(dim, dim, kernel_size=sr_ratio, stride=sr_ratio)
             self.norm = nn.LayerNorm(dim)
 
     def forward(self, x, H, W):
@@ -234,8 +233,8 @@ class GroupBlock(ViTBlock):
                          attn_drop, drop_path, act_layer, norm_layer)
         del self.attn
         if ws == 1:
-            self.attn = Attention(dim, num_heads, qkv_bias, qk_scale,
-                                  attn_drop, drop, sr_ratio)
+            self.attn = Attention(dim, num_heads, qkv_bias, qk_scale, attn_drop,
+                                  drop, sr_ratio)
         else:
             self.attn = GroupAttention(dim, num_heads, qkv_bias, qk_scale,
                                        attn_drop, drop, ws)

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/hardnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 3 - 11
paddlers/models/ppcls/arch/backbone/model_zoo/inception_v4.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -145,11 +145,7 @@ class InceptionA(nn.Layer):
             act="relu",
             name="inception_a" + name + "_3x3")
         self._conv4_1 = ConvBNLayer(
-            384,
-            64,
-            1,
-            act="relu",
-            name="inception_a" + name + "_3x3_2_reduce")
+            384, 64, 1, act="relu", name="inception_a" + name + "_3x3_2_reduce")
         self._conv4_2 = ConvBNLayer(
             64,
             96,
@@ -214,11 +210,7 @@ class InceptionB(nn.Layer):
         self._conv2 = ConvBNLayer(
             1024, 384, 1, act="relu", name="inception_b" + name + "_1x1_2")
         self._conv3_1 = ConvBNLayer(
-            1024,
-            192,
-            1,
-            act="relu",
-            name="inception_b" + name + "_1x7_reduce")
+            1024, 192, 1, act="relu", name="inception_b" + name + "_1x7_reduce")
         self._conv3_2 = ConvBNLayer(
             192,
             224, (1, 7),

+ 3 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/levit.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -366,8 +366,8 @@ class LeViT(nn.Layer):
         down_ops.append([''])
         resolution = img_size // patch_size
         for i, (ed, kd, dpth, nh, ar, mr, do) in enumerate(
-                zip(embed_dim, key_dim, depth, num_heads, attn_ratio,
-                    mlp_ratio, down_ops)):
+                zip(embed_dim, key_dim, depth, num_heads, attn_ratio, mlp_ratio,
+                    down_ops)):
             for _ in range(dpth):
                 self.blocks.append(
                     Residual(

+ 2 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/mixnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -691,8 +691,7 @@ class MixNet(nn.Layer):
             "final_pool", nn.AvgPool2D(
                 kernel_size=7, stride=1))
 
-        self.output = nn.Linear(
-            in_features=in_channels, out_features=class_num)
+        self.output = nn.Linear(in_features=in_channels, out_features=class_num)
 
     def forward(self, x):
         x = self.features(x)

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/mobilenet_v2.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/pvt_v2.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/rednet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/regnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/repvgg.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/res2net.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 2 - 5
paddlers/models/ppcls/arch/backbone/model_zoo/res2net_vd.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -278,10 +278,7 @@ def _load_pretrained(pretrained, model, model_url, use_ssld=False):
 def Res2Net50_vd_26w_4s(pretrained=False, use_ssld=False, **kwargs):
     model = Res2Net_vd(layers=50, scales=4, width=26, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["Res2Net50_vd_26w_4s"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["Res2Net50_vd_26w_4s"], use_ssld=use_ssld)
     return model
 
 

+ 2 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/resnest.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -515,8 +515,7 @@ class ResNeSt(nn.Layer):
         self.max_pool2d = MaxPool2D(kernel_size=3, stride=2, padding=1)
 
         self.layer1 = ResNeStLayer(
-            inplanes=self.stem_width * 2
-            if self.deep_stem else self.stem_width,
+            inplanes=self.stem_width * 2 if self.deep_stem else self.stem_width,
             planes=64,
             blocks=self.layers[0],
             radix=radix,

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/resnet_vc.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/resnext.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 5 - 17
paddlers/models/ppcls/arch/backbone/model_zoo/resnext_vd.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -280,38 +280,26 @@ def ResNeXt50_vd_64x4d(pretrained=False, use_ssld=False, **kwargs):
 def ResNeXt101_vd_32x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=101, cardinality=32, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["ResNeXt101_vd_32x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["ResNeXt101_vd_32x4d"], use_ssld=use_ssld)
     return model
 
 
 def ResNeXt101_vd_64x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=101, cardinality=64, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["ResNeXt101_vd_64x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["ResNeXt101_vd_64x4d"], use_ssld=use_ssld)
     return model
 
 
 def ResNeXt152_vd_32x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=152, cardinality=32, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["ResNeXt152_vd_32x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["ResNeXt152_vd_32x4d"], use_ssld=use_ssld)
     return model
 
 
 def ResNeXt152_vd_64x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=152, cardinality=64, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["ResNeXt152_vd_64x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["ResNeXt152_vd_64x4d"], use_ssld=use_ssld)
     return model

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/rexnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 3 - 9
paddlers/models/ppcls/arch/backbone/model_zoo/se_resnext.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -347,18 +347,12 @@ def SE_ResNeXt50_32x4d(pretrained=False, use_ssld=False, **kwargs):
 def SE_ResNeXt101_32x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=101, cardinality=32, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["SE_ResNeXt101_32x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["SE_ResNeXt101_32x4d"], use_ssld=use_ssld)
     return model
 
 
 def SE_ResNeXt152_64x4d(pretrained=False, use_ssld=False, **kwargs):
     model = ResNeXt(layers=152, cardinality=64, **kwargs)
     _load_pretrained(
-        pretrained,
-        model,
-        MODEL_URLS["SE_ResNeXt152_64x4d"],
-        use_ssld=use_ssld)
+        pretrained, model, MODEL_URLS["SE_ResNeXt152_64x4d"], use_ssld=use_ssld)
     return model

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/se_resnext_vd.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 3 - 11
paddlers/models/ppcls/arch/backbone/model_zoo/shufflenet_v2.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -98,11 +98,7 @@ class ConvBNLayer(Layer):
 
 
 class InvertedResidual(Layer):
-    def __init__(self,
-                 in_channels,
-                 out_channels,
-                 stride,
-                 act="relu",
+    def __init__(self, in_channels, out_channels, stride, act="relu",
                  name=None):
         super(InvertedResidual, self).__init__()
         self._conv_pw = ConvBNLayer(
@@ -146,11 +142,7 @@ class InvertedResidual(Layer):
 
 
 class InvertedResidualDS(Layer):
-    def __init__(self,
-                 in_channels,
-                 out_channels,
-                 stride,
-                 act="relu",
+    def __init__(self, in_channels, out_channels, stride, act="relu",
                  name=None):
         super(InvertedResidualDS, self).__init__()
 

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/squeezenet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 4 - 5
paddlers/models/ppcls/arch/backbone/model_zoo/swin_transformer.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -156,8 +156,7 @@ class WindowAttention(nn.Layer):
         relative_coords[:, :, 1] += self.window_size[1] - 1
         relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
         relative_position_index = relative_coords.sum(-1)  # Wh*Ww, Wh*Ww
-        self.register_buffer("relative_position_index",
-                             relative_position_index)
+        self.register_buffer("relative_position_index", relative_position_index)
 
         self.qkv = nn.Linear(dim, dim * 3, bias_attr=qkv_bias)
         self.attn_drop = nn.Dropout(attn_drop)
@@ -581,8 +580,8 @@ class PatchEmbed(nn.Layer):
 
     def flops(self):
         Ho, Wo = self.patches_resolution
-        flops = Ho * Wo * self.embed_dim * self.in_chans * (
-            self.patch_size[0] * self.patch_size[1])
+        flops = Ho * Wo * self.embed_dim * self.in_chans * (self.patch_size[0] *
+                                                            self.patch_size[1])
         if self.norm is not None:
             flops += Ho * Wo * self.embed_dim
         return flops

+ 3 - 5
paddlers/models/ppcls/arch/backbone/model_zoo/tnt.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -112,8 +112,7 @@ class Attention(nn.Layer):
     def forward(self, x):
         B, N, C = x.shape
         qk = self.qk(x).reshape(
-            (B, N, 2, self.num_heads, self.head_dim)).transpose(
-                (2, 0, 3, 1, 4))
+            (B, N, 2, self.num_heads, self.head_dim)).transpose((2, 0, 3, 1, 4))
 
         q, k = qk[0], qk[1]
         v = self.v(x).reshape(
@@ -125,8 +124,7 @@ class Attention(nn.Layer):
         attn = self.attn_drop(attn)
 
         x = paddle.matmul(attn, v)
-        x = x.transpose((0, 2, 1, 3)).reshape(
-            (B, N, x.shape[-1] * x.shape[-3]))
+        x = x.transpose((0, 2, 1, 3)).reshape((B, N, x.shape[-1] * x.shape[-3]))
         x = self.proj(x)
         x = self.proj_drop(x)
         return x

+ 1 - 1
paddlers/models/ppcls/arch/backbone/model_zoo/vision_transformer.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 2 - 3
paddlers/models/ppcls/arch/backbone/model_zoo/xception_deeplab.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -76,8 +76,7 @@ def gen_bottleneck_params(backbone='xception_65'):
         }
     else:
         raise Exception(
-            "xception backbont only support xception_41/xception_65/xception_71"
-        )
+            "xception backbont only support xception_41/xception_65/xception_71")
     return bottleneck_params
 
 

+ 2 - 2
paddlers/models/ppcls/arch/gears/__init__.py

@@ -26,7 +26,7 @@ def build_gear(config):
         'ArcMargin', 'CosMargin', 'CircleMargin', 'FC', 'VehicleNeck'
     ]
     module_name = config.pop('name')
-    assert module_name in support_dict, Exception(
-        'head only support {}'.format(support_dict))
+    assert module_name in support_dict, Exception('head only support {}'.format(
+        support_dict))
     module_class = eval(module_name)(**config)
     return module_class

+ 0 - 1
paddlers/models/ppcls/arch/slim/prune.py

@@ -37,7 +37,6 @@ def prune_model(config, model):
         model.pruner = None
 
 
-
 def _prune_model(config, model):
     from paddleslim.analysis import dygraph_flops as flops
     logger.info("FLOPs before pruning: {}GFLOPs".format(

+ 1 - 1
paddlers/models/ppcls/arch/utils.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 2
paddlers/models/ppcls/data/dataloader/dali.py

@@ -150,8 +150,7 @@ def dali_dataloader(config, mode, device, seed=None):
     config_dataloader = config[mode]
     seed = 42 if seed is None else seed
     ops = [
-        list(x.keys())[0]
-        for x in config_dataloader["dataset"]["transform_ops"]
+        list(x.keys())[0] for x in config_dataloader["dataset"]["transform_ops"]
     ]
     support_ops_train = [
         "DecodeImage", "NormalizeImage", "RandFlipImage", "RandCropImage"

+ 2 - 2
paddlers/models/ppcls/data/dataloader/pk_sampler.py

@@ -75,8 +75,8 @@ class PKSampler(DistributedBatchSampler):
                 logger.error("PKSampler prob list error")
             else:
                 logger.info(
-                    "PKSampler: sum of prob list not equal to 1, diff is {}, change the last prob".format(diff)
-                )
+                    "PKSampler: sum of prob list not equal to 1, diff is {}, change the last prob".
+                    format(diff))
 
     def __iter__(self):
         label_per_batch = self.batch_size // self.sample_per_label

+ 1 - 1
paddlers/models/ppcls/data/postprocess/topk.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 2
paddlers/models/ppcls/data/preprocess/ops/autoaugment.py

@@ -160,8 +160,7 @@ class SVHNPolicy(object):
             SubPolicy(0.3, "contrast", 3, 0.8, "rotate", 4, fillcolor),
             SubPolicy(0.8, "invert", 5, 0.0, "translateY", 2, fillcolor),
             SubPolicy(0.7, "shearY", 6, 0.4, "solarize", 8, fillcolor),
-            SubPolicy(0.6, "invert", 4, 0.8, "rotate", 4, fillcolor),
-            SubPolicy(
+            SubPolicy(0.6, "invert", 4, 0.8, "rotate", 4, fillcolor), SubPolicy(
                 0.3, "shearY", 7, 0.9, "translateX", 3, fillcolor), SubPolicy(
                     0.1, "shearX", 6, 0.6, "invert", 5, fillcolor), SubPolicy(
                         0.7, "solarize", 2, 0.6, "translateY", 7,

+ 16 - 13
paddlers/models/ppcls/data/preprocess/ops/functional.py

@@ -18,7 +18,6 @@ import numpy as np
 from PIL import Image, ImageOps, ImageEnhance
 
 
-
 def int_parameter(level, maxval):
     """Helper function to scale `val` between 0 and maxval .
     Args:
@@ -76,36 +75,40 @@ def shear_x(pil_img, level):
     level = float_parameter(sample_level(level), 0.3)
     if np.random.uniform() > 0.5:
         level = -level
-    return pil_img.transform(pil_img.size,
-                             Image.AFFINE, (1, level, 0, 0, 1, 0),
-                             resample=Image.BILINEAR)
+    return pil_img.transform(
+        pil_img.size,
+        Image.AFFINE, (1, level, 0, 0, 1, 0),
+        resample=Image.BILINEAR)
 
 
 def shear_y(pil_img, level):
     level = float_parameter(sample_level(level), 0.3)
     if np.random.uniform() > 0.5:
         level = -level
-    return pil_img.transform(pil_img.size,
-                             Image.AFFINE, (1, 0, 0, level, 1, 0),
-                             resample=Image.BILINEAR)
+    return pil_img.transform(
+        pil_img.size,
+        Image.AFFINE, (1, 0, 0, level, 1, 0),
+        resample=Image.BILINEAR)
 
 
 def translate_x(pil_img, level):
     level = int_parameter(sample_level(level), pil_img.size[0] / 3)
     if np.random.random() > 0.5:
         level = -level
-    return pil_img.transform(pil_img.size,
-                             Image.AFFINE, (1, 0, level, 0, 1, 0),
-                             resample=Image.BILINEAR)
+    return pil_img.transform(
+        pil_img.size,
+        Image.AFFINE, (1, 0, level, 0, 1, 0),
+        resample=Image.BILINEAR)
 
 
 def translate_y(pil_img, level):
     level = int_parameter(sample_level(level), pil_img.size[1] / 3)
     if np.random.random() > 0.5:
         level = -level
-    return pil_img.transform(pil_img.size,
-                             Image.AFFINE, (1, 0, 0, 0, 1, level),
-                             resample=Image.BILINEAR)
+    return pil_img.transform(
+        pil_img.size,
+        Image.AFFINE, (1, 0, 0, 0, 1, level),
+        resample=Image.BILINEAR)
 
 
 # operation that overlaps with ImageNet-C's test set

+ 2 - 2
paddlers/models/ppcls/data/preprocess/ops/grid.py

@@ -77,8 +77,8 @@ class GridMask(object):
         mask = Image.fromarray(np.uint8(mask))
         mask = mask.rotate(r)
         mask = np.asarray(mask)
-        mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) //
-                    2 + w]
+        mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) // 2
+                    + w]
 
         if self.mode == 1:
             mask = 1 - mask

+ 2 - 4
paddlers/models/ppcls/data/preprocess/ops/operators.py

@@ -99,8 +99,7 @@ class DecodeImage(object):
         data = np.frombuffer(img, dtype='uint8')
         img = cv2.imdecode(data, 1)
         if self.to_rgb:
-            assert img.shape[2] == 3, 'invalid shape of image[%s]' % (
-                img.shape)
+            assert img.shape[2] == 3, 'invalid shape of image[%s]' % (img.shape)
             img = img[:, :, ::-1]
 
         if self.channel_first:
@@ -348,8 +347,7 @@ class AugMix(object):
 
         ws = np.float32(
             np.random.dirichlet([self.aug_prob_coeff] * self.mixture_width))
-        m = np.float32(
-            np.random.beta(self.aug_prob_coeff, self.aug_prob_coeff))
+        m = np.float32(np.random.beta(self.aug_prob_coeff, self.aug_prob_coeff))
 
         # image = Image.fromarray(image)
         mix = np.zeros(image.shape)

+ 1 - 2
paddlers/models/ppcls/data/preprocess/ops/timm_autoaugment.py

@@ -864,8 +864,7 @@ class RawTimmAutoAugment(object):
             self.augment_func = rand_augment_transform(config_str, aa_params)
         elif config_str.startswith('augmix'):
             aa_params['translate_pct'] = 0.3
-            self.augment_func = augment_and_mix_transform(config_str,
-                                                          aa_params)
+            self.augment_func = augment_and_mix_transform(config_str, aa_params)
         elif config_str.startswith('auto'):
             self.augment_func = auto_augment_transform(config_str, aa_params)
         else:

+ 4 - 4
paddlers/models/ppcls/engine/engine.py

@@ -146,8 +146,8 @@ class Engine(object):
                         self.config["DataLoader"]["Eval"], "Gallery",
                         self.device, self.use_dali)
                     self.query_dataloader = build_dataloader(
-                        self.config["DataLoader"]["Eval"], "Query",
-                        self.device, self.use_dali)
+                        self.config["DataLoader"]["Eval"], "Query", self.device,
+                        self.use_dali)
 
         # build loss
         if self.mode == "train":
@@ -212,8 +212,8 @@ class Engine(object):
                 load_dygraph_pretrain_from_url(
                     self.model, self.config["Global"]["pretrained_model"])
             else:
-                load_dygraph_pretrain(
-                    self.model, self.config["Global"]["pretrained_model"])
+                load_dygraph_pretrain(self.model,
+                                      self.config["Global"]["pretrained_model"])
 
         # build optimizer
         if self.mode == 'train':

+ 2 - 4
paddlers/models/ppcls/engine/evaluation/retrieval.py

@@ -40,8 +40,7 @@ def retrieval_eval(engine, epoch_id=0):
         sections.append(len(query_feas) % sim_block_size)
     fea_blocks = paddle.split(query_feas, num_or_sections=sections)
     if query_query_id is not None:
-        query_id_blocks = paddle.split(
-            query_query_id, num_or_sections=sections)
+        query_id_blocks = paddle.split(query_query_id, num_or_sections=sections)
     image_id_blocks = paddle.split(query_img_id, num_or_sections=sections)
     metric_key = None
 
@@ -110,8 +109,7 @@ def cal_feature(engine, name='gallery'):
             break
         if idx % engine.config["Global"]["print_batch_step"] == 0:
             logger.info(
-                f"{name} feature calculation process: [{idx}/{len(dataloader)}]"
-            )
+                f"{name} feature calculation process: [{idx}/{len(dataloader)}]")
         if engine.use_dali:
             batch = [
                 paddle.to_tensor(batch[0]['data']),

+ 1 - 1
paddlers/models/ppcls/loss/celoss.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/loss/dmlloss.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 1 - 1
paddlers/models/ppcls/loss/rkdloss.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 3 - 5
paddlers/models/ppcls/metric/metrics.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -80,8 +80,7 @@ class mAP(nn.Layer):
 
         #calc map
         precision_mask = paddle.multiply(equal_flag, precision)
-        ap = paddle.sum(precision_mask, axis=1) / paddle.sum(equal_flag,
-                                                             axis=1)
+        ap = paddle.sum(precision_mask, axis=1) / paddle.sum(equal_flag, axis=1)
         metric_dict["mAP"] = paddle.mean(ap).numpy()[0]
         return metric_dict
 
@@ -285,8 +284,7 @@ class AccuracyScore(MutiLabelMetric):
 
     def __init__(self, base="label"):
         super().__init__()
-        assert base in ["sample", "label"
-                        ], 'must be one of ["sample", "label"]'
+        assert base in ["sample", "label"], 'must be one of ["sample", "label"]'
         self.base = base
 
     def __call__(self, output, target):

+ 1 - 1
paddlers/models/ppcls/optimizer/learning_rate.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.

+ 4 - 3
paddlers/models/ppcls/optimizer/optimizer.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -197,8 +197,9 @@ class AdamW(object):
 
         if self.one_dim_param_no_weight_decay:
             self.no_weight_decay_param_name_list += [
-                p.name for model in model_list
-                for n, p in model.named_parameters() if len(p.shape) == 1
+                p.name
+                for model in model_list for n, p in model.named_parameters()
+                if len(p.shape) == 1
             ] if model_list else []
 
         opt = optim.AdamW(

+ 2 - 3
paddlers/models/ppcls/static/program.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -344,8 +344,7 @@ def run(dataloader,
     for k in fetchs:
         metric_dict[k] = fetchs[k][1]
 
-    metric_dict["batch_time"] = AverageMeter(
-        'batch_cost', '.5f', postfix=" s,")
+    metric_dict["batch_time"] = AverageMeter('batch_cost', '.5f', postfix=" s,")
     metric_dict["reader_time"] = AverageMeter(
         'reader_cost', '.5f', postfix=" s,")
 

+ 3 - 3
paddlers/models/ppcls/static/train.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -69,8 +69,8 @@ def main(args):
 
     mode = "train"
 
-    log_file = os.path.join(global_config['output_dir'],
-                            config["Arch"]["name"], f"{mode}.log")
+    log_file = os.path.join(global_config['output_dir'], config["Arch"]["name"],
+                            f"{mode}.log")
     init_logger(name='root', log_file=log_file)
     print_config(config)
 

+ 2 - 3
paddlers/models/ppcls/utils/config.py

@@ -1,4 +1,4 @@
-# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -175,8 +175,7 @@ def get_config(fname, overrides=None, show=False):
     """
     Read config from file
     """
-    assert os.path.exists(fname), (
-        'config file({}) is not exist'.format(fname))
+    assert os.path.exists(fname), ('config file({}) is not exist'.format(fname))
     config = parse_config(fname)
     override_config(config, overrides)
     if show:

+ 1 - 2
paddlers/models/ppcls/utils/feature_maps_visualization/fm_vis.py

@@ -50,8 +50,7 @@ def create_operators(interpolation=1):
     img_std = [0.229, 0.224, 0.225]
     img_scale = 1.0 / 255.0
 
-    resize_op = utils.ResizeImage(
-        resize_short=256, interpolation=interpolation)
+    resize_op = utils.ResizeImage(resize_short=256, interpolation=interpolation)
     crop_op = utils.CropImage(size=(size, size))
     normalize_op = utils.NormalizeImage(
         scale=img_scale, mean=img_mean, std=img_std)

+ 2 - 2
paddlers/models/ppcls/utils/feature_maps_visualization/resnet.py

@@ -1,4 +1,4 @@
-# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
+# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -298,7 +298,7 @@ class ResNet(TheseusLayer):
             [[input_image_channel, 32, 3, 2], [32, 32, 3, 1], [32, 64, 3, 1]]
         }
 
-        self.stem = nn.Sequential(* [
+        self.stem = nn.Sequential(*[
             ConvBNLayer(
                 num_channels=in_c,
                 num_filters=out_c,

+ 1 - 2
paddlers/models/ppcls/utils/feature_maps_visualization/utils.py

@@ -24,8 +24,7 @@ class DecodeImage(object):
         data = np.frombuffer(img, dtype='uint8')
         img = cv2.imdecode(data, 1)
         if self.to_rgb:
-            assert img.shape[2] == 3, 'invalid shape of image[%s]' % (
-                img.shape)
+            assert img.shape[2] == 3, 'invalid shape of image[%s]' % (img.shape)
             img = img[:, :, ::-1]
 
         return img

+ 8 - 4
paddlers/models/ppcls/utils/gallery2fc.py

@@ -51,7 +51,8 @@ class GalleryLayer(paddle.nn.Layer):
                 self.gallery_images.append(image_file)
                 gallery_docs.append(ori_line.strip())
                 gallery_labels.append(line[1].strip())
-        self.gallery_layer = paddle.nn.Linear(embedding_size, len(self.gallery_images), bias_attr=False)
+        self.gallery_layer = paddle.nn.Linear(
+            embedding_size, len(self.gallery_images), bias_attr=False)
         self.gallery_layer.skip_quant = True
         output_label_str = ""
         for i, label_i in enumerate(gallery_labels):
@@ -71,18 +72,21 @@ class GalleryLayer(paddle.nn.Layer):
         embedding_size = self.configs["Arch"]["Head"]["embedding_size"]
         batch_index = 0
         input_tensor = paddle.zeros(self.image_shape)
-        gallery_feature = paddle.zeros((len(self.gallery_images), embedding_size))
+        gallery_feature = paddle.zeros(
+            (len(self.gallery_images), embedding_size))
         for i, image_path in enumerate(self.gallery_images):
             image = cv2.imread(image_path)[:, :, ::-1]
             for op in preprocess_ops:
                 image = op(image)
             input_tensor[batch_index] = image
             batch_index += 1
-            if batch_index == self.batch_size or i == len(self.gallery_images) - 1:
+            if batch_index == self.batch_size or i == len(
+                    self.gallery_images) - 1:
                 batch_feature = feature_extractor(input_tensor)["features"]
                 for j in range(batch_index):
                     feature = batch_feature[j]
-                    norm_feature = paddle.nn.functional.normalize(feature, axis=0)
+                    norm_feature = paddle.nn.functional.normalize(
+                        feature, axis=0)
                     gallery_feature[i - batch_index + j + 1] = norm_feature
         self.gallery_layer.set_state_dict({"_layer.weight": gallery_feature.T})
 

Some files were not shown because too many files changed in this diff