Просмотр исходного кода

Allow Semi-automatic Contiguous Integration of CV Development Kits (#91)

* Update CV devkits and add auto-upgrade script

* Fix mask2geojson args
Lin Manhui 2 лет назад
Родитель
Сommit
0c06c62bbc
100 измененных файлов с 1395 добавлено и 272 удалено
  1. 3 0
      .gitignore
  2. 1 0
      .pre-commit-config.yaml
  3. 2 5
      paddlers/__init__.py
  4. 11 5
      paddlers/models/__init__.py
  5. 0 2
      paddlers/models/hash.txt
  6. 1 1
      paddlers/models/paddleseg/__init__.py
  7. 0 0
      paddlers/models/paddleseg/core/__init__.py
  8. 4 4
      paddlers/models/paddleseg/core/infer.py
  9. 3 3
      paddlers/models/paddleseg/core/predict.py
  10. 5 6
      paddlers/models/paddleseg/core/train.py
  11. 2 2
      paddlers/models/paddleseg/core/val.py
  12. 0 0
      paddlers/models/paddleseg/cvlibs/__init__.py
  13. 2 2
      paddlers/models/paddleseg/cvlibs/callbacks.py
  14. 3 3
      paddlers/models/paddleseg/cvlibs/config.py
  15. 2 2
      paddlers/models/paddleseg/cvlibs/manager.py
  16. 26 6
      paddlers/models/paddleseg/cvlibs/param_init.py
  17. 0 0
      paddlers/models/paddleseg/datasets/__init__.py
  18. 6 6
      paddlers/models/paddleseg/datasets/ade.py
  19. 5 5
      paddlers/models/paddleseg/datasets/chase_db1.py
  20. 3 3
      paddlers/models/paddleseg/datasets/cityscapes.py
  21. 3 3
      paddlers/models/paddleseg/datasets/cocostuff.py
  22. 5 5
      paddlers/models/paddleseg/datasets/dataset.py
  23. 5 5
      paddlers/models/paddleseg/datasets/drive.py
  24. 6 6
      paddlers/models/paddleseg/datasets/eg1800.py
  25. 5 5
      paddlers/models/paddleseg/datasets/hrf.py
  26. 4 4
      paddlers/models/paddleseg/datasets/mini_deep_globe_road_extraction.py
  27. 4 4
      paddlers/models/paddleseg/datasets/optic_disc_seg.py
  28. 4 4
      paddlers/models/paddleseg/datasets/pascal_context.py
  29. 2 2
      paddlers/models/paddleseg/datasets/pp_humanseg14k.py
  30. 10 7
      paddlers/models/paddleseg/datasets/pssl.py
  31. 5 5
      paddlers/models/paddleseg/datasets/stare.py
  32. 6 6
      paddlers/models/paddleseg/datasets/supervisely.py
  33. 6 6
      paddlers/models/paddleseg/datasets/voc.py
  34. 1 0
      paddlers/models/paddleseg/hash.txt
  35. 2 0
      paddlers/models/paddleseg/models/__init__.py
  36. 3 3
      paddlers/models/paddleseg/models/ann.py
  37. 3 3
      paddlers/models/paddleseg/models/attention_unet.py
  38. 1 0
      paddlers/models/paddleseg/models/backbones/__init__.py
  39. 2 2
      paddlers/models/paddleseg/models/backbones/ghostnet.py
  40. 29 14
      paddlers/models/paddleseg/models/backbones/hrnet.py
  41. 2 2
      paddlers/models/paddleseg/models/backbones/lite_hrnet.py
  42. 3 3
      paddlers/models/paddleseg/models/backbones/mix_transformer.py
  43. 2 2
      paddlers/models/paddleseg/models/backbones/mobilenetv2.py
  44. 3 3
      paddlers/models/paddleseg/models/backbones/mobilenetv3.py
  45. 3 3
      paddlers/models/paddleseg/models/backbones/resnet_vd.py
  46. 2 2
      paddlers/models/paddleseg/models/backbones/shufflenetv2.py
  47. 3 3
      paddlers/models/paddleseg/models/backbones/stdcnet.py
  48. 3 3
      paddlers/models/paddleseg/models/backbones/swin_transformer.py
  49. 3 3
      paddlers/models/paddleseg/models/backbones/top_transformer.py
  50. 0 0
      paddlers/models/paddleseg/models/backbones/transformer_utils.py
  51. 949 0
      paddlers/models/paddleseg/models/backbones/uhrnet.py
  52. 3 3
      paddlers/models/paddleseg/models/backbones/vision_transformer.py
  53. 3 3
      paddlers/models/paddleseg/models/backbones/xception_deeplab.py
  54. 3 3
      paddlers/models/paddleseg/models/bisenet.py
  55. 3 3
      paddlers/models/paddleseg/models/bisenetv1.py
  56. 3 3
      paddlers/models/paddleseg/models/ccnet.py
  57. 3 3
      paddlers/models/paddleseg/models/danet.py
  58. 3 3
      paddlers/models/paddleseg/models/ddrnet.py
  59. 5 5
      paddlers/models/paddleseg/models/decoupled_segnet.py
  60. 3 3
      paddlers/models/paddleseg/models/deeplab.py
  61. 8 4
      paddlers/models/paddleseg/models/dmnet.py
  62. 3 3
      paddlers/models/paddleseg/models/dnlnet.py
  63. 4 6
      paddlers/models/paddleseg/models/emanet.py
  64. 3 3
      paddlers/models/paddleseg/models/encnet.py
  65. 3 3
      paddlers/models/paddleseg/models/enet.py
  66. 3 3
      paddlers/models/paddleseg/models/espnet.py
  67. 3 3
      paddlers/models/paddleseg/models/espnetv1.py
  68. 3 3
      paddlers/models/paddleseg/models/fast_scnn.py
  69. 3 3
      paddlers/models/paddleseg/models/fastfcn.py
  70. 3 3
      paddlers/models/paddleseg/models/fcn.py
  71. 3 3
      paddlers/models/paddleseg/models/gcnet.py
  72. 3 3
      paddlers/models/paddleseg/models/ginet.py
  73. 3 3
      paddlers/models/paddleseg/models/glore.py
  74. 5 5
      paddlers/models/paddleseg/models/gscnn.py
  75. 3 3
      paddlers/models/paddleseg/models/hardnet.py
  76. 3 3
      paddlers/models/paddleseg/models/hrnet_contrast.py
  77. 3 3
      paddlers/models/paddleseg/models/isanet.py
  78. 1 0
      paddlers/models/paddleseg/models/layers/__init__.py
  79. 1 1
      paddlers/models/paddleseg/models/layers/activation.py
  80. 1 1
      paddlers/models/paddleseg/models/layers/attention.py
  81. 4 2
      paddlers/models/paddleseg/models/layers/layer_libs.py
  82. 1 1
      paddlers/models/paddleseg/models/layers/nonlocal2d.py
  83. 123 0
      paddlers/models/paddleseg/models/layers/polaried_self_attention.py
  84. 1 1
      paddlers/models/paddleseg/models/layers/pyramid_pool.py
  85. 2 2
      paddlers/models/paddleseg/models/layers/tensor_fusion.py
  86. 0 0
      paddlers/models/paddleseg/models/layers/tensor_fusion_helper.py
  87. 0 0
      paddlers/models/paddleseg/models/layers/wrap_functions.py
  88. 0 0
      paddlers/models/paddleseg/models/losses/__init__.py
  89. 1 1
      paddlers/models/paddleseg/models/losses/binary_cross_entropy_loss.py
  90. 1 1
      paddlers/models/paddleseg/models/losses/bootstrapped_cross_entropy.py
  91. 1 1
      paddlers/models/paddleseg/models/losses/cross_entropy_loss.py
  92. 1 1
      paddlers/models/paddleseg/models/losses/decoupledsegnet_relax_boundary_loss.py
  93. 1 1
      paddlers/models/paddleseg/models/losses/detail_aggregate_loss.py
  94. 1 1
      paddlers/models/paddleseg/models/losses/dice_loss.py
  95. 2 2
      paddlers/models/paddleseg/models/losses/edge_attention_loss.py
  96. 1 1
      paddlers/models/paddleseg/models/losses/focal_loss.py
  97. 1 1
      paddlers/models/paddleseg/models/losses/gscnn_dual_task_loss.py
  98. 1 1
      paddlers/models/paddleseg/models/losses/kl_loss.py
  99. 1 1
      paddlers/models/paddleseg/models/losses/l1_loss.py
  100. 3 3
      paddlers/models/paddleseg/models/losses/lovasz_loss.py

+ 3 - 0
.gitignore

@@ -132,5 +132,8 @@ dmypy.json
 # Pyre type checker
 .pyre/
 
+# Training output
 /tutorials/train/**/output/
 /log
+
+/playground/

+ 1 - 0
.pre-commit-config.yaml

@@ -1,3 +1,4 @@
+exclude: ^paddlers/models
 repos:
 -   repo: https://github.com/PaddlePaddle/mirrors-yapf.git
     rev: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37

+ 2 - 5
paddlers/__init__.py

@@ -15,10 +15,7 @@
 __version__ = '0.0.0.dev0'
 
 from paddlers.utils.env import get_environ_info, init_parallel_env
-init_parallel_env()
+from . import tasks, datasets, transforms, utils, tools, models, deploy
 
+init_parallel_env()
 env_info = get_environ_info()
-
-log_level = 2
-
-from . import tasks, datasets, transforms, utils, tools, models, deploy

+ 11 - 5
paddlers/models/__init__.py

@@ -12,8 +12,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import ppcls, ppdet, ppseg, ppgan
-import paddlers.models.ppseg.models.losses as seg_losses
-import paddlers.models.ppdet.modeling.losses as det_losses
-import paddlers.models.ppcls.loss as clas_losses
-import paddlers.models.ppgan.models.criterions as res_losses
+from . import paddleseg as ppseg
+
+from .ppcls import loss as clas_losses
+# TODO: Disable ppdet import warning
+from .ppdet.modeling import losses as det_losses
+from .paddleseg.models import losses as seg_losses
+from .ppgan.models import criterions as res_losses
+
+# Initialize ppcls logger, otherwise it raises error
+from .ppcls.utils import logger as ppcls_logger
+ppcls_logger.init_logger()

+ 0 - 2
paddlers/models/hash.txt

@@ -1,2 +0,0 @@
-ppdet ba2aad26e6bc1e5c2dad76ca96692a0d63eccfac
-ppseg f6c73b478cdf00f40ae69edd35bf6bce2a1687ef

+ 1 - 1
paddlers/models/ppseg/__init__.py → paddlers/models/paddleseg/__init__.py

@@ -14,4 +14,4 @@
 
 from . import models, datasets, transforms
 
-__version__ = 'develop'
+__version__ = '2.7.0'

+ 0 - 0
paddlers/models/ppseg/core/__init__.py → paddlers/models/paddleseg/core/__init__.py


+ 4 - 4
paddlers/models/ppseg/core/infer.py → paddlers/models/paddleseg/core/infer.py

@@ -34,10 +34,10 @@ def reverse_transform(pred, trans_info, mode='nearest'):
             h, w = item[1][0], item[1][1]
             if paddle.get_device() == 'cpu' and dtype in intTypeList:
                 pred = paddle.cast(pred, 'float32')
-                pred = F.interpolate(pred, (h, w), mode=mode)
+                pred = F.interpolate(pred, [h, w], mode=mode)
                 pred = paddle.cast(pred, dtype)
             else:
-                pred = F.interpolate(pred, (h, w), mode=mode)
+                pred = F.interpolate(pred, [h, w], mode=mode)
         elif trans_mode == 'padding':
             h, w = item[1][0], item[1][1]
             pred = pred[:, :, 0:h, 0:w]
@@ -211,7 +211,7 @@ def aug_inference(model,
     for scale in scales:
         h = int(h_input * scale + 0.5)
         w = int(w_input * scale + 0.5)
-        im = F.interpolate(im, (h, w), mode='bilinear')
+        im = F.interpolate(im, [h, w], mode='bilinear')
         for flip in flip_comb:
             im_flip = tensor_flip(im, flip)
             logit = inference(
@@ -221,7 +221,7 @@ def aug_inference(model,
                 crop_size=crop_size,
                 stride=stride)
             logit = tensor_flip(logit, flip)
-            logit = F.interpolate(logit, (h_input, w_input), mode='bilinear')
+            logit = F.interpolate(logit, [h_input, w_input], mode='bilinear')
 
             logit = F.softmax(logit, axis=1)
             final_logit = final_logit + logit

+ 3 - 3
paddlers/models/ppseg/core/predict.py → paddlers/models/paddleseg/core/predict.py

@@ -19,9 +19,9 @@ import cv2
 import numpy as np
 import paddle
 
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.core import infer
-from paddlers.models.ppseg.utils import logger, progbar, visualize
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.core import infer
+from paddlers.models.paddleseg.utils import logger, progbar, visualize
 
 
 def mkdir(path):

+ 5 - 6
paddlers/models/ppseg/core/train.py → paddlers/models/paddleseg/core/train.py

@@ -20,10 +20,9 @@ import shutil
 import paddle
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.utils import (TimeAverager, calculate_eta, resume,
-                                         logger, worker_init_fn, train_profiler,
-                                         op_flops_funs)
-from paddlers.models.ppseg.core.val import evaluate
+from paddlers.models.paddleseg.utils import (TimeAverager, calculate_eta, resume, logger,
+                             worker_init_fn, train_profiler, op_flops_funs)
+from paddlers.models.paddleseg.core.val import evaluate
 
 
 def check_logits_losses(logits_list, losses):
@@ -329,7 +328,7 @@ def train(model,
             model, [1, c, h, w],
             custom_ops={paddle.nn.SyncBatchNorm: op_flops_funs.count_syncbn})
 
-    # Sleep for half a second to let dataloader release resources.
-    time.sleep(0.5)
+    # Sleep for a second to let dataloader release resources.
+    time.sleep(1)
     if use_vdl:
         log_writer.close()

+ 2 - 2
paddlers/models/ppseg/core/val.py → paddlers/models/paddleseg/core/val.py

@@ -19,8 +19,8 @@ import time
 import paddle
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.utils import metrics, TimeAverager, calculate_eta, logger, progbar
-from paddlers.models.ppseg.core import infer
+from paddlers.models.paddleseg.utils import metrics, TimeAverager, calculate_eta, logger, progbar
+from paddlers.models.paddleseg.core import infer
 
 np.set_printoptions(suppress=True)
 

+ 0 - 0
paddlers/models/ppseg/cvlibs/__init__.py → paddlers/models/paddleseg/cvlibs/__init__.py


+ 2 - 2
paddlers/models/ppseg/cvlibs/callbacks.py → paddlers/models/paddleseg/cvlibs/callbacks.py

@@ -19,8 +19,8 @@ import numpy as np
 import paddle
 from paddle.distributed.parallel import ParallelEnv
 from visualdl import LogWriter
-from paddlers.models.ppseg.utils.progbar import Progbar
-import paddlers.models.ppseg.utils.logger as logger
+from paddlers.models.paddleseg.utils.progbar import Progbar
+import paddlers.models.paddleseg.utils.logger as logger
 
 
 class CallbackList(object):

+ 3 - 3
paddlers/models/ppseg/cvlibs/config.py → paddlers/models/paddleseg/cvlibs/config.py

@@ -22,8 +22,8 @@ import paddle
 import yaml
 import six
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import logger
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import logger
 
 
 class Config(object):
@@ -54,7 +54,7 @@ class Config(object):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs.config import Config
+        from paddlers.models.paddleseg.cvlibs.config import Config
 
         # Create a cfg object with yaml file path.
         cfg = Config(yaml_cfg_path)

+ 2 - 2
paddlers/models/ppseg/cvlibs/manager.py → paddlers/models/paddleseg/cvlibs/manager.py

@@ -31,7 +31,7 @@ class ComponentManager:
 
     Examples 1:
 
-        from paddlers.models.ppseg.cvlibs.manager import ComponentManager
+        from paddlers.models.paddleseg.cvlibs.manager import ComponentManager
 
         model_manager = ComponentManager()
 
@@ -49,7 +49,7 @@ class ComponentManager:
     Examples 2:
 
         # Or an easier way, using it as a Python decorator, while just add it above the class declaration.
-        from paddlers.models.ppseg.cvlibs.manager import ComponentManager
+        from paddlers.models.paddleseg.cvlibs.manager import ComponentManager
 
         model_manager = ComponentManager()
 

+ 26 - 6
paddlers/models/ppseg/cvlibs/param_init.py → paddlers/models/paddleseg/cvlibs/param_init.py

@@ -24,7 +24,7 @@ def constant_init(param, **kwargs):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs import param_init
+        from paddlers.models.paddleseg.cvlibs import param_init
         import paddle.nn as nn
 
         linear = nn.Linear(2, 4)
@@ -46,7 +46,7 @@ def normal_init(param, **kwargs):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs import param_init
+        from paddlers.models.paddleseg.cvlibs import param_init
         import paddle.nn as nn
 
         linear = nn.Linear(2, 4)
@@ -79,7 +79,7 @@ def kaiming_normal_init(param, **kwargs):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs import param_init
+        from paddlers.models.paddleseg.cvlibs import param_init
         import paddle.nn as nn
 
         linear = nn.Linear(2, 4)
@@ -91,6 +91,26 @@ def kaiming_normal_init(param, **kwargs):
     initializer(param, param.block)
 
 
+def trunc_normal_init(param, **kwargs):
+    r"""
+    Initialize the input tensor with The Random TruncatedNormal (Gaussian) distribution initializer.
+
+    Args:
+        param (Tensor): Tensor that needs to be initialized.
+
+    Examples:
+
+        from paddlers.models.paddleseg.cvlibs import param_init
+        import paddle.nn as nn
+
+        linear = nn.Linear(2, 4)
+        param_init.trunc_normal_init(linear.weight, mean=0.0, std=0.02)
+
+    """
+    initializer = nn.initializer.TruncatedNormal(**kwargs)
+    initializer(param, param.block)
+
+
 def kaiming_uniform(param, **kwargs):
     r"""Implements the Kaiming Uniform initializer
     This class implements the weight initialization from the paper
@@ -109,7 +129,7 @@ def kaiming_uniform(param, **kwargs):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs import param_init
+        from paddlers.models.paddleseg.cvlibs import param_init
         import paddle.nn as nn
 
         linear = nn.Linear(2, 4)
@@ -136,11 +156,11 @@ def xavier_uniform(param, **kwargs):
 
     Examples:
 
-        from paddlers.models.ppseg.cvlibs import param_init
+        from paddlers.models.paddleseg.cvlibs import param_init
         import paddle.nn as nn
 
         linear = nn.Linear(2, 4)
         param_init.xavier_uniform(linear.weight)
     """
     initializer = nn.initializer.XavierUniform(**kwargs)
-    initializer(param, param.block)
+    initializer(param, param.block)

+ 0 - 0
paddlers/models/ppseg/datasets/__init__.py → paddlers/models/paddleseg/datasets/__init__.py


+ 6 - 6
paddlers/models/ppseg/datasets/ade.py → paddlers/models/paddleseg/datasets/ade.py

@@ -17,12 +17,12 @@ import os
 import numpy as np
 from PIL import Image
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-import paddlers.models.ppseg.transforms.functional as F
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+import paddlers.models.paddleseg.transforms.functional as F
 
 URL = "http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip"
 

+ 5 - 5
paddlers/models/ppseg/datasets/chase_db1.py → paddlers/models/paddleseg/datasets/chase_db1.py

@@ -14,11 +14,11 @@
 
 import os
 
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
 
 URL = 'https://bj.bcebos.com/paddleseg/dataset/chase_db1/chase_db1.zip'
 

+ 3 - 3
paddlers/models/ppseg/datasets/cityscapes.py → paddlers/models/paddleseg/datasets/cityscapes.py

@@ -15,9 +15,9 @@
 import os
 import glob
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 
 @manager.DATASETS.add_component

+ 3 - 3
paddlers/models/ppseg/datasets/cocostuff.py → paddlers/models/paddleseg/datasets/cocostuff.py

@@ -15,9 +15,9 @@
 import os
 import glob
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 
 @manager.DATASETS.add_component

+ 5 - 5
paddlers/models/ppseg/datasets/dataset.py → paddlers/models/paddleseg/datasets/dataset.py

@@ -18,9 +18,9 @@ import paddle
 import numpy as np
 from PIL import Image
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-import paddlers.models.ppseg.transforms.functional as F
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+import paddlers.models.paddleseg.transforms.functional as F
 
 
 @manager.DATASETS.add_component
@@ -46,8 +46,8 @@ class Dataset(paddle.io.Dataset):
 
         Examples:
 
-            import paddlers.models.ppseg as ppseg.transforms as T
-            from paddlers.models.ppseg.datasets import Dataset
+            import paddleseg.transforms as T
+            from paddlers.models.paddleseg.datasets import Dataset
 
             transforms = [T.RandomPaddingCrop(crop_size=(512,512)), T.Normalize()]
             dataset_root = 'dataset_root_path'

+ 5 - 5
paddlers/models/ppseg/datasets/drive.py → paddlers/models/paddleseg/datasets/drive.py

@@ -14,11 +14,11 @@
 
 import os
 
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
 
 URL = 'https://bj.bcebos.com/paddleseg/dataset/drive/drive.zip'
 

+ 6 - 6
paddlers/models/ppseg/datasets/eg1800.py → paddlers/models/paddleseg/datasets/eg1800.py

@@ -18,12 +18,12 @@ import copy
 import cv2
 import numpy as np
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-import paddlers.models.ppseg.transforms.functional as F
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+import paddlers.models.paddleseg.transforms.functional as F
 
 URL = "https://paddleseg.bj.bcebos.com/dataset/EG1800.zip"
 

+ 5 - 5
paddlers/models/ppseg/datasets/hrf.py → paddlers/models/paddleseg/datasets/hrf.py

@@ -14,11 +14,11 @@
 
 import os
 
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
 
 URL = 'https://bj.bcebos.com/paddleseg/dataset/hrf/hrf.zip'
 

+ 4 - 4
paddlers/models/ppseg/datasets/mini_deep_globe_road_extraction.py → paddlers/models/paddleseg/datasets/mini_deep_globe_road_extraction.py

@@ -15,10 +15,10 @@
 import os
 
 from .dataset import Dataset
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 URL = "https://paddleseg.bj.bcebos.com/dataset/MiniDeepGlobeRoadExtraction.zip"
 

+ 4 - 4
paddlers/models/ppseg/datasets/optic_disc_seg.py → paddlers/models/paddleseg/datasets/optic_disc_seg.py

@@ -15,10 +15,10 @@
 import os
 
 from .dataset import Dataset
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 URL = "https://paddleseg.bj.bcebos.com/dataset/optic_disc_seg.zip"
 

+ 4 - 4
paddlers/models/ppseg/datasets/pascal_context.py → paddlers/models/paddleseg/datasets/pascal_context.py

@@ -15,16 +15,16 @@
 import os
 
 from PIL import Image
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 
 @manager.DATASETS.add_component
 class PascalContext(Dataset):
     """
     PascalVOC2010 dataset `http://host.robots.ox.ac.uk/pascal/VOC/`.
-    If you want to use pascal context dataset, please run the convert_voc2010.py in tools firstly.
+    If you want to use pascal context dataset, please run the convert_voc2010.py in tools/data firstly.
 
     Args:
         transforms (list): Transforms for image.

+ 2 - 2
paddlers/models/ppseg/datasets/pp_humanseg14k.py → paddlers/models/paddleseg/datasets/pp_humanseg14k.py

@@ -15,8 +15,8 @@
 import os
 
 from .dataset import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 
 @manager.DATASETS.add_component

+ 10 - 7
paddlers/models/ppseg/datasets/pssl.py → paddlers/models/paddleseg/datasets/pssl.py

@@ -15,9 +15,9 @@
 import os
 import numpy as np
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 
 @manager.DATASETS.add_component
@@ -120,8 +120,9 @@ class PSSLDataset(Dataset):
 
     def __getitem__(self, idx):
         image_path, label_path = self.file_list[idx]
+        data = {'img': image_path, 'trans_info': [], 'gt_fields': []}
 
-        # transform label
+        # get class_id by image path
         class_name = (image_path.split('/')[-1]).split('_')[0]
         class_id = self.class_id_dict[class_name]
 
@@ -129,7 +130,9 @@ class PSSLDataset(Dataset):
         gt_semantic_seg = np.zeros_like(pssl_seg, dtype=np.int64) + 1000
         # [0, 999] for imagenet classes, 1000 for background, others(-1) will be ignored during training.
         gt_semantic_seg[pssl_seg == 1] = class_id
+        data['label'] = gt_semantic_seg
 
-        im, label = self.transforms(im=image_path, label=gt_semantic_seg)
-
-        return im, label
+        if self.mode == 'train':
+            data['gt_fields'].append('label')
+        data = self.transforms(data)
+        return data

+ 5 - 5
paddlers/models/ppseg/datasets/stare.py → paddlers/models/paddleseg/datasets/stare.py

@@ -14,11 +14,11 @@
 
 import os
 
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
 
 URL = 'https://bj.bcebos.com/paddleseg/dataset/stare/stare.zip'
 

+ 6 - 6
paddlers/models/ppseg/datasets/supervisely.py → paddlers/models/paddleseg/datasets/supervisely.py

@@ -18,12 +18,12 @@ import copy
 import cv2
 import numpy as np
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-import paddlers.models.ppseg.transforms.functional as F
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+import paddlers.models.paddleseg.transforms.functional as F
 
 URL = "https://paddleseg.bj.bcebos.com/dataset/Supervisely_face.zip"
 

+ 6 - 6
paddlers/models/ppseg/datasets/voc.py → paddlers/models/paddleseg/datasets/voc.py

@@ -14,11 +14,11 @@
 
 import os
 
-from paddlers.models.ppseg.datasets import Dataset
-from paddlers.models.ppseg.utils.download import download_file_and_uncompress
-from paddlers.models.ppseg.utils import seg_env
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.transforms import Compose
+from paddlers.models.paddleseg.datasets import Dataset
+from paddlers.models.paddleseg.utils.download import download_file_and_uncompress
+from paddlers.models.paddleseg.utils import seg_env
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.transforms import Compose
 
 URL = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar"
 
@@ -27,7 +27,7 @@ URL = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.ta
 class PascalVOC(Dataset):
     """
     PascalVOC2012 dataset `http://host.robots.ox.ac.uk/pascal/VOC/`.
-    If you want to augment the dataset, please run the voc_augment.py in tools.
+    If you want to augment the dataset, please run the voc_augment.py in tools/data.
 
     Args:
         transforms (list): Transforms for image.

+ 1 - 0
paddlers/models/paddleseg/hash.txt

@@ -0,0 +1 @@
+13aac35b31b147d2dd9976821ade13dabebff685

+ 2 - 0
paddlers/models/ppseg/models/__init__.py → paddlers/models/paddleseg/models/__init__.py

@@ -63,4 +63,6 @@ from .mobileseg import MobileSeg
 from .upernet import UPerNet
 from .sinet import SINet
 from .lraspp import LRASPP
+from .mscale_ocrnet import MscaleOCRNet
 from .topformer import TopFormer
+from .rtformer import RTFormer

+ 3 - 3
paddlers/models/ppseg/models/ann.py → paddlers/models/paddleseg/models/ann.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/attention_unet.py → paddlers/models/paddleseg/models/attention_unet.py

@@ -14,9 +14,9 @@
 
 import paddle
 import paddle.nn as nn
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg import utils
 import numpy as np
 
 

+ 1 - 0
paddlers/models/ppseg/models/backbones/__init__.py → paddlers/models/paddleseg/models/backbones/__init__.py

@@ -25,3 +25,4 @@ from .lite_hrnet import *
 from .shufflenetv2 import *
 from .ghostnet import *
 from .top_transformer import *
+from .uhrnet import *

+ 2 - 2
paddlers/models/ppseg/models/backbones/ghostnet.py → paddlers/models/paddleseg/models/backbones/ghostnet.py

@@ -23,8 +23,8 @@ from paddle.nn import Conv2D, BatchNorm, AdaptiveAvgPool2D, Linear
 from paddle.regularizer import L2Decay
 from paddle.nn.initializer import Uniform, KaimingNormal
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils, logger
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils, logger
 
 __all__ = ["GhostNet_x0_5", "GhostNet_x1_0", "GhostNet_x1_3"]
 

+ 29 - 14
paddlers/models/ppseg/models/backbones/hrnet.py → paddlers/models/paddleseg/models/backbones/hrnet.py

@@ -18,9 +18,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 __all__ = [
     "HRNet_W18_Small_V1", "HRNet_W18_Small_V2", "HRNet_W18", "HRNet_W30",
@@ -54,6 +54,7 @@ class HRNet(nn.Layer):
         has_se (bool, optional): Whether to use Squeeze-and-Excitation module. Default False.
         align_corners (bool, optional): An argument of F.interpolate. It should be set to False when the feature size is even,
             e.g. 1024x512, otherwise it is True, e.g. 769x769. Default: False.
+        use_psa (bool, optional): Usage of the polarized self attention moudle. Default False.
     """
 
     def __init__(self,
@@ -73,7 +74,8 @@ class HRNet(nn.Layer):
                  stage4_num_channels=(18, 36, 72, 144),
                  has_se=False,
                  align_corners=False,
-                 padding_same=True):
+                 padding_same=True,
+                 use_psa=False):
         super(HRNet, self).__init__()
         self.pretrained = pretrained
         self.stage1_num_modules = stage1_num_modules
@@ -130,7 +132,8 @@ class HRNet(nn.Layer):
             has_se=self.has_se,
             name="st2",
             align_corners=align_corners,
-            padding_same=padding_same)
+            padding_same=padding_same,
+            use_psa=use_psa)
 
         self.tr2 = TransitionLayer(
             in_channels=self.stage2_num_channels,
@@ -145,7 +148,8 @@ class HRNet(nn.Layer):
             has_se=self.has_se,
             name="st3",
             align_corners=align_corners,
-            padding_same=padding_same)
+            padding_same=padding_same,
+            use_psa=use_psa)
 
         self.tr3 = TransitionLayer(
             in_channels=self.stage3_num_channels,
@@ -160,7 +164,8 @@ class HRNet(nn.Layer):
             has_se=self.has_se,
             name="st4",
             align_corners=align_corners,
-            padding_same=padding_same)
+            padding_same=padding_same,
+            use_psa=use_psa)
 
         self.init_weight()
 
@@ -284,7 +289,8 @@ class Branches(nn.Layer):
                  out_channels,
                  has_se=False,
                  name=None,
-                 padding_same=True):
+                 padding_same=True,
+                 use_psa=False):
         super(Branches, self).__init__()
 
         self.basic_block_list = []
@@ -301,7 +307,8 @@ class Branches(nn.Layer):
                         has_se=has_se,
                         name=name + '_branch_layer_' + str(i + 1) + '_' +
                         str(j + 1),
-                        padding_same=padding_same))
+                        padding_same=padding_same,
+                        use_psa=use_psa))
                 self.basic_block_list[i].append(basic_block_func)
 
     def forward(self, x):
@@ -390,7 +397,8 @@ class BasicBlock(nn.Layer):
                  has_se=False,
                  downsample=False,
                  name=None,
-                 padding_same=True):
+                 padding_same=True,
+                 use_psa=False):
         super(BasicBlock, self).__init__()
 
         self.has_se = has_se
@@ -403,6 +411,8 @@ class BasicBlock(nn.Layer):
             stride=stride,
             padding=1 if not padding_same else 'same',
             bias_attr=False)
+        self.deattn = layers.PolarizedSelfAttentionModule(
+            num_filters, num_filters) if use_psa else nn.Identity()
         self.conv2 = layers.ConvBN(
             in_channels=num_filters,
             out_channels=num_filters,
@@ -430,6 +440,7 @@ class BasicBlock(nn.Layer):
     def forward(self, x):
         residual = x
         conv1 = self.conv1(x)
+        conv1 = self.deattn(conv1)
         conv2 = self.conv2(conv1)
 
         if self.downsample:
@@ -489,7 +500,8 @@ class Stage(nn.Layer):
                  multi_scale_output=True,
                  name=None,
                  align_corners=False,
-                 padding_same=True):
+                 padding_same=True,
+                 use_psa=False):
         super(Stage, self).__init__()
 
         self._num_modules = num_modules
@@ -518,7 +530,8 @@ class Stage(nn.Layer):
                         has_se=has_se,
                         name=name + '_' + str(i + 1),
                         align_corners=align_corners,
-                        padding_same=padding_same))
+                        padding_same=padding_same,
+                        use_psa=use_psa))
 
             self.stage_func_list.append(stage_func)
 
@@ -538,7 +551,8 @@ class HighResolutionModule(nn.Layer):
                  multi_scale_output=True,
                  name=None,
                  align_corners=False,
-                 padding_same=True):
+                 padding_same=True,
+                 use_psa=False):
         super(HighResolutionModule, self).__init__()
 
         self.branches_func = Branches(
@@ -547,7 +561,8 @@ class HighResolutionModule(nn.Layer):
             out_channels=num_filters,
             has_se=has_se,
             name=name,
-            padding_same=padding_same)
+            padding_same=padding_same,
+            use_psa=use_psa)
 
         self.fuse_func = FuseLayers(
             in_channels=num_filters,

+ 2 - 2
paddlers/models/ppseg/models/backbones/lite_hrnet.py → paddlers/models/paddleseg/models/backbones/lite_hrnet.py

@@ -24,8 +24,8 @@ from paddle import ParamAttr
 from paddle.regularizer import L2Decay
 from paddle.nn.initializer import Normal, Constant
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg import utils
 
 __all__ = [
     "Lite_HRNet_18", "Lite_HRNet_30", "Lite_HRNet_naive",

+ 3 - 3
paddlers/models/ppseg/models/backbones/mix_transformer.py → paddlers/models/paddleseg/models/backbones/mix_transformer.py

@@ -20,9 +20,9 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 import paddle.nn.initializer as paddle_init
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
-from paddlers.models.ppseg.models.backbones.transformer_utils import *
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
+from paddlers.models.paddleseg.models.backbones.transformer_utils import *
 
 
 class Mlp(nn.Layer):

+ 2 - 2
paddlers/models/ppseg/models/backbones/mobilenetv2.py → paddlers/models/paddleseg/models/backbones/mobilenetv2.py

@@ -19,8 +19,8 @@ import paddle.nn.functional as F
 from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
 from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg import utils
 
 __all__ = [
     "MobileNetV2_x0_25",

+ 3 - 3
paddlers/models/ppseg/models/backbones/mobilenetv3.py → paddlers/models/paddleseg/models/backbones/mobilenetv3.py

@@ -18,9 +18,9 @@ from paddle import ParamAttr
 from paddle.regularizer import L2Decay
 from paddle.nn import AdaptiveAvgPool2D, BatchNorm, Conv2D, Dropout, Linear
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils, logger
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils, logger
+from paddlers.models.paddleseg.models import layers
 
 __all__ = [
     "MobileNetV3_small_x0_35", "MobileNetV3_small_x0_5",

+ 3 - 3
paddlers/models/ppseg/models/backbones/resnet_vd.py → paddlers/models/paddleseg/models/backbones/resnet_vd.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 __all__ = [
     "ResNet18_vd", "ResNet34_vd", "ResNet50_vd", "ResNet101_vd", "ResNet152_vd"

+ 2 - 2
paddlers/models/ppseg/models/backbones/shufflenetv2.py → paddlers/models/paddleseg/models/backbones/shufflenetv2.py

@@ -18,8 +18,8 @@ from paddle.nn import Layer, Conv2D, MaxPool2D, AdaptiveAvgPool2D, BatchNorm, Li
 from paddle.nn.initializer import KaimingNormal
 from paddle.nn.functional import swish
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils, logger
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils, logger
 
 __all__ = [
     'ShuffleNetV2_x0_25', 'ShuffleNetV2_x0_33', 'ShuffleNetV2_x0_5',

+ 3 - 3
paddlers/models/ppseg/models/backbones/stdcnet.py → paddlers/models/paddleseg/models/backbones/stdcnet.py

@@ -17,9 +17,9 @@ import math
 import paddle
 import paddle.nn as nn
 
-from paddlers.models.ppseg.utils import utils
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models.layers.layer_libs import SyncBatchNorm
+from paddlers.models.paddleseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models.layers.layer_libs import SyncBatchNorm
 
 __all__ = ["STDC1", "STDC2"]
 

+ 3 - 3
paddlers/models/ppseg/models/backbones/swin_transformer.py → paddlers/models/paddleseg/models/backbones/swin_transformer.py

@@ -17,9 +17,9 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 import numpy as np
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
-from paddlers.models.ppseg.models.backbones.transformer_utils import *
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
+from paddlers.models.paddleseg.models.backbones.transformer_utils import *
 
 
 class Mlp(nn.Layer):

+ 3 - 3
paddlers/models/ppseg/models/backbones/top_transformer.py → paddlers/models/paddleseg/models/backbones/top_transformer.py

@@ -19,9 +19,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.models.backbones.transformer_utils import Identity, DropPath
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.models.backbones.transformer_utils import Identity, DropPath
 
 __all__ = ["TopTransformer_Base", "TopTransformer_Small", "TopTransformer_Tiny"]
 

+ 0 - 0
paddlers/models/ppseg/models/backbones/transformer_utils.py → paddlers/models/paddleseg/models/backbones/transformer_utils.py


+ 949 - 0
paddlers/models/paddleseg/models/backbones/uhrnet.py

@@ -0,0 +1,949 @@
+# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+import math
+
+import paddle
+import paddle.nn as nn
+import paddle.nn.functional as F
+
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
+
+__all__ = ["UHRNet_W18_Small", "UHRNet_W18", "UHRNet_W48"]
+
+
+class UHRNet(nn.Layer):
+    """
+    The UHRNet implementation based on PaddlePaddle.
+
+    The original article refers to
+    Jian Wang, et, al. "U-HRNet: Delving into Improving Semantic Representation of High Resolution Network for Dense Prediction"
+    (https://arxiv.org/pdf/2210.07140.pdf).
+
+    Args:
+        in_channels (int, optional): The channels of input image. Default: 3.
+        pretrained (str): The path of pretrained model.
+        stage1_num_modules (int): Number of modules for stage1. Default 1.
+        stage1_num_blocks (list): Number of blocks per module for stage1. Default [4].
+        stage1_num_channels (list): Number of channels per branch for stage1. Default [64].
+        stage2_num_modules (int): Number of modules for stage2. Default 1.
+        stage2_num_blocks (list): Number of blocks per module for stage2. Default [4, 4]
+        stage2_num_channels (list): Number of channels per branch for stage2. Default [18, 36].
+        stage3_num_modules (int): Number of modules for stage3. Default 5.
+        stage3_num_blocks (list): Number of blocks per module for stage3. Default [4, 4]
+        stage3_num_channels (list): Number of channels per branch for stage3. Default [36, 72].
+        stage4_num_modules (int): Number of modules for stage4. Default 2.
+        stage4_num_blocks (list): Number of blocks per module for stage4. Default [4, 4]
+        stage4_num_channels (list): Number of channels per branch for stage4. Default [72. 144].
+        stage5_num_modules (int): Number of modules for stage5. Default 2.
+        stage5_num_blocks (list): Number of blocks per module for stage5. Default [4, 4]
+        stage5_num_channels (list): Number of channels per branch for stage5. Default [144, 288].
+        stage6_num_modules (int): Number of modules for stage6. Default 1.
+        stage6_num_blocks (list): Number of blocks per module for stage6. Default [4, 4]
+        stage6_num_channels (list): Number of channels per branch for stage6. Default [72. 144].
+        stage7_num_modules (int): Number of modules for stage7. Default 1.
+        stage7_num_blocks (list): Number of blocks per module for stage7. Default [4, 4]
+        stage7_num_channels (list): Number of channels per branch for stage7. Default [36, 72].
+        stage8_num_modules (int): Number of modules for stage8. Default 1.
+        stage8_num_blocks (list): Number of blocks per module for stage8. Default [4, 4]
+        stage8_num_channels (list): Number of channels per branch for stage8. Default [18, 36].
+        stage9_num_modules (int): Number of modules for stage9. Default 1.
+        stage9_num_blocks (list): Number of blocks per module for stage9. Default [4]
+        stage9_num_channels (list): Number of channels per branch for stage9. Default [18].
+        has_se (bool): Whether to use Squeeze-and-Excitation module. Default False.
+        align_corners (bool, optional): An argument of F.interpolate. It should be set to False when the feature size is even,
+            e.g. 1024x512, otherwise it is True, e.g. 769x769. Default: False.
+    """
+
+    def __init__(self,
+                 in_channels=3,
+                 pretrained=None,
+                 stage1_num_modules=1,
+                 stage1_num_blocks=(4, ),
+                 stage1_num_channels=(64, ),
+                 stage2_num_modules=1,
+                 stage2_num_blocks=(4, 4),
+                 stage2_num_channels=(18, 36),
+                 stage3_num_modules=5,
+                 stage3_num_blocks=(4, 4),
+                 stage3_num_channels=(36, 72),
+                 stage4_num_modules=2,
+                 stage4_num_blocks=(4, 4),
+                 stage4_num_channels=(72, 144),
+                 stage5_num_modules=2,
+                 stage5_num_blocks=(4, 4),
+                 stage5_num_channels=(144, 288),
+                 stage6_num_modules=1,
+                 stage6_num_blocks=(4, 4),
+                 stage6_num_channels=(72, 144),
+                 stage7_num_modules=1,
+                 stage7_num_blocks=(4, 4),
+                 stage7_num_channels=(36, 72),
+                 stage8_num_modules=1,
+                 stage8_num_blocks=(4, 4),
+                 stage8_num_channels=(18, 36),
+                 stage9_num_modules=1,
+                 stage9_num_blocks=(4, ),
+                 stage9_num_channels=(18, ),
+                 has_se=False,
+                 align_corners=False):
+        super(UHRNet, self).__init__()
+        self.pretrained = pretrained
+        self.has_se = has_se
+        self.align_corners = align_corners
+        self.feat_channels = [
+            sum([
+                stage5_num_channels[-1], stage6_num_channels[-1],
+                stage7_num_channels[-1], stage8_num_channels[-1],
+                stage9_num_channels[-1]
+            ]) // 2
+        ]
+
+        cur_stride = 1
+        # stem net
+        self.conv_layer1_1 = layers.ConvBNReLU(
+            in_channels=3,
+            out_channels=64,
+            kernel_size=3,
+            stride=2,
+            padding='same',
+            bias_attr=False)
+        cur_stride *= 2
+
+        self.conv_layer1_2 = layers.ConvBNReLU(
+            in_channels=64,
+            out_channels=64,
+            kernel_size=3,
+            stride=2,
+            padding='same',
+            bias_attr=False)
+        cur_stride *= 2
+
+        self.la1 = Layer1(
+            num_channels=64,
+            num_blocks=stage1_num_blocks[0],
+            num_filters=stage1_num_channels[0],
+            has_se=has_se,
+            name="layer2")
+
+        self.tr1 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage1_num_channels[0] * 4,
+            stride_cur=[
+                cur_stride * (2**i) for i in range(len(stage2_num_channels))
+            ],
+            out_channels=stage2_num_channels,
+            align_corners=self.align_corners,
+            name="tr1")
+        self.st2 = Stage(
+            num_channels=stage2_num_channels,
+            num_modules=stage2_num_modules,
+            num_blocks=stage2_num_blocks,
+            num_filters=stage2_num_channels,
+            has_se=self.has_se,
+            name="st2",
+            align_corners=align_corners)
+        cur_stride *= 2
+
+        self.tr2 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage2_num_channels[-1],
+            stride_cur=[
+                cur_stride * (2**i) for i in range(len(stage3_num_channels))
+            ],
+            out_channels=stage3_num_channels,
+            align_corners=self.align_corners,
+            name="tr2")
+        self.st3 = Stage(
+            num_channels=stage3_num_channels,
+            num_modules=stage3_num_modules,
+            num_blocks=stage3_num_blocks,
+            num_filters=stage3_num_channels,
+            has_se=self.has_se,
+            name="st3",
+            align_corners=align_corners)
+        cur_stride *= 2
+
+        self.tr3 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage3_num_channels[-1],
+            stride_cur=[
+                cur_stride * (2**i) for i in range(len(stage4_num_channels))
+            ],
+            out_channels=stage4_num_channels,
+            align_corners=self.align_corners,
+            name="tr3")
+        self.st4 = Stage(
+            num_channels=stage4_num_channels,
+            num_modules=stage4_num_modules,
+            num_blocks=stage4_num_blocks,
+            num_filters=stage4_num_channels,
+            has_se=self.has_se,
+            name="st4",
+            align_corners=align_corners)
+        cur_stride *= 2
+
+        self.tr4 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage4_num_channels[-1],
+            stride_cur=[
+                cur_stride * (2**i) for i in range(len(stage5_num_channels))
+            ],
+            out_channels=stage5_num_channels,
+            align_corners=self.align_corners,
+            name="tr4")
+        self.st5 = Stage(
+            num_channels=stage5_num_channels,
+            num_modules=stage5_num_modules,
+            num_blocks=stage5_num_blocks,
+            num_filters=stage5_num_channels,
+            has_se=self.has_se,
+            name="st5",
+            align_corners=align_corners)
+
+        self.tr5 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage5_num_channels[0],
+            stride_cur=[
+                cur_stride // (2**(len(stage6_num_channels) - i - 1))
+                for i in range(len(stage6_num_channels))
+            ],
+            out_channels=stage6_num_channels,
+            align_corners=self.align_corners,
+            name="tr5")
+        self.st6 = Stage(
+            num_channels=stage6_num_channels,
+            num_modules=stage6_num_modules,
+            num_blocks=stage6_num_blocks,
+            num_filters=stage6_num_channels,
+            has_se=self.has_se,
+            name="st6",
+            align_corners=align_corners)
+        cur_stride = cur_stride // 2
+
+        self.tr6 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage6_num_channels[0],
+            stride_cur=[
+                cur_stride // (2**(len(stage7_num_channels) - i - 1))
+                for i in range(len(stage7_num_channels))
+            ],
+            out_channels=stage7_num_channels,
+            align_corners=self.align_corners,
+            name="tr6")
+        self.st7 = Stage(
+            num_channels=stage7_num_channels,
+            num_modules=stage7_num_modules,
+            num_blocks=stage7_num_blocks,
+            num_filters=stage7_num_channels,
+            has_se=self.has_se,
+            name="st7",
+            align_corners=align_corners)
+        cur_stride = cur_stride // 2
+
+        self.tr7 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage7_num_channels[0],
+            stride_cur=[
+                cur_stride // (2**(len(stage8_num_channels) - i - 1))
+                for i in range(len(stage8_num_channels))
+            ],
+            out_channels=stage8_num_channels,
+            align_corners=self.align_corners,
+            name="tr7")
+        self.st8 = Stage(
+            num_channels=stage8_num_channels,
+            num_modules=stage8_num_modules,
+            num_blocks=stage8_num_blocks,
+            num_filters=stage8_num_channels,
+            has_se=self.has_se,
+            name="st8",
+            align_corners=align_corners)
+        cur_stride = cur_stride // 2
+
+        self.tr8 = TransitionLayer(
+            stride_pre=cur_stride,
+            in_channel=stage8_num_channels[0],
+            stride_cur=[
+                cur_stride // (2**(len(stage9_num_channels) - i - 1))
+                for i in range(len(stage9_num_channels))
+            ],
+            out_channels=stage9_num_channels,
+            align_corners=self.align_corners,
+            name="tr8")
+        self.st9 = Stage(
+            num_channels=stage9_num_channels,
+            num_modules=stage9_num_modules,
+            num_blocks=stage9_num_blocks,
+            num_filters=stage9_num_channels,
+            has_se=self.has_se,
+            name="st9",
+            align_corners=align_corners)
+
+        self.last_layer = nn.Sequential(
+            layers.ConvBNReLU(
+                in_channels=self.feat_channels[0],
+                out_channels=self.feat_channels[0],
+                kernel_size=1,
+                padding='same',
+                stride=1),
+            nn.Conv2D(
+                in_channels=self.feat_channels[0],
+                out_channels=19,
+                kernel_size=1,
+                stride=1,
+                padding=0))
+        self.init_weight()
+
+    def _concat(self, x1, x2):
+        x1 = F.avg_pool3d(
+            x1.unsqueeze(1), kernel_size=(2, 1, 1), stride=(2, 1, 1)).squeeze(1)
+        x2 = F.avg_pool3d(
+            x2.unsqueeze(1), kernel_size=(2, 1, 1), stride=(2, 1, 1)).squeeze(1)
+        return paddle.concat([x1, x2], axis=1)
+
+    def forward(self, x):
+        conv1 = self.conv_layer1_1(x)
+        conv2 = self.conv_layer1_2(conv1)
+
+        la1 = self.la1(conv2)
+
+        tr1 = self.tr1(la1)
+        st2 = self.st2(tr1)
+        skip21 = st2[0]
+
+        tr2 = self.tr2(st2[-1])
+        st3 = self.st3(tr2)
+        skip31 = st3[0]
+
+        tr3 = self.tr3(st3[-1])
+        st4 = self.st4(tr3)
+        skip41 = st4[0]
+
+        tr4 = self.tr4(st4[-1])
+        st5 = self.st5(tr4)
+        x5 = st5[-1]
+
+        tr5 = self.tr5(st5[0], shape=paddle.shape(skip41)[-2:])
+        tr5[0] = self._concat(tr5[0], skip41)
+        st6 = self.st6(tr5)
+        x4 = st6[-1]
+
+        tr6 = self.tr6(st6[0], shape=paddle.shape(skip31)[-2:])
+        tr6[0] = self._concat(tr6[0], skip31)
+        st7 = self.st7(tr6)
+        x3 = st7[-1]
+
+        tr7 = self.tr7(st7[0], shape=paddle.shape(skip21)[-2:])
+        tr7[0] = self._concat(tr7[0], skip21)
+        st8 = self.st8(tr7)
+        x2 = st8[-1]
+
+        tr8 = self.tr8(st8[0])
+        st9 = self.st9(tr8)
+        x1 = st9[-1]
+
+        x = [x1, x2, x3, x4, x5]
+        for i in range(len(x)):
+            x[i] = F.avg_pool3d(
+                x[i].unsqueeze(1), kernel_size=(2, 1, 1), stride=(2, 1,
+                                                                  1)).squeeze(1)
+
+        # upsampling
+        x0_h, x0_w = paddle.shape(x[0])[-2:]
+        for i in range(1, len(x)):
+            x[i] = F.interpolate(
+                x[i],
+                size=[x0_h, x0_w],
+                mode='bilinear',
+                align_corners=self.align_corners)
+        x = paddle.concat(x, axis=1)
+
+        return [x]
+
+    def init_weight(self):
+        for layer in self.sublayers():
+            if isinstance(layer, nn.Conv2D):
+                param_init.normal_init(layer.weight, std=0.001)
+            elif isinstance(layer, (nn.BatchNorm, nn.SyncBatchNorm)):
+                param_init.constant_init(layer.weight, value=1.0)
+                param_init.constant_init(layer.bias, value=0.0)
+        if self.pretrained is not None:
+            utils.load_pretrained_model(self, self.pretrained)
+
+
+class Layer1(nn.Layer):
+    def __init__(self,
+                 num_channels,
+                 num_filters,
+                 num_blocks,
+                 has_se=False,
+                 name=None):
+        super(Layer1, self).__init__()
+
+        self.bottleneck_block_list = []
+
+        for i in range(num_blocks):
+            bottleneck_block = self.add_sublayer(
+                "bb_{}_{}".format(name, i + 1),
+                BottleneckBlock(
+                    num_channels=num_channels if i == 0 else num_filters * 4,
+                    num_filters=num_filters,
+                    has_se=has_se,
+                    stride=1,
+                    downsample=True if i == 0 else False,
+                    name=name + '_' + str(i + 1)))
+            self.bottleneck_block_list.append(bottleneck_block)
+
+    def forward(self, x):
+        conv = x
+        for block_func in self.bottleneck_block_list:
+            conv = block_func(conv)
+        return conv
+
+
+class TransitionLayer(nn.Layer):
+    def __init__(self,
+                 stride_pre,
+                 in_channel,
+                 stride_cur,
+                 out_channels,
+                 align_corners=False,
+                 name=None):
+        super(TransitionLayer, self).__init__()
+        self.align_corners = align_corners
+        num_out = len(out_channels)
+        if num_out != len(stride_cur):
+            raise ValueError(
+                'The length of `out_channels` does not equal to the length of `stride_cur`'
+                .format(num_out, len(stride_cur)))
+        self.conv_bn_func_list = []
+        for i in range(num_out):
+            residual = None
+            if stride_cur[i] == stride_pre:
+                if in_channel != out_channels[i]:
+                    residual = self.add_sublayer(
+                        "transition_{}_layer_{}".format(name, i + 1),
+                        layers.ConvBNReLU(
+                            in_channels=in_channel,
+                            out_channels=out_channels[i],
+                            kernel_size=3,
+                            padding='same',
+                            bias_attr=False))
+            elif stride_cur[i] > stride_pre:
+                residual = self.add_sublayer(
+                    "transition_{}_layer_{}".format(name, i + 1),
+                    layers.ConvBNReLU(
+                        in_channels=in_channel,
+                        out_channels=out_channels[i],
+                        kernel_size=3,
+                        stride=2,
+                        padding='same',
+                        bias_attr=False))
+            else:
+                residual = self.add_sublayer(
+                    "transition_{}_layer_{}".format(name, i + 1),
+                    layers.ConvBNReLU(
+                        in_channels=in_channel,
+                        out_channels=out_channels[i],
+                        kernel_size=1,
+                        stride=1,
+                        padding='same',
+                        bias_attr=False))
+            self.conv_bn_func_list.append(residual)
+
+    def forward(self, x, shape=None):
+        outs = []
+        for conv_bn_func in self.conv_bn_func_list:
+            if conv_bn_func is None:
+                outs.append(x)
+            else:
+                out = conv_bn_func(x)
+                if shape is not None:
+                    out = F.interpolate(
+                        out,
+                        shape,
+                        mode='bilinear',
+                        align_corners=self.align_corners)
+                outs.append(out)
+        return outs
+
+
+class Branches(nn.Layer):
+    def __init__(self,
+                 num_blocks,
+                 in_channels,
+                 out_channels,
+                 has_se=False,
+                 name=None):
+        super(Branches, self).__init__()
+
+        self.basic_block_list = []
+
+        for i in range(len(out_channels)):
+            self.basic_block_list.append([])
+            for j in range(num_blocks[i]):
+                in_ch = in_channels[i] if j == 0 else out_channels[i]
+                basic_block_func = self.add_sublayer(
+                    "bb_{}_branch_layer_{}_{}".format(name, i + 1, j + 1),
+                    BasicBlock(
+                        num_channels=in_ch,
+                        num_filters=out_channels[i],
+                        has_se=has_se,
+                        name=name + '_branch_layer_' + str(i + 1) + '_' +
+                        str(j + 1)))
+                self.basic_block_list[i].append(basic_block_func)
+
+    def forward(self, x):
+        outs = []
+        for idx, input in enumerate(x):
+            conv = input
+            for basic_block_func in self.basic_block_list[idx]:
+                conv = basic_block_func(conv)
+            outs.append(conv)
+        return outs
+
+
+class BottleneckBlock(nn.Layer):
+    def __init__(self,
+                 num_channels,
+                 num_filters,
+                 has_se,
+                 stride=1,
+                 downsample=False,
+                 name=None):
+        super(BottleneckBlock, self).__init__()
+
+        self.has_se = has_se
+        self.downsample = downsample
+
+        self.conv1 = layers.ConvBNReLU(
+            in_channels=num_channels,
+            out_channels=num_filters,
+            kernel_size=1,
+            padding='same',
+            bias_attr=False)
+
+        self.conv2 = layers.ConvBNReLU(
+            in_channels=num_filters,
+            out_channels=num_filters,
+            kernel_size=3,
+            stride=stride,
+            padding='same',
+            bias_attr=False)
+
+        self.conv3 = layers.ConvBN(
+            in_channels=num_filters,
+            out_channels=num_filters * 4,
+            kernel_size=1,
+            padding='same',
+            bias_attr=False)
+
+        if self.downsample:
+            self.conv_down = layers.ConvBN(
+                in_channels=num_channels,
+                out_channels=num_filters * 4,
+                kernel_size=1,
+                padding='same',
+                bias_attr=False)
+
+        if self.has_se:
+            self.se = SELayer(
+                num_channels=num_filters * 4,
+                num_filters=num_filters * 4,
+                reduction_ratio=16,
+                name=name + '_fc')
+
+    def forward(self, x):
+        residual = x
+        conv1 = self.conv1(x)
+        conv2 = self.conv2(conv1)
+        conv3 = self.conv3(conv2)
+
+        if self.downsample:
+            residual = self.conv_down(x)
+
+        if self.has_se:
+            conv3 = self.se(conv3)
+
+        y = conv3 + residual
+        y = F.relu(y)
+        return y
+
+
+class BasicBlock(nn.Layer):
+    def __init__(self,
+                 num_channels,
+                 num_filters,
+                 stride=1,
+                 has_se=False,
+                 downsample=False,
+                 name=None):
+        super(BasicBlock, self).__init__()
+
+        self.has_se = has_se
+        self.downsample = downsample
+
+        self.conv1 = layers.ConvBNReLU(
+            in_channels=num_channels,
+            out_channels=num_filters,
+            kernel_size=3,
+            stride=stride,
+            padding='same',
+            bias_attr=False)
+        self.conv2 = layers.ConvBN(
+            in_channels=num_filters,
+            out_channels=num_filters,
+            kernel_size=3,
+            padding='same',
+            bias_attr=False)
+
+        if self.downsample:
+            self.conv_down = layers.ConvBNReLU(
+                in_channels=num_channels,
+                out_channels=num_filters,
+                kernel_size=1,
+                padding='same',
+                bias_attr=False)
+
+        if self.has_se:
+            self.se = SELayer(
+                num_channels=num_filters,
+                num_filters=num_filters,
+                reduction_ratio=16,
+                name=name + '_fc')
+
+    def forward(self, x):
+        residual = x
+        conv1 = self.conv1(x)
+        conv2 = self.conv2(conv1)
+
+        if self.downsample:
+            residual = self.conv_down(x)
+
+        if self.has_se:
+            conv2 = self.se(conv2)
+
+        y = conv2 + residual
+        y = F.relu(y)
+        return y
+
+
+class SELayer(nn.Layer):
+    def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
+        super(SELayer, self).__init__()
+
+        self.pool2d_gap = nn.AdaptiveAvgPool2d(1)
+
+        self._num_channels = num_channels
+
+        med_ch = int(num_channels / reduction_ratio)
+        stdv = 1.0 / math.sqrt(num_channels * 1.0)
+        self.squeeze = nn.Linear(
+            num_channels,
+            med_ch,
+            act="relu",
+            param_attr=paddle.ParamAttr(
+                initializer=nn.initializer.Uniform(-stdv, stdv)))
+
+        stdv = 1.0 / math.sqrt(med_ch * 1.0)
+        self.excitation = nn.Linear(
+            med_ch,
+            num_filters,
+            act="sigmoid",
+            param_attr=paddle.ParamAttr(
+                initializer=nn.initializer.Uniform(-stdv, stdv)))
+
+    def forward(self, x):
+        pool = self.pool2d_gap(x)
+        pool = paddle.reshape(pool, shape=[-1, self._num_channels])
+        squeeze = self.squeeze(pool)
+        excitation = self.excitation(squeeze)
+        excitation = paddle.reshape(
+            excitation, shape=[-1, self._num_channels, 1, 1])
+        out = x * excitation
+        return out
+
+
+class Stage(nn.Layer):
+    def __init__(self,
+                 num_channels,
+                 num_modules,
+                 num_blocks,
+                 num_filters,
+                 has_se=False,
+                 multi_scale_output=True,
+                 name=None,
+                 align_corners=False):
+        super(Stage, self).__init__()
+
+        self._num_modules = num_modules
+
+        self.stage_func_list = []
+        for i in range(num_modules):
+            if i == num_modules - 1 and not multi_scale_output:
+                stage_func = self.add_sublayer(
+                    "stage_{}_{}".format(name, i + 1),
+                    HighResolutionModule(
+                        num_channels=num_channels,
+                        num_blocks=num_blocks,
+                        num_filters=num_filters,
+                        has_se=has_se,
+                        multi_scale_output=False,
+                        name=name + '_' + str(i + 1),
+                        align_corners=align_corners))
+            else:
+                stage_func = self.add_sublayer(
+                    "stage_{}_{}".format(name, i + 1),
+                    HighResolutionModule(
+                        num_channels=num_channels,
+                        num_blocks=num_blocks,
+                        num_filters=num_filters,
+                        has_se=has_se,
+                        name=name + '_' + str(i + 1),
+                        align_corners=align_corners))
+
+            self.stage_func_list.append(stage_func)
+
+    def forward(self, x):
+        out = x
+        for idx in range(self._num_modules):
+            out = self.stage_func_list[idx](out)
+        return out
+
+
+class HighResolutionModule(nn.Layer):
+    def __init__(self,
+                 num_channels,
+                 num_blocks,
+                 num_filters,
+                 has_se=False,
+                 multi_scale_output=True,
+                 name=None,
+                 align_corners=False):
+        super(HighResolutionModule, self).__init__()
+
+        self.branches_func = Branches(
+            num_blocks=num_blocks,
+            in_channels=num_channels,
+            out_channels=num_filters,
+            has_se=has_se,
+            name=name)
+
+        self.fuse_func = FuseLayers(
+            in_channels=num_filters,
+            out_channels=num_filters,
+            multi_scale_output=multi_scale_output,
+            name=name,
+            align_corners=align_corners)
+
+    def forward(self, x):
+        out = self.branches_func(x)
+        out = self.fuse_func(out)
+        return out
+
+
+class FuseLayers(nn.Layer):
+    def __init__(self,
+                 in_channels,
+                 out_channels,
+                 multi_scale_output=True,
+                 name=None,
+                 align_corners=False):
+        super(FuseLayers, self).__init__()
+
+        self._actual_ch = len(in_channels) if multi_scale_output else 1
+        self._in_channels = in_channels
+        self.align_corners = align_corners
+
+        self.residual_func_list = []
+        for i in range(self._actual_ch):
+            for j in range(len(in_channels)):
+                if j > i:
+                    residual_func = self.add_sublayer(
+                        "residual_{}_layer_{}_{}".format(name, i + 1, j + 1),
+                        layers.ConvBN(
+                            in_channels=in_channels[j],
+                            out_channels=out_channels[i],
+                            kernel_size=1,
+                            padding='same',
+                            bias_attr=False))
+                    self.residual_func_list.append(residual_func)
+                elif j < i:
+                    pre_num_filters = in_channels[j]
+                    for k in range(i - j):
+                        if k == i - j - 1:
+                            residual_func = self.add_sublayer(
+                                "residual_{}_layer_{}_{}_{}".format(
+                                    name, i + 1, j + 1, k + 1),
+                                layers.ConvBN(
+                                    in_channels=pre_num_filters,
+                                    out_channels=out_channels[i],
+                                    kernel_size=3,
+                                    stride=2,
+                                    padding='same',
+                                    bias_attr=False))
+                            pre_num_filters = out_channels[i]
+                        else:
+                            residual_func = self.add_sublayer(
+                                "residual_{}_layer_{}_{}_{}".format(
+                                    name, i + 1, j + 1, k + 1),
+                                layers.ConvBNReLU(
+                                    in_channels=pre_num_filters,
+                                    out_channels=out_channels[j],
+                                    kernel_size=3,
+                                    stride=2,
+                                    padding='same',
+                                    bias_attr=False))
+                            pre_num_filters = out_channels[j]
+                        self.residual_func_list.append(residual_func)
+        if len(self.residual_func_list) == 0:
+            self.residual_func_list.append(
+                self.add_sublayer("identity",
+                                  nn.Identity()))  # for flops calculation
+
+    def forward(self, x):
+        outs = []
+        residual_func_idx = 0
+        for i in range(self._actual_ch):
+            residual = x[i]
+            residual_shape = paddle.shape(residual)[-2:]
+
+            for j in range(len(self._in_channels)):
+                if j > i:
+                    y = self.residual_func_list[residual_func_idx](x[j])
+                    residual_func_idx += 1
+
+                    y = F.interpolate(
+                        y,
+                        residual_shape,
+                        mode='bilinear',
+                        align_corners=self.align_corners)
+                    residual = residual + y
+                elif j < i:
+                    y = x[j]
+                    for k in range(i - j):
+                        y = self.residual_func_list[residual_func_idx](y)
+                        residual_func_idx += 1
+
+                    residual = residual + y
+
+            residual = F.relu(residual)
+            outs.append(residual)
+
+        return outs
+
+
+@manager.BACKBONES.add_component
+def UHRNet_W18_Small(**kwargs):
+    model = UHRNet(
+        stage1_num_modules=1,
+        stage1_num_blocks=[2],
+        stage1_num_channels=[64],
+        stage2_num_modules=1,
+        stage2_num_blocks=[2, 2],
+        stage2_num_channels=[18, 36],
+        stage3_num_modules=2,
+        stage3_num_blocks=[2, 2],
+        stage3_num_channels=[36, 72],
+        stage4_num_modules=2,
+        stage4_num_blocks=[2, 2],
+        stage4_num_channels=[72, 144],
+        stage5_num_modules=2,
+        stage5_num_blocks=[2, 2],
+        stage5_num_channels=[144, 288],
+        stage6_num_modules=1,
+        stage6_num_blocks=[2, 2],
+        stage6_num_channels=[72, 144],
+        stage7_num_modules=1,
+        stage7_num_blocks=[2, 2],
+        stage7_num_channels=[36, 72],
+        stage8_num_modules=1,
+        stage8_num_blocks=[2, 2],
+        stage8_num_channels=[18, 36],
+        stage9_num_modules=1,
+        stage9_num_blocks=[2],
+        stage9_num_channels=[18],
+        **kwargs)
+    return model
+
+
+@manager.BACKBONES.add_component
+def UHRNet_W18(**kwargs):
+    model = UHRNet(
+        stage1_num_modules=1,
+        stage1_num_blocks=(4, ),
+        stage1_num_channels=(64, ),
+        stage2_num_modules=1,
+        stage2_num_blocks=(4, 4),
+        stage2_num_channels=(18, 36),
+        stage3_num_modules=5,
+        stage3_num_blocks=(4, 4),
+        stage3_num_channels=(36, 72),
+        stage4_num_modules=2,
+        stage4_num_blocks=(4, 4),
+        stage4_num_channels=(72, 144),
+        stage5_num_modules=2,
+        stage5_num_blocks=(4, 4),
+        stage5_num_channels=(144, 288),
+        stage6_num_modules=1,
+        stage6_num_blocks=(4, 4),
+        stage6_num_channels=(72, 144),
+        stage7_num_modules=1,
+        stage7_num_blocks=(4, 4),
+        stage7_num_channels=(36, 72),
+        stage8_num_modules=1,
+        stage8_num_blocks=(4, 4),
+        stage8_num_channels=(18, 36),
+        stage9_num_modules=1,
+        stage9_num_blocks=(4, ),
+        stage9_num_channels=(18, ),
+        **kwargs)
+    return model
+
+
+@manager.BACKBONES.add_component
+def UHRNet_W48(**kwargs):
+    model = UHRNet(
+        stage1_num_modules=1,
+        stage1_num_blocks=(4, ),
+        stage1_num_channels=(64, ),
+        stage2_num_modules=1,
+        stage2_num_blocks=(4, 4),
+        stage2_num_channels=(48, 96),
+        stage3_num_modules=5,
+        stage3_num_blocks=(4, 4),
+        stage3_num_channels=(96, 192),
+        stage4_num_modules=2,
+        stage4_num_blocks=(4, 4),
+        stage4_num_channels=(192, 384),
+        stage5_num_modules=2,
+        stage5_num_blocks=(4, 4),
+        stage5_num_channels=(384, 768),
+        stage6_num_modules=1,
+        stage6_num_blocks=(4, 4),
+        stage6_num_channels=(192, 384),
+        stage7_num_modules=1,
+        stage7_num_blocks=(4, 4),
+        stage7_num_channels=(96, 192),
+        stage8_num_modules=1,
+        stage8_num_blocks=(4, 4),
+        stage8_num_channels=(48, 96),
+        stage9_num_modules=1,
+        stage9_num_blocks=(4, ),
+        stage9_num_channels=(48, ),
+        **kwargs)
+    return model

+ 3 - 3
paddlers/models/ppseg/models/backbones/vision_transformer.py → paddlers/models/paddleseg/models/backbones/vision_transformer.py

@@ -20,9 +20,9 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 import numpy as np
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils, logger
-from paddlers.models.ppseg.models.backbones.transformer_utils import to_2tuple, DropPath, Identity
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils, logger
+from paddlers.models.paddleseg.models.backbones.transformer_utils import to_2tuple, DropPath, Identity
 
 
 class Mlp(nn.Layer):

+ 3 - 3
paddlers/models/ppseg/models/backbones/xception_deeplab.py → paddlers/models/paddleseg/models/backbones/xception_deeplab.py

@@ -15,9 +15,9 @@
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
+from paddlers.models.paddleseg.models import layers
 
 __all__ = ["Xception41_deeplab", "Xception65_deeplab", "Xception71_deeplab"]
 

+ 3 - 3
paddlers/models/ppseg/models/bisenet.py → paddlers/models/paddleseg/models/bisenet.py

@@ -18,9 +18,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/bisenetv1.py → paddlers/models/paddleseg/models/bisenetv1.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/ccnet.py → paddlers/models/paddleseg/models/ccnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/danet.py → paddlers/models/paddleseg/models/danet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/ddrnet.py → paddlers/models/paddleseg/models/ddrnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 class DualResNet(nn.Layer):

+ 5 - 5
paddlers/models/ppseg/models/decoupled_segnet.py → paddlers/models/paddleseg/models/decoupled_segnet.py

@@ -18,11 +18,11 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.models.backbones import resnet_vd
-from paddlers.models.ppseg.models import deeplab
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.models.backbones import resnet_vd
+from paddlers.models.paddleseg.models import deeplab
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/deeplab.py → paddlers/models/paddleseg/models/deeplab.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 __all__ = ['DeepLabV3P', 'DeepLabV3']
 

+ 8 - 4
paddlers/models/ppseg/models/dmnet.py → paddlers/models/paddleseg/models/dmnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component
@@ -129,7 +129,11 @@ class DCM(nn.Layer):
         generated_filter = self.filter_gen_conv(self.avg_pool(x))
         x = self.input_redu_conv(x)
         b, c, h, w = x.shape
-        x = x.reshape([1, b * c, h, w])
+        assert b > 0, "The batch size of x need to be bigger than 0, but got {}.".format(
+            b)
+        x = paddle.unsqueeze(
+            paddle.flatten(
+                x, start_axis=0, stop_axis=1), axis=0)
         generated_filter = generated_filter.reshape(
             [b * c, 1, self.filter_size, self.filter_size])
 

+ 3 - 3
paddlers/models/ppseg/models/dnlnet.py → paddlers/models/paddleseg/models/dnlnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 4 - 6
paddlers/models/ppseg/models/emanet.py → paddlers/models/paddleseg/models/emanet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component
@@ -209,9 +209,7 @@ class EMAU(nn.Layer):
             mu = F.normalize(mu, axis=1, p=2)
             mu = self.mu * (1 - self.momentum) + mu * self.momentum
             if paddle.distributed.get_world_size() > 1:
-                out = paddle.distributed.all_reduce(mu)
-                if out is not None:
-                    mu = out
+                paddle.distributed.all_reduce(mu)
                 mu /= paddle.distributed.get_world_size()
             self.mu = mu
 

+ 3 - 3
paddlers/models/ppseg/models/encnet.py → paddlers/models/paddleseg/models/encnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/enet.py → paddlers/models/paddleseg/models/enet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager, param_init
 
 __all__ = ['ENet']
 

+ 3 - 3
paddlers/models/ppseg/models/espnet.py → paddlers/models/paddleseg/models/espnet.py

@@ -18,9 +18,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/espnetv1.py → paddlers/models/paddleseg/models/espnetv1.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/fast_scnn.py → paddlers/models/paddleseg/models/fast_scnn.py

@@ -16,9 +16,9 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 
 import paddle
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 __all__ = ['FastSCNN']
 

+ 3 - 3
paddlers/models/ppseg/models/fastfcn.py → paddlers/models/paddleseg/models/fastfcn.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/fcn.py → paddlers/models/paddleseg/models/fcn.py

@@ -16,9 +16,9 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 
 import paddle
-from paddlers.models.ppseg import utils
-from paddlers.models.ppseg.cvlibs import manager, param_init
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg import utils
+from paddlers.models.paddleseg.cvlibs import manager, param_init
+from paddlers.models.paddleseg.models import layers
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/gcnet.py → paddlers/models/paddleseg/models/gcnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/ginet.py → paddlers/models/paddleseg/models/ginet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 from paddle.nn import functional as F
 
-from paddlers.models.ppseg.utils import utils
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/glore.py → paddlers/models/paddleseg/models/glore.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 5 - 5
paddlers/models/ppseg/models/gscnn.py → paddlers/models/paddleseg/models/gscnn.py

@@ -18,11 +18,11 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.models.backbones import resnet_vd
-from paddlers.models.ppseg.models import deeplab
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.models.backbones import resnet_vd
+from paddlers.models.paddleseg.models import deeplab
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/hardnet.py → paddlers/models/paddleseg/models/hardnet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/hrnet_contrast.py → paddlers/models/paddleseg/models/hrnet_contrast.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 3 - 3
paddlers/models/ppseg/models/isanet.py → paddlers/models/paddleseg/models/isanet.py

@@ -16,9 +16,9 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.utils import utils
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.utils import utils
 
 
 @manager.MODELS.add_component

+ 1 - 0
paddlers/models/ppseg/models/layers/__init__.py → paddlers/models/paddleseg/models/layers/__init__.py

@@ -19,3 +19,4 @@ from .attention import AttentionBlock
 from .nonlocal2d import NonLocal2D
 from .wrap_functions import *
 from .tensor_fusion import UAFM_SpAtten, UAFM_SpAtten_S, UAFM_ChAtten, UAFM_ChAtten_S, UAFM, UAFMMobile, UAFMMobile_SpAtten
+from .polaried_self_attention import PolarizedSelfAttentionModule

+ 1 - 1
paddlers/models/ppseg/models/layers/activation.py → paddlers/models/paddleseg/models/layers/activation.py

@@ -33,7 +33,7 @@ class Activation(nn.Layer):
 
     Examples:
 
-        from paddlers.models.ppseg.models.common.activation import Activation
+        from paddlers.models.paddleseg.models.common.activation import Activation
 
         relu = Activation("relu")
         print(relu)

+ 1 - 1
paddlers/models/ppseg/models/layers/attention.py → paddlers/models/paddleseg/models/layers/attention.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.models import layers
 
 
 class AttentionBlock(nn.Layer):

+ 4 - 2
paddlers/models/ppseg/models/layers/layer_libs.py → paddlers/models/paddleseg/models/layers/layer_libs.py

@@ -17,12 +17,14 @@ import os
 import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.models import layers
 
 
 def SyncBatchNorm(*args, **kwargs):
     """In cpu environment nn.SyncBatchNorm does not have kernel so use nn.BatchNorm2D instead"""
-    if paddle.get_device() == 'cpu' or os.environ.get('PADDLESEG_EXPORT_STAGE'):
+    if paddle.get_device() == 'cpu' or os.environ.get(
+            'PADDLESEG_EXPORT_STAGE') or 'xpu' in paddle.get_device(
+            ) or 'npu' in paddle.get_device():
         return nn.BatchNorm2D(*args, **kwargs)
     elif paddle.distributed.ParallelEnv().nranks == 1:
         return nn.BatchNorm2D(*args, **kwargs)

+ 1 - 1
paddlers/models/ppseg/models/layers/nonlocal2d.py → paddlers/models/paddleseg/models/layers/nonlocal2d.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.models import layers
 
 
 class NonLocal2D(nn.Layer):

+ 123 - 0
paddlers/models/paddleseg/models/layers/polaried_self_attention.py

@@ -0,0 +1,123 @@
+# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# 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.
+
+import paddle
+import paddle.nn as nn
+
+from paddlers.models.paddleseg.cvlibs import param_init
+
+
+class PolarizedSelfAttentionModule(nn.Layer):
+    '''
+    The original article refers to refer to https://arxiv.org/pdf/2107.00782
+    
+    Args:
+        inplanes (int): Input channels of feature.
+        planes (int): Output channels of feature.
+        kernel_size (int, optional): The kernel size of Conv2D. Default: 1
+        stride (int, optional): The stride length of Conv2D. Default: 1
+    '''
+
+    def __init__(self, inplanes, planes, kernel_size=1, stride=1):
+        super().__init__()
+        self.inplanes = inplanes
+        self.inter_planes = planes // 2
+        self.planes = planes
+        self.kernel_size = kernel_size
+        self.stride = stride
+        self.padding = (kernel_size - 1) // 2
+        self.conv_q_right = nn.Conv2D(
+            self.inplanes,
+            1,
+            kernel_size=1,
+            stride=stride,
+            padding=0,
+            bias_attr=False)
+        self.conv_v_right = nn.Conv2D(
+            self.inplanes,
+            self.inter_planes,
+            kernel_size=1,
+            stride=stride,
+            padding=0,
+            bias_attr=False)
+        self.conv_up = nn.Conv2D(
+            self.inter_planes,
+            self.planes,
+            kernel_size=1,
+            stride=1,
+            padding=0,
+            bias_attr=False)
+        self.softmax_right = nn.Softmax(axis=2)
+        self.sigmoid = nn.Sigmoid()
+        self.conv_q_left = nn.Conv2D(
+            self.inplanes,
+            self.inter_planes,
+            kernel_size=1,
+            stride=stride,
+            padding=0,
+            bias_attr=False)
+        self.avg_pool = nn.AdaptiveAvgPool2D(1)
+        self.conv_v_left = nn.Conv2D(
+            self.inplanes,
+            self.inter_planes,
+            kernel_size=1,
+            stride=stride,
+            padding=0,
+            bias_attr=False)
+        self.softmax_left = nn.Softmax(axis=2)
+
+        self.init_weight()
+
+    def spatial_pool(self, x):
+        input_x = self.conv_v_right(x)
+        batch, _, height, width = paddle.shape(input_x)
+        input_x = input_x.reshape((batch, self.inter_planes, height * width))
+        context_mask = self.conv_q_right(x)
+        context_mask = context_mask.reshape((batch, 1, height * width))
+        context_mask = self.softmax_right(context_mask)
+        context = paddle.matmul(input_x, context_mask.transpose((0, 2, 1)))
+        context = context.unsqueeze(-1)
+        context = self.conv_up(context)
+        mask_ch = self.sigmoid(context)
+        out = x * mask_ch
+        return out
+
+    def channel_pool(self, x):
+        g_x = self.conv_q_left(x)
+        batch, channel, height, width = paddle.shape(g_x)
+        avg_x = self.avg_pool(g_x)
+        batch, channel, avg_x_h, avg_x_w = paddle.shape(avg_x)
+        avg_x = avg_x.reshape((batch, channel, avg_x_h * avg_x_w))
+        avg_x = paddle.reshape(avg_x, [batch, avg_x_h * avg_x_w, channel])
+        theta_x = self.conv_v_left(x).reshape(
+            (batch, self.inter_planes, height * width))
+        context = paddle.matmul(avg_x, theta_x)
+        context = self.softmax_left(context)
+        context = context.reshape((batch, 1, height, width))
+        mask_sp = self.sigmoid(context)
+        out = x * mask_sp
+        return out
+
+    def forward(self, x):
+        context_channel = self.spatial_pool(x)
+        context_spatial = self.channel_pool(x)
+        out = context_spatial + context_channel
+        return out
+
+    def init_weight(self):
+        param_init.kaiming_normal_init(self.conv_q_right.weight)
+        param_init.kaiming_normal_init(self.conv_v_right.weight)
+        param_init.kaiming_normal_init(self.conv_q_left.weight)
+        param_init.kaiming_normal_init(self.conv_v_left.weight)
+        param_init.kaiming_normal_init(self.conv_up.weight)

+ 1 - 1
paddlers/models/ppseg/models/layers/pyramid_pool.py → paddlers/models/paddleseg/models/layers/pyramid_pool.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn.functional as F
 from paddle import nn
 
-from paddlers.models.ppseg.models import layers
+from paddlers.models.paddleseg.models import layers
 
 
 class ASPPModule(nn.Layer):

+ 2 - 2
paddlers/models/ppseg/models/layers/tensor_fusion.py → paddlers/models/paddleseg/models/layers/tensor_fusion.py

@@ -17,8 +17,8 @@ import paddle.nn as nn
 import paddle.nn.functional as F
 from paddle import ParamAttr
 from paddle.nn.initializer import Constant
-from paddlers.models.ppseg.models import layers
-from paddlers.models.ppseg.models.layers import tensor_fusion_helper as helper
+from paddlers.models.paddleseg.models import layers
+from paddlers.models.paddleseg.models.layers import tensor_fusion_helper as helper
 
 
 class UAFM(nn.Layer):

+ 0 - 0
paddlers/models/ppseg/models/layers/tensor_fusion_helper.py → paddlers/models/paddleseg/models/layers/tensor_fusion_helper.py


+ 0 - 0
paddlers/models/ppseg/models/layers/wrap_functions.py → paddlers/models/paddleseg/models/layers/wrap_functions.py


+ 0 - 0
paddlers/models/ppseg/models/losses/__init__.py → paddlers/models/paddleseg/models/losses/__init__.py


+ 1 - 1
paddlers/models/ppseg/models/losses/binary_cross_entropy_loss.py → paddlers/models/paddleseg/models/losses/binary_cross_entropy_loss.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/bootstrapped_cross_entropy.py → paddlers/models/paddleseg/models/losses/bootstrapped_cross_entropy.py

@@ -16,7 +16,7 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/cross_entropy_loss.py → paddlers/models/paddleseg/models/losses/cross_entropy_loss.py

@@ -16,7 +16,7 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/decoupledsegnet_relax_boundary_loss.py → paddlers/models/paddleseg/models/losses/decoupledsegnet_relax_boundary_loss.py

@@ -18,7 +18,7 @@ from paddle import nn
 import paddle.nn.functional as F
 from scipy.ndimage import shift
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/detail_aggregate_loss.py → paddlers/models/paddleseg/models/losses/detail_aggregate_loss.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/dice_loss.py → paddlers/models/paddleseg/models/losses/dice_loss.py

@@ -13,7 +13,7 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 2 - 2
paddlers/models/ppseg/models/losses/edge_attention_loss.py → paddlers/models/paddleseg/models/losses/edge_attention_loss.py

@@ -16,8 +16,8 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
-from paddlers.models.ppseg.models import losses
+from paddlers.models.paddleseg.cvlibs import manager
+from paddlers.models.paddleseg.models import losses
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/focal_loss.py → paddlers/models/paddleseg/models/losses/focal_loss.py

@@ -17,7 +17,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/gscnn_dual_task_loss.py → paddlers/models/paddleseg/models/losses/gscnn_dual_task_loss.py

@@ -13,7 +13,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/kl_loss.py → paddlers/models/paddleseg/models/losses/kl_loss.py

@@ -16,7 +16,7 @@ import paddle
 import paddle.nn as nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 1 - 1
paddlers/models/ppseg/models/losses/l1_loss.py → paddlers/models/paddleseg/models/losses/l1_loss.py

@@ -16,7 +16,7 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component

+ 3 - 3
paddlers/models/ppseg/models/losses/lovasz_loss.py → paddlers/models/paddleseg/models/losses/lovasz_loss.py

@@ -22,7 +22,7 @@ import paddle
 from paddle import nn
 import paddle.nn.functional as F
 
-from paddlers.models.ppseg.cvlibs import manager
+from paddlers.models.paddleseg.cvlibs import manager
 
 
 @manager.LOSSES.add_component
@@ -77,8 +77,8 @@ class LovaszHingeLoss(nn.Layer):
         """
         if logits.shape[1] == 2:
             logits = binary_channel_to_unary(logits)
-        loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels,
-                                                        self.ignore_index))
+        loss = lovasz_hinge_flat(
+            *flatten_binary_scores(logits, labels, self.ignore_index))
         return loss
 
 

Некоторые файлы не были показаны из-за большого количества измененных файлов