Kaynağa Gözat

Squashed commit of the following:

commit de5651935774034c31be300c421529aafa54b43f
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 02:14:40 2022 +0800

    Update workflow

commit 25d7f0cdd2fee5e01eaf4c222baec14043c718ad
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 01:57:45 2022 +0800

    Test cuda

commit e908e613797cf4b8d60bee012b54ebfc5888f021
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 01:39:43 2022 +0800

    Fix bug

commit 563fb86d03fd509283f626443029e82e3cfcae48
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 01:33:05 2022 +0800

    Test deploy

commit 8d285f94604552bc5944768bb89251ab1a53c1eb
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 01:08:12 2022 +0800

    Do not check tools

commit 227d98eb7967f0444f5bf7c04fc17d98f6f6dfc1
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 00:36:51 2022 +0800

    test_static_forward->test_to_static

commit 91a3cfafb22a645eebaaaff887ca7c7ace2ccb44
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Mon Jul 18 00:13:04 2022 +0800

    Fix bugs in test_models

commit c0bd01ef9d9493f1fe8dc4301a51213cd0814ced
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 23:45:17 2022 +0800

    Update URL

commit b2718b73a9d93f4c27bc0957b7b9349d4be028ba
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 23:36:32 2022 +0800

    Update workflow

commit 25ad5ae1477719c1e90e0b1005667c46508e4d9e
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 23:31:31 2022 +0800

    Fix bugs

commit 4780b365a73557d71f2a1156657cadb2e235e9d1
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 23:05:00 2022 +0800

    Update workflow

commit bb79a83447e2efc12d0e1c3a7ed52effcac4e635
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 22:48:31 2022 +0800

    Update workflow

commit 1dec49161a7c03c651910772064149f86a2545c0
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 22:39:17 2022 +0800

    Update workflow

commit 9df503913be03f147ed4d7d01a4b765f0a03cc1e
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 22:26:07 2022 +0800

    Fast tests

commit 665a872e6dbf0c326d12c4a86d99315bed6be417
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 21:16:18 2022 +0800

    Update workflows

commit d8978cbb2c4638a6cba9bc1378eedeefa36bee60
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 20:52:30 2022 +0800

    Add empty file

commit d704501b570a2b9314b3b0974e775a58e2cccfaf
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 20:47:11 2022 +0800

    Add install coverage

commit dd7c2c4bfd89eb0236ba8f39155a72ba04bf9bce
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 20:39:45 2022 +0800

    Add yapf disable

commit 1bdebad783ddeab165acafbadd69b42f587bb742
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 20:19:57 2022 +0800

    Apply pre-commit check

commit 2de597a5d67bbe2c9bd8ac3f6f3b27b886e9b7d4
Merge: 5f06a86 dfb6978
Author: Bobholamovic <bob1998425@hotmail.com>
Date:   Sun Jul 17 19:52:54 2022 +0800

    Merge branch 'unittest' into develop

commit 5f06a861735bb80cde0bca63889ac647664958ad
Author: Lin Manhui <mhlin425@whu.edu.cn>
Date:   Sun Jul 17 19:23:06 2022 +0800

    Add unittests (#2)

    * Init unittest

    * Add rs_model unittests

    * Refactor tools

    * Add data for unittests

    * Add transforms unittests

    * test_utils->testing_utils

    * Add tool unittests

    * Add scripts for unittests

    * Rename and polish doc/comments

    * Update test_operators.py

    * Add coco det data

    * Add tools unittests

    * Update style

    * Update rs_models unittests

    * Add predictor unittests

    * Add tutorial tests

    * Add github workflow
Bobholamovic 3 yıl önce
ebeveyn
işleme
228cab666e
100 değiştirilmiş dosya ile 1595 ekleme ve 1542 silme
  1. 41 25
      .github/workflows/build_and_test.yaml
  2. 25 0
      .github/workflows/lint.yaml
  3. 1 3
      docs/README.md
  4. 6 6
      docs/apis/model_zoo.md
  5. 2 2
      docs/data/coco_tools_cn.md
  6. 1 1
      docs/data/dataset_summary.md
  7. 1 1
      docs/quick_start.md
  8. 1 1
      paddlers/__init__.py
  9. 1 1
      paddlers/custom_models/__init__.py
  10. 1 1
      paddlers/custom_models/cd/backbones/__init__.py
  11. 1 1
      paddlers/custom_models/cd/bit.py
  12. 1 1
      paddlers/custom_models/cd/layers/__init__.py
  13. 4 2
      paddlers/custom_models/cd/layers/blocks.py
  14. 9 8
      paddlers/custom_models/gan/generators/rcan.py
  15. 6 7
      paddlers/custom_models/gan/rcan_model.py
  16. 1 1
      paddlers/datasets/__init__.py
  17. 1 1
      paddlers/models/__init__.py
  18. 1 1
      paddlers/models/ppcls/__init__.py
  19. 11 22
      paddlers/models/ppcls/arch/backbone/model_zoo/xception.py
  20. 1 1
      paddlers/models/ppcls/data/utils/__init__.py
  21. 8 4
      paddlers/models/ppcls/loss/deephashloss.py
  22. 3 2
      paddlers/models/ppcls/loss/googlenetloss.py
  23. 2 4
      paddlers/models/ppcls/loss/multilabelloss.py
  24. 9 6
      paddlers/models/ppcls/loss/pairwisecosface.py
  25. 1 2
      paddlers/models/ppcls/loss/supconloss.py
  26. 1 2
      paddlers/models/ppcls/metric/__init__.py
  27. 1 2
      paddlers/models/ppcls/utils/download.py
  28. 2 2
      paddlers/models/ppdet/metrics/json_results.py
  29. 2 4
      paddlers/models/ppdet/modeling/architectures/meta_arch.py
  30. 8 8
      paddlers/models/ppdet/modeling/backbones/vgg.py
  31. 5 8
      paddlers/models/ppdet/modeling/bbox_utils.py
  32. 19 25
      paddlers/models/ppdet/modeling/layers.py
  33. 13 17
      paddlers/models/ppdet/modeling/ops.py
  34. 2 6
      paddlers/models/ppdet/modeling/proposal_generator/anchor_generator.py
  35. 9 15
      paddlers/models/ppdet/modeling/proposal_generator/target.py
  36. 3 3
      paddlers/models/ppdet/utils/download.py
  37. 1 1
      paddlers/models/ppgan/apps/__init__.py
  38. 19 14
      paddlers/models/ppgan/apps/drn_predictor.py
  39. 2 1
      paddlers/models/ppgan/apps/esrgan_predictor.py
  40. 17 14
      paddlers/models/ppgan/apps/lesrcnn_predictor.py
  41. 53 49
      paddlers/models/ppgan/apps/midas/blocks.py
  42. 9 4
      paddlers/models/ppgan/apps/midas/midas_net.py
  43. 17 16
      paddlers/models/ppgan/apps/midas/transforms.py
  44. 2 2
      paddlers/models/ppgan/apps/midas/utils.py
  45. 4 4
      paddlers/models/ppgan/apps/mpr_predictor.py
  46. 16 14
      paddlers/models/ppgan/apps/pan_predictor.py
  47. 12 14
      paddlers/models/ppgan/datasets/animeganv2_dataset.py
  48. 3 3
      paddlers/models/ppgan/datasets/base_dataset.py
  49. 1 0
      paddlers/models/ppgan/datasets/base_sr_dataset.py
  50. 17 14
      paddlers/models/ppgan/datasets/builder.py
  51. 1 0
      paddlers/models/ppgan/datasets/common_vision_dataset.py
  52. 29 26
      paddlers/models/ppgan/datasets/firstorder_dataset.py
  53. 2 2
      paddlers/models/ppgan/datasets/image_folder.py
  54. 1 0
      paddlers/models/ppgan/datasets/paired_dataset.py
  55. 4 5
      paddlers/models/ppgan/datasets/preprocess/__init__.py
  56. 1 0
      paddlers/models/ppgan/datasets/preprocess/builder.py
  57. 3 0
      paddlers/models/ppgan/datasets/preprocess/io.py
  58. 17 18
      paddlers/models/ppgan/datasets/preprocess/transforms.py
  59. 1 0
      paddlers/models/ppgan/datasets/single_dataset.py
  60. 5 4
      paddlers/models/ppgan/datasets/starganv2_dataset.py
  61. 1 0
      paddlers/models/ppgan/datasets/unpaired_dataset.py
  62. 18 16
      paddlers/models/ppgan/engine/trainer.py
  63. 8 4
      paddlers/models/ppgan/faceutils/dlibutils/face_align.py
  64. 2 1
      paddlers/models/ppgan/faceutils/face_detection/detection/blazeface/detect.py
  65. 32 28
      paddlers/models/ppgan/faceutils/face_detection/detection/blazeface/net_blazeface.py
  66. 2 2
      paddlers/models/ppgan/faceutils/face_detection/detection/sfd/detect.py
  67. 27 63
      paddlers/models/ppgan/faceutils/face_detection/detection/sfd/net_s3fd.py
  68. 8 8
      paddlers/models/ppgan/faceutils/face_detection/utils.py
  69. 13 15
      paddlers/models/ppgan/faceutils/face_enhancement/face_enhance.py
  70. 4 3
      paddlers/models/ppgan/faceutils/face_segmentation/face_seg.py
  71. 32 26
      paddlers/models/ppgan/metrics/lpips.py
  72. 2 3
      paddlers/models/ppgan/metrics/psnr_ssim.py
  73. 6 4
      paddlers/models/ppgan/models/animeganv2_model.py
  74. 10 11
      paddlers/models/ppgan/models/base_model.py
  75. 1 0
      paddlers/models/ppgan/models/basicvsr_model.py
  76. 1 0
      paddlers/models/ppgan/models/criterions/gan_loss.py
  77. 15 11
      paddlers/models/ppgan/models/criterions/photopen_perceptual_loss.py
  78. 20 12
      paddlers/models/ppgan/models/criterions/pixel_loss.py
  79. 1 0
      paddlers/models/ppgan/models/cycle_gan_model.py
  80. 1 0
      paddlers/models/ppgan/models/dc_gan_model.py
  81. 25 26
      paddlers/models/ppgan/models/discriminators/discriminator_animegan.py
  82. 22 18
      paddlers/models/ppgan/models/discriminators/discriminator_firstorder.py
  83. 11 13
      paddlers/models/ppgan/models/discriminators/discriminator_lapstyle.py
  84. 35 33
      paddlers/models/ppgan/models/discriminators/discriminator_photopen.py
  85. 36 38
      paddlers/models/ppgan/models/discriminators/discriminator_ugatit.py
  86. 62 118
      paddlers/models/ppgan/models/discriminators/syncnet.py
  87. 27 58
      paddlers/models/ppgan/models/discriminators/vgg_discriminator.py
  88. 31 24
      paddlers/models/ppgan/models/discriminators/wav2lip_disc_qual.py
  89. 3 2
      paddlers/models/ppgan/models/drn_model.py
  90. 3 2
      paddlers/models/ppgan/models/edvr_model.py
  91. 7 10
      paddlers/models/ppgan/models/esrgan_model.py
  92. 12 12
      paddlers/models/ppgan/models/gan_model.py
  93. 36 40
      paddlers/models/ppgan/models/generators/basicvsr_plus_plus.py
  94. 277 257
      paddlers/models/ppgan/models/generators/edvr.py
  95. 105 74
      paddlers/models/ppgan/models/generators/generater_animegan.py
  96. 83 53
      paddlers/models/ppgan/models/generators/generater_lapstyle.py
  97. 79 50
      paddlers/models/ppgan/models/generators/generater_photopen.py
  98. 27 27
      paddlers/models/ppgan/models/generators/generator_firstorder.py
  99. 49 48
      paddlers/models/ppgan/models/generators/generator_starganv2.py
  100. 20 31
      paddlers/models/ppgan/models/generators/iconvsr.py

+ 41 - 25
.github/workflows/build_and_test.yaml

@@ -5,7 +5,6 @@ on:
     branches:
       - develop
     paths-ignore:
-      - ".github/**"
       - "docs/**"
       - "README.md"
   pull_request:
@@ -23,6 +22,19 @@ jobs:
       matrix:
         os: [ubuntu-latest, windows-latest]
         python-version: ["3.7", "3.8"]
+        include:
+          - python-version: "3.7"
+            os: windows-latest
+            gdal-whl-url: https://download.lfd.uci.edu/pythonlibs/archived/cp37/GDAL-3.3.3-cp37-cp37m-win_amd64.whl
+          - python-version: "3.7"
+            os: ubuntu-latest
+            gdal-whl-url: https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
+          - python-version: "3.8"
+            os: windows-latest
+            gdal-whl-url: https://download.lfd.uci.edu/pythonlibs/archived/GDAL-3.3.3-cp38-cp38-win_amd64.whl
+          - python-version: "3.8"
+            os: ubuntu-latest
+            gdal-whl-url: https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
     steps:
       - uses: actions/checkout@v3
       - name: Set up Python ${{ matrix.python-version }}
@@ -35,40 +47,44 @@ jobs:
         run: python -m pip install paddlepaddle==2.3.1 -i https://mirror.baidu.com/pypi/simple
       - name: Install PaddleRS
         run: |
-          pip install -r requirements.txt
-          pip install -e .
-      - name: Install pre-commit hooks
-        run: |
-          pip install pre-commit
-          pre-commit install
-      - name: Lint
-        run: pre-commit run --all-files
+          python -m pip install -r requirements.txt
+          python -m pip install -e .
+      - name: Install GDAL
+        run: python -m pip install ${{ matrix.gdal-whl-url }}
       - name: Run unittests
         run: |
           cd tests
-          bash check_coverage.sh
+          bash run_fast_tests.sh
         shell: bash
 
   build_and_test_cuda102:
-    runs-on: ubuntu-latest
+    runs-on: ubuntu-18.04
     container:
-      image: registry.baidubce.com/paddlepaddle/paddle:2.3.1-gpu-cuda10.2-cudnn7
+      image: pytorch/pytorch:1.9.0-cuda10.2-cudnn7-devel
     steps:
       - uses: actions/checkout@v3
+      - name: Fetch GPG keys
+        run: |
+          apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub
+          apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub
       - name: Upgrade pip
         run: python -m pip install pip --upgrade --user
-      - name: Install PaddleRS
-        run: |
-          pip install -r requirements.txt
-          pip install -e .
-      - name: Install pre-commit hooks
+      - name: Install dependencies
         run: |
-          pip install pre-commit
-          pre-commit install
-      - name: Lint
-        run: pre-commit run --all-files
-      - name: Run unittests
+          apt-get update
+          apt-get install ffmpeg libsm6 libxext6 git ninja-build libglib2.0-0 libsm6 libxrender-dev libxext6 -y
+      - name: Install PaddlePaddle
+        run: python -m pip install paddlepaddle-gpu==2.3.1 -i https://mirror.baidu.com/pypi/simple
+      - name: Install PaddleRS
         run: |
-          cd tests
-          bash check_coverage.sh
-        shell: bash
+          python -m pip install -r requirements.txt
+          python -m pip install -e .
+          python --version
+      - name: Install GDAL
+        run: python -m pip install https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
+      # Do not run unittests, because there is NO GPU in the machine.
+      # - name: Run unittests
+      #   run: |
+      #     cd tests
+      #     bash run_fast_tests.sh
+      #   shell: bash

+ 25 - 0
.github/workflows/lint.yaml

@@ -0,0 +1,25 @@
+name: lint
+
+on: [push, pull_request]
+
+concurrency:
+  group: ${{ github.workflow }}-${{ github.ref }}
+  cancel-in-progress: true
+
+jobs:
+  lint:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v3
+      - name: Set up Python 3.7
+        uses: actions/setup-python@v4
+        with:
+          python-version: 3.7
+      - name: Upgrade pip
+        run: python -m pip install pip --upgrade --user
+      - name: Install pre-commit hooks
+        run: |
+          pip install pre-commit
+          pre-commit install
+      - name: Lint
+        run: pre-commit run --all-files

+ 1 - 3
docs/README.md

@@ -1,5 +1,3 @@
-PaddleSeg commit fec42fd869b6f796c74cd510671595e3512bc8e9
-
 # 开发规范
 请注意,paddlers/models/ppxxx系列除了修改import路径和支持多通道模型外,不要增删改任何代码。
-新增的模型需放在paddlers/models/下的seg、det、cls、cd目录下。
+新增的模型需放在paddlers/models/下的seg、det、cls、cd目录下。

+ 6 - 6
docs/apis/model_zoo.md

@@ -4,18 +4,18 @@ PaddleRS的基础模型库来自[PaddleClas](https://github.com/PaddlePaddle/Pad
 
 ## 自定义模型库
 
-| 模型名称        | 用途     | 
-| --------------- | -------- | 
+| 模型名称        | 用途     |
+| --------------- | -------- |
 | FarSeg          | 语义分割 |
 | BIT             | 变化检测 |
 | CDNet           | 变化检测 |
 | DSIFN           | 变化检测 |
 | STANet          | 变化检测 |
-| SNUNet          | 变化检测 | 
+| SNUNet          | 变化检测 |
 | DSAMNet         | 变化检测 |
-| FCEarlyFusion | 变化检测 | 
-| FCSiamConc    | 变化检测 | 
-| FCSiamDiff    | 变化检测 | 
+| FCEarlyFusion | 变化检测 |
+| FCSiamConc    | 变化检测 |
+| FCSiamDiff    | 变化检测 |
 
 
 ## 如何导入

+ 2 - 2
docs/data/coco_tools_cn.md

@@ -85,7 +85,7 @@ Args_show = True
 
 ------------------------------------------------Info------------------------------------------------
 json read...
-json keys: dict_keys(['info', 'licenses', 'images', 'annotations', 'categories']) 
+json keys: dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
 
 ***********************info***********************
  Content Type: dict
@@ -409,7 +409,7 @@ Args_show = True
 
 ------------------------------------------------Info------------------------------------------------
 json read...
-json keys: dict_keys(['images', 'categories']) 
+json keys: dict_keys(['images', 'categories'])
 
 **********************images**********************
  Content Type: list

+ 1 - 1
docs/data/dataset_summary.md

@@ -215,4 +215,4 @@
 | [9-5](https://aistudio.baidu.com/aistudio/datasetdetail/136567) | [WHU TCL SatMVS 1.0](http://gpcv.whu.edu.cn/data/whu_tlc.html) | 图像生成   | 5120 * 5120                            | 1         | 300      | __     | tif, jpg | __              | 2.1m, 2.5m         | __           | 卫星影像           | ZY3                                                  | 2021     | 武汉大学                                                  | http://gpcv.whu.edu.cn/data/whu_tlc.html                     | https://aistudio.baidu.com/aistudio/datasetdetail/136567 |
 | [9-6](https://aistudio.baidu.com/aistudio/datasetdetail/136567) | [WHU TCL SatMVS 2.0](http://gpcv.whu.edu.cn/data/whu_tlc.html) | 图像生成   | 768 * 384                              | 1         | 5011     | __     | tif      | __              | 2.1m, 2.5m         | __           | 卫星影像           | ZY3                                                  | 2021     | 武汉大学                                                  | http://gpcv.whu.edu.cn/data/whu_tlc.html                     | https://aistudio.baidu.com/aistudio/datasetdetail/136567 |
 | 9-7                                                          | [DLR-ACD](https://www.dlr.de/eoc/en/desktopdefault.aspx/tabid-12760/22294_read-58354/) | 图像生成   | 3619 * 5226                            | 3         | 33       | 1      | __       | __              | 0.045~ 0.15m       | __           | 航拍影像           | 航拍影像                                             | 2019     | German Aerospace Center                                   | https://www.dlr.de/eoc/en/desktopdefault.aspx/tabid-12760/22294_read-58354/ |                                                          |
-| 9-8                                                          | [SEN12MS-CR](https://mediatum.ub.tum.de/1554803)             | 图像生成   | 256 * 256                              | 13, 2     | 122218   | __     | __       | __              | __                 | __           | 卫星影像           | Sentinel1, Sentinel2                                 | 2020     | TUM                                                       | https://mediatum.ub.tum.de/1554803                           |                                                          |
+| 9-8                                                          | [SEN12MS-CR](https://mediatum.ub.tum.de/1554803)             | 图像生成   | 256 * 256                              | 13, 2     | 122218   | __     | __       | __              | __                 | __           | 卫星影像           | Sentinel1, Sentinel2                                 | 2020     | TUM                                                       | https://mediatum.ub.tum.de/1554803                           |                                                          |

+ 1 - 1
docs/quick_start.md

@@ -39,4 +39,4 @@ python -m paddle.distributed.launch --gpus 0,1 tutorials/train/semantic_segmenta
 visualdl --logdir output/deeplabv3p_resnet50_multi_channel/vdl_log --port 8001
 ```
 
-服务启动后,使用浏览器打开 https://0.0.0.0:8001 或 https://localhost:8001
+服务启动后,使用浏览器打开 https://0.0.0.0:8001 或 https://localhost:8001

+ 1 - 1
paddlers/__init__.py

@@ -21,4 +21,4 @@ env_info = get_environ_info()
 
 log_level = 2
 
-from . import tasks, datasets, transforms, utils, tools, models, deploy
+from . import tasks, datasets, transforms, utils, tools, models, deploy

+ 1 - 1
paddlers/custom_models/__init__.py

@@ -12,4 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import cls, det, seg, gan, cd
+from . import cls, det, seg, gan, cd

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

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

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

@@ -172,7 +172,7 @@ class BIT(nn.Layer):
         else:
             token1 = self._get_reshaped_tokens(x1)
             token2 = self._get_reshaped_tokens(x2)
-            
+
         # Transformer encoder forward
         token = paddle.concat([token1, token2], axis=1)
         token = self.encode(token)

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

@@ -13,4 +13,4 @@
 # limitations under the License.
 
 from .blocks import *
-from .attention import ChannelAttention, SpatialAttention, CBAM
+from .attention import ChannelAttention, SpatialAttention, CBAM

+ 4 - 2
paddlers/custom_models/cd/layers/blocks.py

@@ -140,12 +140,14 @@ class Conv7x7(BasicConv):
 
 class MaxPool2x2(nn.MaxPool2D):
     def __init__(self, **kwargs):
-        super(MaxPool2x2, self).__init__(kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
+        super(MaxPool2x2, self).__init__(
+            kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
 
 
 class MaxUnPool2x2(nn.MaxUnPool2D):
     def __init__(self, **kwargs):
-        super(MaxUnPool2x2, self).__init__(kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
+        super(MaxUnPool2x2, self).__init__(
+            kernel_size=2, stride=(2, 2), padding=(0, 0), **kwargs)
 
 
 class ConvTransposed3x3(nn.Layer):

+ 9 - 8
paddlers/custom_models/gan/generators/rcan.py

@@ -8,14 +8,15 @@ from .builder import GENERATORS
 
 
 def default_conv(in_channels, out_channels, kernel_size, bias=True):
-    weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.XavierUniform(),
-                                   need_clip =True)
-    return nn.Conv2D(in_channels,
-                     out_channels,
-                     kernel_size,
-                     padding=(kernel_size // 2),
-                     weight_attr=weight_attr,
-                     bias_attr=bias)
+    weight_attr = paddle.ParamAttr(
+        initializer=paddle.nn.initializer.XavierUniform(), need_clip=True)
+    return nn.Conv2D(
+        in_channels,
+        out_channels,
+        kernel_size,
+        padding=(kernel_size // 2),
+        weight_attr=weight_attr,
+        bias_attr=bias)
 
 
 class MeanShift(nn.Conv2D):

+ 6 - 7
paddlers/custom_models/gan/rcan_model.py

@@ -27,6 +27,7 @@ from ...models.ppgan.modules.init import reset_parameters
 class RCANModel(BaseModel):
     """Base SR model for single image super-resolution.
     """
+
     def __init__(self, generator, pixel_criterion=None, use_init_weight=False):
         """
         Args:
@@ -69,16 +70,14 @@ class RCANModel(BaseModel):
             loss_pixel.backward()
             optims['optim'].step()
         else:
-            print('Skip this batch {}! (Loss: {})'.format(
-                    self.batch + 1, loss_pixel.item()
-                ))
+            print('Skip this batch {}! (Loss: {})'.format(self.batch + 1,
+                                                          loss_pixel.item()))
         self.batch += 1
 
         if self.batch % 1000 == 0:
-            self.error_last = loss_pixel.item()/1000
+            self.error_last = loss_pixel.item() / 1000
             print("update error_last:{}".format(self.error_last))
 
-
     def test_iter(self, metrics=None):
         self.nets['generator'].eval()
         with paddle.no_grad():
@@ -99,8 +98,8 @@ class RCANModel(BaseModel):
 
 def init_sr_weight(net):
     def reset_func(m):
-        if hasattr(m, 'weight') and (not isinstance(
-                m, (nn.BatchNorm, nn.BatchNorm2D))):
+        if hasattr(m, 'weight') and (
+                not isinstance(m, (nn.BatchNorm, nn.BatchNorm2D))):
             reset_parameters(m)
 
     net.apply(reset_func)

+ 1 - 1
paddlers/datasets/__init__.py

@@ -17,4 +17,4 @@ from .coco import COCODetection
 from .seg_dataset import SegDataset
 from .cd_dataset import CDDataset
 from .clas_dataset import ClasDataset
-from .sr_dataset import SRdataset, ComposeTrans
+from .sr_dataset import SRdataset, ComposeTrans

+ 1 - 1
paddlers/models/__init__.py

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

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

@@ -20,4 +20,4 @@ from . import optimizer
 from .arch import *
 from .optimizer import *
 from .data import *
-from .utils import *
+from .utils import *

+ 11 - 22
paddlers/models/ppcls/arch/backbone/model_zoo/xception.py

@@ -201,22 +201,14 @@ class MiddleFlow(nn.Layer):
         super(MiddleFlow, self).__init__()
 
         self.block_num = block_num
-        self._conv_0 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_0")
-        self._conv_1 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_1")
-        self._conv_2 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_2")
-        self._conv_3 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_3")
-        self._conv_4 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_4")
-        self._conv_5 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_5")
-        self._conv_6 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_6")
-        self._conv_7 = MiddleFlowBottleneckBlock(
-            728, 728, name="middle_flow_7")
+        self._conv_0 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_0")
+        self._conv_1 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_1")
+        self._conv_2 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_2")
+        self._conv_3 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_3")
+        self._conv_4 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_4")
+        self._conv_5 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_5")
+        self._conv_6 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_6")
+        self._conv_7 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_7")
         if block_num == 16:
             self._conv_8 = MiddleFlowBottleneckBlock(
                 728, 728, name="middle_flow_8")
@@ -297,8 +289,7 @@ class ExitFlow(nn.Layer):
 
         name = "exit_flow"
 
-        self._conv_0 = ExitFlowBottleneckBlock(
-            728, 728, 1024, name=name + "_1")
+        self._conv_0 = ExitFlowBottleneckBlock(728, 728, 1024, name=name + "_1")
         self._conv_1 = SeparableConv(1024, 1536, stride=1, name=name + "_2")
         self._conv_2 = SeparableConv(1536, 2048, stride=1, name=name + "_3")
         self._pool = AdaptiveAvgPool2D(1)
@@ -362,16 +353,14 @@ def Xception41(pretrained=False, use_ssld=False, **kwargs):
 
 
 def Xception65(pretrained=False, use_ssld=False, **kwargs):
-    model = Xception(
-        entry_flow_block_num=3, middle_flow_block_num=16, **kwargs)
+    model = Xception(entry_flow_block_num=3, middle_flow_block_num=16, **kwargs)
     _load_pretrained(
         pretrained, model, MODEL_URLS["Xception65"], use_ssld=use_ssld)
     return model
 
 
 def Xception71(pretrained=False, use_ssld=False, **kwargs):
-    model = Xception(
-        entry_flow_block_num=5, middle_flow_block_num=16, **kwargs)
+    model = Xception(entry_flow_block_num=5, middle_flow_block_num=16, **kwargs)
     _load_pretrained(
         pretrained, model, MODEL_URLS["Xception71"], use_ssld=use_ssld)
     return model

+ 1 - 1
paddlers/models/ppcls/data/utils/__init__.py

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

+ 8 - 4
paddlers/models/ppcls/loss/deephashloss.py

@@ -15,6 +15,7 @@
 import paddle
 import paddle.nn as nn
 
+
 class DSHSDLoss(nn.Layer):
     """
     # DSHSD(IEEE ACCESS 2019)
@@ -23,6 +24,7 @@ class DSHSDLoss(nn.Layer):
     # [DSHSD] epoch:250, bit:48,  dataset:nuswide_21, MAP:0.809, Best MAP: 0.815
     # [DSHSD] epoch:135, bit:48,  dataset:imagenet,   MAP:0.647, Best MAP: 0.647
     """
+
     def __init__(self, alpha, multi_label=False):
         super(DSHSDLoss, self).__init__()
         self.alpha = alpha
@@ -65,6 +67,7 @@ class LCDSHLoss(nn.Layer):
     # [LCDSH] epoch:145, bit:48, dataset:cifar10-1,  MAP:0.798, Best MAP: 0.798
     # [LCDSH] epoch:183, bit:48, dataset:nuswide_21, MAP:0.833, Best MAP: 0.834
     """
+
     def __init__(self, n_class, _lambda):
         super(LCDSHLoss, self).__init__()
         self._lambda = _lambda
@@ -75,9 +78,11 @@ class LCDSHLoss(nn.Layer):
 
         # label to ont-hot
         label = paddle.flatten(label)
-        label = paddle.nn.functional.one_hot(label,  self.n_class).astype("float32")
-        
-        s = 2 * (paddle.matmul(label, label, transpose_y=True) > 0).astype("float32") - 1
+        label = paddle.nn.functional.one_hot(label,
+                                             self.n_class).astype("float32")
+
+        s = 2 * (paddle.matmul(
+            label, label, transpose_y=True) > 0).astype("float32") - 1
         inner_product = paddle.matmul(feature, feature, transpose_y=True) * 0.5
 
         inner_product = inner_product.clip(min=-50, max=50)
@@ -89,4 +94,3 @@ class LCDSHLoss(nn.Layer):
         L2 = (sigmoid(inner_product) - sigmoid(inner_product_)).pow(2).mean()
 
         return {"lcdshloss": L1 + self._lambda * L2}
-

+ 3 - 2
paddlers/models/ppcls/loss/googlenetloss.py

@@ -19,10 +19,11 @@ class GoogLeNetLoss(nn.Layer):
     """
     Cross entropy loss used after googlenet
     """
+
     def __init__(self, epsilon=None):
         super().__init__()
-        assert (epsilon is None or epsilon <= 0 or epsilon >= 1), "googlenet is not support label_smooth"
-            
+        assert (epsilon is None or epsilon <= 0 or
+                epsilon >= 1), "googlenet is not support label_smooth"
 
     def forward(self, inputs, label):
         input0, input1, input2 = inputs

+ 2 - 4
paddlers/models/ppcls/loss/multilabelloss.py

@@ -26,11 +26,9 @@ class MultiLabelLoss(nn.Layer):
     def _binary_crossentropy(self, input, target, class_num):
         if self.epsilon is not None:
             target = self._labelsmoothing(target, class_num)
-            cost = F.binary_cross_entropy_with_logits(
-                logit=input, label=target)
+            cost = F.binary_cross_entropy_with_logits(logit=input, label=target)
         else:
-            cost = F.binary_cross_entropy_with_logits(
-                logit=input, label=target)
+            cost = F.binary_cross_entropy_with_logits(logit=input, label=target)
 
         return cost
 

+ 9 - 6
paddlers/models/ppcls/loss/pairwisecosface.py

@@ -36,8 +36,10 @@ class PairwiseCosface(nn.Layer):
         dist_mat = paddle.matmul(embedding, embedding, transpose_y=True)
 
         N = dist_mat.shape[0]
-        is_pos = targets.reshape([N,1]).expand([N,N]).equal(paddle.t(targets.reshape([N,1]).expand([N,N]))).astype('float')
-        is_neg = targets.reshape([N,1]).expand([N,N]).not_equal(paddle.t(targets.reshape([N,1]).expand([N,N]))).astype('float')
+        is_pos = targets.reshape([N, 1]).expand([N, N]).equal(
+            paddle.t(targets.reshape([N, 1]).expand([N, N]))).astype('float')
+        is_neg = targets.reshape([N, 1]).expand([N, N]).not_equal(
+            paddle.t(targets.reshape([N, 1]).expand([N, N]))).astype('float')
 
         # Mask scores related to itself
         is_pos = is_pos - paddle.eye(N, N)
@@ -48,8 +50,9 @@ class PairwiseCosface(nn.Layer):
         logit_p = -self.gamma * s_p + (-99999999.) * (1 - is_pos)
         logit_n = self.gamma * (s_n + self.margin) + (-99999999.) * (1 - is_neg)
 
-        loss = F.softplus(paddle.logsumexp(logit_p, axis=1) + paddle.logsumexp(logit_n, axis=1)).mean()
-  
-        return {"PairwiseCosface": loss}
-
+        loss = F.softplus(
+            paddle.logsumexp(
+                logit_p, axis=1) + paddle.logsumexp(
+                    logit_n, axis=1)).mean()
 
+        return {"PairwiseCosface": loss}

+ 1 - 2
paddlers/models/ppcls/loss/supconloss.py

@@ -58,8 +58,7 @@ class SupConLoss(nn.Layer):
         elif labels is not None:
             labels = labels.reshape([-1, 1])
             if labels.shape[0] != batch_size:
-                raise ValueError(
-                    'Num of labels does not match num of features')
+                raise ValueError('Num of labels does not match num of features')
             mask = paddle.cast(
                 paddle.equal(labels, paddle.t(labels)), 'float32')
         else:

+ 1 - 2
paddlers/models/ppcls/metric/__init__.py

@@ -34,8 +34,7 @@ class CombinedMetrics(nn.Layer):
             metric_name = list(config)[0]
             metric_params = config[metric_name]
             if metric_params is not None:
-                self.metric_func_list.append(
-                    eval(metric_name)(**metric_params))
+                self.metric_func_list.append(eval(metric_name)(**metric_params))
             else:
                 self.metric_func_list.append(eval(metric_name)())
 

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

@@ -120,8 +120,7 @@ def get_path_from_url(url,
     # Mainly used to solve the problem of downloading data from different 
     # machines in the case of multiple machines. Different ips will download 
     # data, and the same ip will only download data once.
-    unique_endpoints = _get_unique_endpoints(ParallelEnv()
-                                             .trainer_endpoints[:])
+    unique_endpoints = _get_unique_endpoints(ParallelEnv().trainer_endpoints[:])
     if osp.exists(fullpath) and check_exist and _md5check(fullpath, md5sum):
         logger.info("Found {}".format(fullpath))
     else:

+ 2 - 2
paddlers/models/ppdet/metrics/json_results.py

@@ -141,8 +141,8 @@ def get_keypoint_res(results, im_id):
             }
             x = kpt[0::3]
             y = kpt[1::3]
-            x0, x1, y0, y1 = np.min(x).item(), np.max(x).item(), np.min(
-                y).item(), np.max(y).item()
+            x0, x1, y0, y1 = np.min(x).item(), np.max(x).item(), np.min(y).item(
+            ), np.max(y).item()
             ann['area'] = (x1 - x0) * (y1 - y0)
             ann['bbox'] = [x0, y0, x1 - x0, y1 - y0]
             anns.append(ann)

+ 2 - 4
paddlers/models/ppdet/modeling/architectures/meta_arch.py

@@ -25,8 +25,7 @@ class BaseArch(nn.Layer):
         self.scale = 1.
         self.mean = paddle.to_tensor([0.485, 0.456, 0.406]).reshape(
             (1, 3, 1, 1))
-        self.std = paddle.to_tensor([0.229, 0.224, 0.225]).reshape(
-            (1, 3, 1, 1))
+        self.std = paddle.to_tensor([0.229, 0.224, 0.225]).reshape((1, 3, 1, 1))
         for item in cfg_transform:
             if 'NormalizeImage' in item:
                 self.mean = paddle.to_tensor(item['NormalizeImage'][
@@ -83,8 +82,7 @@ class BaseArch(nn.Layer):
         nms_threshold = 0.5
         keep_top_k = 100
 
-        if self.__class__.__name__ in ('CascadeRCNN', 'FasterRCNN', 'MaskRCNN'
-                                       ):
+        if self.__class__.__name__ in ('CascadeRCNN', 'FasterRCNN', 'MaskRCNN'):
             num_classes = self.bbox_head.num_classes
             keep_top_k = self.bbox_post_process.nms.keep_top_k
             nms_threshold = self.bbox_post_process.nms.nms_threshold

+ 8 - 8
paddlers/models/ppdet/modeling/backbones/vgg.py

@@ -109,12 +109,12 @@ class L2NormScale(nn.Layer):
 @register
 @serializable
 class VGG(nn.Layer):
-    def __init__(
-            self,
-            depth=16,
-            normalizations=[20., -1, -1, -1, -1, -1],
-            extra_block_filters=[[256, 512, 1, 2, 3], [128, 256, 1, 2, 3],
-                                 [128, 256, 0, 1, 3], [128, 256, 0, 1, 3]]):
+    def __init__(self,
+                 depth=16,
+                 normalizations=[20., -1, -1, -1, -1, -1],
+                 extra_block_filters=[[256, 512, 1, 2, 3], [128, 256, 1, 2, 3],
+                                      [128, 256, 0, 1, 3],
+                                      [128, 256, 0, 1, 3]]):
         super(VGG, self).__init__()
 
         assert depth in [16, 19], \
@@ -159,8 +159,8 @@ class VGG(nn.Layer):
         for i, v in enumerate(self.extra_block_filters):
             assert len(v) == 5, "extra_block_filters size not fix"
             extra_conv = self.add_sublayer("conv{}".format(6 + i),
-                                           ExtraBlock(last_channels, v[0],
-                                                      v[1], v[2], v[3], v[4]))
+                                           ExtraBlock(last_channels, v[0], v[1],
+                                                      v[2], v[3], v[4]))
             last_channels = v[1]
             self.extra_convs.append(extra_conv)
             self._out_channels.append(last_channels)

+ 5 - 8
paddlers/models/ppdet/modeling/bbox_utils.py

@@ -265,8 +265,7 @@ def decode_yolo(box, anchor, downsample_ratio):
     """
     x, y, w, h = box
     na, grid_h, grid_w = x.shape[1:4]
-    grid = make_grid(grid_h, grid_w, x.dtype).reshape(
-        (1, 1, grid_h, grid_w, 2))
+    grid = make_grid(grid_h, grid_w, x.dtype).reshape((1, 1, grid_h, grid_w, 2))
     x1 = (x + grid[:, :, :, :, 0:1]) / grid_w
     y1 = (y + grid[:, :, :, :, 1:2]) / grid_h
 
@@ -345,8 +344,7 @@ def bbox_iou(box1, box2, giou=False, diou=False, ciou=False, eps=1e-9):
             # convex diagonal squared
             c2 = cw**2 + ch**2 + eps
             # center distance
-            rho2 = (
-                (px1 + px2 - gx1 - gx2)**2 + (py1 + py2 - gy1 - gy2)**2) / 4
+            rho2 = ((px1 + px2 - gx1 - gx2)**2 + (py1 + py2 - gy1 - gy2)**2) / 4
             if diou:
                 return iou - rho2 / c2
             else:
@@ -461,8 +459,8 @@ def rbox2delta(proposals, gt, means=[0, 0, 0, 0, 0], stds=[1, 1, 1, 1, 1]):
     coord = gt[..., 0:2] - proposals[..., 0:2]
     dx = (np.cos(proposals[..., 4]) * coord[..., 0] + np.sin(proposals[..., 4])
           * coord[..., 1]) / proposals_widths
-    dy = (-np.sin(proposals[..., 4]) * coord[..., 0] +
-          np.cos(proposals[..., 4]) * coord[..., 1]) / proposals_heights
+    dy = (-np.sin(proposals[..., 4]) * coord[..., 0] + np.cos(proposals[..., 4])
+          * coord[..., 1]) / proposals_heights
     dw = np.log(gt_widths / proposals_widths)
     dh = np.log(gt_heights / proposals_heights)
     da = (gt_angle - proposals_angle)
@@ -553,8 +551,7 @@ def poly2rbox(polys):
 def cal_line_length(point1, point2):
     import math
     return math.sqrt(
-        math.pow(point1[0] - point2[0], 2) + math.pow(point1[1] - point2[1],
-                                                      2))
+        math.pow(point1[0] - point2[0], 2) + math.pow(point1[1] - point2[1], 2))
 
 
 def get_best_begin_point_single(coordinate):

+ 19 - 25
paddlers/models/ppdet/modeling/layers.py

@@ -170,12 +170,10 @@ class ConvNormLayer(nn.Layer):
         norm_lr = 0. if freeze_norm else 1.
         param_attr = ParamAttr(
             learning_rate=norm_lr,
-            regularizer=L2Decay(norm_decay)
-            if norm_decay is not None else None)
+            regularizer=L2Decay(norm_decay) if norm_decay is not None else None)
         bias_attr = ParamAttr(
             learning_rate=norm_lr,
-            regularizer=L2Decay(norm_decay)
-            if norm_decay is not None else None)
+            regularizer=L2Decay(norm_decay) if norm_decay is not None else None)
         if norm_type in ['bn', 'sync_bn']:
             self.norm = nn.BatchNorm2D(
                 ch_out, weight_attr=param_attr, bias_attr=bias_attr)
@@ -293,19 +291,18 @@ class DropBlock(nn.Layer):
 @register
 @serializable
 class AnchorGeneratorSSD(object):
-    def __init__(
-            self,
-            steps=[8, 16, 32, 64, 100, 300],
-            aspect_ratios=[[2.], [2., 3.], [2., 3.], [2., 3.], [2.], [2.]],
-            min_ratio=15,
-            max_ratio=90,
-            base_size=300,
-            min_sizes=[30.0, 60.0, 111.0, 162.0, 213.0, 264.0],
-            max_sizes=[60.0, 111.0, 162.0, 213.0, 264.0, 315.0],
-            offset=0.5,
-            flip=True,
-            clip=False,
-            min_max_aspect_ratios_order=False):
+    def __init__(self,
+                 steps=[8, 16, 32, 64, 100, 300],
+                 aspect_ratios=[[2.], [2., 3.], [2., 3.], [2., 3.], [2.], [2.]],
+                 min_ratio=15,
+                 max_ratio=90,
+                 base_size=300,
+                 min_sizes=[30.0, 60.0, 111.0, 162.0, 213.0, 264.0],
+                 max_sizes=[60.0, 111.0, 162.0, 213.0, 264.0, 315.0],
+                 offset=0.5,
+                 flip=True,
+                 clip=False,
+                 min_max_aspect_ratios_order=False):
         self.steps = steps
         self.aspect_ratios = aspect_ratios
         self.min_ratio = min_ratio
@@ -1035,19 +1032,16 @@ class MaskMatrixNMS(object):
 
         seg_masks = paddle.flatten(seg_masks, start_axis=1, stop_axis=-1)
         # inter.
-        inter_matrix = paddle.mm(seg_masks,
-                                 paddle.transpose(seg_masks, [1, 0]))
+        inter_matrix = paddle.mm(seg_masks, paddle.transpose(seg_masks, [1, 0]))
         n_samples = paddle.shape(cate_labels)
         # union.
         sum_masks_x = paddle.expand(sum_masks, shape=[n_samples, n_samples])
         # iou.
         iou_matrix = (inter_matrix / (
-            sum_masks_x + paddle.transpose(sum_masks_x, [1, 0]) - inter_matrix)
-                      )
+            sum_masks_x + paddle.transpose(sum_masks_x, [1, 0]) - inter_matrix))
         iou_matrix = paddle.triu(iou_matrix, diagonal=1)
         # label_specific matrix.
-        cate_labels_x = paddle.expand(
-            cate_labels, shape=[n_samples, n_samples])
+        cate_labels_x = paddle.expand(cate_labels, shape=[n_samples, n_samples])
         label_matrix = paddle.cast(
             (cate_labels_x == paddle.transpose(cate_labels_x, [1, 0])),
             'float32')
@@ -1304,8 +1298,8 @@ class MultiHeadAttention(nn.Layer):
         if self._qkv_same_embed_dim:
             tensor = F.linear(
                 x=tensor,
-                weight=self.in_proj_weight[:, index * self.embed_dim:(
-                    index + 1) * self.embed_dim],
+                weight=self.in_proj_weight[:, index * self.embed_dim:(index + 1)
+                                           * self.embed_dim],
                 bias=self.in_proj_bias[index * self.embed_dim:(index + 1) *
                                        self.embed_dim]
                 if self.in_proj_bias is not None else None)

+ 13 - 17
paddlers/models/ppdet/modeling/ops.py

@@ -690,13 +690,12 @@ def yolo_box(
     if not isinstance(class_num, int):
         raise TypeError("Attr class_num of yolo_box must be an integer")
     if not isinstance(conf_thresh, float):
-        raise TypeError(
-            "Attr ignore_thresh of yolo_box must be a float number")
+        raise TypeError("Attr ignore_thresh of yolo_box must be a float number")
 
     if in_dygraph_mode():
         attrs = ('anchors', anchors, 'class_num', class_num, 'conf_thresh',
-                 conf_thresh, 'downsample_ratio', downsample_ratio,
-                 'clip_bbox', clip_bbox, 'scale_x_y', scale_x_y)
+                 conf_thresh, 'downsample_ratio', downsample_ratio, 'clip_bbox',
+                 clip_bbox, 'scale_x_y', scale_x_y)
         boxes, scores = core.ops.yolo_box(x, origin_shape, *attrs)
         return boxes, scores
     else:
@@ -978,8 +977,8 @@ def multiclass_nms(bboxes,
                  score_threshold, 'nms_top_k', nms_top_k, 'nms_threshold',
                  nms_threshold, 'keep_top_k', keep_top_k, 'nms_eta', nms_eta,
                  'normalized', normalized)
-        output, index, nms_rois_num = core.ops.multiclass_nms3(
-            bboxes, scores, rois_num, *attrs)
+        output, index, nms_rois_num = core.ops.multiclass_nms3(bboxes, scores,
+                                                               rois_num, *attrs)
         if not return_index:
             index = None
         return output, nms_rois_num, index
@@ -1116,10 +1115,10 @@ def matrix_nms(bboxes,
 
     if in_dygraph_mode():
         attrs = ('background_label', background_label, 'score_threshold',
-                 score_threshold, 'post_threshold', post_threshold,
-                 'nms_top_k', nms_top_k, 'gaussian_sigma', gaussian_sigma,
-                 'use_gaussian', use_gaussian, 'keep_top_k', keep_top_k,
-                 'normalized', normalized)
+                 score_threshold, 'post_threshold', post_threshold, 'nms_top_k',
+                 nms_top_k, 'gaussian_sigma', gaussian_sigma, 'use_gaussian',
+                 use_gaussian, 'keep_top_k', keep_top_k, 'normalized',
+                 normalized)
         out, index, rois_num = core.ops.matrix_nms(bboxes, scores, *attrs)
         if not return_index:
             index = None
@@ -1503,9 +1502,9 @@ def generate_proposals(scores,
     """
     if in_dygraph_mode():
         assert return_rois_num, "return_rois_num should be True in dygraph mode."
-        attrs = ('pre_nms_topN', pre_nms_top_n, 'post_nms_topN',
-                 post_nms_top_n, 'nms_thresh', nms_thresh, 'min_size',
-                 min_size, 'eta', eta, 'pixel_offset', pixel_offset)
+        attrs = ('pre_nms_topN', pre_nms_top_n, 'post_nms_topN', post_nms_top_n,
+                 'nms_thresh', nms_thresh, 'min_size', min_size, 'eta', eta,
+                 'pixel_offset', pixel_offset)
         rpn_rois, rpn_roi_probs, rpn_rois_num = core.ops.generate_proposals_v2(
             scores, bbox_deltas, im_shape, anchors, variances, *attrs)
         return rpn_rois, rpn_roi_probs, rpn_rois_num
@@ -1575,10 +1574,7 @@ def sigmoid_cross_entropy_with_logits(input,
     return output
 
 
-def smooth_l1(input,
-              label,
-              inside_weight=None,
-              outside_weight=None,
+def smooth_l1(input, label, inside_weight=None, outside_weight=None,
               sigma=None):
     input_new = paddle.multiply(input, inside_weight)
     label_new = paddle.multiply(label, inside_weight)

+ 2 - 6
paddlers/models/ppdet/modeling/proposal_generator/anchor_generator.py

@@ -77,8 +77,7 @@ class AnchorGenerator(nn.Layer):
 
     def _calculate_anchors(self, num_features):
         sizes = self._broadcast_params(self.anchor_sizes, num_features)
-        aspect_ratios = self._broadcast_params(self.aspect_ratios,
-                                               num_features)
+        aspect_ratios = self._broadcast_params(self.aspect_ratios, num_features)
         cell_anchors = [
             self.generate_cell_anchors(s, a)
             for s, a in zip(sizes, aspect_ratios)
@@ -94,10 +93,7 @@ class AnchorGenerator(nn.Layer):
         shifts_x = paddle.arange(
             offset * stride, grid_width * stride, step=stride, dtype='float32')
         shifts_y = paddle.arange(
-            offset * stride,
-            grid_height * stride,
-            step=stride,
-            dtype='float32')
+            offset * stride, grid_height * stride, step=stride, dtype='float32')
         shift_y, shift_x = paddle.meshgrid(shifts_y, shifts_x)
         shift_x = paddle.reshape(shift_x, [-1])
         shift_y = paddle.reshape(shift_y, [-1])

+ 9 - 15
paddlers/models/ppdet/modeling/proposal_generator/target.py

@@ -40,14 +40,12 @@ def rpn_anchor_target(anchors,
             anchors, gt_bbox, rpn_positive_overlap, rpn_negative_overlap, True,
             ignore_thresh, is_crowd_i, assign_on_cpu)
         # Step2: sample anchor
-        fg_inds, bg_inds = subsample_labels(match_labels,
-                                            rpn_batch_size_per_im,
+        fg_inds, bg_inds = subsample_labels(match_labels, rpn_batch_size_per_im,
                                             rpn_fg_fraction, 0, use_random)
         # Fill with the ignore label (-1), then set positive and negative labels
         labels = paddle.full(match_labels.shape, -1, dtype='int32')
         if bg_inds.shape[0] > 0:
-            labels = paddle.scatter(labels, bg_inds,
-                                    paddle.zeros_like(bg_inds))
+            labels = paddle.scatter(labels, bg_inds, paddle.zeros_like(bg_inds))
         if fg_inds.shape[0] > 0:
             labels = paddle.scatter(labels, fg_inds, paddle.ones_like(fg_inds))
         # Step3: make output
@@ -261,15 +259,14 @@ def sample_bbox(matches,
                                   paddle.ones_like(gt_classes) * num_classes,
                                   gt_classes)
         gt_classes = paddle.where(match_labels == -1,
-                                  paddle.ones_like(gt_classes) * -1,
-                                  gt_classes)
+                                  paddle.ones_like(gt_classes) * -1, gt_classes)
     if is_cascade:
         index = paddle.arange(matches.shape[0])
         return index, gt_classes
     rois_per_image = int(batch_size_per_im)
 
-    fg_inds, bg_inds = subsample_labels(gt_classes, rois_per_image,
-                                        fg_fraction, num_classes, use_random)
+    fg_inds, bg_inds = subsample_labels(gt_classes, rois_per_image, fg_fraction,
+                                        num_classes, use_random)
     if fg_inds.shape[0] == 0 and bg_inds.shape[0] == 0:
         # fake output labeled with -1 when all boxes are neither
         # foreground nor background
@@ -364,9 +361,7 @@ def generate_mask_target(gt_segms, rois, labels_int32, sampled_gt_inds,
                     rasterize_polygons_within_box(new_segm[j], boxes[j],
                                                   resolution))
         else:
-            results.append(
-                paddle.ones(
-                    [resolution, resolution], dtype='int32'))
+            results.append(paddle.ones([resolution, resolution], dtype='int32'))
 
         fg_classes = paddle.gather(labels_per_im, fg_inds)
         weight = paddle.ones([fg_rois.shape[0]], dtype='float32')
@@ -484,8 +479,8 @@ def libra_sample_neg(max_overlaps,
         if floor_thr > 0:
             floor_set = set(
                 np.where(
-                    np.logical_and(max_overlaps >= 0, max_overlaps <
-                                   floor_thr))[0])
+                    np.logical_and(max_overlaps >= 0, max_overlaps < floor_thr))
+                [0])
             iou_sampling_set = set(np.where(max_overlaps >= floor_thr)[0])
         elif floor_thr == 0:
             floor_set = set(np.where(max_overlaps == 0)[0])
@@ -614,8 +609,7 @@ def libra_sample_bbox(matches,
                                   paddle.ones_like(gt_classes) * num_classes,
                                   gt_classes)
         gt_classes = paddle.where(match_labels == -1,
-                                  paddle.ones_like(gt_classes) * -1,
-                                  gt_classes)
+                                  paddle.ones_like(gt_classes) * -1, gt_classes)
         sampled_gt_classes = paddle.gather(gt_classes, sampled_inds)
 
         return sampled_inds, sampled_gt_classes

+ 3 - 3
paddlers/models/ppdet/utils/download.py

@@ -170,9 +170,9 @@ def get_dataset_path(path, annotation, image_dir):
     if _dataset_exists(path, annotation, image_dir):
         return path
 
-    logger.info(
-        "Dataset {} is not valid for reason above, try searching {} or "
-        "downloading dataset...".format(osp.realpath(path), DATASET_HOME))
+    logger.info("Dataset {} is not valid for reason above, try searching {} or "
+                "downloading dataset...".format(
+                    osp.realpath(path), DATASET_HOME))
 
     data_name = os.path.split(path.strip().lower())[-1]
     for name, dataset in DATASETS.items():

+ 1 - 1
paddlers/models/ppgan/apps/__init__.py

@@ -17,4 +17,4 @@ from .mpr_predictor import MPRPredictor
 from .drn_predictor import DRNPredictor
 from .pan_predictor import PANPredictor
 from .lesrcnn_predictor import LESRCNNPredictor
-from .esrgan_predictor import ESRGANPredictor
+from .esrgan_predictor import ESRGANPredictor

+ 19 - 14
paddlers/models/ppgan/apps/drn_predictor.py

@@ -16,7 +16,7 @@ import os
 import numpy as np
 from PIL import Image
 
-import paddle 
+import paddle
 from ppgan.models.generators import DRNGenerator
 from ppgan.utils.download import get_path_from_url
 from ppgan.utils.logger import get_logger
@@ -25,21 +25,25 @@ from .base_predictor import BasePredictor
 
 REALSR_WEIGHT_URL = 'https://paddlegan.bj.bcebos.com/models/DRNSx4.pdparams'
 
+
 class DRNPredictor(BasePredictor):
     def __init__(self, output='output', weight_path=None):
         self.input = input
-        self.output = os.path.join(output, 'DRN') #定义超分的结果保存的路径,为output路径+模型名所在文件夹
-        self.model = DRNGenerator((2, 4)) # 实例化模型
+        self.output = os.path.join(output,
+                                   'DRN')  #定义超分的结果保存的路径,为output路径+模型名所在文件夹
+        self.model = DRNGenerator((2, 4))  # 实例化模型
         if weight_path is None:
             weight_path = get_path_from_url(REALSR_WEIGHT_URL)
-        state_dict = paddle.load(weight_path) #加载权重
-        state_dict = state_dict['generator'] 
+        state_dict = paddle.load(weight_path)  #加载权重
+        state_dict = state_dict['generator']
         self.model.load_dict(state_dict)
         self.model.eval()
+
     # 标准化
     def norm(self, img):
         img = np.array(img).transpose([2, 0, 1]).astype('float32') / 1.0
         return img.astype('float32')
+
     # 去标准化
     def denorm(self, img):
         img = img.transpose((1, 2, 0))
@@ -54,14 +58,15 @@ class DRNPredictor(BasePredictor):
         elif isinstance(img, Image.Image):
             ori_img = img
 
-        img = self.norm(ori_img) #图像标准化
-        x = paddle.to_tensor(img[np.newaxis, ...]) #转成tensor
+        img = self.norm(ori_img)  #图像标准化
+        x = paddle.to_tensor(img[np.newaxis, ...])  #转成tensor
         with paddle.no_grad():
-            out = self.model(x)[2] # 执行预测,DRN模型会输出三个tensor,第一个是原始低分辨率影像,第二个是放大两倍,第三个才是我们所需要的最后的结果
-            
+            out = self.model(
+                x
+            )[2]  # 执行预测,DRN模型会输出三个tensor,第一个是原始低分辨率影像,第二个是放大两倍,第三个才是我们所需要的最后的结果
 
-        pred_img = self.denorm(out.numpy()[0]) #tensor转成numpy的array并去标准化
-        pred_img = Image.fromarray(pred_img) # array转图像
+        pred_img = self.denorm(out.numpy()[0])  #tensor转成numpy的array并去标准化
+        pred_img = Image.fromarray(pred_img)  # array转图像
         return pred_img
 
     #输入图像文件路径
@@ -70,15 +75,15 @@ class DRNPredictor(BasePredictor):
         if not os.path.exists(self.output):
             os.makedirs(self.output)
 
-        pred_img = self.run_image(input) #对输入的图片进行预测
+        pred_img = self.run_image(input)  #对输入的图片进行预测
         out_path = None
         if self.output:
             try:
                 base_name = os.path.splitext(os.path.basename(input))[0]
             except:
                 base_name = 'result'
-            out_path = os.path.join(self.output, base_name + '.png') #保存路径
-            pred_img.save(out_path) #保存输出图片
+            out_path = os.path.join(self.output, base_name + '.png')  #保存路径
+            pred_img.save(out_path)  #保存输出图片
             logger = get_logger()
             logger.info('Image saved to {}'.format(out_path))
 

+ 2 - 1
paddlers/models/ppgan/apps/esrgan_predictor.py

@@ -28,6 +28,7 @@ from .base_predictor import BasePredictor
 
 SR_WEIGHT_URL = 'https://paddlegan.bj.bcebos.com/models/esrgan_x4.pdparams'
 
+
 class ESRGANPredictor(BasePredictor):
     def __init__(self, output='output', weight_path=None):
         self.input = input
@@ -83,4 +84,4 @@ class ESRGANPredictor(BasePredictor):
             logger = get_logger()
             logger.info('Image saved to {}'.format(out_path))
 
-        return pred_img, out_path
+        return pred_img, out_path

+ 17 - 14
paddlers/models/ppgan/apps/lesrcnn_predictor.py

@@ -16,7 +16,7 @@ import os
 import numpy as np
 from PIL import Image
 
-import paddle 
+import paddle
 from ppgan.models.generators import LESRCNNGenerator
 from ppgan.utils.download import get_path_from_url
 from ppgan.utils.logger import get_logger
@@ -25,21 +25,25 @@ from .base_predictor import BasePredictor
 
 REALSR_WEIGHT_URL = 'https://paddlegan.bj.bcebos.com/models/lesrcnn_x4.pdparams'
 
+
 class LESRCNNPredictor(BasePredictor):
     def __init__(self, output='output', weight_path=None):
         self.input = input
-        self.output = os.path.join(output, 'LESRCNN') #定义超分的结果保存的路径,为output路径+模型名所在文件夹
-        self.model = LESRCNNGenerator() # 实例化模型
+        self.output = os.path.join(output,
+                                   'LESRCNN')  #定义超分的结果保存的路径,为output路径+模型名所在文件夹
+        self.model = LESRCNNGenerator()  # 实例化模型
         if weight_path is None:
             weight_path = get_path_from_url(REALSR_WEIGHT_URL)
-        state_dict = paddle.load(weight_path) #加载权重
-        state_dict = state_dict['generator'] 
+        state_dict = paddle.load(weight_path)  #加载权重
+        state_dict = state_dict['generator']
         self.model.load_dict(state_dict)
         self.model.eval()
+
     # 标准化
     def norm(self, img):
         img = np.array(img).transpose([2, 0, 1]).astype('float32') / 255.0
         return img.astype('float32')
+
     # 去标准化
     def denorm(self, img):
         img = img.transpose((1, 2, 0))
@@ -54,14 +58,13 @@ class LESRCNNPredictor(BasePredictor):
         elif isinstance(img, Image.Image):
             ori_img = img
 
-        img = self.norm(ori_img) #图像标准化
-        x = paddle.to_tensor(img[np.newaxis, ...]) #转成tensor
+        img = self.norm(ori_img)  #图像标准化
+        x = paddle.to_tensor(img[np.newaxis, ...])  #转成tensor
         with paddle.no_grad():
             out = self.model(x)
-            
 
-        pred_img = self.denorm(out.numpy()[0]) #tensor转成numpy的array并去标准化
-        pred_img = Image.fromarray(pred_img) # array转图像
+        pred_img = self.denorm(out.numpy()[0])  #tensor转成numpy的array并去标准化
+        pred_img = Image.fromarray(pred_img)  # array转图像
         return pred_img
 
     #输入图像文件路径
@@ -70,16 +73,16 @@ class LESRCNNPredictor(BasePredictor):
         if not os.path.exists(self.output):
             os.makedirs(self.output)
 
-        pred_img = self.run_image(input) #对输入的图片进行预测
+        pred_img = self.run_image(input)  #对输入的图片进行预测
         out_path = None
         if self.output:
             try:
                 base_name = os.path.splitext(os.path.basename(input))[0]
             except:
                 base_name = 'result'
-            out_path = os.path.join(self.output, base_name + '.png') #保存路径
-            pred_img.save(out_path) #保存输出图片
+            out_path = os.path.join(self.output, base_name + '.png')  #保存路径
+            pred_img.save(out_path)  #保存输出图片
             logger = get_logger()
             logger.info('Image saved to {}'.format(out_path))
 
-        return pred_img, out_path
+        return pred_img, out_path

+ 53 - 49
paddlers/models/ppgan/apps/midas/blocks.py

@@ -13,10 +13,8 @@ def _make_encoder(backbone,
     if backbone == "resnext101_wsl":
         # resnext101_wsl
         pretrained = _make_pretrained_resnext101_wsl(use_pretrained)
-        scratch = _make_scratch([256, 512, 1024, 2048],
-                                features,
-                                groups=groups,
-                                expand=expand)
+        scratch = _make_scratch(
+            [256, 512, 1024, 2048], features, groups=groups, expand=expand)
     else:
         print(f"Backbone '{backbone}' not implemented")
         assert False
@@ -36,34 +34,38 @@ def _make_scratch(in_shape, out_shape, groups=1, expand=False):
         out_shape3 = out_shape * 4
         out_shape4 = out_shape * 8
 
-    scratch.layer1_rn = nn.Conv2D(in_shape[0],
-                                  out_shape1,
-                                  kernel_size=3,
-                                  stride=1,
-                                  padding=1,
-                                  bias_attr=False,
-                                  groups=groups)
-    scratch.layer2_rn = nn.Conv2D(in_shape[1],
-                                  out_shape2,
-                                  kernel_size=3,
-                                  stride=1,
-                                  padding=1,
-                                  bias_attr=False,
-                                  groups=groups)
-    scratch.layer3_rn = nn.Conv2D(in_shape[2],
-                                  out_shape3,
-                                  kernel_size=3,
-                                  stride=1,
-                                  padding=1,
-                                  bias_attr=False,
-                                  groups=groups)
-    scratch.layer4_rn = nn.Conv2D(in_shape[3],
-                                  out_shape4,
-                                  kernel_size=3,
-                                  stride=1,
-                                  padding=1,
-                                  bias_attr=False,
-                                  groups=groups)
+    scratch.layer1_rn = nn.Conv2D(
+        in_shape[0],
+        out_shape1,
+        kernel_size=3,
+        stride=1,
+        padding=1,
+        bias_attr=False,
+        groups=groups)
+    scratch.layer2_rn = nn.Conv2D(
+        in_shape[1],
+        out_shape2,
+        kernel_size=3,
+        stride=1,
+        padding=1,
+        bias_attr=False,
+        groups=groups)
+    scratch.layer3_rn = nn.Conv2D(
+        in_shape[2],
+        out_shape3,
+        kernel_size=3,
+        stride=1,
+        padding=1,
+        bias_attr=False,
+        groups=groups)
+    scratch.layer4_rn = nn.Conv2D(
+        in_shape[3],
+        out_shape4,
+        kernel_size=3,
+        stride=1,
+        padding=1,
+        bias_attr=False,
+        groups=groups)
 
     return scratch
 
@@ -89,6 +91,7 @@ def _make_pretrained_resnext101_wsl(use_pretrained):
 class ResidualConvUnit(nn.Layer):
     """Residual convolution module.
     """
+
     def __init__(self, features):
         """Init.
 
@@ -97,19 +100,21 @@ class ResidualConvUnit(nn.Layer):
         """
         super().__init__()
 
-        self.conv1 = nn.Conv2D(features,
-                               features,
-                               kernel_size=3,
-                               stride=1,
-                               padding=1,
-                               bias_attr=True)
-
-        self.conv2 = nn.Conv2D(features,
-                               features,
-                               kernel_size=3,
-                               stride=1,
-                               padding=1,
-                               bias_attr=True)
+        self.conv1 = nn.Conv2D(
+            features,
+            features,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            bias_attr=True)
+
+        self.conv2 = nn.Conv2D(
+            features,
+            features,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            bias_attr=True)
 
         self.relu = nn.ReLU()
 
@@ -133,6 +138,7 @@ class ResidualConvUnit(nn.Layer):
 class FeatureFusionBlock(nn.Layer):
     """Feature fusion block.
     """
+
     def __init__(self, features):
         """Init.
 
@@ -156,9 +162,7 @@ class FeatureFusionBlock(nn.Layer):
             output += self.resConfUnit1(xs[1])
 
         output = self.resConfUnit2(output)
-        output = nn.functional.interpolate(output,
-                                           scale_factor=2,
-                                           mode="bilinear",
-                                           align_corners=True)
+        output = nn.functional.interpolate(
+            output, scale_factor=2, mode="bilinear", align_corners=True)
 
         return output

+ 9 - 4
paddlers/models/ppgan/apps/midas/midas_net.py

@@ -22,6 +22,7 @@ class BaseModel(paddle.nn.Layer):
 class MidasNet(BaseModel):
     """Network for monocular depth estimation.
     """
+
     def __init__(self, path=None, features=256, non_negative=True):
         """Init.
 
@@ -47,11 +48,15 @@ class MidasNet(BaseModel):
         self.scratch.refinenet1 = FeatureFusionBlock(features)
 
         output_conv = [
-            nn.Conv2D(features, 128, kernel_size=3, stride=1, padding=1),
-            nn.Upsample(scale_factor=2, mode="bilinear"),
-            nn.Conv2D(128, 32, kernel_size=3, stride=1, padding=1),
+            nn.Conv2D(
+                features, 128, kernel_size=3, stride=1, padding=1),
+            nn.Upsample(
+                scale_factor=2, mode="bilinear"),
+            nn.Conv2D(
+                128, 32, kernel_size=3, stride=1, padding=1),
             nn.ReLU(),
-            nn.Conv2D(32, 1, kernel_size=1, stride=1, padding=0),
+            nn.Conv2D(
+                32, 1, kernel_size=1, stride=1, padding=0),
             nn.ReLU() if non_negative else nn.Identity(),
         ]
         if non_negative:

+ 17 - 16
paddlers/models/ppgan/apps/midas/transforms.py

@@ -8,6 +8,7 @@ import math
 class Resize(object):
     """Resize sample to given size (width, height).
     """
+
     def __init__(self,
                  width,
                  height,
@@ -96,15 +97,15 @@ class Resize(object):
                     f"resize_method {self.__resize_method} not implemented")
 
         if self.__resize_method == "lower_bound":
-            new_height = self.constrain_to_multiple_of(scale_height * height,
-                                                       min_val=self.__height)
-            new_width = self.constrain_to_multiple_of(scale_width * width,
-                                                      min_val=self.__width)
+            new_height = self.constrain_to_multiple_of(
+                scale_height * height, min_val=self.__height)
+            new_width = self.constrain_to_multiple_of(
+                scale_width * width, min_val=self.__width)
         elif self.__resize_method == "upper_bound":
-            new_height = self.constrain_to_multiple_of(scale_height * height,
-                                                       max_val=self.__height)
-            new_width = self.constrain_to_multiple_of(scale_width * width,
-                                                      max_val=self.__width)
+            new_height = self.constrain_to_multiple_of(
+                scale_height * height, max_val=self.__height)
+            new_width = self.constrain_to_multiple_of(
+                scale_width * width, max_val=self.__width)
         elif self.__resize_method == "minimal":
             new_height = self.constrain_to_multiple_of(scale_height * height)
             new_width = self.constrain_to_multiple_of(scale_width * width)
@@ -122,26 +123,24 @@ class Resize(object):
         sample["image"] = cv2.resize(
             sample["image"],
             (width, height),
-            interpolation=self.__image_interpolation_method,
-        )
+            interpolation=self.__image_interpolation_method, )
 
         if self.__resize_target:
             if "disparity" in sample:
                 sample["disparity"] = cv2.resize(
                     sample["disparity"],
                     (width, height),
-                    interpolation=cv2.INTER_NEAREST,
-                )
+                    interpolation=cv2.INTER_NEAREST, )
 
             if "depth" in sample:
-                sample["depth"] = cv2.resize(sample["depth"], (width, height),
-                                             interpolation=cv2.INTER_NEAREST)
+                sample["depth"] = cv2.resize(
+                    sample["depth"], (width, height),
+                    interpolation=cv2.INTER_NEAREST)
 
             sample["mask"] = cv2.resize(
                 sample["mask"].astype(np.float32),
                 (width, height),
-                interpolation=cv2.INTER_NEAREST,
-            )
+                interpolation=cv2.INTER_NEAREST, )
             sample["mask"] = sample["mask"].astype(bool)
 
         return sample
@@ -150,6 +149,7 @@ class Resize(object):
 class NormalizeImage(object):
     """Normlize image by given mean and std.
     """
+
     def __init__(self, mean, std):
         self.__mean = mean
         self.__std = std
@@ -163,6 +163,7 @@ class NormalizeImage(object):
 class PrepareForNet(object):
     """Prepare sample for usage as network input.
     """
+
     def __init__(self):
         pass
 

+ 2 - 2
paddlers/models/ppgan/apps/midas/utils.py

@@ -26,8 +26,8 @@ def write_pfm(path, image, scale=1):
 
         if len(image.shape) == 3 and image.shape[2] == 3:  # color image
             color = True
-        elif (len(image.shape) == 2
-              or len(image.shape) == 3 and image.shape[2] == 1):  # greyscale
+        elif (len(image.shape) == 2 or len(image.shape) == 3 and
+              image.shape[2] == 1):  # greyscale
             color = False
         else:
             raise Exception(

+ 4 - 4
paddlers/models/ppgan/apps/mpr_predictor.py

@@ -91,10 +91,10 @@ class MPRPredictor(BasePredictor):
     def get_images(self, images_path):
         if os.path.isdir(images_path):
             return natsorted(
-                glob(os.path.join(images_path, '*.jpg')) +
-                glob(os.path.join(images_path, '*.JPG')) +
-                glob(os.path.join(images_path, '*.png')) +
-                glob(os.path.join(images_path, '*.PNG')))
+                glob(os.path.join(images_path, '*.jpg')) + glob(
+                    os.path.join(images_path, '*.JPG')) + glob(
+                        os.path.join(images_path, '*.png')) + glob(
+                            os.path.join(images_path, '*.PNG')))
         else:
             return [images_path]
 

+ 16 - 14
paddlers/models/ppgan/apps/pan_predictor.py

@@ -16,7 +16,7 @@ import os
 import numpy as np
 from PIL import Image
 
-import paddle 
+import paddle
 from ppgan.models.generators import PAN
 from ppgan.utils.download import get_path_from_url
 from ppgan.utils.logger import get_logger
@@ -25,21 +25,25 @@ from .base_predictor import BasePredictor
 
 REALSR_WEIGHT_URL = 'https://paddlegan.bj.bcebos.com/models/pan_x4.pdparams'
 
+
 class PANPredictor(BasePredictor):
     def __init__(self, output='output', weight_path=None):
         self.input = input
-        self.output = os.path.join(output, 'PAN') #定义超分的结果保存的路径,为output路径+模型名所在文件夹
-        self.model = PAN(3, 3, 40, 24, 16) # 实例化模型
+        self.output = os.path.join(output,
+                                   'PAN')  #定义超分的结果保存的路径,为output路径+模型名所在文件夹
+        self.model = PAN(3, 3, 40, 24, 16)  # 实例化模型
         if weight_path is None:
             weight_path = get_path_from_url(REALSR_WEIGHT_URL)
-        state_dict = paddle.load(weight_path) #加载权重
-        state_dict = state_dict['generator'] 
+        state_dict = paddle.load(weight_path)  #加载权重
+        state_dict = state_dict['generator']
         self.model.load_dict(state_dict)
         self.model.eval()
+
     # 标准化
     def norm(self, img):
         img = np.array(img).transpose([2, 0, 1]).astype('float32') / 255.0
         return img.astype('float32')
+
     # 去标准化
     def denorm(self, img):
         img = img.transpose((1, 2, 0))
@@ -54,14 +58,13 @@ class PANPredictor(BasePredictor):
         elif isinstance(img, Image.Image):
             ori_img = img
 
-        img = self.norm(ori_img) #图像标准化
-        x = paddle.to_tensor(img[np.newaxis, ...]) #转成tensor
+        img = self.norm(ori_img)  #图像标准化
+        x = paddle.to_tensor(img[np.newaxis, ...])  #转成tensor
         with paddle.no_grad():
             out = self.model(x)
-            
 
-        pred_img = self.denorm(out.numpy()[0]) #tensor转成numpy的array并去标准化
-        pred_img = Image.fromarray(pred_img) # array转图像
+        pred_img = self.denorm(out.numpy()[0])  #tensor转成numpy的array并去标准化
+        pred_img = Image.fromarray(pred_img)  # array转图像
         return pred_img
 
     #输入图像文件路径
@@ -70,17 +73,16 @@ class PANPredictor(BasePredictor):
         if not os.path.exists(self.output):
             os.makedirs(self.output)
 
-        pred_img = self.run_image(input) #对输入的图片进行预测
+        pred_img = self.run_image(input)  #对输入的图片进行预测
         out_path = None
         if self.output:
             try:
                 base_name = os.path.splitext(os.path.basename(input))[0]
             except:
                 base_name = 'result'
-            out_path = os.path.join(self.output, base_name + '.png') #保存路径
-            pred_img.save(out_path) #保存输出图片
+            out_path = os.path.join(self.output, base_name + '.png')  #保存路径
+            pred_img.save(out_path)  #保存输出图片
             logger = get_logger()
             logger.info('Image saved to {}'.format(out_path))
 
         return pred_img, out_path
-

+ 12 - 14
paddlers/models/ppgan/datasets/animeganv2_dataset.py

@@ -27,6 +27,7 @@ from .preprocess.builder import build_transforms
 class AnimeGANV2Dataset(paddle.io.Dataset):
     """
     """
+
     def __init__(self,
                  dataroot,
                  style,
@@ -50,18 +51,14 @@ class AnimeGANV2Dataset(paddle.io.Dataset):
         self.anime_root = os.path.join(self.root, f'{self.style}', 'style')
         self.smooth_root = os.path.join(self.root, f'{self.style}', 'smooth')
 
-        self.real = ImageFolder(self.real_root,
-                                transform=self.transform_real,
-                                loader=self.loader)
-        self.anime = ImageFolder(self.anime_root,
-                                 transform=self.transform_anime,
-                                 loader=self.loader)
-        self.anime_gray = ImageFolder(self.anime_root,
-                                      transform=self.transform_gray,
-                                      loader=self.loader)
-        self.smooth_gray = ImageFolder(self.smooth_root,
-                                       transform=self.transform_gray,
-                                       loader=self.loader)
+        self.real = ImageFolder(
+            self.real_root, transform=self.transform_real, loader=self.loader)
+        self.anime = ImageFolder(
+            self.anime_root, transform=self.transform_anime, loader=self.loader)
+        self.anime_gray = ImageFolder(
+            self.anime_root, transform=self.transform_gray, loader=self.loader)
+        self.smooth_gray = ImageFolder(
+            self.smooth_root, transform=self.transform_gray, loader=self.loader)
         self.sizes = [
             len(fold) for fold in [self.real, self.anime, self.smooth_gray]
         ]
@@ -70,8 +67,9 @@ class AnimeGANV2Dataset(paddle.io.Dataset):
 
     @staticmethod
     def loader(path):
-        return cv2.cvtColor(cv2.imread(path, flags=cv2.IMREAD_COLOR),
-                            cv2.COLOR_BGR2RGB)
+        return cv2.cvtColor(
+            cv2.imread(
+                path, flags=cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
 
     def reshuffle(self):
         indexs = []

+ 3 - 3
paddlers/models/ppgan/datasets/base_dataset.py

@@ -57,9 +57,8 @@ def scandir(dir_path, suffix=None, recursive=False):
                     yield rel_path
             else:
                 if recursive:
-                    yield from _scandir(entry.path,
-                                        suffix=suffix,
-                                        recursive=recursive)
+                    yield from _scandir(
+                        entry.path, suffix=suffix, recursive=recursive)
                 else:
                     continue
 
@@ -79,6 +78,7 @@ class BaseDataset(Dataset, metaclass=ABCMeta):
         preprocess (list[dict]): A sequence of data preprocess config.
 
     """
+
     def __init__(self, preprocess=None):
         super(BaseDataset, self).__init__()
 

+ 1 - 0
paddlers/models/ppgan/datasets/base_sr_dataset.py

@@ -23,6 +23,7 @@ from .builder import DATASETS
 @DATASETS.register()
 class SRDataset(BaseDataset):
     """Base super resulotion dataset for image restoration."""
+
     def __init__(self,
                  lq_folder,
                  gt_folder,

+ 17 - 14
paddlers/models/ppgan/datasets/builder.py

@@ -48,21 +48,24 @@ def build_dataloader(cfg, is_train=True, distributed=True):
     dataset = build_dataset(cfg_)
 
     if distributed:
-        sampler = DistributedBatchSampler(dataset,
-                                          batch_size=batch_size,
-                                          shuffle=True if is_train else False,
-                                          drop_last=True if is_train else False)
+        sampler = DistributedBatchSampler(
+            dataset,
+            batch_size=batch_size,
+            shuffle=True if is_train else False,
+            drop_last=True if is_train else False)
 
-        dataloader = paddle.io.DataLoader(dataset,
-                                          batch_sampler=sampler,
-                                          num_workers=num_workers,
-                                          use_shared_memory=use_shared_memory)
+        dataloader = paddle.io.DataLoader(
+            dataset,
+            batch_sampler=sampler,
+            num_workers=num_workers,
+            use_shared_memory=use_shared_memory)
     else:
-        dataloader = paddle.io.DataLoader(dataset,
-                                          batch_size=batch_size,
-                                          shuffle=True if is_train else False,
-                                          drop_last=True if is_train else False,
-                                          use_shared_memory=use_shared_memory,
-                                          num_workers=num_workers)
+        dataloader = paddle.io.DataLoader(
+            dataset,
+            batch_size=batch_size,
+            shuffle=True if is_train else False,
+            drop_last=True if is_train else False,
+            use_shared_memory=use_shared_memory,
+            num_workers=num_workers)
 
     return dataloader

+ 1 - 0
paddlers/models/ppgan/datasets/common_vision_dataset.py

@@ -25,6 +25,7 @@ class CommonVisionDataset(paddle.io.Dataset):
     """
     Dataset for using paddle vision default datasets, such as mnist, flowers.
     """
+
     def __init__(self,
                  dataset_name,
                  transforms=None,

+ 29 - 26
paddlers/models/ppgan/datasets/firstorder_dataset.py

@@ -48,13 +48,13 @@ class FirstOrderDataset(Dataset):
             file_idx_set = list(file_idx_set)
             if len(file_idx_set) != 0:
                 if POOL_SIZE == 0:
-                    for idx in tqdm.tqdm(file_idx_set,
-                                         desc='Extracting frames'):
+                    for idx in tqdm.tqdm(
+                            file_idx_set, desc='Extracting frames'):
                         _ = self.frameDataset[idx]
                 else:
                     # multiprocessing
-                    bar = tqdm.tqdm(total=len(file_idx_set),
-                                    desc='Extracting frames')
+                    bar = tqdm.tqdm(
+                        total=len(file_idx_set), desc='Extracting frames')
                     with Pool(POOL_SIZE) as pl:
                         _p = 0
                         while _p <= len(file_idx_set) - 1:
@@ -90,10 +90,10 @@ def read_video(name: Path, frame_shape=tuple([256, 256, 3]), saveto='folder'):
       - folder with videos
     """
     if name.is_dir():
-        frames = sorted(name.iterdir(),
-                        key=lambda x: int(x.with_suffix('').name))
-        video_array = np.array([imread(path) for path in frames],
-                               dtype='float32')
+        frames = sorted(
+            name.iterdir(), key=lambda x: int(x.with_suffix('').name))
+        video_array = np.array(
+            [imread(path) for path in frames], dtype='float32')
         return video_array
     elif name.suffix.lower() in ['.gif', '.mp4', '.mov']:
         try:
@@ -123,7 +123,8 @@ def read_video(name: Path, frame_shape=tuple([256, 256, 3]), saveto='folder'):
             except FileExistsError:
                 pass
             for idx, img in enumerate(video_array_reshape):
-                cv2.imwrite(str(sub_dir.joinpath('%i.png' % idx)), img[:,:,[2,1,0]])
+                cv2.imwrite(
+                    str(sub_dir.joinpath('%i.png' % idx)), img[:, :, [2, 1, 0]])
             name.unlink()
         return video_array_reshape
     else:
@@ -138,6 +139,7 @@ class FramesDataset(Dataset):
       - folder with all frames
     FramesDataset[i]: obtain sample from i-th video in self.videos
     """
+
     def __init__(self, cfg):
         self.root_dir = Path(cfg['dataroot'])
         self.videos = None
@@ -161,8 +163,8 @@ class FramesDataset(Dataset):
         else:
             train_videos = list(self.root_dir.joinpath('train').iterdir())
         test_videos = list(self.root_dir.joinpath('test').iterdir())
-        self.root_dir = self.root_dir.joinpath(
-            'train' if self.is_train else 'test')
+        self.root_dir = self.root_dir.joinpath('train'
+                                               if self.is_train else 'test')
 
         if self.is_train:
             self.videos = train_videos
@@ -184,23 +186,22 @@ class FramesDataset(Dataset):
             path = self.videos[idx]
         video_name = path.name
         if self.is_train and path.is_dir():
-            frames = sorted(path.iterdir(),
-                            key=lambda x: int(x.with_suffix('').name))
+            frames = sorted(
+                path.iterdir(), key=lambda x: int(x.with_suffix('').name))
             num_frames = len(frames)
             frame_idx = np.sort(
-                np.random.choice(num_frames, replace=True, size=2))
+                np.random.choice(
+                    num_frames, replace=True, size=2))
             video_array = [imread(str(frames[idx])) for idx in frame_idx]
         else:
             if self.create_frames_folder:
-                video_array = read_video(path,
-                                         frame_shape=self.frame_shape,
-                                         saveto='folder')
+                video_array = read_video(
+                    path, frame_shape=self.frame_shape, saveto='folder')
                 self.videos[idx] = path.with_suffix(
                     '')  # rename /xx/xx/xx.gif -> /xx/xx/xx
             else:
-                video_array = read_video(path,
-                                         frame_shape=self.frame_shape,
-                                         saveto=None)
+                video_array = read_video(
+                    path, frame_shape=self.frame_shape, saveto=None)
             num_frames = len(video_array)
             frame_idx = np.sort(
                 np.random.choice(
@@ -220,13 +221,14 @@ class FramesDataset(Dataset):
         if self.is_train:
             if self.transform is not None:  #modify
                 t = self.transform(tuple(video_array))
-                out['driving'] = t[0].transpose(2, 0, 1).astype(
-                    np.float32) / 255.0
-                out['source'] = t[1].transpose(2, 0, 1).astype(
-                    np.float32) / 255.0
+                out['driving'] = t[0].transpose(2, 0,
+                                                1).astype(np.float32) / 255.0
+                out['source'] = t[1].transpose(2, 0,
+                                               1).astype(np.float32) / 255.0
             else:
-                source = np.array(video_array[0],
-                                  dtype='float32') / 255.0  # shape is [H, W, C]
+                source = np.array(
+                    video_array[0],
+                    dtype='float32') / 255.0  # shape is [H, W, C]
                 driving = np.array(
                     video_array[1],
                     dtype='float32') / 255.0  # shape is [H, W, C]
@@ -250,6 +252,7 @@ class DatasetRepeater(Dataset):
     """
     Pass several times over the same dataset for better i/o performance
     """
+
     def __init__(self, dataset, num_repeats=100):
         self.dataset = dataset
         self.num_repeats = num_repeats

+ 2 - 2
paddlers/models/ppgan/datasets/image_folder.py

@@ -70,8 +70,8 @@ class ImageFolder(Dataset):
         imgs = make_dataset(root)
         if len(imgs) == 0:
             raise (RuntimeError("Found 0 images in: " + root + "\n"
-                                "Supported image extensions are: " +
-                                ",".join(IMG_EXTENSIONS)))
+                                "Supported image extensions are: " + ",".join(
+                                    IMG_EXTENSIONS)))
 
         self.root = root
         self.imgs = imgs

+ 1 - 0
paddlers/models/ppgan/datasets/paired_dataset.py

@@ -20,6 +20,7 @@ from .base_dataset import BaseDataset
 class PairedDataset(BaseDataset):
     """A dataset class for paired image dataset.
     """
+
     def __init__(self, dataroot, preprocess):
         """Initialize this dataset class.
 

+ 4 - 5
paddlers/models/ppgan/datasets/preprocess/__init__.py

@@ -1,8 +1,7 @@
 from .io import LoadImageFromFile, ReadImageSequence, GetNeighboringFramesIdx
-from .transforms import (PairedRandomCrop, PairedRandomHorizontalFlip,
-                         PairedRandomVerticalFlip, PairedRandomTransposeHW,
-                         SRPairedRandomCrop, SplitPairedImage, SRNoise,
-                         NormalizeSequence, MirrorVideoSequence,
-                         TransposeSequence)
+from .transforms import (
+    PairedRandomCrop, PairedRandomHorizontalFlip, PairedRandomVerticalFlip,
+    PairedRandomTransposeHW, SRPairedRandomCrop, SplitPairedImage, SRNoise,
+    NormalizeSequence, MirrorVideoSequence, TransposeSequence)
 
 from .builder import build_preprocess

+ 1 - 0
paddlers/models/ppgan/datasets/preprocess/builder.py

@@ -35,6 +35,7 @@ class Compose(object):
         object will call each given :attr:`transforms` sequencely.
 
     """
+
     def __init__(self, functions):
         self.functions = functions
 

+ 3 - 0
paddlers/models/ppgan/datasets/preprocess/io.py

@@ -18,6 +18,7 @@ class LoadImageFromFile(object):
             `datas` dict with name of `f'ori_{key}'`. Default: False.
         kwargs (dict): Args for file client.
     """
+
     def __init__(self,
                  key='image',
                  flag=-1,
@@ -74,6 +75,7 @@ class ReadImageSequence(LoadImageFromFile):
             `datas` dict with name of `f'ori_{key}'`. Default: False.
         kwargs (dict): Args for file client.
     """
+
     def __call__(self, datas):
         """Call function.
 
@@ -130,6 +132,7 @@ class GetNeighboringFramesIdx:
             sequence. Default: 0.
         filename_tmpl (str): Template for file name. Default: '{:08d}.png'.
     """
+
     def __init__(self, interval_list, start_idx=0, filename_tmpl='{:08d}.png'):
         self.interval_list = interval_list
         self.filename_tmpl = filename_tmpl

+ 17 - 18
paddlers/models/ppgan/datasets/preprocess/transforms.py

@@ -61,8 +61,8 @@ class Transforms():
         data = tuple(data)
         for transform in self.transforms:
             data = transform(data)
-            if hasattr(transform, 'params') and isinstance(
-                    transform.params, dict):
+            if hasattr(transform, 'params') and isinstance(transform.params,
+                                                           dict):
                 datas.update(transform.params)
 
         if len(self.input_keys) > 1:
@@ -176,6 +176,7 @@ class PairedRandomTransposeHW(T.BaseTransform):
         prob (float): The propability to transpose the images.
         keys (list[str]): The images to be transposed.
     """
+
     def __init__(self, prob=0.5, keys=None):
         self.keys = keys
         self.prob = prob
@@ -220,6 +221,7 @@ class TransposeSequence(T.Transpose):
             fake_img_seq = transform(fake_img_seq)
 
     """
+
     def _apply_image(self, img):
         if isinstance(img, list):
             imgs = []
@@ -277,6 +279,7 @@ class NormalizeSequence(T.Normalize):
             fake_img_seq = normalize_seq(fake_img_seq)
 
     """
+
     def _apply_image(self, img):
         if isinstance(img, list):
             imgs = [
@@ -302,6 +305,7 @@ class SRPairedRandomCrop(T.BaseTransform):
         scale (int): model upscale factor.
         gt_patch_size (int): cropped gt patch size.
     """
+
     def __init__(self, scale, gt_patch_size, scale_list=False, keys=None):
         self.gt_patch_size = gt_patch_size
         self.scale = scale
@@ -339,16 +343,16 @@ class SRPairedRandomCrop(T.BaseTransform):
             ]
             top_gt, left_gt = int(top * scale), int(left * scale)
             gt = [
-                v[top_gt:top_gt + self.gt_patch_size,
-                  left_gt:left_gt + self.gt_patch_size, ...] for v in gt
+                v[top_gt:top_gt + self.gt_patch_size, left_gt:left_gt +
+                  self.gt_patch_size, ...] for v in gt
             ]
         else:
             # crop lq patch
             lq = lq[top:top + lq_patch_size, left:left + lq_patch_size, ...]
             # crop corresponding gt patch
             top_gt, left_gt = int(top * scale), int(left * scale)
-            gt = gt[top_gt:top_gt + self.gt_patch_size,
-                    left_gt:left_gt + self.gt_patch_size, ...]
+            gt = gt[top_gt:top_gt + self.gt_patch_size, left_gt:left_gt +
+                    self.gt_patch_size, ...]
 
             if self.scale_list and self.scale == 4:
                 lqx2 = F.resize(gt, (lq_patch_size * 2, lq_patch_size * 2),
@@ -368,14 +372,14 @@ class SRNoise(T.BaseTransform):
         noise_path (str): directory of noise image.
         size (int): cropped noise patch size.
     """
+
     def __init__(self, noise_path, size, keys=None):
         self.noise_path = noise_path
         self.noise_imgs = sorted(glob.glob(noise_path + '*.png'))
         self.size = size
         self.keys = keys
         self.transform = T.Compose([
-            T.RandomCrop(size),
-            T.Transpose(),
+            T.RandomCrop(size), T.Transpose(),
             T.Normalize([0., 0., 0.], [255., 255., 255.])
         ])
 
@@ -396,6 +400,7 @@ class RandomResizedCropProb(T.RandomResizedCrop):
         prob (float): probabilty of using random-resized cropping.
         size (int): cropped size.
     """
+
     def __init__(self, prob, size, scale, ratio, interpolation, keys=None):
         super().__init__(size, scale, ratio, interpolation)
         self.prob = prob
@@ -480,21 +485,14 @@ class ResizeToScale(T.BaseTransform):
 
 @TRANSFORMS.register()
 class PairedColorJitter(T.BaseTransform):
-    def __init__(self,
-                 brightness=0,
-                 contrast=0,
-                 saturation=0,
-                 hue=0,
+    def __init__(self, brightness=0, contrast=0, saturation=0, hue=0,
                  keys=None):
         super().__init__(keys=keys)
         self.brightness = T.transforms._check_input(brightness, 'brightness')
         self.contrast = T.transforms._check_input(contrast, 'contrast')
         self.saturation = T.transforms._check_input(saturation, 'saturation')
-        self.hue = T.transforms._check_input(hue,
-                                             'hue',
-                                             center=0,
-                                             bound=(-0.5, 0.5),
-                                             clip_first_on_zero=False)
+        self.hue = T.transforms._check_input(
+            hue, 'hue', center=0, bound=(-0.5, 0.5), clip_first_on_zero=False)
 
     def _get_params(self, input):
         """Get a randomized transform to be applied on image.
@@ -545,6 +543,7 @@ class MirrorVideoSequence:
     Args:
         keys (list[str]): The frame lists to be extended.
     """
+
     def __init__(self, keys=None):
         self.keys = keys
 

+ 1 - 0
paddlers/models/ppgan/datasets/single_dataset.py

@@ -20,6 +20,7 @@ from .builder import DATASETS
 class SingleDataset(BaseDataset):
     """
     """
+
     def __init__(self, dataroot, preprocess):
         """Initialize single dataset class.
 

+ 5 - 4
paddlers/models/ppgan/datasets/starganv2_dataset.py

@@ -107,6 +107,7 @@ class ReferenceDataset(Dataset):
 class StarGANv2Dataset(BaseDataset):
     """
     """
+
     def __init__(self, dataroot, is_train, preprocess, test_count=0):
         """Initialize single dataset class.
 
@@ -125,10 +126,10 @@ class StarGANv2Dataset(BaseDataset):
         else:
             files = os.listdir(self.dataroot)
             if 'src' in files and 'ref' in files:
-                self.src_loader = ImageFolder(os.path.join(
-                    self.dataroot, 'src'))
-                self.ref_loader = ImageFolder(os.path.join(
-                    self.dataroot, 'ref'))
+                self.src_loader = ImageFolder(
+                    os.path.join(self.dataroot, 'src'))
+                self.ref_loader = ImageFolder(
+                    os.path.join(self.dataroot, 'ref'))
             else:
                 self.src_loader = ImageFolder(self.dataroot)
                 self.ref_loader = ImageFolder(self.dataroot)

+ 1 - 0
paddlers/models/ppgan/datasets/unpaired_dataset.py

@@ -23,6 +23,7 @@ from .builder import DATASETS
 class UnpairedDataset(BaseDataset):
     """
     """
+
     def __init__(self, dataroot_a, dataroot_b, max_size, is_train, preprocess):
         """Initialize unpaired dataset class.
 

+ 18 - 16
paddlers/models/ppgan/engine/trainer.py

@@ -29,6 +29,7 @@ from ..utils.filesystem import makedirs, save, load
 from ..utils.timer import TimeAverager
 from ..utils.profiler import add_profiler_step
 
+
 class IterLoader:
     def __init__(self, dataloader):
         self._dataloader = dataloader
@@ -71,6 +72,7 @@ class Trainer:
     #                     |                                    ||
     #         save checkpoint (model.nets)                     \/
     """
+
     def __init__(self, cfg):
         # base config
         self.logger = logging.getLogger(__name__)
@@ -220,8 +222,8 @@ class Trainer:
 
     def test(self):
         if not hasattr(self, 'test_dataloader'):
-            self.test_dataloader = build_dataloader(self.cfg.dataset.test,
-                                                    is_train=False)
+            self.test_dataloader = build_dataloader(
+                self.cfg.dataset.test, is_train=False)
         iter_loader = IterLoader(self.test_dataloader)
         if self.max_eval_steps is None:
             self.max_eval_steps = len(self.test_dataloader)
@@ -235,9 +237,8 @@ class Trainer:
 
         for i in range(self.max_eval_steps):
             if self.max_eval_steps < self.log_interval or i % self.log_interval == 0:
-                self.logger.info('Test iter: [%d/%d]' %
-                                 (i * self.world_size,
-                                  self.max_eval_steps * self.world_size))
+                self.logger.info('Test iter: [%d/%d]' % (
+                    i * self.world_size, self.max_eval_steps * self.world_size))
 
             data = next(iter_loader)
             self.model.setup_input(data)
@@ -248,8 +249,8 @@ class Trainer:
                 current_paths = self.model.get_image_paths()
                 current_visuals = self.model.get_current_visuals()
 
-                if len(current_visuals) > 0 and list(
-                        current_visuals.values())[0].shape == 4:
+                if len(current_visuals) > 0 and list(current_visuals.values())[
+                        0].shape == 4:
                     num_samples = list(current_visuals.values())[0].shape[0]
                 else:
                     num_samples = 1
@@ -267,10 +268,11 @@ class Trainer:
                         else:
                             visual_results.update({name: img_tensor})
 
-                self.visual('visual_test',
-                            visual_results=visual_results,
-                            step=self.batch_id,
-                            is_save_image=True)
+                self.visual(
+                    'visual_test',
+                    visual_results=visual_results,
+                    step=self.batch_id,
+                    is_save_image=True)
 
         if self.metrics:
             for metric_name, metric in self.metrics.items():
@@ -398,9 +400,9 @@ class Trainer:
             try:
                 if self.by_epoch:
                     checkpoint_name_to_be_removed = os.path.join(
-                        self.output_dir, 'epoch_%s_%s.pdparams' %
-                        ((epoch - keep * self.weight_interval) //
-                         self.iters_per_epoch, name))
+                        self.output_dir, 'epoch_%s_%s.pdparams' % (
+                            (epoch - keep * self.weight_interval) //
+                            self.iters_per_epoch, name))
                 else:
                     checkpoint_name_to_be_removed = os.path.join(
                         self.output_dir, 'iter_%s_%s.pdparams' %
@@ -432,8 +434,8 @@ class Trainer:
         for net_name, net in self.model.nets.items():
             if net_name in state_dicts:
                 net.set_state_dict(state_dicts[net_name])
-                self.logger.info(
-                    'Loaded pretrained weight for net {}'.format(net_name))
+                self.logger.info('Loaded pretrained weight for net {}'.format(
+                    net_name))
             else:
                 self.logger.warning(
                     'Can not find state dict of net {}. Skip load pretrained weight for net {}'

+ 8 - 4
paddlers/models/ppgan/faceutils/dlibutils/face_align.py

@@ -56,7 +56,8 @@ def align(image, lms):
     # rotation angle
     left_eye_corner = lms[36]
     right_eye_corner = lms[45]
-    radian = np.arctan((left_eye_corner[1] - right_eye_corner[1]) / (left_eye_corner[0] - right_eye_corner[0]))
+    radian = np.arctan((left_eye_corner[1] - right_eye_corner[1]) /
+                       (left_eye_corner[0] - right_eye_corner[0]))
 
     # image size after rotating
     height, width, _ = image.shape
@@ -73,7 +74,8 @@ def align(image, lms):
     M = np.array([[cos, sin, (1 - cos) * width / 2. - sin * height / 2. + Tx],
                   [-sin, cos, sin * width / 2. + (1 - cos) * height / 2. + Ty]])
 
-    image_rotate = cv2.warpAffine(image, M, (new_w, new_h), borderValue=(255, 255, 255))
+    image_rotate = cv2.warpAffine(
+        image, M, (new_w, new_h), borderValue=(255, 255, 255))
 
     landmarks = np.concatenate([lms, np.ones((lms.shape[0], 1))], axis=1)
     landmarks_rotate = np.dot(M, landmarks.T).T
@@ -99,7 +101,8 @@ def crop(image, lms):
         top -= ((right - left) - (bottom - top)) // 2
         bottom = top + (right - left)
 
-    image_crop = np.ones((bottom - top + 1, right - left + 1, 3), np.uint8) * 255
+    image_crop = np.ones((bottom - top + 1, right - left + 1, 3),
+                         np.uint8) * 255
 
     h, w = image.shape[:2]
     left_white = max(0, -left)
@@ -111,5 +114,6 @@ def crop(image, lms):
     bottom = min(bottom, h - 1)
     bottom_white = top_white + (bottom - top)
 
-    image_crop[top_white:bottom_white+1, left_white:right_white+1] = image[top:bottom+1, left:right+1].copy()
+    image_crop[top_white:bottom_white + 1, left_white:right_white + 1] = image[
+        top:bottom + 1, left:right + 1].copy()
     return image_crop

+ 2 - 1
paddlers/models/ppgan/faceutils/face_detection/detection/blazeface/detect.py

@@ -65,7 +65,8 @@ def batch_detect(net, img_batch):
         ymax = pred[:, 2:3]
         locs = np.concatenate((xmin, ymin, xmax, ymax), axis=1)
         bboxlists.append(
-            np.concatenate((locs * orig_size + shift, scores), axis=1))
+            np.concatenate(
+                (locs * orig_size + shift, scores), axis=1))
 
     return bboxlists
 

+ 32 - 28
paddlers/models/ppgan/faceutils/face_detection/detection/blazeface/net_blazeface.py

@@ -22,18 +22,19 @@ class BlazeBlock(nn.Layer):
             padding = (kernel_size - 1) // 2
 
         self.convs = nn.Sequential(
-            nn.Conv2D(in_channels=in_channels,
-                      out_channels=in_channels,
-                      kernel_size=kernel_size,
-                      stride=stride,
-                      padding=padding,
-                      groups=in_channels),
-            nn.Conv2D(in_channels=in_channels,
-                      out_channels=out_channels,
-                      kernel_size=1,
-                      stride=1,
-                      padding=0),
-        )
+            nn.Conv2D(
+                in_channels=in_channels,
+                out_channels=in_channels,
+                kernel_size=kernel_size,
+                stride=stride,
+                padding=padding,
+                groups=in_channels),
+            nn.Conv2D(
+                in_channels=in_channels,
+                out_channels=out_channels,
+                kernel_size=1,
+                stride=1,
+                padding=0), )
 
         self.act = nn.ReLU()
 
@@ -52,6 +53,7 @@ class BlazeBlock(nn.Layer):
 class BlazeFace(nn.Layer):
     """The BlazeFace face detection model.
     """
+
     def __init__(self):
         super(BlazeFace, self).__init__()
 
@@ -70,32 +72,34 @@ class BlazeFace(nn.Layer):
 
     def _define_layers(self):
         self.backbone1 = nn.Sequential(
-            nn.Conv2D(in_channels=3,
-                      out_channels=24,
-                      kernel_size=5,
-                      stride=2,
-                      padding=0),
+            nn.Conv2D(
+                in_channels=3,
+                out_channels=24,
+                kernel_size=5,
+                stride=2,
+                padding=0),
             nn.ReLU(),
             BlazeBlock(24, 24),
             BlazeBlock(24, 28),
-            BlazeBlock(28, 32, stride=2),
+            BlazeBlock(
+                28, 32, stride=2),
             BlazeBlock(32, 36),
             BlazeBlock(36, 42),
-            BlazeBlock(42, 48, stride=2),
+            BlazeBlock(
+                42, 48, stride=2),
             BlazeBlock(48, 56),
             BlazeBlock(56, 64),
             BlazeBlock(64, 72),
             BlazeBlock(72, 80),
-            BlazeBlock(80, 88),
-        )
+            BlazeBlock(80, 88), )
 
         self.backbone2 = nn.Sequential(
-            BlazeBlock(88, 96, stride=2),
-            BlazeBlock(96, 96),
+            BlazeBlock(
+                88, 96, stride=2),
             BlazeBlock(96, 96),
             BlazeBlock(96, 96),
             BlazeBlock(96, 96),
-        )
+            BlazeBlock(96, 96), )
 
         self.classifier_8 = nn.Conv2D(88, 2, 1)
         self.classifier_16 = nn.Conv2D(96, 6, 1)
@@ -240,8 +244,8 @@ class BlazeFace(nn.Layer):
         output_detections = []
         for i in range(raw_box_tensor.shape[0]):
             boxes = paddle.to_tensor(detection_boxes[i, mask[i]])
-            scores = paddle.to_tensor(
-                detection_scores[i, mask[i]]).unsqueeze(axis=-1)
+            scores = paddle.to_tensor(detection_scores[i, mask[i]]).unsqueeze(
+                axis=-1)
             output_detections.append(paddle.concat((boxes, scores), axis=-1))
 
         return output_detections
@@ -296,8 +300,8 @@ class BlazeFace(nn.Layer):
 
             first_box = detection[:4]
             other_boxes = detections[remaining, :4]
-            ious = overlap_similarity(paddle.to_tensor(first_box),
-                                      paddle.to_tensor(other_boxes))
+            ious = overlap_similarity(
+                paddle.to_tensor(first_box), paddle.to_tensor(other_boxes))
 
             mask = ious > self.min_suppression_threshold
             mask = mask.numpy()

+ 2 - 2
paddlers/models/ppgan/faceutils/face_detection/detection/sfd/detect.py

@@ -95,8 +95,8 @@ def batch_detect(net, imgs):
             box = batch_decode(paddle.to_tensor(loc), priors, variances)
             box = box[:, 0] * 1.0
             bboxlist.append(
-                paddle.concat([box, paddle.to_tensor(score).unsqueeze(1)],
-                              1).numpy())
+                paddle.concat([box, paddle.to_tensor(score).unsqueeze(1)], 1)
+                .numpy())
     bboxlist = np.array(bboxlist)
     if 0 == len(bboxlist):
         bboxlist = np.zeros((1, BB, 5))

+ 27 - 63
paddlers/models/ppgan/faceutils/face_detection/detection/sfd/net_s3fd.py

@@ -23,8 +23,8 @@ class L2Norm(nn.Layer):
         self.n_channels = n_channels
         self.scale = scale
         self.eps = 1e-10
-        self.weight = paddle.create_parameter(shape=[self.n_channels],
-                                              dtype='float32')
+        self.weight = paddle.create_parameter(
+            shape=[self.n_channels], dtype='float32')
         self.weight.set_value(paddle.zeros([self.n_channels]) + self.scale)
 
     def forward(self, x):
@@ -67,67 +67,31 @@ class s3fd(nn.Layer):
         self.conv4_3_norm = L2Norm(512, scale=8)
         self.conv5_3_norm = L2Norm(512, scale=5)
 
-        self.conv3_3_norm_mbox_conf = nn.Conv2D(256,
-                                                4,
-                                                kernel_size=3,
-                                                stride=1,
-                                                padding=1)
-        self.conv3_3_norm_mbox_loc = nn.Conv2D(256,
-                                               4,
-                                               kernel_size=3,
-                                               stride=1,
-                                               padding=1)
-        self.conv4_3_norm_mbox_conf = nn.Conv2D(512,
-                                                2,
-                                                kernel_size=3,
-                                                stride=1,
-                                                padding=1)
-        self.conv4_3_norm_mbox_loc = nn.Conv2D(512,
-                                               4,
-                                               kernel_size=3,
-                                               stride=1,
-                                               padding=1)
-        self.conv5_3_norm_mbox_conf = nn.Conv2D(512,
-                                                2,
-                                                kernel_size=3,
-                                                stride=1,
-                                                padding=1)
-        self.conv5_3_norm_mbox_loc = nn.Conv2D(512,
-                                               4,
-                                               kernel_size=3,
-                                               stride=1,
-                                               padding=1)
-
-        self.fc7_mbox_conf = nn.Conv2D(1024,
-                                       2,
-                                       kernel_size=3,
-                                       stride=1,
-                                       padding=1)
-        self.fc7_mbox_loc = nn.Conv2D(1024,
-                                      4,
-                                      kernel_size=3,
-                                      stride=1,
-                                      padding=1)
-        self.conv6_2_mbox_conf = nn.Conv2D(512,
-                                           2,
-                                           kernel_size=3,
-                                           stride=1,
-                                           padding=1)
-        self.conv6_2_mbox_loc = nn.Conv2D(512,
-                                          4,
-                                          kernel_size=3,
-                                          stride=1,
-                                          padding=1)
-        self.conv7_2_mbox_conf = nn.Conv2D(256,
-                                           2,
-                                           kernel_size=3,
-                                           stride=1,
-                                           padding=1)
-        self.conv7_2_mbox_loc = nn.Conv2D(256,
-                                          4,
-                                          kernel_size=3,
-                                          stride=1,
-                                          padding=1)
+        self.conv3_3_norm_mbox_conf = nn.Conv2D(
+            256, 4, kernel_size=3, stride=1, padding=1)
+        self.conv3_3_norm_mbox_loc = nn.Conv2D(
+            256, 4, kernel_size=3, stride=1, padding=1)
+        self.conv4_3_norm_mbox_conf = nn.Conv2D(
+            512, 2, kernel_size=3, stride=1, padding=1)
+        self.conv4_3_norm_mbox_loc = nn.Conv2D(
+            512, 4, kernel_size=3, stride=1, padding=1)
+        self.conv5_3_norm_mbox_conf = nn.Conv2D(
+            512, 2, kernel_size=3, stride=1, padding=1)
+        self.conv5_3_norm_mbox_loc = nn.Conv2D(
+            512, 4, kernel_size=3, stride=1, padding=1)
+
+        self.fc7_mbox_conf = nn.Conv2D(
+            1024, 2, kernel_size=3, stride=1, padding=1)
+        self.fc7_mbox_loc = nn.Conv2D(
+            1024, 4, kernel_size=3, stride=1, padding=1)
+        self.conv6_2_mbox_conf = nn.Conv2D(
+            512, 2, kernel_size=3, stride=1, padding=1)
+        self.conv6_2_mbox_loc = nn.Conv2D(
+            512, 4, kernel_size=3, stride=1, padding=1)
+        self.conv7_2_mbox_conf = nn.Conv2D(
+            256, 2, kernel_size=3, stride=1, padding=1)
+        self.conv7_2_mbox_loc = nn.Conv2D(
+            256, 4, kernel_size=3, stride=1, padding=1)
 
     def forward(self, x):
         h = F.relu(self.conv1_1(x))

+ 8 - 8
paddlers/models/ppgan/faceutils/face_detection/utils.py

@@ -55,8 +55,8 @@ def crop(image, center, scale, resolution=256.0):
     br = transform([resolution, resolution], center, scale, resolution, True)
     br = br.numpy()
     if image.ndim > 2:
-        newDim = np.array([br[1] - ul[1], br[0] - ul[0], image.shape[2]],
-                          dtype=np.int32)
+        newDim = np.array(
+            [br[1] - ul[1], br[0] - ul[0], image.shape[2]], dtype=np.int32)
         newImg = np.zeros(newDim, dtype=np.uint8)
     else:
         newDim = np.array([br[1] - ul[1], br[0] - ul[0]], dtype=np.int)
@@ -69,10 +69,10 @@ def crop(image, center, scale, resolution=256.0):
         [max(1, -ul[1] + 1), min(br[1], ht) - ul[1]], dtype=np.int32)
     oldX = np.array([max(1, ul[0] + 1), min(br[0], wd)], dtype=np.int32)
     oldY = np.array([max(1, ul[1] + 1), min(br[1], ht)], dtype=np.int32)
-    newImg[newY[0] - 1:newY[1],
-           newX[0] - 1:newX[1]] = image[oldY[0] - 1:oldY[1],
-                                        oldX[0] - 1:oldX[1], :]
-    newImg = cv2.resize(newImg,
-                        dsize=(int(resolution), int(resolution)),
-                        interpolation=cv2.INTER_LINEAR)
+    newImg[newY[0] - 1:newY[1], newX[0] - 1:newX[1]] = image[oldY[0] - 1:oldY[
+        1], oldX[0] - 1:oldX[1], :]
+    newImg = cv2.resize(
+        newImg,
+        dsize=(int(resolution), int(resolution)),
+        interpolation=cv2.INTER_LINEAR)
     return newImg

+ 13 - 15
paddlers/models/ppgan/faceutils/face_enhancement/face_enhance.py

@@ -25,11 +25,7 @@ GPEN_weights = 'https://paddlegan.bj.bcebos.com/models/GPEN-512.pdparams'
 
 
 class FaceEnhancement(object):
-    def __init__(self,
-                 path_to_enhance=None,
-                 size = 512,
-                 batch_size=1
-                 ):
+    def __init__(self, path_to_enhance=None, size=512, batch_size=1):
         super(FaceEnhancement, self).__init__()
 
         # Initialise the face detector
@@ -38,17 +34,19 @@ class FaceEnhancement(object):
             model_weights = paddle.load(model_weights_path)
         else:
             model_weights = paddle.load(path_to_enhance)
-            
+
         self.face_enhance = GPEN(size=512, style_dim=512, n_mlp=8)
         self.face_enhance.load_dict(model_weights)
         self.face_enhance.eval()
         self.size = size
         self.mask = np.zeros((512, 512), np.float32)
-        cv2.rectangle(self.mask, (26, 26), (486, 486), (1, 1, 1), -1, cv2.LINE_AA)
+        cv2.rectangle(self.mask, (26, 26), (486, 486), (1, 1, 1), -1,
+                      cv2.LINE_AA)
         self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
         self.mask = cv2.GaussianBlur(self.mask, (101, 101), 11)
-        self.mask = paddle.tile(paddle.to_tensor(self.mask).unsqueeze(0).unsqueeze(-1), repeat_times=[batch_size,1,1,3]).numpy()
-        
+        self.mask = paddle.tile(
+            paddle.to_tensor(self.mask).unsqueeze(0).unsqueeze(-1),
+            repeat_times=[batch_size, 1, 1, 3]).numpy()
 
     def enhance_from_image(self, img):
         if isinstance(img, np.ndarray):
@@ -65,14 +63,14 @@ class FaceEnhancement(object):
         else:
             assert img.shape[1:] == [3, 512, 512]
             img_ori = img.transpose([0, 2, 3, 1]).numpy()
-        img_t = (img/255. - 0.5) / 0.5
-        
+        img_t = (img / 255. - 0.5) / 0.5
+
         with paddle.no_grad():
             out, __ = self.face_enhance(img_t)
-        
+
         image_tensor = out * 0.5 + 0.5
-        image_tensor = image_tensor.transpose([0, 2, 3, 1]) # RGB
+        image_tensor = image_tensor.transpose([0, 2, 3, 1])  # RGB
         image_numpy = paddle.clip(image_tensor, 0, 1) * 255.0
-        
+
         out = image_numpy.astype(np.uint8).cpu().numpy()
-        return out * self.mask + (1-self.mask) * img_ori 
+        return out * self.mask + (1 - self.mask) * img_ori

+ 4 - 3
paddlers/models/ppgan/faceutils/face_segmentation/face_seg.py

@@ -21,13 +21,13 @@ from paddle.utils.download import get_path_from_url
 from .fcn import FCN
 from .hrnet import HRNet_W18
 
-
 BISENET_WEIGHT_URL = 'https://paddlegan.bj.bcebos.com/models/faceseg_FCN-HRNetW18.pdparams'
 
 
 class FaceSeg:
     def __init__(self):
-        save_pth = get_path_from_url(BISENET_WEIGHT_URL, osp.split(osp.realpath(__file__))[0])
+        save_pth = get_path_from_url(BISENET_WEIGHT_URL,
+                                     osp.split(osp.realpath(__file__))[0])
 
         self.net = FCN(num_classes=2, backbone=HRNet_W18())
         state_dict = paddle.load(save_pth)
@@ -47,7 +47,8 @@ class FaceSeg:
         return mask
 
     def input_transform(self, image):
-        image_input = cv2.resize(image, (384, 384), interpolation=cv2.INTER_AREA)
+        image_input = cv2.resize(
+            image, (384, 384), interpolation=cv2.INTER_AREA)
         image_input = (image_input / 255.)[np.newaxis, :, :, :]
         image_input = np.transpose(image_input, (0, 3, 1, 2)).astype(np.float32)
         image_input = paddle.to_tensor(image_input)

+ 32 - 26
paddlers/models/ppgan/metrics/lpips.py

@@ -45,6 +45,7 @@ class LPIPSMetric(paddle.metric.Metric):
     Returns:
         float: lpips result.
     """
+
     def __init__(self, net='vgg', version='0.1', mean=None, std=None):
         self.net = net
         self.version = version
@@ -76,10 +77,10 @@ class LPIPSMetric(paddle.metric.Metric):
         for pred, gt in zip(preds, gts):
             pred, gt = pred.astype(np.float32) / 255., gt.astype(
                 np.float32) / 255.
-            pred = paddle.vision.transforms.normalize(pred.transpose([2, 0, 1]),
-                                                      self.mean, self.std)
-            gt = paddle.vision.transforms.normalize(gt.transpose([2, 0, 1]),
-                                                    self.mean, self.std)
+            pred = paddle.vision.transforms.normalize(
+                pred.transpose([2, 0, 1]), self.mean, self.std)
+            gt = paddle.vision.transforms.normalize(
+                gt.transpose([2, 0, 1]), self.mean, self.std)
 
             with paddle.no_grad():
                 value = self.loss_fn(
@@ -110,11 +111,13 @@ def spatial_average(in_tens, keepdim=True):
 # assumes scale factor is same for H and W
 def upsample(in_tens, out_HW=(64, 64)):
     in_H, in_W = in_tens.shape[2], in_tens.shape[3]
-    scale_factor_H, scale_factor_W = 1. * out_HW[0] / in_H, 1. * out_HW[1] / in_W
+    scale_factor_H, scale_factor_W = 1. * out_HW[0] / in_H, 1. * out_HW[
+        1] / in_W
 
-    return nn.Upsample(scale_factor=(scale_factor_H, scale_factor_W),
-                       mode='bilinear',
-                       align_corners=False)(in_tens)
+    return nn.Upsample(
+        scale_factor=(scale_factor_H, scale_factor_W),
+        mode='bilinear',
+        align_corners=False)(in_tens)
 
 
 def normalize_tensor(in_feat, eps=1e-10):
@@ -143,8 +146,8 @@ class LPIPS(nn.Layer):
         if (verbose):
             print(
                 'Setting up [%s] perceptual loss: trunk [%s], v[%s], spatial [%s]'
-                % ('LPIPS' if lpips else 'baseline', net, version,
-                   'on' if spatial else 'off'))
+                % ('LPIPS' if lpips else 'baseline', net, version, 'on'
+                   if spatial else 'off'))
 
         self.pnet_type = net
         self.pnet_tune = pnet_tune
@@ -207,31 +210,35 @@ class LPIPS(nn.Layer):
         feats0, feats1, diffs = {}, {}, {}
 
         for kk in range(self.L):
-            feats0[kk], feats1[kk] = normalize_tensor(
-                outs0[kk]), normalize_tensor(outs1[kk])
+            feats0[kk], feats1[kk] = normalize_tensor(outs0[
+                kk]), normalize_tensor(outs1[kk])
             diffs[kk] = (feats0[kk] - feats1[kk])**2
 
         if (self.lpips):
             if (self.spatial):
                 res = [
-                    upsample(self.lins[kk].model(diffs[kk]),
-                             out_HW=in0.shape[2:]) for kk in range(self.L)
+                    upsample(
+                        self.lins[kk].model(diffs[kk]), out_HW=in0.shape[2:])
+                    for kk in range(self.L)
                 ]
             else:
                 res = [
-                    spatial_average(self.lins[kk].model(diffs[kk]),
-                                    keepdim=True) for kk in range(self.L)
+                    spatial_average(
+                        self.lins[kk].model(diffs[kk]), keepdim=True)
+                    for kk in range(self.L)
                 ]
         else:
             if (self.spatial):
                 res = [
-                    upsample(diffs[kk].sum(dim=1, keepdim=True),
-                             out_HW=in0.shape[2:]) for kk in range(self.L)
+                    upsample(
+                        diffs[kk].sum(dim=1, keepdim=True),
+                        out_HW=in0.shape[2:]) for kk in range(self.L)
                 ]
             else:
                 res = [
-                    spatial_average(diffs[kk].sum(dim=1, keepdim=True),
-                                    keepdim=True) for kk in range(self.L)
+                    spatial_average(
+                        diffs[kk].sum(dim=1, keepdim=True), keepdim=True)
+                    for kk in range(self.L)
                 ]
 
         val = res[0]
@@ -251,8 +258,7 @@ class ScalingLayer(nn.Layer):
             'shift',
             paddle.to_tensor([-.030, -.088, -.188]).reshape([1, 3, 1, 1]))
         self.register_buffer(
-            'scale',
-            paddle.to_tensor([.458, .448, .450]).reshape([1, 3, 1, 1]))
+            'scale', paddle.to_tensor([.458, .448, .450]).reshape([1, 3, 1, 1]))
 
     def forward(self, inp):
         return (inp - self.shift) / self.scale
@@ -260,14 +266,14 @@ class ScalingLayer(nn.Layer):
 
 class NetLinLayer(nn.Layer):
     ''' A single linear layer which does a 1x1 conv '''
+
     def __init__(self, chn_in, chn_out=1, use_dropout=False):
         super(NetLinLayer, self).__init__()
 
-        layers = [
-            nn.Dropout(),
-        ] if (use_dropout) else []
+        layers = [nn.Dropout(), ] if (use_dropout) else []
         layers += [
-            nn.Conv2D(chn_in, chn_out, 1, stride=1, padding=0, bias_attr=False),
+            nn.Conv2D(
+                chn_in, chn_out, 1, stride=1, padding=0, bias_attr=False),
         ]
         self.model = nn.Sequential(*layers)
 

+ 2 - 3
paddlers/models/ppgan/metrics/psnr_ssim.py

@@ -170,9 +170,8 @@ def _ssim(img1, img2):
     sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
     sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
 
-    ssim_map = ((2 * mu1_mu2 + C1) *
-                (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
-                                       (sigma1_sq + sigma2_sq + C2))
+    ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / (
+        (mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
     return ssim_map.mean()
 
 

+ 6 - 4
paddlers/models/ppgan/models/animeganv2_model.py

@@ -29,6 +29,7 @@ from ..utils.filesystem import load
 class AnimeGANV2Model(BaseModel):
     """ This class implements the AnimeGANV2 model.
     """
+
     def __init__(self,
                  generator,
                  discriminator=None,
@@ -126,10 +127,11 @@ class AnimeGANV2Model(BaseModel):
 
     @staticmethod
     def rgb2yuv(rgb):
-        kernel = paddle.to_tensor([[0.299, -0.14714119, 0.61497538],
-                                   [0.587, -0.28886916, -0.51496512],
-                                   [0.114, 0.43601035, -0.10001026]],
-                                  dtype='float32')
+        kernel = paddle.to_tensor(
+            [[0.299, -0.14714119, 0.61497538],
+             [0.587, -0.28886916, -0.51496512],
+             [0.114, 0.43601035, -0.10001026]],
+            dtype='float32')
         rgb = paddle.transpose(rgb, (0, 2, 3, 1))
         yuv = paddle.matmul(rgb, kernel)
         return yuv

+ 10 - 11
paddlers/models/ppgan/models/base_model.py

@@ -49,6 +49,7 @@ class BaseModel(ABC):
     #         save checkpoint (model.nets)                     \/
 
     """
+
     def __init__(self, params=None):
         """Initialize the BaseModel class.
 
@@ -126,8 +127,8 @@ class BaseModel(ABC):
                 parameters = []
                 for net_name in net_names:
                     parameters += self.nets[net_name].parameters()
-                self.optimizers[opt_name] = build_optimizer(
-                    cfg_, lr, parameters)
+                self.optimizers[opt_name] = build_optimizer(cfg_, lr,
+                                                            parameters)
 
         return self.optimizers
 
@@ -187,17 +188,15 @@ class BaseModel(ABC):
         inputs_num = 0
         for net in export_model:
             input_spec = [
-                paddle.static.InputSpec(shape=inputs_size[inputs_num + i],
-                                        dtype="float32")
+                paddle.static.InputSpec(
+                    shape=inputs_size[inputs_num + i], dtype="float32")
                 for i in range(net["inputs_num"])
             ]
             inputs_num = inputs_num + net["inputs_num"]
-            static_model = paddle.jit.to_static(self.nets[net["name"]],
-                                                input_spec=input_spec)
+            static_model = paddle.jit.to_static(
+                self.nets[net["name"]], input_spec=input_spec)
             if output_dir is None:
                 output_dir = 'inference_model'
-            paddle.jit.save(
-                static_model,
-                os.path.join(
-                    output_dir, '{}_{}'.format(self.__class__.__name__.lower(),
-                                               net["name"])))
+            paddle.jit.save(static_model,
+                            os.path.join(output_dir, '{}_{}'.format(
+                                self.__class__.__name__.lower(), net["name"])))

+ 1 - 0
paddlers/models/ppgan/models/basicvsr_model.py

@@ -29,6 +29,7 @@ class BasicVSRModel(BaseSRModel):
 
     Paper: BasicVSR: The Search for Essential Components in Video Super-Resolution and Beyond, CVPR, 2021
     """
+
     def __init__(self, generator, fix_iter, lr_mult, pixel_criterion=None):
         """Initialize the BasicVSR class.
 

+ 1 - 0
paddlers/models/ppgan/models/criterions/gan_loss.py

@@ -27,6 +27,7 @@ class GANLoss(nn.Layer):
     The GANLoss class abstracts away the need to create the target label tensor
     that has the same size as the input.
     """
+
     def __init__(self,
                  gan_mode,
                  target_real_label=1.0,

+ 15 - 11
paddlers/models/ppgan/models/criterions/photopen_perceptual_loss.py

@@ -9,6 +9,7 @@ from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
 from ppgan.utils.download import get_path_from_url
 from .builder import CRITERIONS
 
+
 class ConvBlock(nn.Layer):
     def __init__(self, input_channels, output_channels, groups, name=None):
         super(ConvBlock, self).__init__()
@@ -63,6 +64,7 @@ class ConvBlock(nn.Layer):
         x = self._pool(x)
         return x
 
+
 class VGG19(nn.Layer):
     def __init__(self, layers=19, class_dim=1000):
         super(VGG19, self).__init__()
@@ -88,13 +90,13 @@ class VGG19(nn.Layer):
         self._drop = Dropout(p=0.5, mode="downscale_in_infer")
         self._fc1 = Linear(
             7 * 7 * 512,
-            4096,)
+            4096, )
         self._fc2 = Linear(
             4096,
-            4096,)
+            4096, )
         self._out = Linear(
             4096,
-            class_dim,)
+            class_dim, )
 
     def forward(self, inputs):
         features = []
@@ -119,14 +121,16 @@ class VGG19(nn.Layer):
         x = self._out(x)
         return x, features
 
+
 @CRITERIONS.register()
 class PhotoPenPerceptualLoss(nn.Layer):
-    def __init__(self, 
-                 crop_size, 
-                 lambda_vgg, 
-#                  pretrained='test/vgg19pretrain.pdparams',
-                 pretrained='https://paddlegan.bj.bcebos.com/models/vgg19pretrain.pdparams',
-                ):
+    def __init__(
+            self,
+            crop_size,
+            lambda_vgg,
+            #                  pretrained='test/vgg19pretrain.pdparams',
+            pretrained='https://paddlegan.bj.bcebos.com/models/vgg19pretrain.pdparams',
+    ):
         super(PhotoPenPerceptualLoss, self).__init__()
         self.model = VGG19()
         weight_path = get_path_from_url(pretrained)
@@ -136,7 +140,7 @@ class PhotoPenPerceptualLoss(nn.Layer):
         self.rates = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0]
         self.crop_size = crop_size
         self.lambda_vgg = lambda_vgg
-        
+
     def forward(self, img_r, img_f):
         img_r = F.interpolate(img_r, (self.crop_size, self.crop_size))
         img_f = F.interpolate(img_f, (self.crop_size, self.crop_size))
@@ -146,5 +150,5 @@ class PhotoPenPerceptualLoss(nn.Layer):
         for i in range(len(feat_r)):
             g_vggloss += self.rates[i] * nn.L1Loss()(feat_r[i], feat_f[i])
         g_vggloss *= self.lambda_vgg
-        
+
         return g_vggloss

+ 20 - 12
paddlers/models/ppgan/models/criterions/pixel_loss.py

@@ -31,6 +31,7 @@ class L1Loss():
         loss_weight (float): Loss weight for L1 loss. Default: 1.0.
 
     """
+
     def __init__(self, reduction='mean', loss_weight=1.0):
         # when loss weight less than zero return None
         if loss_weight <= 0:
@@ -59,6 +60,7 @@ class CharbonnierLoss():
         eps (float): Default: 1e-12.
 
     """
+
     def __init__(self, eps=1e-12, reduction='sum'):
         self.eps = eps
         self.reduction = reduction
@@ -90,6 +92,7 @@ class MSELoss():
         loss_weight (float): Loss weight for MSE loss. Default: 1.0.
 
     """
+
     def __init__(self, reduction='mean', loss_weight=1.0):
         # when loss weight less than zero return None
         if loss_weight <= 0:
@@ -119,6 +122,7 @@ class BCEWithLogitsLoss():
             Supported choices are 'none' | 'mean' | 'sum'. Default: 'mean'.
         loss_weight (float): Loss weight for MSE loss. Default: 1.0.
     """
+
     def __init__(self, reduction='mean', loss_weight=1.0):
         # when loss weight less than zero return None
         if loss_weight <= 0:
@@ -161,6 +165,7 @@ def calc_emd_loss(pred, target):
 class CalcStyleEmdLoss():
     """Calc Style Emd Loss.
     """
+
     def __init__(self):
         super(CalcStyleEmdLoss, self).__init__()
 
@@ -183,6 +188,7 @@ class CalcStyleEmdLoss():
 class CalcContentReltLoss():
     """Calc Content Relt Loss.
     """
+
     def __init__(self):
         super(CalcContentReltLoss, self).__init__()
 
@@ -207,6 +213,7 @@ class CalcContentReltLoss():
 class CalcContentLoss():
     """Calc Content Loss.
     """
+
     def __init__(self):
         self.mse_loss = nn.MSELoss()
 
@@ -221,14 +228,15 @@ class CalcContentLoss():
         if (norm == False):
             return self.mse_loss(pred, target)
         else:
-            return self.mse_loss(mean_variance_norm(pred),
-                                 mean_variance_norm(target))
+            return self.mse_loss(
+                mean_variance_norm(pred), mean_variance_norm(target))
 
 
 @CRITERIONS.register()
 class CalcStyleLoss():
     """Calc Style Loss.
     """
+
     def __init__(self):
         self.mse_loss = nn.MSELoss()
 
@@ -241,31 +249,31 @@ class CalcStyleLoss():
         """
         pred_mean, pred_std = calc_mean_std(pred)
         target_mean, target_std = calc_mean_std(target)
-        return self.mse_loss(pred_mean, target_mean) + self.mse_loss(
-            pred_std, target_std)
+        return self.mse_loss(pred_mean, target_mean) + self.mse_loss(pred_std,
+                                                                     target_std)
 
 
 @CRITERIONS.register()
 class EdgeLoss():
     def __init__(self):
         k = paddle.to_tensor([[.05, .25, .4, .25, .05]])
-        self.kernel = paddle.matmul(k.t(),k).unsqueeze(0).tile([3,1,1,1])
+        self.kernel = paddle.matmul(k.t(), k).unsqueeze(0).tile([3, 1, 1, 1])
         self.loss = CharbonnierLoss()
 
     def conv_gauss(self, img):
         n_channels, _, kw, kh = self.kernel.shape
-        img = F.pad(img, [kw//2, kh//2, kw//2, kh//2], mode='replicate')
+        img = F.pad(img, [kw // 2, kh // 2, kw // 2, kh // 2], mode='replicate')
         return F.conv2d(img, self.kernel, groups=n_channels)
 
     def laplacian_kernel(self, current):
-        filtered    = self.conv_gauss(current)    # filter
-        down        = filtered[:,:,::2,::2]               # downsample
-        new_filter  = paddle.zeros_like(filtered)
-        new_filter[:,:,::2,::2] = down*4                  # upsample
-        filtered    = self.conv_gauss(new_filter) # filter
+        filtered = self.conv_gauss(current)  # filter
+        down = filtered[:, :, ::2, ::2]  # downsample
+        new_filter = paddle.zeros_like(filtered)
+        new_filter[:, :, ::2, ::2] = down * 4  # upsample
+        filtered = self.conv_gauss(new_filter)  # filter
         diff = current - filtered
         return diff
 
     def __call__(self, x, y):
         loss = self.loss(self.laplacian_kernel(x), self.laplacian_kernel(y))
-        return loss
+        return loss

+ 1 - 0
paddlers/models/ppgan/models/cycle_gan_model.py

@@ -31,6 +31,7 @@ class CycleGANModel(BaseModel):
 
     CycleGAN paper: https://arxiv.org/pdf/1703.10593.pdf
     """
+
     def __init__(self,
                  generator,
                  discriminator=None,

+ 1 - 0
paddlers/models/ppgan/models/dc_gan_model.py

@@ -28,6 +28,7 @@ class DCGANModel(BaseModel):
     This class implements the DCGAN model, for learning a distribution from input images.
     DCGAN paper: https://arxiv.org/pdf/1511.06434
     """
+
     def __init__(self, generator, discriminator=None, gan_criterion=None):
         """Initialize the DCGAN class.
         Args:

+ 25 - 26
paddlers/models/ppgan/models/discriminators/discriminator_animegan.py

@@ -11,34 +11,34 @@ from ...modules.utils import spectral_norm
 
 @DISCRIMINATORS.register()
 class AnimeDiscriminator(nn.Layer):
-    def __init__(self, channel: int = 64, nblocks: int = 3) -> None:
+    def __init__(self, channel: int=64, nblocks: int=3) -> None:
         super().__init__()
         channel = channel // 2
         last_channel = channel
         f = [
             spectral_norm(
-                nn.Conv2D(3, channel, 3, stride=1, padding=1, bias_attr=False)),
+                nn.Conv2D(
+                    3, channel, 3, stride=1, padding=1, bias_attr=False)),
             nn.LeakyReLU(0.2)
         ]
         in_h = 256
         for i in range(1, nblocks):
             f.extend([
                 spectral_norm(
-                    nn.Conv2D(last_channel,
-                              channel * 2,
-                              3,
-                              stride=2,
-                              padding=1,
-                              bias_attr=False)),
-                nn.LeakyReLU(0.2),
-                spectral_norm(
-                    nn.Conv2D(channel * 2,
-                              channel * 4,
-                              3,
-                              stride=1,
-                              padding=1,
-                              bias_attr=False)),
-                nn.GroupNorm(1, channel * 4),
+                    nn.Conv2D(
+                        last_channel,
+                        channel * 2,
+                        3,
+                        stride=2,
+                        padding=1,
+                        bias_attr=False)), nn.LeakyReLU(0.2), spectral_norm(
+                            nn.Conv2D(
+                                channel * 2,
+                                channel * 4,
+                                3,
+                                stride=1,
+                                padding=1,
+                                bias_attr=False)), nn.GroupNorm(1, channel * 4),
                 nn.LeakyReLU(0.2)
             ])
             last_channel = channel * 4
@@ -49,15 +49,14 @@ class AnimeDiscriminator(nn.Layer):
 
         self.head = nn.Sequential(*[
             spectral_norm(
-                nn.Conv2D(last_channel,
-                          channel * 2,
-                          3,
-                          stride=1,
-                          padding=1,
-                          bias_attr=False)),
-            nn.GroupNorm(1, channel * 2),
-            nn.LeakyReLU(0.2),
-            spectral_norm(
+                nn.Conv2D(
+                    last_channel,
+                    channel * 2,
+                    3,
+                    stride=1,
+                    padding=1,
+                    bias_attr=False)), nn.GroupNorm(1, channel * 2),
+            nn.LeakyReLU(0.2), spectral_norm(
                 nn.Conv2D(
                     channel * 2, 1, 3, stride=1, padding=1, bias_attr=False))
         ])

+ 22 - 18
paddlers/models/ppgan/models/discriminators/discriminator_firstorder.py

@@ -31,6 +31,7 @@ class FirstOrderDiscriminator(nn.Layer):
         loss_weights:
             discriminator_gan (int): weight of discriminator loss
     """
+
     def __init__(self, discriminator_cfg, common_params, train_params):
         super(FirstOrderDiscriminator, self).__init__()
         self.discriminator = MultiScaleDiscriminator(**discriminator_cfg,
@@ -47,8 +48,8 @@ class FirstOrderDiscriminator(nn.Layer):
         kp_driving = generated['kp_driving']
         discriminator_maps_generated = self.discriminator(
             pyramide_generated, kp=detach_kp(kp_driving))
-        discriminator_maps_real = self.discriminator(pyramide_real,
-                                                     kp=detach_kp(kp_driving))
+        discriminator_maps_real = self.discriminator(
+            pyramide_real, kp=detach_kp(kp_driving))
 
         loss_values = {}
         value_total = 0
@@ -66,6 +67,7 @@ class DownBlock2d(nn.Layer):
     """
     Simple block for processing video (encoder).
     """
+
     def __init__(self,
                  in_features,
                  out_features,
@@ -74,16 +76,15 @@ class DownBlock2d(nn.Layer):
                  pool=False,
                  sn=False):
         super(DownBlock2d, self).__init__()
-        self.conv = nn.Conv2D(in_features,
-                              out_features,
-                              kernel_size=kernel_size)
+        self.conv = nn.Conv2D(
+            in_features, out_features, kernel_size=kernel_size)
         if sn:
             self.conv = spectral_norm(self.conv)
         else:
             self.sn = None
         if norm:
-            self.norm = nn.InstanceNorm2D(num_features=out_features,
-                                          epsilon=1e-05)
+            self.norm = nn.InstanceNorm2D(
+                num_features=out_features, epsilon=1e-05)
         else:
             self.norm = None
 
@@ -117,19 +118,21 @@ class Discriminator(nn.Layer):
         down_blocks = []
         for i in range(num_blocks):
             down_blocks.append(
-                DownBlock2d(num_channels + num_kp * use_kp if i == 0 else min(
-                    max_features, block_expansion * (2**i)),
-                            min(max_features, block_expansion * (2**(i + 1))),
-                            norm=(i != 0),
-                            kernel_size=4,
-                            pool=(i != num_blocks - 1),
-                            sn=sn))
+                DownBlock2d(
+                    num_channels + num_kp * use_kp
+                    if i == 0 else min(max_features, block_expansion * (2**i)),
+                    min(max_features, block_expansion * (2**(i + 1))),
+                    norm=(i != 0),
+                    kernel_size=4,
+                    pool=(i != num_blocks - 1),
+                    sn=sn))
 
         self.down_blocks = nn.LayerList(down_blocks)
-        self.conv = nn.Conv2D(self.down_blocks[len(self.down_blocks) -
-                                               1].conv.parameters()[0].shape[0],
-                              1,
-                              kernel_size=1)
+        self.conv = nn.Conv2D(
+            self.down_blocks[len(self.down_blocks) - 1].conv.parameters()[0]
+            .shape[0],
+            1,
+            kernel_size=1)
         if sn:
             self.conv = spectral_norm(self.conv)
         else:
@@ -156,6 +159,7 @@ class MultiScaleDiscriminator(nn.Layer):
     """
     Multi-scale (scale) discriminator
     """
+
     def __init__(self, scales=(), **kwargs):
         super(MultiScaleDiscriminator, self).__init__()
         self.scales = scales

+ 11 - 13
paddlers/models/ppgan/models/discriminators/discriminator_lapstyle.py

@@ -25,27 +25,25 @@ class LapStyleDiscriminator(nn.Layer):
         num_layer = 3
         num_channel = 32
         self.head = nn.Sequential(
-            ('conv',
-             nn.Conv2D(3, num_channel, kernel_size=3, stride=1, padding=1)),
-            ('norm', nn.BatchNorm2D(num_channel)),
+            ('conv', nn.Conv2D(
+                3, num_channel, kernel_size=3, stride=1,
+                padding=1)), ('norm', nn.BatchNorm2D(num_channel)),
             ('LeakyRelu', nn.LeakyReLU(0.2)))
         self.body = nn.Sequential()
         for i in range(num_layer - 2):
             self.body.add_sublayer(
                 'conv%d' % (i + 1),
-                nn.Conv2D(num_channel,
-                          num_channel,
-                          kernel_size=3,
-                          stride=1,
-                          padding=1))
+                nn.Conv2D(
+                    num_channel,
+                    num_channel,
+                    kernel_size=3,
+                    stride=1,
+                    padding=1))
             self.body.add_sublayer('norm%d' % (i + 1),
                                    nn.BatchNorm2D(num_channel))
             self.body.add_sublayer('LeakyRelu%d' % (i + 1), nn.LeakyReLU(0.2))
-        self.tail = nn.Conv2D(num_channel,
-                              1,
-                              kernel_size=3,
-                              stride=1,
-                              padding=1)
+        self.tail = nn.Conv2D(
+            num_channel, 1, kernel_size=3, stride=1, padding=1)
 
     def forward(self, x):
         x = self.head(x)

+ 35 - 33
paddlers/models/ppgan/models/discriminators/discriminator_photopen.py

@@ -25,44 +25,47 @@ from ppgan.utils.photopen import build_norm_layer, simam, Dict
 from .builder import DISCRIMINATORS
 
 
-
 class NLayersDiscriminator(nn.Layer):
     def __init__(self, opt):
         super(NLayersDiscriminator, self).__init__()
-        
+
         kw = 4
         padw = int(np.ceil((kw - 1.0) / 2))
         nf = opt.ndf
         input_nc = self.compute_D_input_nc(opt)
         layer_count = 0
 
-        layer = nn.Sequential(
-            nn.Conv2D(input_nc, nf, kw, 2, padw),
-            nn.GELU()
-        )
-        self.add_sublayer('block_'+str(layer_count), layer)
+        layer = nn.Sequential(nn.Conv2D(input_nc, nf, kw, 2, padw), nn.GELU())
+        self.add_sublayer('block_' + str(layer_count), layer)
         layer_count += 1
 
-        feat_size_prev = np.floor((opt.crop_size + padw * 2 - (kw - 2)) / 2).astype('int64')
+        feat_size_prev = np.floor(
+            (opt.crop_size + padw * 2 - (kw - 2)) / 2).astype('int64')
         InstanceNorm = build_norm_layer('instance')
         for n in range(1, opt.n_layers_D):
             nf_prev = nf
             nf = min(nf * 2, 512)
             stride = 1 if n == opt.n_layers_D - 1 else 2
-            feat_size = np.floor((feat_size_prev + padw * 2 - (kw - stride)) / stride).astype('int64')
+            feat_size = np.floor((feat_size_prev + padw * 2 - (kw - stride)) /
+                                 stride).astype('int64')
             feat_size_prev = feat_size
             layer = nn.Sequential(
-                spectral_norm(nn.Conv2D(nf_prev, nf, kw, stride, padw, 
-                    weight_attr=None,
-                    bias_attr=None)),
+                spectral_norm(
+                    nn.Conv2D(
+                        nf_prev,
+                        nf,
+                        kw,
+                        stride,
+                        padw,
+                        weight_attr=None,
+                        bias_attr=None)),
                 InstanceNorm(nf),
-                nn.GELU()
-            )
-            self.add_sublayer('block_'+str(layer_count), layer)
+                nn.GELU())
+            self.add_sublayer('block_' + str(layer_count), layer)
             layer_count += 1
 
         layer = nn.Conv2D(nf, 1, kw, 1, padw)
-        self.add_sublayer('block_'+str(layer_count), layer)
+        self.add_sublayer('block_' + str(layer_count), layer)
         layer_count += 1
 
     def forward(self, input):
@@ -80,22 +83,22 @@ class NLayersDiscriminator(nn.Layer):
         if not opt.no_instance:
             input_nc += 1
         return input_nc
-    
+
+
 @DISCRIMINATORS.register()
 class MultiscaleDiscriminator(nn.Layer):
-    def __init__(self,
-                 ndf,
-                 num_D,
-                 crop_size,
-                 label_nc,
-                 output_nc,
-                 contain_dontcare_label,
-                 no_instance,
-                 n_layers_D,
-                 
-                ):
+    def __init__(
+            self,
+            ndf,
+            num_D,
+            crop_size,
+            label_nc,
+            output_nc,
+            contain_dontcare_label,
+            no_instance,
+            n_layers_D, ):
         super(MultiscaleDiscriminator, self).__init__()
-        
+
         opt = {
             'ndf': ndf,
             'num_D': num_D,
@@ -105,7 +108,6 @@ class MultiscaleDiscriminator(nn.Layer):
             'contain_dontcare_label': contain_dontcare_label,
             'no_instance': no_instance,
             'n_layers_D': n_layers_D,
-
         }
         opt = Dict(opt)
 
@@ -115,16 +117,16 @@ class MultiscaleDiscriminator(nn.Layer):
             feat_size = opt.crop_size
             for j in range(i):
                 sequence += [nn.AvgPool2D(3, 2, 1)]
-                feat_size = np.floor((feat_size + 1 * 2 - (3 - 2)) / 2).astype('int64')
+                feat_size = np.floor(
+                    (feat_size + 1 * 2 - (3 - 2)) / 2).astype('int64')
             opt.crop_size = feat_size
             sequence += [NLayersDiscriminator(opt)]
             opt.crop_size = crop_size_bkp
             sequence = nn.Sequential(*sequence)
-            self.add_sublayer('nld_'+str(i), sequence)
+            self.add_sublayer('nld_' + str(i), sequence)
 
     def forward(self, input):
         output = []
         for layer in self._sub_layers.values():
             output.append(layer(input))
         return output
-

+ 36 - 38
paddlers/models/ppgan/models/discriminators/discriminator_ugatit.py

@@ -13,63 +13,61 @@ class UGATITDiscriminator(nn.Layer):
     def __init__(self, input_nc, ndf=64, n_layers=5):
         super(UGATITDiscriminator, self).__init__()
         model = [
-            nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"),
-            spectral_norm(
-                nn.Conv2D(input_nc,
-                          ndf,
-                          kernel_size=4,
-                          stride=2,
-                          padding=0,
-                          bias_attr=True)),
-            nn.LeakyReLU(0.2)
+            nn.Pad2D(
+                padding=[1, 1, 1, 1], mode="reflect"), spectral_norm(
+                    nn.Conv2D(
+                        input_nc,
+                        ndf,
+                        kernel_size=4,
+                        stride=2,
+                        padding=0,
+                        bias_attr=True)), nn.LeakyReLU(0.2)
         ]
 
         for i in range(1, n_layers - 2):
             mult = 2**(i - 1)
             model += [
-                nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"),
-                spectral_norm(
-                    nn.Conv2D(ndf * mult,
-                              ndf * mult * 2,
-                              kernel_size=4,
-                              stride=2,
-                              padding=0,
-                              bias_attr=True)),
-                nn.LeakyReLU(0.2)
+                nn.Pad2D(
+                    padding=[1, 1, 1, 1], mode="reflect"), spectral_norm(
+                        nn.Conv2D(
+                            ndf * mult,
+                            ndf * mult * 2,
+                            kernel_size=4,
+                            stride=2,
+                            padding=0,
+                            bias_attr=True)), nn.LeakyReLU(0.2)
             ]
 
         mult = 2**(n_layers - 2 - 1)
         model += [
-            nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"),
-            spectral_norm(
-                nn.Conv2D(ndf * mult,
-                          ndf * mult * 2,
-                          kernel_size=4,
-                          stride=1,
-                          padding=0,
-                          bias_attr=True)),
-            nn.LeakyReLU(0.2)
+            nn.Pad2D(
+                padding=[1, 1, 1, 1], mode="reflect"), spectral_norm(
+                    nn.Conv2D(
+                        ndf * mult,
+                        ndf * mult * 2,
+                        kernel_size=4,
+                        stride=1,
+                        padding=0,
+                        bias_attr=True)), nn.LeakyReLU(0.2)
         ]
 
         # Class Activation Map
         mult = 2**(n_layers - 2)
         self.gap_fc = spectral_norm(nn.Linear(ndf * mult, 1, bias_attr=False))
         self.gmp_fc = spectral_norm(nn.Linear(ndf * mult, 1, bias_attr=False))
-        self.conv1x1 = nn.Conv2D(ndf * mult * 2,
-                                 ndf * mult,
-                                 kernel_size=1,
-                                 stride=1,
-                                 bias_attr=True)
+        self.conv1x1 = nn.Conv2D(
+            ndf * mult * 2, ndf * mult, kernel_size=1, stride=1, bias_attr=True)
         self.leaky_relu = nn.LeakyReLU(0.2)
 
         self.pad = nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect")
         self.conv = spectral_norm(
-            nn.Conv2D(ndf * mult,
-                      1,
-                      kernel_size=4,
-                      stride=1,
-                      padding=0,
-                      bias_attr=False))
+            nn.Conv2D(
+                ndf * mult,
+                1,
+                kernel_size=4,
+                stride=1,
+                padding=0,
+                bias_attr=False))
 
         self.model = nn.Sequential(*model)
 

+ 62 - 118
paddlers/models/ppgan/models/discriminators/syncnet.py

@@ -17,126 +17,70 @@ class SyncNetColor(nn.Layer):
         super(SyncNetColor, self).__init__()
 
         self.face_encoder = nn.Sequential(
-            ConvBNRelu(15, 32, kernel_size=(7, 7), stride=1, padding=3),
-            ConvBNRelu(32, 64, kernel_size=5, stride=(1, 2), padding=1),
-            ConvBNRelu(64,
-                       64,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(64,
-                       64,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(64, 128, kernel_size=3, stride=2, padding=1),
-            ConvBNRelu(128,
-                       128,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(128,
-                       128,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(128,
-                       128,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(128, 256, kernel_size=3, stride=2, padding=1),
-            ConvBNRelu(256,
-                       256,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(256,
-                       256,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(256, 512, kernel_size=3, stride=2, padding=1),
-            ConvBNRelu(512,
-                       512,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(512,
-                       512,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(512, 512, kernel_size=3, stride=2, padding=1),
-            ConvBNRelu(512, 512, kernel_size=3, stride=1, padding=0),
-            ConvBNRelu(512, 512, kernel_size=1, stride=1, padding=0),
-        )
+            ConvBNRelu(
+                15, 32, kernel_size=(7, 7), stride=1, padding=3),
+            ConvBNRelu(
+                32, 64, kernel_size=5, stride=(1, 2), padding=1),
+            ConvBNRelu(
+                64, 64, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                64, 64, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                64, 128, kernel_size=3, stride=2, padding=1),
+            ConvBNRelu(
+                128, 128, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                128, 128, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                128, 128, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                128, 256, kernel_size=3, stride=2, padding=1),
+            ConvBNRelu(
+                256, 256, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                256, 256, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                256, 512, kernel_size=3, stride=2, padding=1),
+            ConvBNRelu(
+                512, 512, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                512, 512, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                512, 512, kernel_size=3, stride=2, padding=1),
+            ConvBNRelu(
+                512, 512, kernel_size=3, stride=1, padding=0),
+            ConvBNRelu(
+                512, 512, kernel_size=1, stride=1, padding=0), )
 
         self.audio_encoder = nn.Sequential(
-            ConvBNRelu(1, 32, kernel_size=3, stride=1, padding=1),
-            ConvBNRelu(32,
-                       32,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(32,
-                       32,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(32, 64, kernel_size=3, stride=(3, 1), padding=1),
-            ConvBNRelu(64,
-                       64,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(64,
-                       64,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(64, 128, kernel_size=3, stride=3, padding=1),
-            ConvBNRelu(128,
-                       128,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(128,
-                       128,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(128, 256, kernel_size=3, stride=(3, 2), padding=1),
-            ConvBNRelu(256,
-                       256,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(256,
-                       256,
-                       kernel_size=3,
-                       stride=1,
-                       padding=1,
-                       residual=True),
-            ConvBNRelu(256, 512, kernel_size=3, stride=1, padding=0),
-            ConvBNRelu(512, 512, kernel_size=1, stride=1, padding=0),
-        )
+            ConvBNRelu(
+                1, 32, kernel_size=3, stride=1, padding=1),
+            ConvBNRelu(
+                32, 32, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                32, 32, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                32, 64, kernel_size=3, stride=(3, 1), padding=1),
+            ConvBNRelu(
+                64, 64, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                64, 64, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                64, 128, kernel_size=3, stride=3, padding=1),
+            ConvBNRelu(
+                128, 128, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                128, 128, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                128, 256, kernel_size=3, stride=(3, 2), padding=1),
+            ConvBNRelu(
+                256, 256, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                256, 256, kernel_size=3, stride=1, padding=1, residual=True),
+            ConvBNRelu(
+                256, 512, kernel_size=3, stride=1, padding=0),
+            ConvBNRelu(
+                512, 512, kernel_size=1, stride=1, padding=0), )
 
     def forward(self, audio_sequences,
                 face_sequences):  # audio_sequences := (B, dim, T)

+ 27 - 58
paddlers/models/ppgan/models/discriminators/vgg_discriminator.py

@@ -16,6 +16,7 @@ class VGGDiscriminator128(nn.Layer):
         num_feat (int): Channel number of base intermediate features.
             Default: 64.
     """
+
     def __init__(self, in_channels, num_feat, norm_layer='batch'):
         super(VGGDiscriminator128, self).__init__()
 
@@ -23,64 +24,32 @@ class VGGDiscriminator128(nn.Layer):
         self.conv0_1 = nn.Conv2D(num_feat, num_feat, 4, 2, 1, bias_attr=False)
         self.bn0_1 = nn.BatchNorm2D(num_feat)
 
-        self.conv1_0 = nn.Conv2D(num_feat,
-                                 num_feat * 2,
-                                 3,
-                                 1,
-                                 1,
-                                 bias_attr=False)
+        self.conv1_0 = nn.Conv2D(
+            num_feat, num_feat * 2, 3, 1, 1, bias_attr=False)
         self.bn1_0 = nn.BatchNorm2D(num_feat * 2)
-        self.conv1_1 = nn.Conv2D(num_feat * 2,
-                                 num_feat * 2,
-                                 4,
-                                 2,
-                                 1,
-                                 bias_attr=False)
+        self.conv1_1 = nn.Conv2D(
+            num_feat * 2, num_feat * 2, 4, 2, 1, bias_attr=False)
         self.bn1_1 = nn.BatchNorm2D(num_feat * 2)
 
-        self.conv2_0 = nn.Conv2D(num_feat * 2,
-                                 num_feat * 4,
-                                 3,
-                                 1,
-                                 1,
-                                 bias_attr=False)
+        self.conv2_0 = nn.Conv2D(
+            num_feat * 2, num_feat * 4, 3, 1, 1, bias_attr=False)
         self.bn2_0 = nn.BatchNorm2D(num_feat * 4)
-        self.conv2_1 = nn.Conv2D(num_feat * 4,
-                                 num_feat * 4,
-                                 4,
-                                 2,
-                                 1,
-                                 bias_attr=False)
+        self.conv2_1 = nn.Conv2D(
+            num_feat * 4, num_feat * 4, 4, 2, 1, bias_attr=False)
         self.bn2_1 = nn.BatchNorm2D(num_feat * 4)
 
-        self.conv3_0 = nn.Conv2D(num_feat * 4,
-                                 num_feat * 8,
-                                 3,
-                                 1,
-                                 1,
-                                 bias_attr=False)
+        self.conv3_0 = nn.Conv2D(
+            num_feat * 4, num_feat * 8, 3, 1, 1, bias_attr=False)
         self.bn3_0 = nn.BatchNorm2D(num_feat * 8)
-        self.conv3_1 = nn.Conv2D(num_feat * 8,
-                                 num_feat * 8,
-                                 4,
-                                 2,
-                                 1,
-                                 bias_attr=False)
+        self.conv3_1 = nn.Conv2D(
+            num_feat * 8, num_feat * 8, 4, 2, 1, bias_attr=False)
         self.bn3_1 = nn.BatchNorm2D(num_feat * 8)
 
-        self.conv4_0 = nn.Conv2D(num_feat * 8,
-                                 num_feat * 8,
-                                 3,
-                                 1,
-                                 1,
-                                 bias_attr=False)
+        self.conv4_0 = nn.Conv2D(
+            num_feat * 8, num_feat * 8, 3, 1, 1, bias_attr=False)
         self.bn4_0 = nn.BatchNorm2D(num_feat * 8)
-        self.conv4_1 = nn.Conv2D(num_feat * 8,
-                                 num_feat * 8,
-                                 4,
-                                 2,
-                                 1,
-                                 bias_attr=False)
+        self.conv4_1 = nn.Conv2D(
+            num_feat * 8, num_feat * 8, 4, 2, 1, bias_attr=False)
         self.bn4_1 = nn.BatchNorm2D(num_feat * 8)
 
         self.linear1 = nn.Linear(num_feat * 8 * 4 * 4, 100)
@@ -95,24 +64,24 @@ class VGGDiscriminator128(nn.Layer):
             f'but received {x.shape}.')
 
         feat = self.lrelu(self.conv0_0(x))
-        feat = self.lrelu(self.bn0_1(
-            self.conv0_1(feat)))  # output spatial size: (64, 64)
+        feat = self.lrelu(
+            self.bn0_1(self.conv0_1(feat)))  # output spatial size: (64, 64)
 
         feat = self.lrelu(self.bn1_0(self.conv1_0(feat)))
-        feat = self.lrelu(self.bn1_1(
-            self.conv1_1(feat)))  # output spatial size: (32, 32)
+        feat = self.lrelu(
+            self.bn1_1(self.conv1_1(feat)))  # output spatial size: (32, 32)
 
         feat = self.lrelu(self.bn2_0(self.conv2_0(feat)))
-        feat = self.lrelu(self.bn2_1(
-            self.conv2_1(feat)))  # output spatial size: (16, 16)
+        feat = self.lrelu(
+            self.bn2_1(self.conv2_1(feat)))  # output spatial size: (16, 16)
 
         feat = self.lrelu(self.bn3_0(self.conv3_0(feat)))
-        feat = self.lrelu(self.bn3_1(
-            self.conv3_1(feat)))  # output spatial size: (8, 8)
+        feat = self.lrelu(
+            self.bn3_1(self.conv3_1(feat)))  # output spatial size: (8, 8)
 
         feat = self.lrelu(self.bn4_0(self.conv4_0(feat)))
-        feat = self.lrelu(self.bn4_1(
-            self.conv4_1(feat)))  # output spatial size: (4, 4)
+        feat = self.lrelu(
+            self.bn4_1(self.conv4_1(feat)))  # output spatial size: (4, 4)
 
         feat = feat.reshape([feat.shape[0], -1])
         feat = self.lrelu(self.linear1(feat))

+ 31 - 24
paddlers/models/ppgan/models/discriminators/wav2lip_disc_qual.py

@@ -17,37 +17,43 @@ class Wav2LipDiscQual(nn.Layer):
 
         self.face_encoder_blocks = nn.LayerList([
             nn.Sequential(
-                NonNormConv2d(3, 32, kernel_size=7, stride=1,
-                              padding=3)),  # 48,96
+                NonNormConv2d(
+                    3, 32, kernel_size=7, stride=1, padding=3)),  # 48,96
             nn.Sequential(
-                NonNormConv2d(32, 64, kernel_size=5, stride=(1, 2),
-                              padding=2),  # 48,48
-                NonNormConv2d(64, 64, kernel_size=5, stride=1, padding=2)),
+                NonNormConv2d(
+                    32, 64, kernel_size=5, stride=(1, 2), padding=2),  # 48,48
+                NonNormConv2d(
+                    64, 64, kernel_size=5, stride=1, padding=2)),
             nn.Sequential(
-                NonNormConv2d(64, 128, kernel_size=5, stride=2,
-                              padding=2),  # 24,24
-                NonNormConv2d(128, 128, kernel_size=5, stride=1, padding=2)),
+                NonNormConv2d(
+                    64, 128, kernel_size=5, stride=2, padding=2),  # 24,24
+                NonNormConv2d(
+                    128, 128, kernel_size=5, stride=1, padding=2)),
             nn.Sequential(
-                NonNormConv2d(128, 256, kernel_size=5, stride=2,
-                              padding=2),  # 12,12
-                NonNormConv2d(256, 256, kernel_size=5, stride=1, padding=2)),
+                NonNormConv2d(
+                    128, 256, kernel_size=5, stride=2, padding=2),  # 12,12
+                NonNormConv2d(
+                    256, 256, kernel_size=5, stride=1, padding=2)),
             nn.Sequential(
-                NonNormConv2d(256, 512, kernel_size=3, stride=2,
-                              padding=1),  # 6,6
-                NonNormConv2d(512, 512, kernel_size=3, stride=1, padding=1)),
+                NonNormConv2d(
+                    256, 512, kernel_size=3, stride=2, padding=1),  # 6,6
+                NonNormConv2d(
+                    512, 512, kernel_size=3, stride=1, padding=1)),
             nn.Sequential(
-                NonNormConv2d(512, 512, kernel_size=3, stride=2,
-                              padding=1),  # 3,3
-                NonNormConv2d(512, 512, kernel_size=3, stride=1, padding=1),
-            ),
+                NonNormConv2d(
+                    512, 512, kernel_size=3, stride=2, padding=1),  # 3,3
+                NonNormConv2d(
+                    512, 512, kernel_size=3, stride=1, padding=1), ),
             nn.Sequential(
-                NonNormConv2d(512, 512, kernel_size=3, stride=1,
-                              padding=0),  # 1, 1
-                NonNormConv2d(512, 512, kernel_size=1, stride=1, padding=0)),
+                NonNormConv2d(
+                    512, 512, kernel_size=3, stride=1, padding=0),  # 1, 1
+                NonNormConv2d(
+                    512, 512, kernel_size=1, stride=1, padding=0)),
         ])
 
         self.binary_pred = nn.Sequential(
-            nn.Conv2D(512, 1, kernel_size=1, stride=1, padding=0), nn.Sigmoid())
+            nn.Conv2D(
+                512, 1, kernel_size=1, stride=1, padding=0), nn.Sigmoid())
         self.label_noise = .0
 
     def get_lower_half(self, face_sequences):
@@ -71,8 +77,9 @@ class Wav2LipDiscQual(nn.Layer):
         binary_pred = self.binary_pred(false_feats).reshape(
             (len(false_feats), -1))
 
-        false_pred_loss = F.binary_cross_entropy(
-            binary_pred, paddle.ones((len(false_feats), 1)))
+        false_pred_loss = F.binary_cross_entropy(binary_pred,
+                                                 paddle.ones(
+                                                     (len(false_feats), 1)))
 
         return false_pred_loss
 

+ 3 - 2
paddlers/models/ppgan/models/drn_model.py

@@ -32,6 +32,7 @@ class DRN(BaseSRModel):
 
     DRN paper: https://arxiv.org/pdf/1809.00219.pdf
     """
+
     def __init__(self,
                  generator,
                  lq_loss_weight=0.1,
@@ -82,8 +83,8 @@ class DRN(BaseSRModel):
         self.lq = paddle.to_tensor(input['lq'])
         self.visual_items['lq'] = self.lq
 
-        if isinstance(self.scale, (list, tuple)) and len(
-                self.scale) == 2 and 'lqx2' in input:
+        if isinstance(self.scale, (
+                list, tuple)) and len(self.scale) == 2 and 'lqx2' in input:
             self.lqx2 = input['lqx2']
 
         if 'gt' in input:

+ 3 - 2
paddlers/models/ppgan/models/edvr_model.py

@@ -27,6 +27,7 @@ class EDVRModel(BaseSRModel):
 
     Paper: EDVR: Video Restoration with Enhanced Deformable Convolutional Networks.
     """
+
     def __init__(self, generator, tsa_iter, pixel_criterion=None):
         """Initialize the EDVR class.
 
@@ -77,8 +78,8 @@ class EDVRModel(BaseSRModel):
 
 def init_edvr_weight(net):
     def reset_func(m):
-        if hasattr(m, 'weight') and (not isinstance(
-                m, (nn.BatchNorm, nn.BatchNorm2D))) and (
+        if hasattr(m, 'weight') and (
+                not isinstance(m, (nn.BatchNorm, nn.BatchNorm2D))) and (
                     not isinstance(m, ResidualBlockNoBN) and
                     (not isinstance(m, DCNPack))):
             reset_parameters(m)

+ 7 - 10
paddlers/models/ppgan/models/esrgan_model.py

@@ -29,6 +29,7 @@ class ESRGAN(BaseSRModel):
 
     ESRGAN paper: https://arxiv.org/pdf/1809.00219.pdf
     """
+
     def __init__(self,
                  generator,
                  discriminator=None,
@@ -71,8 +72,8 @@ class ESRGAN(BaseSRModel):
             l_total += l_pix
             self.losses['loss_pix'] = l_pix
         if self.perceptual_criterion:
-            l_g_percep, l_g_style = self.perceptual_criterion(
-                self.output, self.gt)
+            l_g_percep, l_g_style = self.perceptual_criterion(self.output,
+                                                              self.gt)
             # l_total += l_pix
             if l_g_percep is not None:
                 l_total += l_g_percep
@@ -86,14 +87,10 @@ class ESRGAN(BaseSRModel):
             self.set_requires_grad(self.nets['discriminator'], False)
             real_d_pred = self.nets['discriminator'](self.gt).detach()
             fake_g_pred = self.nets['discriminator'](self.output)
-            l_g_real = self.gan_criterion(real_d_pred -
-                                          paddle.mean(fake_g_pred),
-                                          False,
-                                          is_disc=False)
-            l_g_fake = self.gan_criterion(fake_g_pred -
-                                          paddle.mean(real_d_pred),
-                                          True,
-                                          is_disc=False)
+            l_g_real = self.gan_criterion(
+                real_d_pred - paddle.mean(fake_g_pred), False, is_disc=False)
+            l_g_fake = self.gan_criterion(
+                fake_g_pred - paddle.mean(real_d_pred), True, is_disc=False)
             l_g_gan = (l_g_real + l_g_fake) / 2
 
             l_total += l_g_gan

+ 12 - 12
paddlers/models/ppgan/models/gan_model.py

@@ -32,6 +32,7 @@ class GANModel(BaseModel):
 
     vanilla GAN paper: https://arxiv.org/abs/1406.2661
     """
+
     def __init__(self,
                  generator,
                  discriminator=None,
@@ -50,12 +51,12 @@ class GANModel(BaseModel):
 
         self.disc_iters = 1 if self.params is None else self.params.get(
             'disc_iters', 1)
-        self.disc_start_iters = (0 if self.params is None else self.params.get(
-            'disc_start_iters', 0))
-        self.samples_every_row = (8 if self.params is None else self.params.get(
-            'samples_every_row', 8))
-        self.visual_interval = (500 if self.params is None else self.params.get(
-            'visual_interval', 500))
+        self.disc_start_iters = (0 if self.params is None else
+                                 self.params.get('disc_start_iters', 0))
+        self.samples_every_row = (8 if self.params is None else
+                                  self.params.get('samples_every_row', 8))
+        self.visual_interval = (500 if self.params is None else
+                                self.params.get('visual_interval', 500))
 
         # define generator
         self.nets['netG'] = build_generator(generator)
@@ -89,7 +90,8 @@ class GANModel(BaseModel):
                 self.n_class = self.nets['netG'].n_class
 
             self.D_real_inputs += [
-                paddle.to_tensor(input['class_id'], dtype='int64')
+                paddle.to_tensor(
+                    input['class_id'], dtype='int64')
             ]
         else:
             self.n_class = 0
@@ -110,9 +112,7 @@ class GANModel(BaseModel):
                 rows_num = (batch_size - 1) // self.samples_every_row + 1
                 class_ids = paddle.randint(0, self.n_class, [rows_num, 1])
                 class_ids = class_ids.tile([1, self.samples_every_row])
-                class_ids = class_ids.reshape([
-                    -1,
-                ])[:batch_size].detach()
+                class_ids = class_ids.reshape([-1, ])[:batch_size].detach()
                 self.G_fixed_inputs[1] = class_ids.detach()
 
     def forward(self):
@@ -143,8 +143,8 @@ class GANModel(BaseModel):
 
         # combine loss and calculate gradients
         if self.criterionGAN.gan_mode in ['vanilla', 'lsgan']:
-            self.loss_D = self.loss_D + (self.loss_D_fake +
-                                         self.loss_D_real) * 0.5
+            self.loss_D = self.loss_D + (self.loss_D_fake + self.loss_D_real
+                                         ) * 0.5
         else:
             self.loss_D = self.loss_D + self.loss_D_fake + self.loss_D_real
 

+ 36 - 40
paddlers/models/ppgan/models/generators/basicvsr_plus_plus.py

@@ -45,6 +45,7 @@ class BasicVSRPlusPlus(nn.Layer):
             or not. If False, the output resolution is equal to the input
             resolution. Default: True.
     """
+
     def __init__(self, mid_channels=64, num_blocks=7, is_low_res_input=True):
 
         super().__init__()
@@ -88,21 +89,16 @@ class BasicVSRPlusPlus(nn.Layer):
             5 * mid_channels, mid_channels, num_blocks)
 
         # upsampling module
-        self.reconstruction = ResidualBlocksWithInputConv(
-            5 * mid_channels, mid_channels, 5)
-        self.upsample1 = PixelShufflePack(mid_channels,
-                                          mid_channels,
-                                          2,
-                                          upsample_kernel=3)
-        self.upsample2 = PixelShufflePack(mid_channels,
-                                          64,
-                                          2,
-                                          upsample_kernel=3)
+        self.reconstruction = ResidualBlocksWithInputConv(5 * mid_channels,
+                                                          mid_channels, 5)
+        self.upsample1 = PixelShufflePack(
+            mid_channels, mid_channels, 2, upsample_kernel=3)
+        self.upsample2 = PixelShufflePack(
+            mid_channels, 64, 2, upsample_kernel=3)
         self.conv_hr = nn.Conv2D(64, 64, 3, 1, 1)
         self.conv_last = nn.Conv2D(64, 3, 3, 1, 1)
-        self.img_upsample = nn.Upsample(scale_factor=4,
-                                        mode='bilinear',
-                                        align_corners=False)
+        self.img_upsample = nn.Upsample(
+            scale_factor=4, mode='bilinear', align_corners=False)
 
         # activation function
         self.lrelu = nn.LeakyReLU(negative_slope=0.1)
@@ -198,10 +194,9 @@ class BasicVSRPlusPlus(nn.Layer):
         if self.is_low_res_input:
             lqs_downsample = lqs
         else:
-            lqs_downsample = F.interpolate(lqs.reshape([-1, c, h, w]),
-                                           scale_factor=0.25,
-                                           mode='bicubic').reshape(
-                                               [n, t, c, h // 4, w // 4])
+            lqs_downsample = F.interpolate(
+                lqs.reshape([-1, c, h, w]), scale_factor=0.25,
+                mode='bicubic').reshape([n, t, c, h // 4, w // 4])
 
         # check whether the input is an extended sequence
         self.check_if_mirror_extended(lqs)
@@ -213,9 +208,10 @@ class BasicVSRPlusPlus(nn.Layer):
         feats['spatial'] = [feats_[:, i, :, :, :] for i in range(0, t)]
 
         # compute optical flow using the low-res inputs
-        assert lqs_downsample.shape[3] >= 64 and lqs_downsample.shape[4] >= 64, (
-            'The height and width of low-res inputs must be at least 64, '
-            f'but got {h} and {w}.')
+        assert lqs_downsample.shape[3] >= 64 and lqs_downsample.shape[
+            4] >= 64, (
+                'The height and width of low-res inputs must be at least 64, '
+                f'but got {h} and {w}.')
         flows_forward, flows_backward = self.compute_flow(lqs_downsample)
 
         # feature propgation
@@ -253,20 +249,20 @@ class BasicVSRPlusPlus(nn.Layer):
                     flow_n2 = flow_n1 + flow_warp(
                         flow_n2, flow_n1.transpose([0, 2, 3, 1]))
 
-                    cond_n2 = flow_warp(feat_n2, flow_n2.transpose([0, 2, 3,
-                                                                    1]))
+                    cond_n2 = flow_warp(feat_n2,
+                                        flow_n2.transpose([0, 2, 3, 1]))
 
                 # flow-guided deformable convolution
                 cond = paddle.concat([cond_n1, feat_current, cond_n2], axis=1)
                 feat_prop = paddle.concat([feat_prop, feat_n2], axis=1)
 
-                feat_prop = self.deform_align_backward_1(
-                    feat_prop, cond, flow_n1, flow_n2)
+                feat_prop = self.deform_align_backward_1(feat_prop, cond,
+                                                         flow_n1, flow_n2)
 
             # concatenate and residual blocks
             feat = [feat_current] + [
-                feats[k][idx]
-                for k in feats if k not in ['spatial', 'backward_1']
+                feats[k][idx] for k in feats
+                if k not in ['spatial', 'backward_1']
             ] + [feat_prop]
 
             feat = paddle.concat(feat, axis=1)
@@ -308,8 +304,8 @@ class BasicVSRPlusPlus(nn.Layer):
                     flow_n2 = flow_n1 + flow_warp(
                         flow_n2, flow_n1.transpose([0, 2, 3, 1]))
 
-                    cond_n2 = flow_warp(feat_n2, flow_n2.transpose([0, 2, 3,
-                                                                    1]))
+                    cond_n2 = flow_warp(feat_n2,
+                                        flow_n2.transpose([0, 2, 3, 1]))
 
                 # flow-guided deformable convolution
                 cond = paddle.concat([cond_n1, feat_current, cond_n2], axis=1)
@@ -320,8 +316,8 @@ class BasicVSRPlusPlus(nn.Layer):
 
             # concatenate and residual blocks
             feat = [feat_current] + [
-                feats[k][idx]
-                for k in feats if k not in ['spatial', 'forward_1']
+                feats[k][idx] for k in feats
+                if k not in ['spatial', 'forward_1']
             ] + [feat_prop]
 
             feat = paddle.concat(feat, axis=1)
@@ -361,20 +357,20 @@ class BasicVSRPlusPlus(nn.Layer):
                     flow_n2 = flow_n1 + flow_warp(
                         flow_n2, flow_n1.transpose([0, 2, 3, 1]))
 
-                    cond_n2 = flow_warp(feat_n2, flow_n2.transpose([0, 2, 3,
-                                                                    1]))
+                    cond_n2 = flow_warp(feat_n2,
+                                        flow_n2.transpose([0, 2, 3, 1]))
 
                 # flow-guided deformable convolution
                 cond = paddle.concat([cond_n1, feat_current, cond_n2], axis=1)
                 feat_prop = paddle.concat([feat_prop, feat_n2], axis=1)
 
-                feat_prop = self.deform_align_backward_2(
-                    feat_prop, cond, flow_n1, flow_n2)
+                feat_prop = self.deform_align_backward_2(feat_prop, cond,
+                                                         flow_n1, flow_n2)
 
             # concatenate and residual blocks
             feat = [feat_current] + [
-                feats[k][idx]
-                for k in feats if k not in ['spatial', 'backward_2']
+                feats[k][idx] for k in feats
+                if k not in ['spatial', 'backward_2']
             ] + [feat_prop]
 
             feat = paddle.concat(feat, axis=1)
@@ -416,8 +412,8 @@ class BasicVSRPlusPlus(nn.Layer):
                     flow_n2 = flow_n1 + flow_warp(
                         flow_n2, flow_n1.transpose([0, 2, 3, 1]))
 
-                    cond_n2 = flow_warp(feat_n2, flow_n2.transpose([0, 2, 3,
-                                                                    1]))
+                    cond_n2 = flow_warp(feat_n2,
+                                        flow_n2.transpose([0, 2, 3, 1]))
 
                 # flow-guided deformable convolution
                 cond = paddle.concat([cond_n1, feat_current, cond_n2], axis=1)
@@ -428,8 +424,8 @@ class BasicVSRPlusPlus(nn.Layer):
 
             # concatenate and residual blocks
             feat = [feat_current] + [
-                feats[k][idx]
-                for k in feats if k not in ['spatial', 'forward_2']
+                feats[k][idx] for k in feats
+                if k not in ['spatial', 'forward_2']
             ] + [feat_prop]
 
             feat = paddle.concat(feat, axis=1)

+ 277 - 257
paddlers/models/ppgan/models/generators/edvr.py

@@ -62,6 +62,7 @@ class ResidualBlockNoBN(nn.Layer):
         nf (int): Channel number of intermediate features.
             Default: 64.
     """
+
     def __init__(self, nf=64):
         super(ResidualBlockNoBN, self).__init__()
         self.nf = nf
@@ -100,6 +101,7 @@ class PredeblurResNetPyramid(nn.Layer):
         nf (int): Channel number of intermediate features. Default: 64.
         HR_in (bool): Whether the input has high resolution. Default: False.
     """
+
     def __init__(self, in_nf=3, nf=64, HR_in=False):
         super(PredeblurResNetPyramid, self).__init__()
         self.in_nf = in_nf
@@ -107,27 +109,31 @@ class PredeblurResNetPyramid(nn.Layer):
         self.HR_in = True if HR_in else False
         self.Leaky_relu = nn.LeakyReLU(negative_slope=0.1)
         if self.HR_in:
-            self.conv_first_1 = nn.Conv2D(in_channels=self.in_nf,
-                                          out_channels=self.nf,
-                                          kernel_size=3,
-                                          stride=1,
-                                          padding=1)
-            self.conv_first_2 = nn.Conv2D(in_channels=self.nf,
-                                          out_channels=self.nf,
-                                          kernel_size=3,
-                                          stride=2,
-                                          padding=1)
-            self.conv_first_3 = nn.Conv2D(in_channels=self.nf,
-                                          out_channels=self.nf,
-                                          kernel_size=3,
-                                          stride=2,
-                                          padding=1)
+            self.conv_first_1 = nn.Conv2D(
+                in_channels=self.in_nf,
+                out_channels=self.nf,
+                kernel_size=3,
+                stride=1,
+                padding=1)
+            self.conv_first_2 = nn.Conv2D(
+                in_channels=self.nf,
+                out_channels=self.nf,
+                kernel_size=3,
+                stride=2,
+                padding=1)
+            self.conv_first_3 = nn.Conv2D(
+                in_channels=self.nf,
+                out_channels=self.nf,
+                kernel_size=3,
+                stride=2,
+                padding=1)
         else:
-            self.conv_first = nn.Conv2D(in_channels=self.in_nf,
-                                        out_channels=self.nf,
-                                        kernel_size=3,
-                                        stride=1,
-                                        padding=1)
+            self.conv_first = nn.Conv2D(
+                in_channels=self.in_nf,
+                out_channels=self.nf,
+                kernel_size=3,
+                stride=1,
+                padding=1)
         self.RB_L1_1 = ResidualBlockNoBN(nf=self.nf)
         self.RB_L1_2 = ResidualBlockNoBN(nf=self.nf)
         self.RB_L1_3 = ResidualBlockNoBN(nf=self.nf)
@@ -136,20 +142,20 @@ class PredeblurResNetPyramid(nn.Layer):
         self.RB_L2_1 = ResidualBlockNoBN(nf=self.nf)
         self.RB_L2_2 = ResidualBlockNoBN(nf=self.nf)
         self.RB_L3_1 = ResidualBlockNoBN(nf=self.nf)
-        self.deblur_L2_conv = nn.Conv2D(in_channels=self.nf,
-                                        out_channels=self.nf,
-                                        kernel_size=3,
-                                        stride=2,
-                                        padding=1)
-        self.deblur_L3_conv = nn.Conv2D(in_channels=self.nf,
-                                        out_channels=self.nf,
-                                        kernel_size=3,
-                                        stride=2,
-                                        padding=1)
-        self.upsample = nn.Upsample(scale_factor=2,
-                                    mode="bilinear",
-                                    align_corners=False,
-                                    align_mode=0)
+        self.deblur_L2_conv = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=2,
+            padding=1)
+        self.deblur_L3_conv = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=2,
+            padding=1)
+        self.upsample = nn.Upsample(
+            scale_factor=2, mode="bilinear", align_corners=False, align_mode=0)
 
     def forward(self, x):
         if self.HR_in:
@@ -189,6 +195,7 @@ class TSAFusion(nn.Layer):
         nframes (int): Number of frames. Default: 5.
         center (int): The index of center frame. Default: 2.
     """
+
     def __init__(self, nf=64, nframes=5, center=2):
         super(TSAFusion, self).__init__()
         self.nf = nf
@@ -196,81 +203,88 @@ class TSAFusion(nn.Layer):
         self.center = center
         self.sigmoid = nn.Sigmoid()
         self.Leaky_relu = nn.LeakyReLU(negative_slope=0.1)
-        self.tAtt_2 = nn.Conv2D(in_channels=self.nf,
-                                out_channels=self.nf,
-                                kernel_size=3,
-                                stride=1,
-                                padding=1)
-        self.tAtt_1 = nn.Conv2D(in_channels=self.nf,
-                                out_channels=self.nf,
-                                kernel_size=3,
-                                stride=1,
-                                padding=1)
-        self.fea_fusion = nn.Conv2D(in_channels=self.nf * self.nframes,
-                                    out_channels=self.nf,
-                                    kernel_size=1,
-                                    stride=1,
-                                    padding=0)
-        self.sAtt_1 = nn.Conv2D(in_channels=self.nf * self.nframes,
-                                out_channels=self.nf,
-                                kernel_size=1,
-                                stride=1,
-                                padding=0)
+        self.tAtt_2 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.tAtt_1 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.fea_fusion = nn.Conv2D(
+            in_channels=self.nf * self.nframes,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
+        self.sAtt_1 = nn.Conv2D(
+            in_channels=self.nf * self.nframes,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
         self.max_pool = nn.MaxPool2D(3, stride=2, padding=1)
         self.avg_pool = nn.AvgPool2D(3, stride=2, padding=1, exclusive=False)
-        self.sAtt_2 = nn.Conv2D(in_channels=2 * self.nf,
-                                out_channels=self.nf,
-                                kernel_size=1,
-                                stride=1,
-                                padding=0)
-        self.sAtt_3 = nn.Conv2D(in_channels=self.nf,
-                                out_channels=self.nf,
-                                kernel_size=3,
-                                stride=1,
-                                padding=1)
+        self.sAtt_2 = nn.Conv2D(
+            in_channels=2 * self.nf,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
+        self.sAtt_3 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
         self.sAtt_4 = nn.Conv2D(
             in_channels=self.nf,
             out_channels=self.nf,
             kernel_size=1,
             stride=1,
-            padding=0,
-        )
-        self.sAtt_5 = nn.Conv2D(in_channels=self.nf,
-                                out_channels=self.nf,
-                                kernel_size=3,
-                                stride=1,
-                                padding=1)
-        self.sAtt_add_1 = nn.Conv2D(in_channels=self.nf,
-                                    out_channels=self.nf,
-                                    kernel_size=1,
-                                    stride=1,
-                                    padding=0)
-        self.sAtt_add_2 = nn.Conv2D(in_channels=self.nf,
-                                    out_channels=self.nf,
-                                    kernel_size=1,
-                                    stride=1,
-                                    padding=0)
-        self.sAtt_L1 = nn.Conv2D(in_channels=self.nf,
-                                 out_channels=self.nf,
-                                 kernel_size=1,
-                                 stride=1,
-                                 padding=0)
+            padding=0, )
+        self.sAtt_5 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.sAtt_add_1 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
+        self.sAtt_add_2 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
+        self.sAtt_L1 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=1,
+            stride=1,
+            padding=0)
         self.sAtt_L2 = nn.Conv2D(
             in_channels=2 * self.nf,
             out_channels=self.nf,
             kernel_size=3,
             stride=1,
-            padding=1,
-        )
-        self.sAtt_L3 = nn.Conv2D(in_channels=self.nf,
-                                 out_channels=self.nf,
-                                 kernel_size=3,
-                                 stride=1,
-                                 padding=1)
-        self.upsample = nn.Upsample(scale_factor=2,
-                                    mode="bilinear",
-                                    align_corners=False,
-                                    align_mode=0)
+            padding=1, )
+        self.sAtt_L3 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.upsample = nn.Upsample(
+            scale_factor=2, mode="bilinear", align_corners=False, align_mode=0)
 
     def forward(self, aligned_fea):
         """
@@ -347,6 +361,7 @@ class DCNPack(nn.Layer):
     Ref:
         Delving Deep into Deformable Alignment in Video Super-Resolution.
     """
+
     def __init__(self,
                  num_filters=64,
                  kernel_size=3,
@@ -361,23 +376,24 @@ class DCNPack(nn.Layer):
         self.num_filters = num_filters
         if isinstance(kernel_size, int):
             self.kernel_size = [kernel_size, kernel_size]
-        self.conv_offset_mask = nn.Conv2D(in_channels=self.num_filters,
-                                          out_channels=self.deformable_groups *
-                                          3 * self.kernel_size[0] *
-                                          self.kernel_size[1],
-                                          kernel_size=self.kernel_size,
-                                          stride=stride,
-                                          padding=padding)
+        self.conv_offset_mask = nn.Conv2D(
+            in_channels=self.num_filters,
+            out_channels=self.deformable_groups * 3 * self.kernel_size[0] *
+            self.kernel_size[1],
+            kernel_size=self.kernel_size,
+            stride=stride,
+            padding=padding)
         self.total_channels = self.deformable_groups * 3 * self.kernel_size[
             0] * self.kernel_size[1]
         self.split_channels = self.total_channels // 3
-        self.dcn = DeformConv2D(in_channels=self.num_filters,
-                                out_channels=self.num_filters,
-                                kernel_size=self.kernel_size,
-                                stride=stride,
-                                padding=padding,
-                                dilation=dilation,
-                                deformable_groups=self.deformable_groups)
+        self.dcn = DeformConv2D(
+            in_channels=self.num_filters,
+            out_channels=self.num_filters,
+            kernel_size=self.kernel_size,
+            stride=stride,
+            padding=padding,
+            dilation=dilation,
+            deformable_groups=self.deformable_groups)
         self.sigmoid = nn.Sigmoid()
         # init conv offset
         constant_init(self.conv_offset_mask, 0., 0.)
@@ -408,104 +424,103 @@ class PCDAlign(nn.Layer):
         nf (int): Channel number of middle features. Default: 64.
         groups (int): Deformable groups. Defaults: 8.
     """
+
     def __init__(self, nf=64, groups=8):
         super(PCDAlign, self).__init__()
         self.nf = nf
         self.groups = groups
         self.Leaky_relu = nn.LeakyReLU(negative_slope=0.1)
-        self.upsample = nn.Upsample(scale_factor=2,
-                                    mode="bilinear",
-                                    align_corners=False,
-                                    align_mode=0)
+        self.upsample = nn.Upsample(
+            scale_factor=2, mode="bilinear", align_corners=False, align_mode=0)
         # Pyramid has three levels:
         # L3: level 3, 1/4 spatial size
         # L2: level 2, 1/2 spatial size
         # L1: level 1, original spatial size
 
         # L3
-        self.PCD_Align_L3_offset_conv1 = nn.Conv2D(in_channels=nf * 2,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L3_offset_conv2 = nn.Conv2D(in_channels=nf,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L3_dcn = DCNPack(num_filters=nf,
-                                        kernel_size=3,
-                                        stride=1,
-                                        padding=1,
-                                        deformable_groups=groups)
+        self.PCD_Align_L3_offset_conv1 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_L3_offset_conv2 = nn.Conv2D(
+            in_channels=nf, out_channels=nf, kernel_size=3, stride=1, padding=1)
+        self.PCD_Align_L3_dcn = DCNPack(
+            num_filters=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            deformable_groups=groups)
         #L2
-        self.PCD_Align_L2_offset_conv1 = nn.Conv2D(in_channels=nf * 2,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L2_offset_conv2 = nn.Conv2D(in_channels=nf * 2,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L2_offset_conv3 = nn.Conv2D(in_channels=nf,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L2_dcn = DCNPack(num_filters=nf,
-                                        kernel_size=3,
-                                        stride=1,
-                                        padding=1,
-                                        deformable_groups=groups)
-        self.PCD_Align_L2_fea_conv = nn.Conv2D(in_channels=nf * 2,
-                                               out_channels=nf,
-                                               kernel_size=3,
-                                               stride=1,
-                                               padding=1)
+        self.PCD_Align_L2_offset_conv1 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_L2_offset_conv2 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_L2_offset_conv3 = nn.Conv2D(
+            in_channels=nf, out_channels=nf, kernel_size=3, stride=1, padding=1)
+        self.PCD_Align_L2_dcn = DCNPack(
+            num_filters=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            deformable_groups=groups)
+        self.PCD_Align_L2_fea_conv = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
         #L1
-        self.PCD_Align_L1_offset_conv1 = nn.Conv2D(in_channels=nf * 2,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L1_offset_conv2 = nn.Conv2D(in_channels=nf * 2,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L1_offset_conv3 = nn.Conv2D(in_channels=nf,
-                                                   out_channels=nf,
-                                                   kernel_size=3,
-                                                   stride=1,
-                                                   padding=1)
-        self.PCD_Align_L1_dcn = DCNPack(num_filters=nf,
-                                        kernel_size=3,
-                                        stride=1,
-                                        padding=1,
-                                        deformable_groups=groups)
-        self.PCD_Align_L1_fea_conv = nn.Conv2D(in_channels=nf * 2,
-                                               out_channels=nf,
-                                               kernel_size=3,
-                                               stride=1,
-                                               padding=1)
+        self.PCD_Align_L1_offset_conv1 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_L1_offset_conv2 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_L1_offset_conv3 = nn.Conv2D(
+            in_channels=nf, out_channels=nf, kernel_size=3, stride=1, padding=1)
+        self.PCD_Align_L1_dcn = DCNPack(
+            num_filters=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            deformable_groups=groups)
+        self.PCD_Align_L1_fea_conv = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
         #cascade
-        self.PCD_Align_cas_offset_conv1 = nn.Conv2D(in_channels=nf * 2,
-                                                    out_channels=nf,
-                                                    kernel_size=3,
-                                                    stride=1,
-                                                    padding=1)
-        self.PCD_Align_cas_offset_conv2 = nn.Conv2D(in_channels=nf,
-                                                    out_channels=nf,
-                                                    kernel_size=3,
-                                                    stride=1,
-                                                    padding=1)
-        self.PCD_Align_cascade_dcn = DCNPack(num_filters=nf,
-                                             kernel_size=3,
-                                             stride=1,
-                                             padding=1,
-                                             deformable_groups=groups)
+        self.PCD_Align_cas_offset_conv1 = nn.Conv2D(
+            in_channels=nf * 2,
+            out_channels=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.PCD_Align_cas_offset_conv2 = nn.Conv2D(
+            in_channels=nf, out_channels=nf, kernel_size=3, stride=1, padding=1)
+        self.PCD_Align_cascade_dcn = DCNPack(
+            num_filters=nf,
+            kernel_size=3,
+            stride=1,
+            padding=1,
+            deformable_groups=groups)
 
     def forward(self, nbr_fea_l, ref_fea_l):
         """Align neighboring frame features to the reference frame features.
@@ -594,6 +609,7 @@ class EDVRNet(nn.Layer):
         with_tsa (bool): Whether has TSA module. Default: True.
         TSA_only (bool): Whether only use TSA module. Default: False.
     """
+
     def __init__(self,
                  in_nf=3,
                  out_nf=3,
@@ -623,90 +639,94 @@ class EDVRNet(nn.Layer):
 
         self.Leaky_relu = nn.LeakyReLU(negative_slope=0.1)
         if self.predeblur:
-            self.pre_deblur = PredeblurResNetPyramid(in_nf=self.in_nf,
-                                                     nf=self.nf,
-                                                     HR_in=self.HR_in)
-            self.cov_1 = nn.Conv2D(in_channels=self.nf,
-                                   out_channels=self.nf,
-                                   kernel_size=1,
-                                   stride=1)
+            self.pre_deblur = PredeblurResNetPyramid(
+                in_nf=self.in_nf, nf=self.nf, HR_in=self.HR_in)
+            self.cov_1 = nn.Conv2D(
+                in_channels=self.nf,
+                out_channels=self.nf,
+                kernel_size=1,
+                stride=1)
         else:
-            self.conv_first = nn.Conv2D(in_channels=self.in_nf,
-                                        out_channels=self.nf,
-                                        kernel_size=3,
-                                        stride=1,
-                                        padding=1)
+            self.conv_first = nn.Conv2D(
+                in_channels=self.in_nf,
+                out_channels=self.nf,
+                kernel_size=3,
+                stride=1,
+                padding=1)
 
         #feature extraction module
         self.feature_extractor = MakeMultiBlocks(ResidualBlockNoBN,
                                                  self.front_RBs, self.nf)
-        self.fea_L2_conv1 = nn.Conv2D(in_channels=self.nf,
-                                      out_channels=self.nf,
-                                      kernel_size=3,
-                                      stride=2,
-                                      padding=1)
-        self.fea_L2_conv2 = nn.Conv2D(in_channels=self.nf,
-                                      out_channels=self.nf,
-                                      kernel_size=3,
-                                      stride=1,
-                                      padding=1)
+        self.fea_L2_conv1 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=2,
+            padding=1)
+        self.fea_L2_conv2 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
         self.fea_L3_conv1 = nn.Conv2D(
             in_channels=self.nf,
             out_channels=self.nf,
             kernel_size=3,
             stride=2,
-            padding=1,
-        )
-        self.fea_L3_conv2 = nn.Conv2D(in_channels=self.nf,
-                                      out_channels=self.nf,
-                                      kernel_size=3,
-                                      stride=1,
-                                      padding=1)
+            padding=1, )
+        self.fea_L3_conv2 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
 
         #PCD alignment module
         self.PCDModule = PCDAlign(nf=self.nf, groups=self.groups)
 
         #TSA Fusion module
         if self.w_TSA:
-            self.TSAModule = TSAFusion(nf=self.nf,
-                                       nframes=self.nframes,
-                                       center=self.center)
+            self.TSAModule = TSAFusion(
+                nf=self.nf, nframes=self.nframes, center=self.center)
         else:
-            self.TSAModule = nn.Conv2D(in_channels=self.nframes * self.nf,
-                                       out_channels=self.nf,
-                                       kernel_size=1,
-                                       stride=1)
+            self.TSAModule = nn.Conv2D(
+                in_channels=self.nframes * self.nf,
+                out_channels=self.nf,
+                kernel_size=1,
+                stride=1)
 
         #reconstruction module
         self.reconstructor = MakeMultiBlocks(ResidualBlockNoBN, self.back_RBs,
                                              self.nf)
-        self.upconv1 = nn.Conv2D(in_channels=self.nf,
-                                 out_channels=4 * self.nf,
-                                 kernel_size=3,
-                                 stride=1,
-                                 padding=1)
+        self.upconv1 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=4 * self.nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
 
         self.pixel_shuffle = nn.PixelShuffle(2)
-        self.upconv2 = nn.Conv2D(in_channels=self.nf,
-                                 out_channels=4 * 64,
-                                 kernel_size=3,
-                                 stride=1,
-                                 padding=1)
-        self.HRconv = nn.Conv2D(in_channels=64,
-                                out_channels=64,
-                                kernel_size=3,
-                                stride=1,
-                                padding=1)
-        self.conv_last = nn.Conv2D(in_channels=64,
-                                   out_channels=self.out_nf,
-                                   kernel_size=3,
-                                   stride=1,
-                                   padding=1)
+        self.upconv2 = nn.Conv2D(
+            in_channels=self.nf,
+            out_channels=4 * 64,
+            kernel_size=3,
+            stride=1,
+            padding=1)
+        self.HRconv = nn.Conv2D(
+            in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1)
+        self.conv_last = nn.Conv2D(
+            in_channels=64,
+            out_channels=self.out_nf,
+            kernel_size=3,
+            stride=1,
+            padding=1)
         if self.scale_factor == 4:
-            self.upsample = nn.Upsample(scale_factor=self.scale_factor,
-                                        mode="bilinear",
-                                        align_corners=False,
-                                        align_mode=0)
+            self.upsample = nn.Upsample(
+                scale_factor=self.scale_factor,
+                mode="bilinear",
+                align_corners=False,
+                align_mode=0)
 
     def forward(self, x):
         """
@@ -753,8 +773,8 @@ class EDVRNet(nn.Layer):
         aligned_fea = []
         for i in range(N):
             nbr_fea_l = [
-                L1_fea[:, i, :, :, :], L2_fea[:, i, :, :, :], L3_fea[:,
-                                                                     i, :, :, :]
+                L1_fea[:, i, :, :, :], L2_fea[:, i, :, :, :],
+                L3_fea[:, i, :, :, :]
             ]
             aligned_fea.append(self.PCDModule(nbr_fea_l, ref_fea_l))
 

+ 105 - 74
paddlers/models/ppgan/models/generators/generater_animegan.py

@@ -13,17 +13,18 @@ class Conv2DNormLReLU(nn.Layer):
     def __init__(self,
                  in_channels: int,
                  out_channels: int,
-                 kernel_size: int = 3,
-                 stride: int = 1,
-                 padding: int = 1,
+                 kernel_size: int=3,
+                 stride: int=1,
+                 padding: int=1,
                  bias_attr=False) -> None:
         super().__init__()
-        self.conv = nn.Conv2D(in_channels,
-                              out_channels,
-                              kernel_size,
-                              stride,
-                              padding,
-                              bias_attr=bias_attr)
+        self.conv = nn.Conv2D(
+            in_channels,
+            out_channels,
+            kernel_size,
+            stride,
+            padding,
+            bias_attr=bias_attr)
         # NOTE layer norm is crucial for animegan!
         self.norm = nn.GroupNorm(1, out_channels)
         self.lrelu = nn.LeakyReLU(0.2)
@@ -39,9 +40,11 @@ class ResBlock(nn.Layer):
     def __init__(self, in_channels: int, out_channels: int) -> None:
         super().__init__()
         self.body = nn.Sequential(
-            Conv2DNormLReLU(in_channels, out_channels, 1, padding=0),
+            Conv2DNormLReLU(
+                in_channels, out_channels, 1, padding=0),
             Conv2DNormLReLU(out_channels, out_channels, 3),
-            nn.Conv2D(out_channels, out_channels // 2, 1, bias_attr=False))
+            nn.Conv2D(
+                out_channels, out_channels // 2, 1, bias_attr=False))
 
     def forward(self, x0):
         x = self.body(x0)
@@ -61,28 +64,30 @@ class InvertedresBlock(nn.Layer):
         self.bottle_channels = round(self.expansion * self.in_channels)
         self.body = nn.Sequential(
             # pw
-            Conv2DNormLReLU(self.in_channels,
-                            self.bottle_channels,
-                            kernel_size=1,
-                            bias_attr=bias_attr),
+            Conv2DNormLReLU(
+                self.in_channels,
+                self.bottle_channels,
+                kernel_size=1,
+                bias_attr=bias_attr),
             # dw
-            nn.Conv2D(self.bottle_channels,
-                      self.bottle_channels,
-                      kernel_size=3,
-                      stride=1,
-                      padding=0,
-                      groups=self.bottle_channels,
-                      bias_attr=True),
+            nn.Conv2D(
+                self.bottle_channels,
+                self.bottle_channels,
+                kernel_size=3,
+                stride=1,
+                padding=0,
+                groups=self.bottle_channels,
+                bias_attr=True),
             nn.GroupNorm(1, self.bottle_channels),
             nn.LeakyReLU(0.2),
             # pw & linear
-            nn.Conv2D(self.bottle_channels,
-                      self.out_channels,
-                      kernel_size=1,
-                      padding=0,
-                      bias_attr=False),
-            nn.GroupNorm(1, self.out_channels),
-        )
+            nn.Conv2D(
+                self.bottle_channels,
+                self.out_channels,
+                kernel_size=1,
+                padding=0,
+                bias_attr=False),
+            nn.GroupNorm(1, self.out_channels), )
 
     def forward(self, x0):
         x = self.body(x0)
@@ -97,26 +102,41 @@ class InvertedresBlock(nn.Layer):
 class AnimeGeneratorLite(nn.Layer):
     def __init__(self) -> None:
         super().__init__()
-        self.A = nn.Sequential(Conv2DNormLReLU(3, 32, 7, padding=3),
-                               Conv2DNormLReLU(32, 32, stride=2),
-                               Conv2DNormLReLU(32, 32))
-
-        self.B = nn.Sequential(Conv2DNormLReLU(32, 64, stride=2),
-                               Conv2DNormLReLU(64, 64), Conv2DNormLReLU(64, 64))
-
-        self.C = nn.Sequential(ResBlock(64, 128), ResBlock(64, 128),
-                               ResBlock(64, 128), ResBlock(64, 128))
-
-        self.D = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
-                               Conv2DNormLReLU(64, 64), Conv2DNormLReLU(64, 64),
-                               Conv2DNormLReLU(64, 64))
-
-        self.E = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
-                               Conv2DNormLReLU(64, 32), Conv2DNormLReLU(32, 32),
-                               Conv2DNormLReLU(32, 32, 7, padding=3))
-
-        self.out = nn.Sequential(nn.Conv2D(32, 3, 1, bias_attr=False),
-                                 nn.Tanh())
+        self.A = nn.Sequential(
+            Conv2DNormLReLU(
+                3, 32, 7, padding=3),
+            Conv2DNormLReLU(
+                32, 32, stride=2),
+            Conv2DNormLReLU(32, 32))
+
+        self.B = nn.Sequential(
+            Conv2DNormLReLU(
+                32, 64, stride=2),
+            Conv2DNormLReLU(64, 64),
+            Conv2DNormLReLU(64, 64))
+
+        self.C = nn.Sequential(
+            ResBlock(64, 128),
+            ResBlock(64, 128), ResBlock(64, 128), ResBlock(64, 128))
+
+        self.D = nn.Sequential(
+            nn.Upsample(
+                scale_factor=2, mode='bilinear'),
+            Conv2DNormLReLU(64, 64),
+            Conv2DNormLReLU(64, 64),
+            Conv2DNormLReLU(64, 64))
+
+        self.E = nn.Sequential(
+            nn.Upsample(
+                scale_factor=2, mode='bilinear'),
+            Conv2DNormLReLU(64, 32),
+            Conv2DNormLReLU(32, 32),
+            Conv2DNormLReLU(
+                32, 32, 7, padding=3))
+
+        self.out = nn.Sequential(
+            nn.Conv2D(
+                32, 3, 1, bias_attr=False), nn.Tanh())
 
     def forward(self, x):
         x = self.A(x)
@@ -132,31 +152,42 @@ class AnimeGeneratorLite(nn.Layer):
 class AnimeGenerator(nn.Layer):
     def __init__(self) -> None:
         super().__init__()
-        self.A = nn.Sequential(Conv2DNormLReLU(3, 32, 7, padding=3),
-                               Conv2DNormLReLU(32, 64, stride=2),
-                               Conv2DNormLReLU(64, 64))
-
-        self.B = nn.Sequential(Conv2DNormLReLU(64, 128, stride=2),
-                               Conv2DNormLReLU(128, 128),
-                               Conv2DNormLReLU(128, 128))
-
-        self.C = nn.Sequential(InvertedresBlock(128, 2, 256),
-                               InvertedresBlock(256, 2, 256),
-                               InvertedresBlock(256, 2, 256),
-                               InvertedresBlock(256, 2, 256),
-                               Conv2DNormLReLU(256, 128))
-
-        self.D = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
-                               Conv2DNormLReLU(128, 128),
-                               Conv2DNormLReLU(128, 128))
-
-        self.E = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear'),
-                               Conv2DNormLReLU(128, 64),
-                               Conv2DNormLReLU(64, 64),
-                               Conv2DNormLReLU(64, 32, 7, padding=3))
-
-        self.out = nn.Sequential(nn.Conv2D(32, 3, 1, bias_attr=False),
-                                 nn.Tanh())
+        self.A = nn.Sequential(
+            Conv2DNormLReLU(
+                3, 32, 7, padding=3),
+            Conv2DNormLReLU(
+                32, 64, stride=2),
+            Conv2DNormLReLU(64, 64))
+
+        self.B = nn.Sequential(
+            Conv2DNormLReLU(
+                64, 128, stride=2),
+            Conv2DNormLReLU(128, 128),
+            Conv2DNormLReLU(128, 128))
+
+        self.C = nn.Sequential(
+            InvertedresBlock(128, 2, 256),
+            InvertedresBlock(256, 2, 256),
+            InvertedresBlock(256, 2, 256),
+            InvertedresBlock(256, 2, 256), Conv2DNormLReLU(256, 128))
+
+        self.D = nn.Sequential(
+            nn.Upsample(
+                scale_factor=2, mode='bilinear'),
+            Conv2DNormLReLU(128, 128),
+            Conv2DNormLReLU(128, 128))
+
+        self.E = nn.Sequential(
+            nn.Upsample(
+                scale_factor=2, mode='bilinear'),
+            Conv2DNormLReLU(128, 64),
+            Conv2DNormLReLU(64, 64),
+            Conv2DNormLReLU(
+                64, 32, 7, padding=3))
+
+        self.out = nn.Sequential(
+            nn.Conv2D(
+                32, 3, 1, bias_attr=False), nn.Tanh())
 
     def forward(self, x):
         x = self.A(x)

+ 83 - 53
paddlers/models/ppgan/models/generators/generater_lapstyle.py

@@ -73,8 +73,8 @@ def adaptive_instance_normalization(content_feat, style_feat):
     style_mean, style_std = calc_mean_std(style_feat)
     content_mean, content_std = calc_mean_std(content_feat)
 
-    normalized_feat = (content_feat -
-                       content_mean.expand(size)) / content_std.expand(size)
+    normalized_feat = (
+        content_feat - content_mean.expand(size)) / content_std.expand(size)
     return normalized_feat * style_std.expand(size) + style_mean.expand(size)
 
 
@@ -88,12 +88,17 @@ class ResnetBlock(nn.Layer):
     Args:
         dim (int): Channel number of intermediate features.
     """
+
     def __init__(self, dim):
         super(ResnetBlock, self).__init__()
-        self.conv_block = nn.Sequential(nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-                                        nn.Conv2D(dim, dim, (3, 3)), nn.ReLU(),
-                                        nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-                                        nn.Conv2D(dim, dim, (3, 3)))
+        self.conv_block = nn.Sequential(
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
+            nn.Conv2D(dim, dim, (3, 3)),
+            nn.ReLU(),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
+            nn.Conv2D(dim, dim, (3, 3)))
 
     def forward(self, x):
         out = x + self.conv_block(x)
@@ -110,11 +115,14 @@ class ConvBlock(nn.Layer):
         dim1 (int): Channel number of input features.
         dim2 (int): Channel number of output features.
     """
+
     def __init__(self, dim1, dim2):
         super(ConvBlock, self).__init__()
-        self.conv_block = nn.Sequential(nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-                                        nn.Conv2D(dim1, dim2, (3, 3)),
-                                        nn.ReLU())
+        self.conv_block = nn.Sequential(
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
+            nn.Conv2D(dim1, dim2, (3, 3)),
+            nn.ReLU())
 
     def forward(self, x):
         out = self.conv_block(x)
@@ -128,6 +136,7 @@ class DecoderNet(nn.Layer):
         Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality
         Artistic Style Transfer.
     """
+
     def __init__(self):
         super(DecoderNet, self).__init__()
 
@@ -142,8 +151,9 @@ class DecoderNet(nn.Layer):
         self.convblock_11 = ConvBlock(64, 64)
         self.upsample = nn.Upsample(scale_factor=2, mode='nearest')
 
-        self.final_conv = nn.Sequential(nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-                                        nn.Conv2D(64, 3, (3, 3)))
+        self.final_conv = nn.Sequential(
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'), nn.Conv2D(64, 3, (3, 3)))
 
     def forward(self, cF, sF):
 
@@ -167,8 +177,6 @@ class DecoderNet(nn.Layer):
         return out
 
 
-
-
 @GENERATORS.register()
 class Encoder(nn.Layer):
     """Encoder of Drafting module.
@@ -176,76 +184,97 @@ class Encoder(nn.Layer):
         Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality
         Artistic Style Transfer.
     """
+
     def __init__(self):
         super(Encoder, self).__init__()
         vgg_net = nn.Sequential(
             nn.Conv2D(3, 3, (1, 1)),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(3, 64, (3, 3)),
             nn.ReLU(),  # relu1-1
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(64, 64, (3, 3)),
             nn.ReLU(),  # relu1-2
-            nn.MaxPool2D((2, 2), (2, 2), (0, 0), ceil_mode=True),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.MaxPool2D(
+                (2, 2), (2, 2), (0, 0), ceil_mode=True),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(64, 128, (3, 3)),
             nn.ReLU(),  # relu2-1
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(128, 128, (3, 3)),
             nn.ReLU(),  # relu2-2
-            nn.MaxPool2D((2, 2), (2, 2), (0, 0), ceil_mode=True),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.MaxPool2D(
+                (2, 2), (2, 2), (0, 0), ceil_mode=True),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(128, 256, (3, 3)),
             nn.ReLU(),  # relu3-1
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(256, 256, (3, 3)),
             nn.ReLU(),  # relu3-2
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(256, 256, (3, 3)),
             nn.ReLU(),  # relu3-3
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(256, 256, (3, 3)),
             nn.ReLU(),  # relu3-4
-            nn.MaxPool2D((2, 2), (2, 2), (0, 0), ceil_mode=True),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.MaxPool2D(
+                (2, 2), (2, 2), (0, 0), ceil_mode=True),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(256, 512, (3, 3)),
             nn.ReLU(),  # relu4-1, this is the last layer used
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu4-2
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu4-3
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu4-4
-            nn.MaxPool2D((2, 2), (2, 2), (0, 0), ceil_mode=True),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.MaxPool2D(
+                (2, 2), (2, 2), (0, 0), ceil_mode=True),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu5-1
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu5-2
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU(),  # relu5-3
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'),
             nn.Conv2D(512, 512, (3, 3)),
             nn.ReLU()  # relu5-4
         )
         weight_path = get_path_from_url(
             'https://paddlegan.bj.bcebos.com/models/vgg_normalised.pdparams')
         vgg_net.set_dict(paddle.load(weight_path))
-        self.enc_1 = nn.Sequential(*list(
-            vgg_net.children())[:4])  # input -> relu1_1
-        self.enc_2 = nn.Sequential(*list(
-            vgg_net.children())[4:11])  # relu1_1 -> relu2_1
-        self.enc_3 = nn.Sequential(*list(
-            vgg_net.children())[11:18])  # relu2_1 -> relu3_1
-        self.enc_4 = nn.Sequential(*list(
-            vgg_net.children())[18:31])  # relu3_1 -> relu4_1
-        self.enc_5 = nn.Sequential(*list(
-            vgg_net.children())[31:44])  # relu4_1 -> relu5_1
+        self.enc_1 = nn.Sequential(
+            *list(vgg_net.children())[:4])  # input -> relu1_1
+        self.enc_2 = nn.Sequential(
+            *list(vgg_net.children())[4:11])  # relu1_1 -> relu2_1
+        self.enc_3 = nn.Sequential(
+            *list(vgg_net.children())[11:18])  # relu2_1 -> relu3_1
+        self.enc_4 = nn.Sequential(
+            *list(vgg_net.children())[18:31])  # relu3_1 -> relu4_1
+        self.enc_5 = nn.Sequential(
+            *list(vgg_net.children())[31:44])  # relu4_1 -> relu5_1
 
     def forward(self, x):
         out = {}
@@ -269,32 +298,33 @@ class RevisionNet(nn.Layer):
         Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality
         Artistic Style Transfer.
     """
+
     def __init__(self, input_nc=6):
         super(RevisionNet, self).__init__()
         DownBlock = []
         DownBlock += [
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-            nn.Conv2D(input_nc, 64, (3, 3)),
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'), nn.Conv2D(input_nc, 64, (3, 3)),
             nn.ReLU()
         ]
         DownBlock += [
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-            nn.Conv2D(64, 64, (3, 3), stride=2),
-            nn.ReLU()
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'), nn.Conv2D(
+                    64, 64, (3, 3), stride=2), nn.ReLU()
         ]
 
         self.resblock = ResnetBlock(64)
 
         UpBlock = []
         UpBlock += [
-            nn.Upsample(scale_factor=2, mode='nearest'),
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-            nn.Conv2D(64, 64, (3, 3)),
+            nn.Upsample(
+                scale_factor=2, mode='nearest'), nn.Pad2D(
+                    [1, 1, 1, 1], mode='reflect'), nn.Conv2D(64, 64, (3, 3)),
             nn.ReLU()
         ]
         UpBlock += [
-            nn.Pad2D([1, 1, 1, 1], mode='reflect'),
-            nn.Conv2D(64, 3, (3, 3))
+            nn.Pad2D(
+                [1, 1, 1, 1], mode='reflect'), nn.Conv2D(64, 3, (3, 3))
         ]
 
         self.DownBlock = nn.Sequential(*DownBlock)

+ 79 - 50
paddlers/models/ppgan/models/generators/generater_photopen.py

@@ -21,6 +21,7 @@ from paddle.nn.utils import spectral_norm
 from ...utils.photopen import build_norm_layer, simam, Dict
 from .builder import GENERATORS
 
+
 class SPADE(nn.Layer):
     def __init__(self, config_text, norm_nc, label_nc):
         super(SPADE, self).__init__()
@@ -57,6 +58,7 @@ class SPADE(nn.Layer):
 
         return out
 
+
 class SPADEResnetBlock(nn.Layer):
     def __init__(self, fin, fout, opt):
         super(SPADEResnetBlock, self).__init__()
@@ -64,7 +66,7 @@ class SPADEResnetBlock(nn.Layer):
         # Attributes
         self.learned_shortcut = (fin != fout)
         fmiddle = min(fin, fout)
-        
+
         # define spade layers
         spade_config_str = opt.norm_G.replace('spectral', '')
         self.spade_0 = SPADE(spade_config_str, fin, opt.semantic_nc)
@@ -75,22 +77,22 @@ class SPADEResnetBlock(nn.Layer):
         # define act_conv layers
         self.act_conv_0 = nn.Sequential(*[
             nn.GELU(),
-            spectral_norm(nn.Conv2D(fin, fmiddle, 3, 1, 1, 
-                weight_attr=None,
-                bias_attr=None)),
-            ])
+            spectral_norm(
+                nn.Conv2D(
+                    fin, fmiddle, 3, 1, 1, weight_attr=None, bias_attr=None)),
+        ])
         self.act_conv_1 = nn.Sequential(*[
             nn.GELU(),
-            spectral_norm(nn.Conv2D(fmiddle, fout, 3, 1, 1, 
-                weight_attr=None,
-                bias_attr=None)),
-            ])
+            spectral_norm(
+                nn.Conv2D(
+                    fmiddle, fout, 3, 1, 1, weight_attr=None, bias_attr=None)),
+        ])
         if self.learned_shortcut:
             self.act_conv_s = nn.Sequential(*[
-                spectral_norm(nn.Conv2D(fin, fout, 1, 1, 0, bias_attr=False,
-                    weight_attr=None)),
-                ])
-
+                spectral_norm(
+                    nn.Conv2D(
+                        fin, fout, 1, 1, 0, bias_attr=False, weight_attr=None)),
+            ])
 
     def forward(self, x, seg):
         x_s = self.shortcut(x, seg)
@@ -107,32 +109,33 @@ class SPADEResnetBlock(nn.Layer):
             x_s = x
         return x_s
 
+
 @GENERATORS.register()
 class SPADEGenerator(nn.Layer):
-    def __init__(self, 
-                 ngf,
-                 num_upsampling_layers,
-                 crop_size,
-                 aspect_ratio,
-                 norm_G,
-                 semantic_nc,
-                 use_vae,
-                 nef,
-                 ):
+    def __init__(
+            self,
+            ngf,
+            num_upsampling_layers,
+            crop_size,
+            aspect_ratio,
+            norm_G,
+            semantic_nc,
+            use_vae,
+            nef, ):
         super(SPADEGenerator, self).__init__()
-        
+
         opt = {
-             'ngf': ngf,
-             'num_upsampling_layers': num_upsampling_layers,
-             'crop_size': crop_size,
-             'aspect_ratio': aspect_ratio,
-             'norm_G': norm_G,
-             'semantic_nc': semantic_nc,
-             'use_vae': use_vae,
-             'nef': nef,
-            }
+            'ngf': ngf,
+            'num_upsampling_layers': num_upsampling_layers,
+            'crop_size': crop_size,
+            'aspect_ratio': aspect_ratio,
+            'norm_G': norm_G,
+            'semantic_nc': semantic_nc,
+            'use_vae': use_vae,
+            'nef': nef,
+        }
         self.opt = Dict(opt)
-        
+
         nf = self.opt.ngf
         self.sw, self.sh = self.compute_latent_vector_size(self.opt)
 
@@ -213,7 +216,8 @@ class SPADEGenerator(nn.Layer):
         sh = round(sw / opt.aspect_ratio)
 
         return sw, sh
-    
+
+
 class VAE_Encoder(nn.Layer):
     def __init__(self, opt):
         super(VAE_Encoder, self).__init__()
@@ -224,31 +228,51 @@ class VAE_Encoder(nn.Layer):
 
         InstanceNorm = build_norm_layer('instance')
         model = [
-            spectral_norm(nn.Conv2D(3, ndf, kw, 2, pw,
-                    weight_attr=None,
-                    bias_attr=None)),
+            spectral_norm(
+                nn.Conv2D(
+                    3, ndf, kw, 2, pw, weight_attr=None, bias_attr=None)),
             InstanceNorm(ndf),
-
             nn.GELU(),
-            spectral_norm(nn.Conv2D(ndf * 1, ndf * 2, kw, 2, pw,
+            spectral_norm(
+                nn.Conv2D(
+                    ndf * 1,
+                    ndf * 2,
+                    kw,
+                    2,
+                    pw,
                     weight_attr=None,
                     bias_attr=None)),
             InstanceNorm(ndf * 2),
-
             nn.GELU(),
-            spectral_norm(nn.Conv2D(ndf * 2, ndf * 4, kw, 2, pw,
+            spectral_norm(
+                nn.Conv2D(
+                    ndf * 2,
+                    ndf * 4,
+                    kw,
+                    2,
+                    pw,
                     weight_attr=None,
                     bias_attr=None)),
             InstanceNorm(ndf * 4),
-
             nn.GELU(),
-            spectral_norm(nn.Conv2D(ndf * 4, ndf * 8, kw, 2, pw,
+            spectral_norm(
+                nn.Conv2D(
+                    ndf * 4,
+                    ndf * 8,
+                    kw,
+                    2,
+                    pw,
                     weight_attr=None,
                     bias_attr=None)),
             InstanceNorm(ndf * 8),
-
             nn.GELU(),
-            spectral_norm(nn.Conv2D(ndf * 8, ndf * 8, kw, 2, pw,
+            spectral_norm(
+                nn.Conv2D(
+                    ndf * 8,
+                    ndf * 8,
+                    kw,
+                    2,
+                    pw,
                     weight_attr=None,
                     bias_attr=None)),
             InstanceNorm(ndf * 8),
@@ -256,12 +280,18 @@ class VAE_Encoder(nn.Layer):
         if opt.crop_size >= 256:
             model += [
                 nn.GELU(),
-                spectral_norm(nn.Conv2D(ndf * 8, ndf * 8, kw, 2, pw,
+                spectral_norm(
+                    nn.Conv2D(
+                        ndf * 8,
+                        ndf * 8,
+                        kw,
+                        2,
+                        pw,
                         weight_attr=None,
                         bias_attr=None)),
                 InstanceNorm(ndf * 8),
             ]
-        model += [nn.GELU(),]
+        model += [nn.GELU(), ]
 
         self.flatten = nn.Flatten(1, -1)
         self.so = 4
@@ -272,8 +302,7 @@ class VAE_Encoder(nn.Layer):
 
     def forward(self, x):
         x = self.model(x)
-        
+
         x = self.flatten(x)
 
         return self.fc_mu(x), self.fc_var(x)
-

+ 27 - 27
paddlers/models/ppgan/models/generators/generator_firstorder.py

@@ -42,6 +42,7 @@ class FirstOrderGenerator(nn.Layer):
                                  equivariance_value, equivariance_jacobian]
 
     """
+
     def __init__(self, generator_cfg, kp_detector_cfg, common_params,
                  train_params, dis_scales):
         super(FirstOrderGenerator, self).__init__()
@@ -59,9 +60,8 @@ class FirstOrderGenerator(nn.Layer):
     def forward(self, x, discriminator, kp_extractor_ori=None):
         kp_source = self.kp_extractor(x['source'])
         kp_driving = self.kp_extractor(x['driving'])
-        generated = self.generator(x['source'],
-                                   kp_source=kp_source,
-                                   kp_driving=kp_driving)
+        generated = self.generator(
+            x['source'], kp_source=kp_source, kp_driving=kp_driving)
         generated.update({'kp_source': kp_source, 'kp_driving': kp_driving})
 
         loss_values = {}
@@ -84,8 +84,8 @@ class FirstOrderGenerator(nn.Layer):
         if self.loss_weights['generator_gan'] != 0:
             discriminator_maps_generated = discriminator(
                 pyramide_generated, kp=detach_kp(kp_driving))
-            discriminator_maps_real = discriminator(pyramide_real,
-                                                    kp=detach_kp(kp_driving))
+            discriminator_maps_real = discriminator(
+                pyramide_real, kp=detach_kp(kp_driving))
             value_total = 0
             for scale in self.disc_scales:
                 key = 'prediction_map_%s' % scale
@@ -118,17 +118,17 @@ class FirstOrderGenerator(nn.Layer):
 
             # Value loss part
             if self.loss_weights['equivariance_value'] != 0:
-                value = paddle.abs(
-                    kp_driving['value'] -
-                    transform.warp_coordinates(transformed_kp['value'])).mean()
+                value = paddle.abs(kp_driving['value'] -
+                                   transform.warp_coordinates(transformed_kp[
+                                       'value'])).mean()
                 loss_values['equivariance_value'] = self.loss_weights[
                     'equivariance_value'] * value
 
             # jacobian loss part
             if self.loss_weights['equivariance_jacobian'] != 0:
-                jacobian_transformed = paddle.matmul(
-                    *broadcast(transform.jacobian(transformed_kp['value']),
-                               transformed_kp['jacobian']))
+                jacobian_transformed = paddle.matmul(*broadcast(
+                    transform.jacobian(transformed_kp['value']),
+                    transformed_kp['jacobian']))
                 normed_driving = paddle.inverse(kp_driving['jacobian'])
                 normed_transformed = jacobian_transformed
                 value = paddle.matmul(
@@ -159,6 +159,7 @@ class VGG19(nn.Layer):
     """
     Vgg19 network for perceptual loss. See Sec 3.3.
     """
+
     def __init__(self, requires_grad=False):
         super(VGG19, self).__init__()
         pretrained_url = 'https://paddlegan.bj.bcebos.com/models/vgg19.pdparams'
@@ -209,10 +210,10 @@ class Transform:
     """
     Random tps transformation for equivariance constraints. See Sec 3.3
     """
+
     def __init__(self, bs, **kwargs):
-        noise = paddle.distribution.Normal(loc=[0],
-                                           scale=[kwargs['sigma_affine']
-                                                  ]).sample([bs, 2, 3])
+        noise = paddle.distribution.Normal(
+            loc=[0], scale=[kwargs['sigma_affine']]).sample([bs, 2, 3])
         noise = noise.reshape((bs, 2, 3))
         self.theta = noise + paddle.tensor.eye(2, 3, dtype='float32').reshape(
             (1, 2, 3))
@@ -234,11 +235,12 @@ class Transform:
         grid = grid.reshape((1, frame.shape[2] * frame.shape[3], 2))
         grid = self.warp_coordinates(grid).reshape(
             (self.bs, frame.shape[2], frame.shape[3], 2))
-        return F.grid_sample(frame,
-                             grid,
-                             mode='bilinear',
-                             padding_mode='reflection',
-                             align_corners=True)
+        return F.grid_sample(
+            frame,
+            grid,
+            mode='bilinear',
+            padding_mode='reflection',
+            align_corners=True)
 
     def warp_coordinates(self, coordinates):
         theta = self.theta.astype('float32')
@@ -251,8 +253,8 @@ class Transform:
         theta_part_a = theta[:, :, :, :2]
         theta_part_b = theta[:, :, :, 2:]
 
-        transformed = paddle.fluid.layers.matmul(
-            *broadcast(theta_part_a, coordinates)) + theta_part_b  #M*p + m0
+        transformed = paddle.fluid.layers.matmul(*broadcast(
+            theta_part_a, coordinates)) + theta_part_b  #M*p + m0
         transformed = transformed.squeeze(-1)
         if self.tps:
             control_points = self.control_points.astype('float32')
@@ -272,12 +274,10 @@ class Transform:
     def jacobian(self, coordinates):
         new_coordinates = self.warp_coordinates(coordinates)
         assert len(new_coordinates.shape) == 3
-        grad_x = paddle.grad(new_coordinates[:, :, 0].sum(),
-                             coordinates,
-                             create_graph=True)
-        grad_y = paddle.grad(new_coordinates[:, :, 1].sum(),
-                             coordinates,
-                             create_graph=True)
+        grad_x = paddle.grad(
+            new_coordinates[:, :, 0].sum(), coordinates, create_graph=True)
+        grad_y = paddle.grad(
+            new_coordinates[:, :, 1].sum(), coordinates, create_graph=True)
         jacobian = paddle.concat(
             [grad_x[0].unsqueeze(-2), grad_y[0].unsqueeze(-2)], axis=-2)
         return jacobian

+ 49 - 48
paddlers/models/ppgan/models/generators/generator_starganv2.py

@@ -21,6 +21,7 @@ class AvgPool2D(nn.Layer):
     Peplace avg_pool2d because paddle.grad will cause avg_pool2d to report an error when training.
     In the future Paddle framework will supports avg_pool2d and remove this class.
     """
+
     def __init__(self):
         super(AvgPool2D, self).__init__()
         self.filter = paddle.to_tensor([[1, 1], [1, 1]], dtype='float32')
@@ -49,12 +50,10 @@ class ResBlk(nn.Layer):
         self.conv1 = nn.Conv2D(dim_in, dim_in, 3, 1, 1)
         self.conv2 = nn.Conv2D(dim_in, dim_out, 3, 1, 1)
         if self.normalize:
-            self.norm1 = nn.InstanceNorm2D(dim_in,
-                                           weight_attr=True,
-                                           bias_attr=True)
-            self.norm2 = nn.InstanceNorm2D(dim_in,
-                                           weight_attr=True,
-                                           bias_attr=True)
+            self.norm1 = nn.InstanceNorm2D(
+                dim_in, weight_attr=True, bias_attr=True)
+            self.norm2 = nn.InstanceNorm2D(
+                dim_in, weight_attr=True, bias_attr=True)
         if self.learned_sc:
             self.conv1x1 = nn.Conv2D(dim_in, dim_out, 1, 1, 0, bias_attr=False)
 
@@ -86,9 +85,8 @@ class ResBlk(nn.Layer):
 class AdaIN(nn.Layer):
     def __init__(self, style_dim, num_features):
         super().__init__()
-        self.norm = nn.InstanceNorm2D(num_features,
-                                      weight_attr=False,
-                                      bias_attr=False)
+        self.norm = nn.InstanceNorm2D(
+            num_features, weight_attr=False, bias_attr=False)
         self.fc = nn.Linear(style_dim, num_features * 2)
 
     def forward(self, x, s):
@@ -170,8 +168,10 @@ class StarGANv2Generator(nn.Layer):
         self.encode = nn.LayerList()
         self.decode = nn.LayerList()
         self.to_rgb = nn.Sequential(
-            nn.InstanceNorm2D(dim_in, weight_attr=True, bias_attr=True),
-            nn.LeakyReLU(0.2), nn.Conv2D(dim_in, 3, 1, 1, 0))
+            nn.InstanceNorm2D(
+                dim_in, weight_attr=True, bias_attr=True),
+            nn.LeakyReLU(0.2),
+            nn.Conv2D(dim_in, 3, 1, 1, 0))
 
         # down/up-sampling blocks
         repeat_num = int(np.log2(img_size)) - 4
@@ -180,28 +180,26 @@ class StarGANv2Generator(nn.Layer):
         for _ in range(repeat_num):
             dim_out = min(dim_in * 2, max_conv_dim)
             self.encode.append(
-                ResBlk(dim_in, dim_out, normalize=True, downsample=True))
+                ResBlk(
+                    dim_in, dim_out, normalize=True, downsample=True))
             if len(self.decode) == 0:
                 self.decode.append(
-                    AdainResBlk(dim_out,
-                                dim_in,
-                                style_dim,
-                                w_hpf=w_hpf,
-                                upsample=True))
+                    AdainResBlk(
+                        dim_out, dim_in, style_dim, w_hpf=w_hpf, upsample=True))
             else:
-                self.decode.insert(0,
-                                   AdainResBlk(dim_out,
-                                               dim_in,
-                                               style_dim,
-                                               w_hpf=w_hpf,
-                                               upsample=True))  # stack-like
+                self.decode.insert(
+                    0,
+                    AdainResBlk(
+                        dim_out, dim_in, style_dim, w_hpf=w_hpf,
+                        upsample=True))  # stack-like
             dim_in = dim_out
 
         # bottleneck blocks
         for _ in range(2):
             self.encode.append(ResBlk(dim_out, dim_out, normalize=True))
             self.decode.insert(
-                0, AdainResBlk(dim_out, dim_out, style_dim, w_hpf=w_hpf))
+                0, AdainResBlk(
+                    dim_out, dim_out, style_dim, w_hpf=w_hpf))
 
         if w_hpf > 0:
             self.hpf = HighPass(w_hpf)
@@ -217,9 +215,8 @@ class StarGANv2Generator(nn.Layer):
             x = block(x, s)
             if (masks is not None) and (x.shape[2] in [32, 64, 128]):
                 mask = masks[0] if x.shape[2] in [32] else masks[1]
-                mask = F.interpolate(mask,
-                                     size=[x.shape[2], x.shape[2]],
-                                     mode='bilinear')
+                mask = F.interpolate(
+                    mask, size=[x.shape[2], x.shape[2]], mode='bilinear')
                 x = x + self.hpf(mask * cache[x.shape[2]])
         return self.to_rgb(x)
 
@@ -239,10 +236,12 @@ class StarGANv2Mapping(nn.Layer):
         self.unshared = nn.LayerList()
         for _ in range(num_domains):
             self.unshared.append(
-                nn.Sequential(nn.Linear(512, 512),
-                              nn.ReLU(), nn.Linear(512, 512), nn.ReLU(),
-                              nn.Linear(512, 512), nn.ReLU(),
-                              nn.Linear(512, style_dim)))
+                nn.Sequential(
+                    nn.Linear(512, 512),
+                    nn.ReLU(),
+                    nn.Linear(512, 512),
+                    nn.ReLU(),
+                    nn.Linear(512, 512), nn.ReLU(), nn.Linear(512, style_dim)))
 
     def forward(self, z, y):
         h = self.shared(z)
@@ -254,8 +253,8 @@ class StarGANv2Mapping(nn.Layer):
         s = []
         for i in range(idx.shape[0]):
             s += [
-                out[idx[i].numpy().astype(np.int).tolist()[0],
-                    y[i].numpy().astype(np.int).tolist()[0]]
+                out[idx[i].numpy().astype(np.int).tolist()[0], y[i].numpy()
+                    .astype(np.int).tolist()[0]]
             ]
         s = paddle.stack(s)
         s = paddle.reshape(s, (s.shape[0], -1))
@@ -300,8 +299,8 @@ class StarGANv2Style(nn.Layer):
         s = []
         for i in range(idx.shape[0]):
             s += [
-                out[idx[i].numpy().astype(np.int).tolist()[0],
-                    y[i].numpy().astype(np.int).tolist()[0]]
+                out[idx[i].numpy().astype(np.int).tolist()[0], y[i].numpy()
+                    .astype(np.int).tolist()[0]]
             ]
         s = paddle.stack(s)
         s = paddle.reshape(s, (s.shape[0], -1))
@@ -320,15 +319,16 @@ class FAN(nn.Layer):
         self.end_relu = end_relu
 
         # Base part
-        self.conv1 = CoordConvTh(256,
-                                 256,
-                                 True,
-                                 False,
-                                 in_channels=3,
-                                 out_channels=64,
-                                 kernel_size=7,
-                                 stride=2,
-                                 padding=3)
+        self.conv1 = CoordConvTh(
+            256,
+            256,
+            True,
+            False,
+            in_channels=3,
+            out_channels=64,
+            kernel_size=7,
+            stride=2,
+            padding=3)
         self.bn1 = nn.BatchNorm2D(64)
         self.conv2 = ConvBlock(64, 128)
         self.conv3 = ConvBlock(128, 128)
@@ -395,9 +395,10 @@ class FAN(nn.Layer):
         heatmaps = outputs[-1][:, :-1, :, :]
         scale_factor = x.shape[2] // heatmaps.shape[2]
         if b_preprocess:
-            heatmaps = F.interpolate(heatmaps,
-                                     scale_factor=scale_factor,
-                                     mode='bilinear',
-                                     align_corners=True)
+            heatmaps = F.interpolate(
+                heatmaps,
+                scale_factor=scale_factor,
+                mode='bilinear',
+                align_corners=True)
             heatmaps = preprocess(heatmaps)
         return heatmaps

+ 20 - 31
paddlers/models/ppgan/models/generators/iconvsr.py

@@ -45,6 +45,7 @@ class IconVSR(nn.Layer):
             then the (0, 5, 10, 15, ...)-th frame will be the keyframes.
             Default: 5.
     """
+
     def __init__(self,
                  mid_channels=64,
                  num_blocks=30,
@@ -64,25 +65,17 @@ class IconVSR(nn.Layer):
         self.spynet.set_state_dict(paddle.load(weight_path))
 
         # information-refill
-        self.edvr = EDVRFeatureExtractor(num_frames=padding * 2 + 1,
-                                         center_frame_idx=padding)
+        self.edvr = EDVRFeatureExtractor(
+            num_frames=padding * 2 + 1, center_frame_idx=padding)
 
         edvr_wight_path = get_path_from_url(
             'https://paddlegan.bj.bcebos.com/models/edvrm.pdparams')
         self.edvr.set_state_dict(paddle.load(edvr_wight_path))
 
-        self.backward_fusion = nn.Conv2D(2 * mid_channels,
-                                         mid_channels,
-                                         3,
-                                         1,
-                                         1,
-                                         bias_attr=True)
-        self.forward_fusion = nn.Conv2D(2 * mid_channels,
-                                        mid_channels,
-                                        3,
-                                        1,
-                                        1,
-                                        bias_attr=True)
+        self.backward_fusion = nn.Conv2D(
+            2 * mid_channels, mid_channels, 3, 1, 1, bias_attr=True)
+        self.forward_fusion = nn.Conv2D(
+            2 * mid_channels, mid_channels, 3, 1, 1, bias_attr=True)
 
         # propagation branches
         self.backward_resblocks = ResidualBlocksWithInputConv(
@@ -92,19 +85,14 @@ class IconVSR(nn.Layer):
 
         # upsample
         # self.fusion = nn.Conv2D(mid_channels * 2, mid_channels, 1, 1, 0)
-        self.upsample1 = PixelShufflePack(mid_channels,
-                                          mid_channels,
-                                          2,
-                                          upsample_kernel=3)
-        self.upsample2 = PixelShufflePack(mid_channels,
-                                          64,
-                                          2,
-                                          upsample_kernel=3)
+        self.upsample1 = PixelShufflePack(
+            mid_channels, mid_channels, 2, upsample_kernel=3)
+        self.upsample2 = PixelShufflePack(
+            mid_channels, 64, 2, upsample_kernel=3)
         self.conv_hr = nn.Conv2D(64, 64, 3, 1, 1)
         self.conv_last = nn.Conv2D(64, 3, 3, 1, 1)
-        self.img_upsample = nn.Upsample(scale_factor=4,
-                                        mode='bilinear',
-                                        align_corners=False)
+        self.img_upsample = nn.Upsample(
+            scale_factor=4, mode='bilinear', align_corners=False)
 
         # activation function
         self.lrelu = nn.LeakyReLU(negative_slope=0.1)
@@ -319,6 +307,7 @@ class EDVRFeatureExtractor(nn.Layer):
             0. Default: 2.
         with_tsa (bool): Whether to use TSA module. Default: True.
     """
+
     def __init__(self,
                  in_channels=3,
                  out_channel=3,
@@ -336,9 +325,8 @@ class EDVRFeatureExtractor(nn.Layer):
         self.with_tsa = with_tsa
 
         self.conv_first = nn.Conv2D(in_channels, mid_channels, 3, 1, 1)
-        self.feature_extraction = make_layer(ResidualBlockNoBN,
-                                             num_blocks_extraction,
-                                             nf=mid_channels)
+        self.feature_extraction = make_layer(
+            ResidualBlockNoBN, num_blocks_extraction, nf=mid_channels)
 
         # generate pyramid features
         self.feat_l2_conv1 = nn.Conv2D(mid_channels, mid_channels, 3, 2, 1)
@@ -350,9 +338,10 @@ class EDVRFeatureExtractor(nn.Layer):
         self.pcd_alignment = PCDAlign(nf=mid_channels, groups=deform_groups)
         # fusion
         if self.with_tsa:
-            self.fusion = TSAFusion(nf=mid_channels,
-                                    nframes=num_frames,
-                                    center=self.center_frame_idx)
+            self.fusion = TSAFusion(
+                nf=mid_channels,
+                nframes=num_frames,
+                center=self.center_frame_idx)
         else:
             self.fusion = nn.Conv2D(num_frames * mid_channels, mid_channels, 1,
                                     1)

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor