├── python ├── ppgan ├── apps │ └── midas │ │ ├── __init__.py │ │ └── README.md ├── faceutils │ ├── face_detection │ │ ├── detection │ │ │ ├── __init__.py │ │ │ ├── sfd │ │ │ │ └── __init__.py │ │ │ └── blazeface │ │ │ │ ├── __init__.py │ │ │ │ └── utils.py │ │ └── __init__.py │ ├── mask │ │ └── __init__.py │ ├── face_segmentation │ │ ├── __init__.py │ │ └── layers.py │ ├── face_enhancement │ │ └── __init__.py │ ├── dlibutils │ │ └── __init__.py │ ├── __init__.py │ └── image.py ├── datasets │ ├── preprocess │ │ └── __init__.py │ ├── empty_dataset.py │ ├── single_dataset.py │ ├── paired_dataset.py │ ├── repeat_dataset.py │ └── __init__.py ├── engine │ └── __init__.py ├── modules │ ├── __init__.py │ ├── fused_act.py │ └── conv.py ├── utils │ ├── __init__.py │ ├── audio_config.py │ ├── timer.py │ ├── filesystem.py │ ├── animate.py │ └── video.py ├── models │ ├── criterions │ │ ├── __init__.py │ │ ├── builder.py │ │ └── gradient_penalty.py │ ├── backbones │ │ └── __init__.py │ ├── builder.py │ ├── generators │ │ └── builder.py │ ├── discriminators │ │ ├── builder.py │ │ ├── discriminator_singan.py │ │ ├── __init__.py │ │ ├── discriminator_starganv2.py │ │ ├── discriminator_aotgan.py │ │ └── discriminator_lapstyle.py │ └── __init__.py ├── version.py ├── __init__.py ├── metrics │ ├── __init__.py │ └── builder.py └── solver │ ├── __init__.py │ ├── optimizer.py │ └── builder.py ├── docs ├── zh_CN │ ├── contribute.md │ ├── tutorials │ │ ├── face_parse.md │ │ ├── face_enhancement.md │ │ ├── ugatit.md │ │ ├── pixel2style2pixel.md │ │ └── starganv2.md │ ├── industrial_solution │ │ ├── video_restore_cn.md │ │ ├── photo_color_cn.md │ │ └── video_frame_cn.md │ └── install.md ├── imgs │ ├── 1.jpg │ ├── A2B.png │ ├── B2A.png │ ├── qq.png │ ├── RSSR.png │ ├── face.png │ ├── fom_dv.mp4 │ ├── mona7s.mp4 │ ├── ppgan.jpg │ ├── cyclegan.png │ ├── fom_demo.png │ ├── monarch.png │ ├── pix2pix.png │ ├── father_23.jpg │ ├── guangquan.m4a │ ├── horse2zebra.png │ ├── mayiyahei.MP4 │ ├── pSp-input.jpg │ ├── pSp-teaser.jpg │ ├── pSp-toonify.png │ ├── ps_source.png │ ├── psgan_arc.png │ ├── ref │ │ └── ps_ref.png │ ├── pSp-inversion.png │ ├── animeganv2_res.jpg │ ├── animeganv2_test.jpg │ ├── face_parse_out.png │ ├── fom_512_vs_256.png │ ├── fom_source_image.png │ ├── pSp-input-crop.png │ ├── stylegan2-sample.png │ ├── photo2cartoon_pipeline.png │ ├── stylegan2fitting-sample.png │ ├── stylegan2mixing-sample.png │ ├── photo2cartoon_data_process.jpg │ ├── stylegan2-sample-mixing-0.png │ ├── stylegan2-teaser-1024x256.png │ ├── stylegan2editing-sample1.png │ ├── stylegan2editing-sample2.png │ ├── stylegan2fitting-sample2.png │ └── fom_source_image_multi_person.jpg ├── luckydraw.md └── en_US │ └── tutorials │ ├── face_parse.md │ └── ugatit.md ├── .style.yapf ├── test_tipc ├── results │ ├── python_fom_results_fp32.txt │ ├── python_singan_results_fp32.txt │ ├── python_cyclegan_results_fp32.txt │ ├── python_pix2pix_results_fp32.txt │ ├── python_stylegan2_results_fp32.txt │ ├── python_basicvsr_results_fp32.txt │ └── python_msvsr_results_fp32.txt ├── docs │ ├── test.png │ ├── compare_right.png │ ├── compare_wrong.png │ ├── test_inference_cpp.md │ └── benchmark_train.md ├── configs │ ├── msvsr │ │ ├── model_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt │ │ ├── train_linux_gpu_normal_amp_infer_python_linux_gpu_cpu.txt │ │ └── train_infer_python.txt │ ├── edvr │ │ ├── train_linux_gpu_normal_amp_infer_python_linux_gpu_cpu.txt │ │ └── train_infer_python.txt │ ├── invdn │ │ └── train_infer_python.txt │ ├── nafnet │ │ └── train_infer_python.txt │ ├── swinir │ │ └── train_infer_python.txt │ ├── singan │ │ └── train_infer_python.txt │ ├── GFPGAN │ │ └── train_infer_python.txt │ ├── aotgan │ │ └── train_infer_python.txt │ ├── esrgan │ │ └── train_infer_python.txt │ ├── StyleGANv2 │ │ └── train_infer_python.txt │ ├── FOMM │ │ └── train_infer_python.txt │ ├── basicvsr │ │ └── train_infer_python.txt │ ├── Pix2pix │ │ └── train_infer_python.txt │ └── CycleGAN │ │ └── train_infer_python.txt ├── common_func.sh ├── test_train_inference_python_npu.sh ├── test_train_inference_python_xpu.sh └── compare_results.py ├── test ├── sem.png ├── pic_.jpg ├── coco_stuff │ ├── test_inst │ │ ├── ADE_train_00000569.png │ │ ├── ADE_train_00000583.png │ │ ├── ADE_train_00000955.png │ │ └── ADE_train_00002089.png │ ├── train_img │ │ ├── ADE_train_00000569.jpg │ │ ├── ADE_train_00000583.jpg │ │ ├── ADE_train_00000955.jpg │ │ └── ADE_train_00002089.jpg │ └── train_inst │ │ ├── ADE_train_00000569.png │ │ ├── ADE_train_00000583.png │ │ ├── ADE_train_00000955.png │ │ └── ADE_train_00002089.png └── readme.txt ├── paddlegan-wechaty-demo └── requirements.txt ├── requirements.txt ├── data ├── realsr_preprocess │ └── paths.yml ├── download_pix2pix_data.py └── download_cyclegan_data.py ├── deploy ├── cpp_infer │ ├── include │ │ ├── process_op.h │ │ └── vsr.h │ └── src │ │ └── process_op.cc └── export_model.md ├── benchmark ├── prepare.sh ├── benchmark.yaml ├── README.md └── analysis_log.py ├── tools ├── psgan_infer.py ├── extract_weight.py ├── main.py └── animegan_picmean.py ├── applications └── tools │ ├── face_parse.py │ ├── animeganv2.py │ ├── mprnet.py │ ├── nafnet_denoising.py │ ├── swinir_denoising.py │ └── photopen.py ├── .pre-commit-config.yaml ├── configs ├── wav2lip.yaml ├── mprnet_denoising.yaml ├── mprnet_deblurring.yaml ├── aotgan.yaml ├── gpen_256_ffhq.yaml ├── lapstyle_draft.yaml ├── mprnet_deraining.yaml ├── wgan_mnist.yaml ├── wav2lip_hq.yaml ├── makeup.yaml ├── singan_finetune.yaml ├── nafnet_denoising.yaml ├── cond_dcgan_mnist.yaml ├── dcgan_mnist.yaml ├── singan_animation.yaml ├── singan_universal.yaml ├── singan_sr.yaml ├── invdn_denoising.yaml ├── lapstyle_rev_first.yaml ├── lapstyle_rev_second.yaml ├── swinir_denoising.yaml ├── photopen.yaml └── animeganv2_pretrain.yaml ├── education └── homework1.md ├── .gitignore └── setup.py /python: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ppgan/apps/midas/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/zh_CN/contribute.md: -------------------------------------------------------------------------------- 1 | ../en_US/contribute.md -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | based_on_style = pep8 3 | column_limit = 80 -------------------------------------------------------------------------------- /test_tipc/results/python_fom_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric l1 loss: 0.1210 2 | -------------------------------------------------------------------------------- /test_tipc/results/python_singan_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric fid: 124.0369 2 | -------------------------------------------------------------------------------- /test_tipc/results/python_cyclegan_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric fid: 67.9814 2 | -------------------------------------------------------------------------------- /test_tipc/results/python_pix2pix_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric fid: 139.3846 2 | -------------------------------------------------------------------------------- /test_tipc/results/python_stylegan2_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric fid: 153.9647 2 | -------------------------------------------------------------------------------- /test/sem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/sem.png -------------------------------------------------------------------------------- /docs/imgs/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/1.jpg -------------------------------------------------------------------------------- /ppgan/faceutils/face_detection/detection/__init__.py: -------------------------------------------------------------------------------- 1 | from .core import FaceDetector 2 | -------------------------------------------------------------------------------- /test/pic_.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/pic_.jpg -------------------------------------------------------------------------------- /docs/imgs/A2B.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/A2B.png -------------------------------------------------------------------------------- /docs/imgs/B2A.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/B2A.png -------------------------------------------------------------------------------- /docs/imgs/qq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/qq.png -------------------------------------------------------------------------------- /docs/imgs/RSSR.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/RSSR.png -------------------------------------------------------------------------------- /docs/imgs/face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/face.png -------------------------------------------------------------------------------- /docs/imgs/fom_dv.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/fom_dv.mp4 -------------------------------------------------------------------------------- /docs/imgs/mona7s.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/mona7s.mp4 -------------------------------------------------------------------------------- /docs/imgs/ppgan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/ppgan.jpg -------------------------------------------------------------------------------- /docs/imgs/cyclegan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/cyclegan.png -------------------------------------------------------------------------------- /docs/imgs/fom_demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/fom_demo.png -------------------------------------------------------------------------------- /docs/imgs/monarch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/monarch.png -------------------------------------------------------------------------------- /docs/imgs/pix2pix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pix2pix.png -------------------------------------------------------------------------------- /test_tipc/results/python_basicvsr_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric psnr: 27.0864 2 | Metric ssim: 0.7835 3 | -------------------------------------------------------------------------------- /test_tipc/results/python_msvsr_results_fp32.txt: -------------------------------------------------------------------------------- 1 | Metric psnr: 23.6020 2 | Metric ssim: 0.5636 3 | 4 | -------------------------------------------------------------------------------- /docs/imgs/father_23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/father_23.jpg -------------------------------------------------------------------------------- /docs/imgs/guangquan.m4a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/guangquan.m4a -------------------------------------------------------------------------------- /docs/imgs/horse2zebra.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/horse2zebra.png -------------------------------------------------------------------------------- /docs/imgs/mayiyahei.MP4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/mayiyahei.MP4 -------------------------------------------------------------------------------- /docs/imgs/pSp-input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pSp-input.jpg -------------------------------------------------------------------------------- /docs/imgs/pSp-teaser.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pSp-teaser.jpg -------------------------------------------------------------------------------- /docs/imgs/pSp-toonify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pSp-toonify.png -------------------------------------------------------------------------------- /docs/imgs/ps_source.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/ps_source.png -------------------------------------------------------------------------------- /docs/imgs/psgan_arc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/psgan_arc.png -------------------------------------------------------------------------------- /docs/imgs/ref/ps_ref.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/ref/ps_ref.png -------------------------------------------------------------------------------- /paddlegan-wechaty-demo/requirements.txt: -------------------------------------------------------------------------------- 1 | paddlepaddle >= 2.1.0 2 | ppgan >= 2.0.0 3 | wechaty ~= 0.7dev16 4 | -------------------------------------------------------------------------------- /ppgan/faceutils/face_detection/__init__.py: -------------------------------------------------------------------------------- 1 | from .api import FaceAlignment, LandmarksType, NetworkSize 2 | -------------------------------------------------------------------------------- /test_tipc/docs/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test_tipc/docs/test.png -------------------------------------------------------------------------------- /docs/imgs/pSp-inversion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pSp-inversion.png -------------------------------------------------------------------------------- /docs/imgs/animeganv2_res.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/animeganv2_res.jpg -------------------------------------------------------------------------------- /docs/imgs/animeganv2_test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/animeganv2_test.jpg -------------------------------------------------------------------------------- /docs/imgs/face_parse_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/face_parse_out.png -------------------------------------------------------------------------------- /docs/imgs/fom_512_vs_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/fom_512_vs_256.png -------------------------------------------------------------------------------- /docs/imgs/fom_source_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/fom_source_image.png -------------------------------------------------------------------------------- /docs/imgs/pSp-input-crop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/pSp-input-crop.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2-sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2-sample.png -------------------------------------------------------------------------------- /ppgan/faceutils/face_detection/detection/sfd/__init__.py: -------------------------------------------------------------------------------- 1 | from .sfd_detector import SFDDetector as FaceDetector 2 | -------------------------------------------------------------------------------- /test_tipc/docs/compare_right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test_tipc/docs/compare_right.png -------------------------------------------------------------------------------- /test_tipc/docs/compare_wrong.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test_tipc/docs/compare_wrong.png -------------------------------------------------------------------------------- /docs/imgs/photo2cartoon_pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/photo2cartoon_pipeline.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2fitting-sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2fitting-sample.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2mixing-sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2mixing-sample.png -------------------------------------------------------------------------------- /ppgan/faceutils/face_detection/detection/blazeface/__init__.py: -------------------------------------------------------------------------------- 1 | from .blazeface_detector import BlazeFaceDetector as FaceDetector 2 | -------------------------------------------------------------------------------- /docs/imgs/photo2cartoon_data_process.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/photo2cartoon_data_process.jpg -------------------------------------------------------------------------------- /docs/imgs/stylegan2-sample-mixing-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2-sample-mixing-0.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2-teaser-1024x256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2-teaser-1024x256.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2editing-sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2editing-sample1.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2editing-sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2editing-sample2.png -------------------------------------------------------------------------------- /docs/imgs/stylegan2fitting-sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/stylegan2fitting-sample2.png -------------------------------------------------------------------------------- /docs/imgs/fom_source_image_multi_person.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/docs/imgs/fom_source_image_multi_person.jpg -------------------------------------------------------------------------------- /test/coco_stuff/test_inst/ADE_train_00000569.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/test_inst/ADE_train_00000569.png -------------------------------------------------------------------------------- /test/coco_stuff/test_inst/ADE_train_00000583.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/test_inst/ADE_train_00000583.png -------------------------------------------------------------------------------- /test/coco_stuff/test_inst/ADE_train_00000955.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/test_inst/ADE_train_00000955.png -------------------------------------------------------------------------------- /test/coco_stuff/test_inst/ADE_train_00002089.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/test_inst/ADE_train_00002089.png -------------------------------------------------------------------------------- /test/coco_stuff/train_img/ADE_train_00000569.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_img/ADE_train_00000569.jpg -------------------------------------------------------------------------------- /test/coco_stuff/train_img/ADE_train_00000583.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_img/ADE_train_00000583.jpg -------------------------------------------------------------------------------- /test/coco_stuff/train_img/ADE_train_00000955.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_img/ADE_train_00000955.jpg -------------------------------------------------------------------------------- /test/coco_stuff/train_img/ADE_train_00002089.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_img/ADE_train_00002089.jpg -------------------------------------------------------------------------------- /test/coco_stuff/train_inst/ADE_train_00000569.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_inst/ADE_train_00000569.png -------------------------------------------------------------------------------- /test/coco_stuff/train_inst/ADE_train_00000583.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_inst/ADE_train_00000583.png -------------------------------------------------------------------------------- /test/coco_stuff/train_inst/ADE_train_00000955.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_inst/ADE_train_00000955.png -------------------------------------------------------------------------------- /test/coco_stuff/train_inst/ADE_train_00002089.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaddlePaddle/PaddleGAN/HEAD/test/coco_stuff/train_inst/ADE_train_00002089.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tqdm 2 | PyYAML>=5.1 3 | scikit-image>=0.14.0 4 | scipy>=1.1.0 5 | opencv-python<=4.6.0.66 6 | imageio==2.9.0 7 | imageio-ffmpeg 8 | librosa==0.8.1 9 | numba 10 | easydict 11 | munch 12 | natsort 13 | matplotlib 14 | -------------------------------------------------------------------------------- /data/realsr_preprocess/paths.yml: -------------------------------------------------------------------------------- 1 | df2k: 2 | tdsr: 3 | source: './data/ntire20/Corrupted-tr-x' 4 | target: './data/ntire20/Corrupted-tr-y' 5 | valid: 6 | dped: 7 | clean: 8 | hr: 9 | train: './data/ntire20/DPEDiphone-tr-x' 10 | valid: './data/ntire20/DPEDiphone-va' 11 | datasets: 12 | df2k: 'data/DF2K' 13 | dped: 'data/DPED' 14 | -------------------------------------------------------------------------------- /ppgan/apps/midas/README.md: -------------------------------------------------------------------------------- 1 | ## Monocular Depth Estimation 2 | 3 | 4 | The implemention of MiDasv2 refers to https://github.com/intel-isl/MiDaS. 5 | 6 | 7 | @article{Ranftl2020, 8 | author = {Ren\'{e} Ranftl and Katrin Lasinger and David Hafner and Konrad Schindler and Vladlen Koltun}, 9 | title = {Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer}, 10 | journal = {IEEE Transactions on Pattern Analysis and Machine Intelligence (TPAMI)}, 11 | year = {2020}, 12 | } 13 | -------------------------------------------------------------------------------- /test_tipc/configs/msvsr/model_linux_gpu_normal_normal_infer_cpp_linux_gpu_cpu.txt: -------------------------------------------------------------------------------- 1 | ===========================cpp_infer_params=========================== 2 | model_name:msvsr 3 | inference:./deploy/cpp_infer/build/vsr 4 | --infer_model_path:./inference/msvsr/multistagevsrmodel_generator.pdmodel 5 | --infer_param_path:./inference/msvsr/multistagevsrmodel_generator.pdiparams 6 | --video_path:./data/low_res.mp4 7 | --output_dir:./test_tipc/output/msvsr 8 | --frame_num:2 9 | --device:GPU 10 | --gpu_id:1 11 | --use_mkldnn:True 12 | --cpu_threads:1 13 | -------------------------------------------------------------------------------- /ppgan/datasets/preprocess/__init__.py: -------------------------------------------------------------------------------- 1 | from .io import LoadImageFromFile, ReadImageSequence, GetNeighboringFramesIdx, GetFrameIdx, GetFrameIdxwithPadding 2 | from .transforms import (PairedRandomCrop, PairedRandomHorizontalFlip, 3 | PairedRandomVerticalFlip, PairedRandomTransposeHW, 4 | SRPairedRandomCrop, SplitPairedImage, SRNoise, 5 | NormalizeSequence, MirrorVideoSequence, 6 | TransposeSequence, PairedToTensor) 7 | 8 | from .builder import build_preprocess 9 | -------------------------------------------------------------------------------- /ppgan/engine/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /ppgan/modules/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /ppgan/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /ppgan/models/criterions/__init__.py: -------------------------------------------------------------------------------- 1 | from .gan_loss import GANLoss 2 | from .perceptual_loss import PerceptualLoss 3 | from .pixel_loss import L1Loss, MSELoss, CharbonnierLoss, \ 4 | CalcStyleEmdLoss, CalcContentReltLoss, \ 5 | CalcContentLoss, CalcStyleLoss, EdgeLoss, PSNRLoss 6 | from .photopen_perceptual_loss import PhotoPenPerceptualLoss 7 | from .gradient_penalty import GradientPenalty 8 | 9 | from .builder import build_criterion 10 | 11 | from .ssim import SSIM 12 | from .id_loss import IDLoss 13 | from .gfpgan_loss import GFPGANGANLoss, GFPGANL1Loss, GFPGANPerceptualLoss 14 | from .aotgan_perceptual_loss import AOTGANCriterionLoss 15 | -------------------------------------------------------------------------------- /deploy/cpp_infer/include/process_op.h: -------------------------------------------------------------------------------- 1 | #include "opencv2/core.hpp" 2 | #include "opencv2/imgcodecs.hpp" 3 | #include "opencv2/imgproc.hpp" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | class Normalize { 17 | public: 18 | virtual void Run(cv::Mat *im, const std::vector &mean, 19 | const std::vector &scale, const bool is_scale = true); 20 | }; 21 | 22 | 23 | // RGB -> CHW 24 | class Permute { 25 | public: 26 | virtual void Run(const cv::Mat *im, float *data); 27 | }; 28 | -------------------------------------------------------------------------------- /ppgan/models/backbones/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | -------------------------------------------------------------------------------- /ppgan/version.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | ppgan_version = '2.1.0' 16 | -------------------------------------------------------------------------------- /ppgan/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .version import ppgan_version as __version__ 16 | -------------------------------------------------------------------------------- /ppgan/faceutils/mask/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .face_parser import FaceParser 16 | -------------------------------------------------------------------------------- /ppgan/faceutils/face_segmentation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .face_seg import FaceSeg 16 | -------------------------------------------------------------------------------- /ppgan/faceutils/face_enhancement/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .face_enhance import FaceEnhancement 16 | -------------------------------------------------------------------------------- /ppgan/faceutils/dlibutils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .dlib_utils import detect, crop, landmarks, crop_from_array 16 | from .face_align import align_crop 17 | -------------------------------------------------------------------------------- /ppgan/metrics/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .psnr_ssim import PSNR, SSIM 16 | from .fid import FID 17 | from .lpips import LPIPSMetric 18 | from .builder import build_metric 19 | -------------------------------------------------------------------------------- /ppgan/utils/audio_config.py: -------------------------------------------------------------------------------- 1 | from .config import AttrDict 2 | 3 | _audio_cfg = AttrDict() 4 | 5 | _audio_cfg.num_mels = 80 6 | _audio_cfg.rescale = True 7 | _audio_cfg.rescaling_max = 0.9 8 | _audio_cfg.use_lws = False 9 | _audio_cfg.n_fft = 800 10 | _audio_cfg.hop_size = 200 11 | _audio_cfg.win_size = 800 12 | _audio_cfg.sample_rate = 16000 13 | _audio_cfg.frame_shift_ms = None 14 | _audio_cfg.signal_normalization = True 15 | _audio_cfg.allow_clipping_in_normalization = True 16 | _audio_cfg.symmetric_mels = True 17 | _audio_cfg.max_abs_value = 4. 18 | _audio_cfg.preemphasize = True 19 | _audio_cfg.preemphasis = 0.97 20 | _audio_cfg.min_level_db = -100 21 | _audio_cfg.ref_level_db = 20 22 | _audio_cfg.fmin = 55 23 | _audio_cfg.fmax = 7600 24 | _audio_cfg.fps = 25 25 | 26 | 27 | def get_audio_config(): 28 | return _audio_cfg 29 | -------------------------------------------------------------------------------- /ppgan/faceutils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from . import dlibutils as dlib 16 | from . import mask 17 | from . import image 18 | from . import face_segmentation 19 | from . import face_detection 20 | -------------------------------------------------------------------------------- /test_tipc/configs/edvr/train_linux_gpu_normal_amp_infer_python_linux_gpu_cpu.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:edvr 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=100 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=4 10 | pretrained_model:null 11 | train_model_name:basicvsr_reds*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/basicvsr_reds/test 13 | null:null 14 | ## 15 | trainer:amp_train 16 | amp_train:tools/main.py --amp --amp_level O2 -c configs/edvr_m_wo_tsa.yaml --seed 123 -o log_config.interval=5 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | -------------------------------------------------------------------------------- /benchmark/prepare.sh: -------------------------------------------------------------------------------- 1 | 2 | #!usr/bin/env bash 3 | 4 | export BENCHMARK_ROOT=/workspace 5 | run_env=$BENCHMARK_ROOT/run_env 6 | log_date=`date "+%Y.%m%d.%H%M%S"` 7 | frame=paddle2.1.3 8 | cuda_version=10.2 9 | save_log_dir=${BENCHMARK_ROOT}/logs/${frame}_${log_date}_${cuda_version}/ 10 | 11 | if [[ -d ${save_log_dir} ]]; then 12 | rm -rf ${save_log_dir} 13 | fi 14 | 15 | # this for update the log_path coding mat 16 | export TRAIN_LOG_DIR=${save_log_dir}/train_log 17 | mkdir -p ${TRAIN_LOG_DIR} 18 | log_path=${TRAIN_LOG_DIR} 19 | 20 | ################################# 配置python, 如: 21 | rm -rf $run_env 22 | mkdir $run_env 23 | echo `which python3.7` 24 | ln -s $(which python3.7)m-config $run_env/python3-config 25 | ln -s $(which python3.7) $run_env/python 26 | ln -s $(which pip3.7) $run_env/pip 27 | 28 | export PATH=$run_env:${PATH} 29 | cd $BENCHMARK_ROOT 30 | pip install -v -e . 31 | -------------------------------------------------------------------------------- /ppgan/solver/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .lr_scheduler import CosineAnnealingRestartLR, LinearDecay, NonLinearDecay 16 | from .optimizer import * 17 | from .builder import build_lr_scheduler 18 | from .builder import build_optimizer 19 | -------------------------------------------------------------------------------- /data/download_pix2pix_data.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | 4 | from ppgan.utils.download import get_path_from_url 5 | 6 | PIX2PIX_URL_ROOT = 'http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/' 7 | 8 | parser = argparse.ArgumentParser(description='download datasets') 9 | parser.add_argument('--name', 10 | type=str, 11 | required=True, 12 | help='dataset name, \ 13 | support dataset name: cityscapes, night2day, edges2handbags, \ 14 | edges2shoes, facades, maps') 15 | 16 | if __name__ == "__main__": 17 | args = parser.parse_args() 18 | 19 | data_url = PIX2PIX_URL_ROOT + args.name + '.tar.gz' 20 | 21 | path = get_path_from_url(data_url) 22 | 23 | dst = os.path.join('data', args.name) 24 | print('symlink {} to {}'.format(path, dst)) 25 | os.symlink(path, dst) 26 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/face_parse.md: -------------------------------------------------------------------------------- 1 | # 人脸解析 2 | 3 | ## 1. 人脸解析简介 4 | 5 | 人脸解析是语义图像分割的一种特殊情况,人脸解析是计算人脸图像中不同语义成分(如头发、嘴唇、鼻子、眼睛等)的像素级标签映射。给定一个输入的人脸图像,人脸解析将为每个语义成分分配一个像素级标签。我们利用BiseNet来解决这个问题。人脸解析工具在很多任务中都有应用,如识别,动画以及合成等。这个工具我们目前应用在换妆模型上。 6 | ## 2. 使用方法 7 | 8 | ### 2.1 测试 9 | 10 | 运行如下命令,可以完成人脸解析任务,程序运行成功后,会在`output`文件夹生成解析后的图片文件。具体命令如下所示: 11 | ``` 12 | cd applications 13 | python tools/face_parse.py --input_image ../docs/imgs/face.png 14 | ``` 15 | 16 | **参数:** 17 | 18 | - input_image: 输入待解析的图片文件路径 19 | 20 | ## 3. 结果展示 21 | ![](../../imgs/face_parse_out.png) 22 | 23 | ### 4. 参考文献 24 | 25 | ``` 26 | @misc{yu2018bisenet, 27 | title={BiSeNet: Bilateral Segmentation Network for Real-time Semantic Segmentation}, 28 | author={Changqian Yu and Jingbo Wang and Chao Peng and Changxin Gao and Gang Yu and Nong Sang}, 29 | year={2018}, 30 | eprint={1808.00897}, 31 | archivePrefix={arXiv}, 32 | primaryClass={cs.CV} 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /ppgan/models/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import copy 16 | import paddle 17 | 18 | from ..utils.registry import Registry 19 | 20 | MODELS = Registry("MODEL") 21 | 22 | 23 | def build_model(cfg): 24 | cfg_ = cfg.copy() 25 | name = cfg_.pop('name', None) 26 | model = MODELS.get(name)(**cfg_) 27 | return model 28 | -------------------------------------------------------------------------------- /ppgan/metrics/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import copy 16 | import paddle 17 | 18 | from ..utils.registry import Registry 19 | 20 | METRICS = Registry("METRIC") 21 | 22 | 23 | def build_metric(cfg): 24 | cfg_ = cfg.copy() 25 | name = cfg_.pop('name', None) 26 | metric = METRICS.get(name)(**cfg_) 27 | return metric 28 | -------------------------------------------------------------------------------- /ppgan/models/generators/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import copy 16 | from ...utils.registry import Registry 17 | 18 | GENERATORS = Registry("GENERATOR") 19 | 20 | 21 | def build_generator(cfg): 22 | cfg_copy = copy.deepcopy(cfg) 23 | name = cfg_copy.pop('name') 24 | generator = GENERATORS.get(name)(**cfg_copy) 25 | return generator 26 | -------------------------------------------------------------------------------- /ppgan/solver/optimizer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import copy 16 | import paddle 17 | 18 | from .builder import OPTIMIZERS 19 | 20 | OPTIMIZERS.register(paddle.optimizer.Adam) 21 | OPTIMIZERS.register(paddle.optimizer.SGD) 22 | OPTIMIZERS.register(paddle.optimizer.Momentum) 23 | OPTIMIZERS.register(paddle.optimizer.RMSProp) 24 | OPTIMIZERS.register(paddle.optimizer.AdamW) 25 | -------------------------------------------------------------------------------- /ppgan/models/discriminators/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import copy 16 | from ...utils.registry import Registry 17 | 18 | DISCRIMINATORS = Registry("DISCRIMINATOR") 19 | 20 | 21 | def build_discriminator(cfg): 22 | cfg_copy = copy.deepcopy(cfg) 23 | name = cfg_copy.pop('name') 24 | discriminator = DISCRIMINATORS.get(name)(**cfg_copy) 25 | return discriminator 26 | -------------------------------------------------------------------------------- /benchmark/benchmark.yaml: -------------------------------------------------------------------------------- 1 | StyleGANv2: 2 | dataset_web: https://paddlegan.bj.bcebos.com/datasets/ffhq_256.tar 3 | config: configs/stylegan_v2_256_ffhq.yaml 4 | fp_item: fp32 5 | bs_item: 8 6 | total_iters: 100 7 | log_interval: 5 8 | 9 | FOMM: 10 | dataset_web: https://paddlegan.bj.bcebos.com/datasets/fom_test_data.tar 11 | config: configs/firstorder_vox_256.yaml 12 | fp_item: fp32 13 | bs_item: 16 14 | epochs: 1 15 | log_interval: 1 16 | 17 | esrgan: 18 | dataset_web: https://paddlegan.bj.bcebos.com/datasets/DIV2KandSet14paddle.tar 19 | config: configs/esrgan_psnr_x4_div2k.yaml 20 | fp_item: fp32 21 | bs_item: 32 22 | total_iters: 300 23 | log_interval: 10 24 | 25 | edvr: 26 | dataset: data/REDS 27 | config: configs/edvr_m_wo_tsa.yaml 28 | fp_item: fp32 29 | bs_item: 4 30 | total_iters: 300 31 | log_interval: 10 32 | 33 | basicvsr: 34 | dataset: data/REDS 35 | config: configs/basicvsr_reds.yaml 36 | fp_item: fp32 37 | bs_item: 2 4 38 | total_iters: 300 39 | log_interval: 10 40 | -------------------------------------------------------------------------------- /test/readme.txt: -------------------------------------------------------------------------------- 1 | ## 本路径下所有文件均为 photopen 模型的训练过程、预测过程的测试使用。 2 | 3 | # 使用预训练模型预测 4 | python applications/tools/photopen.py --semantic_label_path test/sem.png --weight_path test/generator.pdparams --output_path output_dir/pic.jpg --config-file configs/photopen.yaml 5 | 6 | # 使用checkpoint预测 7 | python applications/tools/photopen.py --semantic_label_path test/sem.png --weight_path output_dir/photopen-2021-10-05-14-38/iter_1_weight.pdparams --output_path output_dir/pic.jpg --config-file configs/photopen.yaml 8 | 9 | 10 | # 训练 11 | python -u tools/main.py --config-file configs/photopen.yaml 12 | 13 | # 继续训练 14 | python -u tools/main.py --config-file configs/photopen.yaml --resume output_dir/photopen-2021-09-30-15-59/iter_3_checkpoint.pdparams 15 | 16 | # 训练,覆盖参数 17 | python -u tools/main.py --config-file configs/photopen.yaml --o model.generator.ngf=1 model.discriminator.ndf=1 18 | 19 | # 测试 20 | python -u tools/main.py --config-file configs/photopen.yaml --evaluate-only --load output_dir/photopen-2021-11-06-20-59/iter_1_checkpoint.pdparams 21 | -------------------------------------------------------------------------------- /ppgan/faceutils/image.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | import cv2 17 | from io import BytesIO 18 | 19 | 20 | def resize_by_max(image, max_side=512, force=False): 21 | h, w = image.shape[:2] 22 | if max(h, w) < max_side and not force: 23 | return image 24 | ratio = max(h, w) / max_side 25 | 26 | w = int(w / ratio + 0.5) 27 | h = int(h / ratio + 0.5) 28 | return cv2.resize(image, (w, h)) 29 | -------------------------------------------------------------------------------- /ppgan/datasets/empty_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .base_dataset import BaseDataset 16 | from .builder import DATASETS 17 | 18 | 19 | @DATASETS.register() 20 | class EmptyDataset(BaseDataset): 21 | ''' 22 | Dataset for models who don't need a dataset. 23 | ''' 24 | def __init__(self, size=1): 25 | super().__init__() 26 | self.size = size 27 | self.data_infos = self.prepare_data_infos() 28 | 29 | def prepare_data_infos(self): 30 | return [{i: 0} for i in range(self.size)] 31 | -------------------------------------------------------------------------------- /ppgan/solver/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from ..utils.registry import Registry 16 | 17 | LRSCHEDULERS = Registry("LRSCHEDULER") 18 | OPTIMIZERS = Registry("OPTIMIZER") 19 | 20 | 21 | def build_lr_scheduler(cfg): 22 | cfg_ = cfg.copy() 23 | name = cfg_.pop('name') 24 | return LRSCHEDULERS.get(name)(**cfg_) 25 | 26 | 27 | def build_optimizer(cfg, lr_scheduler, parameters=None): 28 | cfg_ = cfg.copy() 29 | name = cfg_.pop('name') 30 | return OPTIMIZERS.get(name)(lr_scheduler, parameters=parameters, **cfg_) 31 | -------------------------------------------------------------------------------- /data/download_cyclegan_data.py: -------------------------------------------------------------------------------- 1 | import os 2 | import argparse 3 | 4 | from ppgan.utils.download import get_path_from_url 5 | 6 | CYCLEGAN_URL_ROOT = 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/' 7 | 8 | parser = argparse.ArgumentParser(description='download datasets') 9 | parser.add_argument('--name', 10 | type=str, 11 | required=True, 12 | help='dataset name, \ 13 | support dataset name: apple2orange, summer2winter_yosemite, \ 14 | horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, \ 15 | vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, \ 16 | ae_photos, cityscapes') 17 | 18 | if __name__ == "__main__": 19 | args = parser.parse_args() 20 | 21 | data_url = CYCLEGAN_URL_ROOT + args.name + '.zip' 22 | 23 | if args.name == 'cityscapes': 24 | data_url = 'https://paddlegan.bj.bcebos.com/datasets/cityscapes.zip' 25 | 26 | path = get_path_from_url(data_url) 27 | 28 | dst = os.path.join('data', args.name) 29 | print('symlink {} to {}'.format(path, dst)) 30 | os.symlink(path, dst) 31 | -------------------------------------------------------------------------------- /ppgan/models/discriminators/discriminator_singan.py: -------------------------------------------------------------------------------- 1 | # code was based on https://github.com/tamarott/SinGAN 2 | 3 | import paddle.nn as nn 4 | 5 | from ..generators.generator_singan import ConvBlock 6 | from .builder import DISCRIMINATORS 7 | 8 | 9 | @DISCRIMINATORS.register() 10 | class SinGANDiscriminator(nn.Layer): 11 | def __init__(self, 12 | nfc=32, 13 | min_nfc=32, 14 | input_nc=3, 15 | num_layers=5, 16 | ker_size=3, 17 | padd_size=0): 18 | super(SinGANDiscriminator, self).__init__() 19 | self.head = ConvBlock(input_nc, nfc, ker_size, padd_size, 1) 20 | self.body = nn.Sequential() 21 | for i in range(num_layers - 2): 22 | N = int(nfc / pow(2, (i + 1))) 23 | block = ConvBlock(max(2 * N, min_nfc), max(N, min_nfc), ker_size, padd_size, 1) 24 | self.body.add_sublayer('block%d' % (i + 1), block) 25 | self.tail = nn.Conv2D(max(N, min_nfc), 1, ker_size, 1, padd_size) 26 | 27 | def forward(self, x): 28 | x = self.head(x) 29 | x = self.body(x) 30 | x = self.tail(x) 31 | return x 32 | -------------------------------------------------------------------------------- /tools/psgan_infer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | import sys 17 | import argparse 18 | 19 | cur_path = os.path.abspath(os.path.dirname(__file__)) 20 | root_path = os.path.split(cur_path)[0] 21 | sys.path.append(root_path) 22 | 23 | from ppgan.utils.options import parse_args 24 | from ppgan.utils.config import get_config 25 | from ppgan.apps.psgan_predictor import PSGANPredictor 26 | 27 | if __name__ == '__main__': 28 | args = parse_args() 29 | cfg = get_config(args.config_file) 30 | predictor = PSGANPredictor(args, cfg) 31 | predictor.run() 32 | -------------------------------------------------------------------------------- /applications/tools/face_parse.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import argparse 16 | 17 | import paddle 18 | from ppgan.apps.face_parse_predictor import FaceParsePredictor 19 | 20 | parser = argparse.ArgumentParser() 21 | parser.add_argument("--input_image", type=str, help="path to source image") 22 | 23 | parser.add_argument("--cpu", dest="cpu", action="store_true", help="cpu mode.") 24 | 25 | if __name__ == "__main__": 26 | args = parser.parse_args() 27 | 28 | if args.cpu: 29 | paddle.set_device('cpu') 30 | 31 | predictor = FaceParsePredictor() 32 | predictor.run(args.input_image) 33 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | - repo: local 2 | hooks: 3 | - id: yapf 4 | name: yapf 5 | entry: yapf 6 | language: system 7 | args: [-i, --style .style.yapf] 8 | files: \.py$ 9 | 10 | - repo: https://github.com/pre-commit/pre-commit-hooks 11 | sha: a11d9314b22d8f8c7556443875b731ef05965464 12 | hooks: 13 | - id: check-merge-conflict 14 | - id: check-symlinks 15 | - id: end-of-file-fixer 16 | - id: trailing-whitespace 17 | - id: detect-private-key 18 | - id: check-symlinks 19 | - id: check-added-large-files 20 | args: ['--maxkb=1800'] 21 | 22 | - repo: local 23 | hooks: 24 | - id: flake8 25 | name: flake8 26 | entry: flake8 27 | language: system 28 | args: 29 | - --count 30 | - --select=E9,F63,F7,F82 31 | - --show-source 32 | - --statistics 33 | files: \.py$ 34 | 35 | - repo: local 36 | hooks: 37 | - id: copyright_checker 38 | name: copyright_checker 39 | entry: python ./.copyright.hook 40 | language: system 41 | files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py)$ 42 | exclude: (?!.*third_party)^.*$ 43 | -------------------------------------------------------------------------------- /docs/luckydraw.md: -------------------------------------------------------------------------------- 1 | 2 | # 获奖名单 3 | 4 | ## 锦鲤奖(1个) 5 | 6 | ### 恭喜以下用户获得小度音箱Pro!! 7 | 8 | 用户**jianchengss** 9 | 10 | 11 | 12 | 13 | 14 | ## 特等奖(1个) 15 | 16 | ### 恭喜以下用户获得膳魔师!! 17 | 18 | 用户**deepplayer** 19 | 20 | 21 | 22 | 23 | 24 | ## 一等奖(2个) 25 | 26 | ### 恭喜以下用户获得50元京东卡!! 27 | 28 | 用户**qiumi0416** 29 | 30 | 用户**varunp2k** 31 | 32 | 33 | 34 | 35 | 36 | ## 二等奖(各3个) 37 | 38 | ### 恭喜以下用户获得二十四节气冰箱贴!! 39 | 40 | 用户**csjsp** 41 | 42 | 用户**HammerChen** 43 | 44 | 用户**virtual8888** 45 | 46 | ### 恭喜以下用户获得飞桨鼠标垫!! 47 | 48 | 用户**LiJinrun** 49 | 50 | 用户**Musk66** 51 | 52 | 用户**Tigershan** 53 | 54 | 55 | 56 | 57 | 58 | ## 三等奖(10个) 59 | 60 | ### 恭喜以下用户获得百度网盘会员!! 61 | 62 | 用户**qibao77** 63 | 64 | 用户**theWhiteG** 65 | 66 | 用户**zhouzijun1111** 67 | 68 | 用户**flyi** 69 | 70 | 用户**pinyaxuan** 71 | 72 | 用户**liangruofei** 73 | 74 | 用户**chenyujing1234** 75 | 76 | 用户**lootnath** 77 | 78 | 用户**paddlefan** 79 | 80 | 用户**FahsaRwatit** 81 | 82 | 83 | 84 | 85 | 86 | ## 领奖方式 87 | 88 | **再次恭喜以上获奖小伙伴们,请大家扫描下方二维码将自己的Github ID、姓名、电话、详细地址填写完整,我们会尽快将奖品寄出~~** 89 | 90 |

91 | 92 |

93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /docs/en_US/tutorials/face_parse.md: -------------------------------------------------------------------------------- 1 | # Face Parsing 2 | 3 | ## 1. Face parsing introduction 4 | 5 | Face parsing address the task that how to parse facial components from face images. We utiize BiseNet to handle this problem and focus on computing the pixel-wise label map of a face image. It is useful for a variety of tasks, including recognition, animation, and synthesis. This application is now working in our makeup transfer model. 6 | 7 | ## 2. How to use 8 | 9 | ### 2.1 Test 10 | 11 | Runing the following command to complete the face parsing task. The output results will be the segmanted face components mask for the input image. 12 | 13 | ``` 14 | cd applications 15 | python tools/face_parse.py --input_image ../docs/imgs/face.png 16 | ``` 17 | 18 | **params:** 19 | 20 | - input_image: path of the input face image 21 | 22 | ## Results 23 | ![](../../imgs/face_parse_out.png) 24 | 25 | ### 4. Reference 26 | 27 | ``` 28 | @misc{yu2018bisenet, 29 | title={BiSeNet: Bilateral Segmentation Network for Real-time Semantic Segmentation}, 30 | author={Changqian Yu and Jingbo Wang and Chao Peng and Changxin Gao and Gang Yu and Nong Sang}, 31 | year={2018}, 32 | eprint={1808.00897}, 33 | archivePrefix={arXiv}, 34 | primaryClass={cs.CV} 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /configs/wav2lip.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 200000000 2 | output_dir: output 3 | checkpoints_dir: checkpoints 4 | 5 | model: 6 | name: Wav2LipModel 7 | syncnet_wt: 0.0 8 | max_eval_steps: 700 9 | generator: 10 | name: Wav2Lip 11 | discriminator: 12 | name: SyncNetColor 13 | 14 | dataset: 15 | train: 16 | name: Wav2LipDataset 17 | dataroot: data/lrs2_preprocessed 18 | filelists_path: ./ 19 | img_size: 96 20 | split: train 21 | batch_size: 8 22 | num_workers: 4 23 | use_shared_memory: False 24 | test: 25 | name: Wav2LipDataset 26 | dataroot: data/lrs2_preprocessed 27 | filelists_path: ./ 28 | img_size: 96 29 | split: val 30 | batch_size: 16 31 | num_workers: 4 32 | use_shared_memory: False 33 | 34 | optimizer: 35 | optimizer_G: 36 | name: Adam 37 | net_names: 38 | - netG 39 | beta1: 0.5 40 | optimizer_D: 41 | name: Adam 42 | net_names: 43 | - netD 44 | beta1: 0.5 45 | 46 | validate: 47 | interval: 3000 48 | save_img: false 49 | 50 | lr_scheduler: 51 | name: LinearDecay 52 | learning_rate: 0.0001 53 | start_epoch: 2000000 54 | decay_epochs: 2000000 55 | # will get from real dataset 56 | iters_per_epoch: 1 57 | 58 | log_config: 59 | interval: 10 60 | visiual_interval: 500 61 | 62 | snapshot_config: 63 | interval: 3000 64 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/face_enhancement.md: -------------------------------------------------------------------------------- 1 | # 人脸增强 2 | 3 | ## 1. 人脸增强简介 4 | 5 | 从严重退化的人脸图像中恢复出人脸是一个非常具有挑战性的问题。由于问题的严重性和复杂的未知退化,直接训练深度神经网络通常无法得到可接受的结果。现有的基于生成对抗网络 (GAN) 的方法可以产生更好的结果,但往往会产生过度平滑的恢复。这里我们提供[GPEN](https://arxiv.org/abs/2105.06070)模型来进行人脸增强。GPEN模型首先学习用于生成高质量人脸图像的GAN并将其嵌入到U形DNN作为先验解码器,然后使用一组合成的低质量人脸图像对GAN先验嵌入DNN进行微调。 GAN 模块的设计是为了确保输入到 GAN 的隐码和噪声可以分别从 DNN 的深层和浅层特征中生成,控制重建图像的全局人脸结构、局部人脸细节和背景。所提出的 GAN 先验嵌入网络 (GPEN) 易于实现,并且可以生成视觉上逼真的结果。实验表明,GPEN 在数量和质量上都比最先进的 BFR 方法取得了显着优越的结果,特别是对于野外严重退化的人脸图像的恢复。 6 | 7 | ## 使用方法 8 | 9 | ### 人脸增强 10 | 11 | 用户使用如下代码进行人脸增强,选择本地图像作为输入: 12 | 13 | ```python 14 | import paddle 15 | from ppgan.faceutils.face_enhancement import FaceEnhancement 16 | 17 | faceenhancer = FaceEnhancement() 18 | img = faceenhancer.enhance_from_image(img) 19 | ``` 20 | 21 | 注意:请将图片转为float类型输入,目前不支持int8类型 22 | 23 | ### 训练 24 | 25 | [详见](../../zh_CN/tutorials/gpen.md) 26 | 27 | ## 人脸增强结果展示 28 | 29 | ![1](https://user-images.githubusercontent.com/79366697/146891109-d204497f-7e71-4899-bc65-e1b101ce6293.jpg) 30 | 31 | ## 参考文献 32 | 33 | ``` 34 | @inproceedings{inproceedings, 35 | author = {Yang, Tao and Ren, Peiran and Xie, Xuansong and Zhang, Lei}, 36 | year = {2021}, 37 | month = {06}, 38 | pages = {672-681}, 39 | title = {GAN Prior Embedded Network for Blind Face Restoration in the Wild}, 40 | doi = {10.1109/CVPR46437.2021.00073} 41 | } 42 | 43 | ``` 44 | -------------------------------------------------------------------------------- /deploy/cpp_infer/src/process_op.cc: -------------------------------------------------------------------------------- 1 | #include "opencv2/core.hpp" 2 | #include "opencv2/imgcodecs.hpp" 3 | #include "opencv2/imgproc.hpp" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | // RGB -> CHW 18 | void Permute::Run(const cv::Mat *im, float *data) { 19 | int rh = im->rows; 20 | int rw = im->cols; 21 | int ch = im->channels(); 22 | for (int i = 0; i < ch; ++i) { 23 | cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i); 24 | } 25 | } 26 | 27 | void Normalize::Run(cv::Mat *im, const std::vector &mean, 28 | const std::vector &scale, const bool is_scale) { 29 | double e = 1.0; 30 | if (is_scale) { 31 | e /= 255.0; 32 | } 33 | (*im).convertTo(*im, CV_32FC3, e); 34 | 35 | for (int h = 0; h < im->rows; h++) { 36 | for (int w = 0; w < im->cols; w++) { 37 | im->at(h, w)[0] = 38 | (im->at(h, w)[0] - mean[0]) * scale[0]; 39 | im->at(h, w)[1] = 40 | (im->at(h, w)[1] - mean[1]) * scale[1]; 41 | im->at(h, w)[2] = 42 | (im->at(h, w)[2] - mean[2]) * scale[2]; 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /ppgan/utils/timer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import time 16 | 17 | 18 | class TimeAverager(object): 19 | def __init__(self): 20 | self.reset() 21 | 22 | def reset(self): 23 | self._cnt = 0 24 | self._total_time = 0 25 | self._total_samples = 0 26 | 27 | def record(self, usetime, num_samples=None): 28 | self._cnt += 1 29 | self._total_time += usetime 30 | if num_samples: 31 | self._total_samples += num_samples 32 | 33 | def get_average(self): 34 | if self._cnt == 0: 35 | return 0 36 | return self._total_time / float(self._cnt) 37 | 38 | def get_ips_average(self): 39 | if not self._total_samples or self._cnt == 0: 40 | return 0 41 | return float(self._total_samples) / self._total_time 42 | -------------------------------------------------------------------------------- /tools/extract_weight.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import paddle 16 | import argparse 17 | 18 | 19 | def parse_args(): 20 | parser = argparse.ArgumentParser( 21 | description='This script extracts weights from a checkpoint') 22 | parser.add_argument('checkpoint', help='checkpoint file') 23 | parser.add_argument('--net-name', 24 | type=str, 25 | help='net name in checkpoint dict') 26 | parser.add_argument('--output', type=str, help='destination file name') 27 | args = parser.parse_args() 28 | return args 29 | 30 | 31 | def main(): 32 | args = parse_args() 33 | assert args.output.endswith(".pdparams") 34 | ckpt = paddle.load(args.checkpoint) 35 | state_dict = ckpt[args.net_name] 36 | paddle.save(state_dict, args.output) 37 | 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /benchmark/README.md: -------------------------------------------------------------------------------- 1 | # PaddGAN模型性能复现 2 | ## 目录 3 | 4 | ``` 5 | ├── README.md # 说明文档 6 | ├── benchmark.yaml # 配置文件,设置测试模型及模型参数 7 | ├── run_all.sh # 执行入口,测试并获取所有生成对抗模型的训练性能 8 | └── run_benchmark.sh # 执行实体,测试单个分割模型的训练性能 9 | ``` 10 | 11 | ## 环境介绍 12 | ### 物理机环境 13 | - 单机(单卡、8卡) 14 | - 系统:CentOS release 7.5 (Final) 15 | - GPU:Tesla V100-SXM2-32GB * 8 16 | - CPU:Intel(R) Xeon(R) Gold 6271C CPU @ 2.60GHz * 80 17 | - CUDA、cudnn Version: cuda10.2-cudnn7 18 | 19 | #### 备注 20 | BasicVSR模型因竞品torch模型只能测4卡,故这里也测4卡。 21 | 22 | 因REDS数据集较大,避免每次下载时间较长,需要在Docker建立好后,将REDS数据集放到/workspace/data/目录一下。 23 | 24 | ### Docker 镜像 25 | 26 | - **镜像版本**: `registry.baidubce.com/paddlepaddle/paddle:2.1.2-gpu-cuda10.2-cudnn7` 27 | - **paddle 版本**: `2.1.2` 28 | - **CUDA 版本**: `10.2` 29 | - **cuDnn 版本**: `7` 30 | 31 | ## 在PaddleGAN目录下,启动测试脚本的方法如下: 32 | ```script 33 | ImageName="registry.baidubce.com/paddlepaddle/paddle:2.1.2-gpu-cuda10.2-cudnn7" 34 | docker pull ${ImageName} 35 | 36 | run_cmd="set -xe; 37 | cd /workspace ; 38 | bash -x benchmark/run_all.sh" 39 | 40 | nvidia-docker run --name test_paddlegan -i \ 41 | --net=host \ 42 | --shm-size=128g \ 43 | -v $PWD:/workspace \ 44 | ${ImageName} /bin/bash -c "${run_cmd}" 45 | ``` 46 | 47 | 如果需要打开profile选项,可以直接替换`run_cmd`为: 48 | ``` 49 | run_cmd="set -xe; 50 | cd /workspace ; 51 | bash -x benchmark/run_all.sh on" 52 | ``` 53 | 54 | ## 输出 55 | 56 | 执行完成后,在PaddleGAN目录会产出模型训练性能数据的文件,比如`esrgan_mp_bs32_fp32_8`等文件。 57 | -------------------------------------------------------------------------------- /configs/mprnet_denoising.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 100000 2 | output_dir: output_dir 3 | 4 | model: 5 | name: MPRModel 6 | generator: 7 | name: MPRNet 8 | n_feat: 80 9 | scale_unetfeats: 48 10 | scale_orsnetfeats: 32 11 | 12 | char_criterion: 13 | name: CharbonnierLoss 14 | edge_criterion: 15 | name: EdgeLoss 16 | 17 | dataset: 18 | train: 19 | name: MPRTrain 20 | rgb_dir: data/SIDD/train 21 | num_workers: 16 22 | batch_size: 4 # 4GPUs 23 | img_options: 24 | patch_size: 256 25 | test: 26 | name: MPRTrain 27 | rgb_dir: data/SIDD/val 28 | num_workers: 1 29 | batch_size: 1 30 | img_options: 31 | patch_size: 256 32 | 33 | lr_scheduler: 34 | name: CosineAnnealingRestartLR 35 | learning_rate: !!float 2e-4 36 | periods: [25000, 25000, 25000, 25000] 37 | restart_weights: [1, 1, 1, 1] 38 | eta_min: !!float 1e-6 39 | 40 | validate: 41 | interval: 5000 42 | save_img: false 43 | 44 | metrics: 45 | psnr: # metric name, can be arbitrary 46 | name: PSNR 47 | crop_border: 4 48 | test_y_channel: True 49 | ssim: 50 | name: SSIM 51 | crop_border: 4 52 | test_y_channel: True 53 | 54 | optimizer: 55 | name: Adam 56 | # add parameters of net_name to optim 57 | # name should in self.nets 58 | net_names: 59 | - generator 60 | beta1: 0.9 61 | beta2: 0.999 62 | epsilon: 1e-8 63 | 64 | log_config: 65 | interval: 10 66 | visiual_interval: 5000 67 | 68 | snapshot_config: 69 | interval: 5000 70 | -------------------------------------------------------------------------------- /test_tipc/configs/invdn/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:invdn 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10 8 | output_dir:./output/ 9 | snapshot_config.interval:lite_train_lite_infer=10 10 | pretrained_model:null 11 | train_model_name:invdn*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/invdn_denoising.yaml --seed 100 -o log_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/invdn_denoising.yaml --inputs_size=1,3,256,256 --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/invdn/invdnmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type invdn --seed 100 -c configs/invdn_denoising.yaml --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | -------------------------------------------------------------------------------- /configs/mprnet_deblurring.yaml: -------------------------------------------------------------------------------- 1 | # epoch: 3000 for total batch size=16 2 | total_iters: 400000 3 | output_dir: output_dir 4 | 5 | model: 6 | name: MPRModel 7 | generator: 8 | name: MPRNet 9 | n_feat: 96 10 | scale_unetfeats: 48 11 | scale_orsnetfeats: 32 12 | 13 | char_criterion: 14 | name: CharbonnierLoss 15 | edge_criterion: 16 | name: EdgeLoss 17 | 18 | dataset: 19 | train: 20 | name: MPRTrain 21 | rgb_dir: data/GoPro/train 22 | num_workers: 4 23 | batch_size: 2 # 8GPUs 24 | img_options: 25 | patch_size: 256 26 | test: 27 | name: MPRVal 28 | rgb_dir: data/GoPro/test 29 | num_workers: 1 30 | batch_size: 1 31 | img_options: 32 | patch_size: 256 33 | 34 | lr_scheduler: 35 | name: CosineAnnealingRestartLR 36 | learning_rate: !!float 1e-4 37 | periods: [400000] 38 | restart_weights: [1] 39 | eta_min: !!float 1e-6 40 | 41 | validate: 42 | interval: 5000 43 | save_img: false 44 | 45 | metrics: 46 | psnr: # metric name, can be arbitrary 47 | name: PSNR 48 | crop_border: 4 49 | test_y_channel: false 50 | ssim: 51 | name: SSIM 52 | crop_border: 4 53 | test_y_channel: false 54 | 55 | optimizer: 56 | name: Adam 57 | # add parameters of net_name to optim 58 | # name should in self.nets 59 | net_names: 60 | - generator 61 | beta1: 0.9 62 | beta2: 0.999 63 | epsilon: 1e-8 64 | 65 | log_config: 66 | interval: 100 67 | visiual_interval: 5000 68 | 69 | snapshot_config: 70 | interval: 5000 71 | -------------------------------------------------------------------------------- /test_tipc/configs/nafnet/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:nafnet 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10 8 | output_dir:./output/ 9 | snapshot_config.interval:lite_train_lite_infer=10 10 | pretrained_model:null 11 | train_model_name:nafnet*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/nafnet_denoising.yaml --seed 100 -o log_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/nafnet_denoising.yaml --inputs_size=1,3,256,256 --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/nafnet/nafnetmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type nafnet --seed 100 -c configs/nafnet_denoising.yaml --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | -------------------------------------------------------------------------------- /test_tipc/configs/swinir/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:swinir 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10 8 | output_dir:./output/ 9 | snapshot_config.interval:lite_train_lite_infer=10 10 | pretrained_model:null 11 | train_model_name:swinir*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/swinir_denoising.yaml --seed 100 -o log_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/swinir_denoising.yaml --inputs_size=1,3,128,128 --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/swinir/swinirmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type swinir --seed 100 -c configs/swinir_denoising.yaml --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | -------------------------------------------------------------------------------- /configs/aotgan.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 1000000 2 | output_dir: output_dir 3 | checkpoints_dir: checkpoints 4 | epochs: 5 5 | 6 | model: 7 | name: AOTGANModel 8 | generator: 9 | name: InpaintGenerator 10 | rates: [1, 2, 4, 8] 11 | block_num: 8 12 | discriminator: 13 | name: Discriminator 14 | inc: 3 15 | criterion: 16 | name: AOTGANCriterionLoss 17 | pretrained: https://paddlegan.bj.bcebos.com/models/vgg19feats.pdparams 18 | l1_weight: 1 19 | perceptual_weight: 1 20 | style_weight: 250 21 | adversal_weight: 0.01 22 | img_size: 512 23 | 24 | dataset: 25 | train: 26 | name: AOTGANDataset 27 | dataset_path: data/aotgan 28 | batch_size: 8 # Multi-Card:4 29 | img_size: 512 30 | test: 31 | name: AOTGANDataset_test 32 | dataset_path: data/aotgan 33 | batch_size: 1 34 | img_size: 512 35 | 36 | lr_scheduler: # abundoned 37 | name: MultiStepDecay 38 | learning_rate: 0.0001 39 | milestones: [990000] 40 | gamma: 0.1 41 | 42 | optimizer: 43 | lr: 0.0001 44 | optimG: 45 | name: Adam 46 | net_names: 47 | - netG 48 | beta1: 0.5 49 | beta2: 0.999 50 | optimD: 51 | name: Adam 52 | net_names: 53 | - netD 54 | beta1: 0.5 55 | beta2: 0.999 56 | 57 | log_config: 58 | interval: 100 59 | visiual_interval: 100 60 | 61 | snapshot_config: 62 | interval: 1000 63 | 64 | predict: 65 | name: AOTGANGenerator 66 | rates: [1, 2, 4, 8] 67 | block_num: 8 68 | img_size: 512 69 | 70 | export_model: 71 | - {name: 'net_gen', inputs_num: 1} 72 | -------------------------------------------------------------------------------- /configs/gpen_256_ffhq.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 200000 2 | output_dir: output_dir 3 | find_unused_parameters: True 4 | 5 | 6 | model: 7 | name: GPENModel 8 | generator: 9 | name: GPENGenerator 10 | size: 256 11 | style_dim: 512 12 | n_mlp: 8 13 | channel_multiplier: 1 14 | narrow: 0.5 15 | discriminator: 16 | name: GPENDiscriminator 17 | size: 256 18 | channel_multiplier: 1 19 | narrow: 0.5 20 | 21 | 22 | export_model: 23 | - {name: 'g_ema', inputs_num: 1} 24 | 25 | dataset: 26 | train: 27 | name: GPENDataset 28 | dataroot: data/ffhq/images256x256/ 29 | num_workers: 0 30 | batch_size: 2 #1gpus 31 | size: 256 32 | 33 | test: 34 | name: GPENDataset 35 | dataroot: data/ffhq/images256x256/ 36 | num_workers: 0 37 | batch_size: 1 38 | size: 256 39 | amount: 100 40 | 41 | 42 | lr_scheduler: 43 | name: CosineAnnealingRestartLR 44 | learning_rate: 0.002 45 | periods: [500000, 500000, 500000, 500000] 46 | restart_weights: [1, 1, 1, 1] 47 | eta_min: 0.002 48 | 49 | 50 | optimizer: 51 | optimG: 52 | name: Adam 53 | net_names: 54 | - netG 55 | beta1: 0.9 56 | beta2: 0.99 57 | optimD: 58 | name: Adam 59 | net_names: 60 | - netD 61 | beta1: 0.9 62 | beta2: 0.99 63 | 64 | log_config: 65 | interval: 100 66 | visiual_interval: 500 67 | 68 | snapshot_config: 69 | interval: 5000 70 | 71 | validate: 72 | interval: 5000 73 | save_img: false 74 | metrics: 75 | fid: 76 | name: FID 77 | batch_size: 1 78 | -------------------------------------------------------------------------------- /configs/lapstyle_draft.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 30000 2 | output_dir: output_dir 3 | checkpoints_dir: checkpoints 4 | min_max: 5 | (0., 1.) 6 | 7 | model: 8 | name: LapStyleDraModel 9 | generator_encode: 10 | name: Encoder 11 | generator_decode: 12 | name: DecoderNet 13 | calc_style_emd_loss: 14 | name: CalcStyleEmdLoss 15 | calc_content_relt_loss: 16 | name: CalcContentReltLoss 17 | calc_content_loss: 18 | name: CalcContentLoss 19 | calc_style_loss: 20 | name: CalcStyleLoss 21 | content_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 22 | style_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 23 | content_weight: 1.0 24 | style_weight: 3.0 25 | 26 | 27 | dataset: 28 | train: 29 | name: LapStyleDataset 30 | content_root: data/coco/train2017/ 31 | style_root: data/starrynew.png 32 | load_size: 136 33 | crop_size: 128 34 | num_workers: 16 35 | batch_size: 5 #1 GPUs 36 | test: 37 | name: LapStyleDataset 38 | content_root: data/coco/test2017/ 39 | style_root: data/starrynew.png 40 | load_size: 128 41 | crop_size: 128 42 | num_workers: 0 43 | batch_size: 1 44 | 45 | lr_scheduler: 46 | name: NonLinearDecay 47 | learning_rate: 1e-4 48 | lr_decay: 5e-5 49 | 50 | optimizer: 51 | optimG: 52 | name: Adam 53 | net_names: 54 | - net_dec 55 | beta1: 0.9 56 | beta2: 0.999 57 | 58 | validate: 59 | interval: 500 60 | save_img: false 61 | 62 | log_config: 63 | interval: 10 64 | visiual_interval: 500 65 | 66 | snapshot_config: 67 | interval: 5000 68 | -------------------------------------------------------------------------------- /configs/mprnet_deraining.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 100000 2 | output_dir: output_dir 3 | 4 | model: 5 | name: MPRModel 6 | generator: 7 | name: MPRNet 8 | n_feat: 40 9 | scale_unetfeats: 20 10 | scale_orsnetfeats: 16 11 | 12 | char_criterion: 13 | name: CharbonnierLoss 14 | edge_criterion: 15 | name: EdgeLoss 16 | 17 | dataset: 18 | train: 19 | name: MPRTrain 20 | rgb_dir: data/Synthetic_Rain_Datasets/train 21 | num_workers: 16 22 | batch_size: 4 # 4GPUs 23 | img_options: 24 | patch_size: 256 25 | test: 26 | name: MPRTrain 27 | rgb_dir: data/Synthetic_Rain_Datasets/test/Rain100L 28 | num_workers: 1 29 | batch_size: 1 30 | img_options: 31 | patch_size: 256 32 | 33 | lr_scheduler: 34 | name: CosineAnnealingRestartLR 35 | learning_rate: !!float 2e-4 36 | periods: [25000, 25000, 25000, 25000] 37 | restart_weights: [1, 1, 1, 1] 38 | eta_min: !!float 1e-6 39 | 40 | validate: 41 | interval: 5000 42 | save_img: false 43 | 44 | metrics: 45 | psnr: # metric name, can be arbitrary 46 | name: PSNR 47 | crop_border: 4 48 | test_y_channel: True 49 | ssim: 50 | name: SSIM 51 | crop_border: 4 52 | test_y_channel: True 53 | 54 | optimizer: 55 | name: Adam 56 | # add parameters of net_name to optim 57 | # name should in self.nets 58 | net_names: 59 | - generator 60 | beta1: 0.9 61 | beta2: 0.999 62 | epsilon: 1e-8 63 | 64 | log_config: 65 | interval: 10 66 | visiual_interval: 5000 67 | 68 | snapshot_config: 69 | interval: 5000 70 | -------------------------------------------------------------------------------- /docs/zh_CN/industrial_solution/video_restore_cn.md: -------------------------------------------------------------------------------- 1 | # 智能影像修复 2 | 3 | PaddleGAN提供一系列影像修复能力,包括 **[图片上色](./photo_color_cn.md)、[视频上色](./video_color_cn.md)、[图片分辨率提升](./photo_sr_cn.md)、[视频分辨率提升](./video_sr_cn.md)**,以及 **[视频流畅度提升](./video_frame_cn.md)**(提高视频播放流畅度)三大功能,使得历史影像恢复往日鲜活的色彩,清晰流畅的呈现于我们眼前。 4 | 5 | 在未来,PaddleGAN也将不断补充与优化影像修复的能力,比如增加去噪、图像修复等功能,还请大家敬请期待! 6 | 7 | ## **一行代码快速进行影像修复** 8 | 9 | ``` 10 | cd applications 11 | python tools/video-enhance.py --input you_video_path.mp4 --process_order DAIN DeOldify PPMSVSR --output output_dir 12 | ``` 13 | 14 | ### **参数** 15 | 16 | - `--input (str)`: 输入的视频路径。 17 | - `--output (str)`: 输出的视频路径。 18 | - `--process_order`: 调用的模型名字和顺序,比如输入为 `DAIN DeOldify PPMSVSR`,则会顺序调用 `DAINPredictor` `DeOldifyPredictor` `PPMSVSRPredictor` 。 19 | - `--cpu`: 开启cpu推理,默认使用GPU推理。 20 | 21 |
22 | 23 |
24 | 25 | ## 详细教程 26 | * 视频修复 27 | * [视频上色](./video_color_cn.md) 28 | * [视频分辨率提升](./video_sr_cn.md) 29 | * [视频流畅度提升](./video_frame_cn.md) 30 | 31 | * 照片修复 32 | * [图片上色](./photo_color_cn.md) 33 | * [图片分辨率提升](./photo_sr_cn.md) 34 | 35 | 36 | ## 在线体验 37 | 为了让大家快速体验影像修复的能力,PaddleGAN在飞桨人工智能学习与实训平台AI Studio准备了完整的实现步骤及详细代码,同时,AI Studio还为大家准备了免费的GPU算力,大家登录即可亲自实践 **[老北京城影像修复](https://aistudio.baidu.com/aistudio/projectdetail/1161285)** 的项目,快上手体验吧! 38 | 39 |
40 | 41 |
42 | -------------------------------------------------------------------------------- /ppgan/models/discriminators/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .vgg_discriminator import VGGDiscriminator128 16 | from .nlayers import NLayerDiscriminator, NLayerDiscriminatorWithClassification 17 | from .discriminator_ugatit import UGATITDiscriminator 18 | from .dcdiscriminator import DCDiscriminator 19 | from .discriminator_animegan import AnimeDiscriminator 20 | from .discriminator_styleganv2 import StyleGANv2Discriminator, GPENDiscriminator 21 | from .syncnet import SyncNetColor 22 | from .wav2lip_disc_qual import Wav2LipDiscQual 23 | from .discriminator_starganv2 import StarGANv2Discriminator 24 | from .discriminator_firstorder import FirstOrderDiscriminator 25 | from .discriminator_lapstyle import LapStyleDiscriminator 26 | from .discriminator_photopen import MultiscaleDiscriminator 27 | from .discriminator_singan import SinGANDiscriminator 28 | from .arcface_arch_paddle import ResNetArcFace 29 | from .discriminator_aotgan import Discriminator 30 | -------------------------------------------------------------------------------- /test_tipc/configs/singan/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:singan 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=100|whole_train_whole_infer=100000 8 | output_dir:./output/ 9 | snapshot_config.interval:lite_train_lite_infer=25|whole_train_whole_infer=10000 10 | pretrained_model:null 11 | train_model_name:singan*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/stone 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/singan_universal.yaml --seed 123 -o log_config.interval=50 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/singan_universal.yaml --inputs_size=1 --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:singan_random_sample 37 | train_model:./inference/singan/singan_random_sample 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type singan --seed 123 -c configs/singan_universal.yaml --output_path test_tipc/output/ 41 | --device:cpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null -------------------------------------------------------------------------------- /configs/wgan_mnist.yaml: -------------------------------------------------------------------------------- 1 | epochs: 200 2 | output_dir: output_dir 3 | 4 | model: 5 | name: GANModel 6 | generator: 7 | name: DeepConvGenerator 8 | latent_dim: 128 9 | output_nc: 1 10 | size: 28 11 | ngf: 64 12 | discriminator: 13 | name: NLayerDiscriminator 14 | ndf: 16 15 | n_layers: 3 16 | input_nc: 1 17 | norm_type: instance 18 | gan_criterion: 19 | name: GANLoss 20 | gan_mode: wgan 21 | params: 22 | disc_iters: 5 23 | visual_interval: 500 24 | 25 | dataset: 26 | train: 27 | name: CommonVisionDataset 28 | dataset_name: MNIST 29 | num_workers: 4 30 | batch_size: 64 31 | return_label: False 32 | transforms: 33 | - name: Normalize 34 | mean: [127.5] 35 | std: [127.5] 36 | keys: [image] 37 | test: 38 | name: CommonVisionDataset 39 | dataset_name: MNIST 40 | num_workers: 0 41 | batch_size: 64 42 | return_label: False 43 | transforms: 44 | - name: Normalize 45 | mean: [127.5] 46 | std: [127.5] 47 | keys: [image] 48 | 49 | 50 | lr_scheduler: 51 | name: LinearDecay 52 | learning_rate: 0.0002 53 | start_epoch: 100 54 | decay_epochs: 100 55 | # will get from real dataset 56 | iters_per_epoch: 1 57 | 58 | optimizer: 59 | optimizer_G: 60 | name: Adam 61 | net_names: 62 | - netG 63 | beta1: 0.5 64 | optimizer_D: 65 | name: Adam 66 | net_names: 67 | - netD 68 | beta1: 0.5 69 | 70 | log_config: 71 | interval: 100 72 | visiual_interval: 500 73 | 74 | snapshot_config: 75 | interval: 5 76 | -------------------------------------------------------------------------------- /deploy/export_model.md: -------------------------------------------------------------------------------- 1 | # PaddleGAN模型导出教程 2 | 3 | ## 一、模型导出 4 | 本章节介绍如何使用`tools/export_model.py`脚本导出模型。 5 | 6 | ### 1、启动参数说明 7 | 8 | | FLAG | 用途 | 默认值 | 备注 | 9 | |:--------------:|:--------------:|:------------:|:-----------------------------------------:| 10 | | -c | 指定配置文件 | None | | 11 | | --load | 指定加载的模型参数路径 | None | | 12 | | -s|--inputs_size | 指定模型输入形状 | None | | 13 | | --output_dir | 模型保存路径 | `./inference_model` | | 14 | 15 | ### 2、使用示例 16 | 17 | 使用训练得到的模型进行试用,这里使用CycleGAN模型为例,脚本如下 18 | 19 | ```bash 20 | # 下载预训练好的CycleGAN_horse2zebra模型 21 | wget https://paddlegan.bj.bcebos.com/models/CycleGAN_horse2zebra.pdparams 22 | 23 | # 导出Cylclegan模型 24 | python -u tools/export_model.py -c configs/cyclegan_horse2zebra.yaml --load CycleGAN_horse2zebra.pdparams --inputs_size="-1,3,-1,-1;-1,3,-1,-1" 25 | ``` 26 | 27 | ### 3、config配置说明 28 | ```python 29 | export_model: 30 | - {name: 'netG_A', inputs_num: 1} 31 | - {name: 'netG_B', inputs_num: 1} 32 | ``` 33 | 以上为```configs/cyclegan_horse2zebra.yaml```中的配置, 由于```CycleGAN_horse2zebra.pdparams```是个字典,需要制定其中用于导出模型的权重键值。```inputs_num``` 34 | 为该网络的输入个数。 35 | 36 | 预测模型会导出到`inference_model/`目录下,分别为`cycleganmodel_netG_A.pdiparams`, `cycleganmodel_netG_A.pdiparams.info`, `cycleganmodel_netG_A.pdmodel`, `cycleganmodel_netG_B.pdiparams`, `cycleganmodel_netG_B.pdiparams.info`, `cycleganmodel_netG_B.pdmodel`,。 37 | -------------------------------------------------------------------------------- /test_tipc/configs/GFPGAN/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:GFPGAN 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=3 10 | pretrained_model:null 11 | train_model_name:gfpgan_ffhq1024*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/gfpgan_ffhq1024.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=10 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/gfpgan_ffhq1024.yaml --inputs_size="1,3,512,512" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/stylegan2/stylegan2model_gen 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type gfpgan --seed 123 -c configs/gfpgan_ffhq1024.yaml --output_path test_tipc/output/ -o validate=None 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:False 51 | null:null 52 | -------------------------------------------------------------------------------- /configs/wav2lip_hq.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 200000000 2 | output_dir: output_hq 3 | checkpoints_dir: checkpoints_hq 4 | 5 | model: 6 | name: Wav2LipModelHq 7 | syncnet_wt: 0. 8 | disc_wt: 0.07 9 | max_eval_steps: 700 10 | generator: 11 | name: Wav2Lip 12 | discriminator_sync: 13 | name: SyncNetColor 14 | discriminator_hq: 15 | name: Wav2LipDiscQual 16 | 17 | export_model: 18 | - {name: 'netG', inputs_num: 2} 19 | 20 | dataset: 21 | train: 22 | name: Wav2LipDataset 23 | dataroot: data/lrs2_preprocessed 24 | filelists_path: ./ 25 | img_size: 96 26 | split: train 27 | batch_size: 8 28 | num_workers: 0 29 | use_shared_memory: False 30 | test: 31 | name: Wav2LipDataset 32 | dataroot: data/lrs2_preprocessed 33 | filelists_path: ./ 34 | img_size: 96 35 | split: val 36 | batch_size: 16 37 | num_workers: 0 38 | use_shared_memory: False 39 | 40 | optimizer: 41 | optimizer_G: 42 | name: Adam 43 | net_names: 44 | - netG 45 | beta1: 0.5 46 | optimizer_DS: 47 | name: Adam 48 | net_names: 49 | - netDS 50 | beta1: 0.5 51 | optimizer_DH: 52 | name: Adam 53 | net_names: 54 | - netDH 55 | beta1: 0.5 56 | 57 | validate: 58 | interval: 3000 59 | save_img: false 60 | 61 | lr_scheduler: 62 | name: LinearDecay 63 | learning_rate: 0.0001 64 | start_epoch: 2000000 65 | decay_epochs: 2000000 66 | # will get from real dataset 67 | iters_per_epoch: 1 68 | 69 | log_config: 70 | interval: 10 71 | visiual_interval: 500 72 | 73 | snapshot_config: 74 | interval: 3000 75 | -------------------------------------------------------------------------------- /ppgan/models/discriminators/discriminator_starganv2.py: -------------------------------------------------------------------------------- 1 | # code was heavily based on https://github.com/clovaai/stargan-v2 2 | # Users should be careful about adopting these functions in any commercial matters. 3 | # https://github.com/clovaai/stargan-v2#license 4 | 5 | import paddle.nn as nn 6 | import paddle 7 | 8 | from .builder import DISCRIMINATORS 9 | from ..generators.generator_starganv2 import ResBlk 10 | 11 | import numpy as np 12 | 13 | 14 | @DISCRIMINATORS.register() 15 | class StarGANv2Discriminator(nn.Layer): 16 | def __init__(self, img_size=256, num_domains=2, max_conv_dim=512): 17 | super().__init__() 18 | dim_in = 2**14 // img_size 19 | blocks = [] 20 | blocks += [nn.Conv2D(3, dim_in, 3, 1, 1)] 21 | 22 | repeat_num = int(np.log2(img_size)) - 2 23 | for _ in range(repeat_num): 24 | dim_out = min(dim_in * 2, max_conv_dim) 25 | blocks += [ResBlk(dim_in, dim_out, downsample=True)] 26 | dim_in = dim_out 27 | 28 | blocks += [nn.LeakyReLU(0.2)] 29 | blocks += [nn.Conv2D(dim_out, dim_out, 4, 1, 0)] 30 | blocks += [nn.LeakyReLU(0.2)] 31 | blocks += [nn.Conv2D(dim_out, num_domains, 1, 1, 0)] 32 | self.main = nn.Sequential(*blocks) 33 | 34 | def forward(self, x, y): 35 | out = self.main(x) 36 | out = paddle.reshape(out, (out.shape[0], -1)) # (batch, num_domains) 37 | idx = paddle.zeros_like(out) 38 | for i in range(idx.shape[0]): 39 | idx[i, y[i]] = 1 40 | s = idx * out 41 | s = paddle.sum(s, axis=1) 42 | return s 43 | -------------------------------------------------------------------------------- /test_tipc/configs/aotgan/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:aotgan 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | epochs:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:aotgan*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/aotgan 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/aotgan.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/aotgan.yaml --inputs_size="-1,4,-1,-1" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/aotgan/aotganmodel_netG 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type aotgan --seed 123 -c configs/aotgan.yaml --output_path test_tipc/output/ 41 | --device:cpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | -------------------------------------------------------------------------------- /configs/makeup.yaml: -------------------------------------------------------------------------------- 1 | epochs: 100 2 | output_dir: tmp 3 | checkpoints_dir: checkpoints 4 | find_unused_parameters: True 5 | 6 | model: 7 | name: MakeupModel 8 | generator: 9 | name: GeneratorPSGANAttention 10 | conv_dim: 64 11 | repeat_num: 6 12 | discriminator: 13 | name: NLayerDiscriminator 14 | ndf: 64 15 | n_layers: 3 16 | input_nc: 3 17 | norm_type: spectral 18 | cycle_criterion: 19 | name: L1Loss 20 | idt_criterion: 21 | name: L1Loss 22 | loss_weight: 0.5 23 | l1_criterion: 24 | name: L1Loss 25 | l2_criterion: 26 | name: MSELoss 27 | gan_criterion: 28 | name: GANLoss 29 | gan_mode: lsgan 30 | 31 | dataset: 32 | train: 33 | name: MakeupDataset 34 | trans_size: 256 35 | dataroot: data/MT-Dataset 36 | cls_list: [non-makeup, makeup] 37 | phase: train 38 | test: 39 | name: MakeupDataset 40 | trans_size: 256 41 | dataroot: data/MT-Dataset 42 | cls_list: [non-makeup, makeup] 43 | phase: test 44 | 45 | 46 | lr_scheduler: 47 | name: LinearDecay 48 | learning_rate: 0.0002 49 | start_epoch: 100 50 | decay_epochs: 100 51 | # will get from real dataset 52 | iters_per_epoch: 1 53 | 54 | optimizer: 55 | optimizer_G: 56 | name: Adam 57 | net_names: 58 | - netG 59 | beta1: 0.5 60 | optimizer_DA: 61 | name: Adam 62 | net_names: 63 | - netD_A 64 | beta1: 0.5 65 | optimizer_DB: 66 | name: Adam 67 | net_names: 68 | - netD_B 69 | beta1: 0.5 70 | 71 | log_config: 72 | interval: 10 73 | visiual_interval: 500 74 | 75 | snapshot_config: 76 | interval: 5 77 | -------------------------------------------------------------------------------- /benchmark/analysis_log.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python 4 | 5 | import re 6 | import sys 7 | import json 8 | 9 | def analyze(model_name, log_file, res_log_file): 10 | time_pat = re.compile(r"ips: (.*) images/s") 11 | 12 | logs = open(log_file).readlines() 13 | logs = ";".join(logs) 14 | time_res = time_pat.findall(logs) 15 | 16 | fail_flag = 0 17 | run_mode = "" 18 | gpu_num = 0 19 | ips = 0 20 | 21 | if time_res == []: 22 | fail_flag = 1 23 | else: 24 | gpu_num = log_file.split('_')[-1] 25 | run_mode = "sp" if gpu_num == 1 else "mp" 26 | 27 | skip_num = 4 28 | total_time = 0 29 | for i in range(skip_num, len(time_res)): 30 | total_time += float(time_res[i]) 31 | ips = total_time / (len(time_res) - skip_num) 32 | 33 | info = {"log_file": log_file, "model_name": model_name, "mission_name": "图像生成", 34 | "direction_id": 0, "run_mode": run_mode, "index": 1, "gpu_num": gpu_num, 35 | "FINAL_RESULT": ips, "JOB_FAIL_FLAG": fail_flag, "UNIT": "images/s"} 36 | json_info = json.dumps(info) 37 | with open(res_log_file, "w") as of: 38 | of.write(json_info) 39 | 40 | if __name__ == "__main__": 41 | if len(sys.argv) != 4: 42 | print("Usage:" + sys.argv[0] + " model_name path/to/log/file path/to/res/log/file") 43 | sys.exit() 44 | 45 | model_name = sys.argv[1] 46 | log_file = sys.argv[2] 47 | res_log_file = sys.argv[3] 48 | 49 | analyze(model_name, log_file, res_log_file) 50 | 51 | 52 | -------------------------------------------------------------------------------- /ppgan/models/discriminators/discriminator_aotgan.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import paddle 16 | import paddle.nn as nn 17 | from paddle.nn.utils import spectral_norm 18 | 19 | from .builder import DISCRIMINATORS 20 | 21 | @DISCRIMINATORS.register() 22 | class Discriminator(nn.Layer): 23 | def __init__(self, inc = 3): 24 | super(Discriminator, self).__init__() 25 | self.conv = nn.Sequential( 26 | spectral_norm(nn.Conv2D(inc, 64, 4, 2, 1, bias_attr=False)), 27 | nn.LeakyReLU(0.2), 28 | spectral_norm(nn.Conv2D(64, 128, 4, 2, 1, bias_attr=False)), 29 | nn.LeakyReLU(0.2), 30 | spectral_norm(nn.Conv2D(128, 256, 4, 2, 1, bias_attr=False)), 31 | nn.LeakyReLU(0.2), 32 | spectral_norm(nn.Conv2D(256, 512, 4, 1, 1, bias_attr=False)), 33 | nn.LeakyReLU(0.2), 34 | nn.Conv2D(512, 1, 4, 1, 1) 35 | ) 36 | 37 | def forward(self, x): 38 | feat = self.conv(x) 39 | return feat 40 | -------------------------------------------------------------------------------- /configs/singan_finetune.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 12000 2 | output_dir: output_dir 3 | 4 | model: 5 | name: SinGANModel 6 | generator: 7 | name: SinGANGenerator 8 | nfc_init: 32 9 | min_nfc_init: 32 10 | noise_zero_pad: True 11 | discriminator: 12 | name: SinGANDiscriminator 13 | nfc_init: 32 14 | min_nfc_init: 32 15 | gan_criterion: 16 | name: GANLoss 17 | gan_mode: wgangp 18 | loss_weight: 1.0 19 | recon_criterion: 20 | name: MSELoss 21 | loss_weight: 10.0 22 | gp_criterion: 23 | name: GradientPenalty 24 | loss_weight: 0.1 25 | train_image: data/singan/stone.png 26 | scale_factor: 0.75 27 | min_size: 25 28 | is_finetune: True 29 | finetune_scale: 1 30 | color_num: 5 31 | 32 | dataset: 33 | train: 34 | name: EmptyDataset 35 | test: 36 | name: SingleDataset 37 | dataroot: data/singan 38 | num_workers: 0 39 | batch_size: 1 40 | preprocess: 41 | - name: LoadImageFromFile 42 | key: A 43 | - name: Transforms 44 | input_keys: [A] 45 | pipeline: 46 | - name: Transpose 47 | - name: Normalize 48 | mean: [127.5, 127.5, 127.5] 49 | std: [127.5, 127.5, 127.5] 50 | 51 | lr_scheduler: 52 | name: MultiStepDecay 53 | learning_rate: 0.0005 54 | milestones: [9600] 55 | gamma: 0.1 56 | 57 | optimizer: 58 | optimizer_G: 59 | name: Adam 60 | beta1: 0.5 61 | beta2: 0.999 62 | optimizer_D: 63 | name: Adam 64 | beta1: 0.5 65 | beta2: 0.999 66 | 67 | log_config: 68 | interval: 100 69 | visiual_interval: 2000 70 | 71 | snapshot_config: 72 | interval: 4000 73 | -------------------------------------------------------------------------------- /docs/zh_CN/industrial_solution/photo_color_cn.md: -------------------------------------------------------------------------------- 1 | # 图片上色 2 | 针对图片的上色,PaddleGAN提供了[DeOldify](https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/en_US/apis/apps.md#ppganappsdeoldifypredictor)模型。 3 | 4 | ## DeOldifyPredictor 5 | 6 | [DeOldify](https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/en_US/apis/apps.md#ppganappsdeoldifypredictor)采用自注意力机制的生成对抗网络,生成器是一个U-NET结构的网络。在图像/视频的上色方面有着较好的效果。 7 | 8 |
9 | 10 |
11 | 12 | ### 参数 13 | 14 | - `output (str,可选的)`: 输出的文件夹路径,默认值:`output`. 15 | - `weight_path (None,可选的)`: 载入的权重路径,如果没有设置,则从云端下载默认的权重到本地。默认值:`None`。 16 | - `artistic (bool)`: 是否使用偏"艺术性"的模型。"艺术性"的模型有可能产生一些有趣的颜色,但是毛刺比较多。 17 | - `render_factor (int)`: 会将该参数乘以16后作为输入帧的resize的值,如果该值设置为32, 18 | 则输入帧会resize到(32 * 16, 32 * 16)的尺寸再输入到网络中。 19 | 20 | 21 | ### 使用方式 22 | **1. API预测** 23 | 24 | ``` 25 | from ppgan.apps import DeOldifyPredictor 26 | deoldify = DeOldifyPredictor() 27 | deoldify.run("/home/aistudio/先烈.jpg") #原图片所在路径 28 | ``` 29 | *`run`接口为图片/视频通用接口,由于这里对象是图片,可以使用`run_image`的接口 30 | 31 | [完整API接口使用说明]() 32 | 33 | **2. 命令行预测** 34 | 35 | ``` 36 | !python applications/tools/video-enhance.py --input /home/aistudio/先烈.jpg \ #原图片路径 37 | --process_order DeOldify \ #对原图片处理的顺序 38 | --output output_dir #成品图片所在的路径 39 | ``` 40 | 41 | ### 在线项目体验 42 | **1. [老北京城影像修复](https://aistudio.baidu.com/aistudio/projectdetail/1161285)** 43 | 44 | **2. [PaddleGAN ❤️ 520特辑](https://aistudio.baidu.com/aistudio/projectdetail/1956943?channelType=0&channel=0)** 45 | -------------------------------------------------------------------------------- /deploy/cpp_infer/include/vsr.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "include/process_op.h" 13 | #include "paddle_inference_api.h" 14 | 15 | namespace PaddleGAN { 16 | 17 | class VSR { 18 | public: 19 | explicit VSR(const std::string& model_path, 20 | const std::string& param_path, 21 | const std::string& device, 22 | const int& gpu_id, 23 | const bool& use_mkldnn, 24 | const int& cpu_threads) { 25 | 26 | this->device_ = device; 27 | this->gpu_id_ = gpu_id; 28 | this->use_mkldnn_ = use_mkldnn_; 29 | this->cpu_threads_ = cpu_threads; 30 | 31 | LoadModel(model_path, param_path); 32 | } 33 | 34 | // Load paddle inference model 35 | void LoadModel(const std::string& model_path, const std::string& param_path); 36 | 37 | // Run predictor 38 | void Run(const std::vector& imgs, std::vector* result = nullptr); 39 | 40 | private: 41 | std::shared_ptr predictor_; 42 | 43 | std::string device_ = "GPU"; 44 | int gpu_id_ = 0; 45 | bool use_mkldnn_ = false; 46 | int cpu_threads_ = 1; 47 | 48 | std::vector mean_ = {0., 0., 0.}; 49 | std::vector scale_ = {1., 1., 1.}; 50 | 51 | // pre/post-process 52 | Permute permute_op_; 53 | Normalize normalize_op_; 54 | std::vector Preprocess(cv::Mat& img); 55 | }; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /configs/nafnet_denoising.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 400000 2 | output_dir: output_dir 3 | 4 | model: 5 | name: NAFNetModel 6 | generator: 7 | name: NAFNet 8 | img_channel: 3 9 | width: 64 10 | enc_blk_nums: [2, 2, 4, 8] 11 | middle_blk_num: 12 12 | dec_blk_nums: [2, 2, 2, 2] 13 | psnr_criterion: 14 | name: PSNRLoss 15 | 16 | dataset: 17 | train: 18 | name: NAFNetTrain 19 | rgb_dir: data/SIDD/train 20 | # TODO fix out of memory for val while training 21 | num_workers: 0 22 | batch_size: 8 # 8GPU 23 | img_options: 24 | patch_size: 256 25 | test: 26 | name: NAFNetVal 27 | rgb_dir: data/SIDD/val 28 | # TODO fix out of memory for val while training 29 | num_workers: 0 30 | batch_size: 1 31 | img_options: 32 | patch_size: 256 33 | 34 | export_model: 35 | - {name: 'generator', inputs_num: 1} 36 | 37 | lr_scheduler: 38 | name: CosineAnnealingRestartLR 39 | learning_rate: 0.001 40 | periods: [400000] 41 | restart_weights: [1] 42 | eta_min: !!float 8e-7 43 | 44 | validate: 45 | interval: 5000 46 | save_img: false 47 | 48 | metrics: 49 | psnr: # metric name, can be arbitrary 50 | name: PSNR 51 | crop_border: 4 52 | test_y_channel: True 53 | ssim: 54 | name: SSIM 55 | crop_border: 4 56 | test_y_channel: True 57 | 58 | optimizer: 59 | name: AdamW 60 | # add parameters of net_name to optim 61 | # name should in self.nets 62 | net_names: 63 | - generator 64 | weight_decay: 0.0 65 | beta1: 0.9 66 | beta2: 0.9 67 | epsilon: 1e-8 68 | 69 | log_config: 70 | interval: 10 71 | visiual_interval: 5000 72 | 73 | snapshot_config: 74 | interval: 5000 75 | -------------------------------------------------------------------------------- /ppgan/datasets/single_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .base_dataset import BaseDataset 16 | from .builder import DATASETS 17 | 18 | 19 | @DATASETS.register() 20 | class SingleDataset(BaseDataset): 21 | """ 22 | """ 23 | def __init__(self, dataroot, preprocess): 24 | """Initialize single dataset class. 25 | 26 | Args: 27 | dataroot (str): Directory of dataset. 28 | preprocess (list[dict]): A sequence of data preprocess config. 29 | """ 30 | super(SingleDataset, self).__init__(preprocess) 31 | self.dataroot = dataroot 32 | self.data_infos = self.prepare_data_infos() 33 | 34 | def prepare_data_infos(self): 35 | """prepare image paths from a folder. 36 | 37 | Returns: 38 | list[dict]: List that contains paired image paths. 39 | """ 40 | data_infos = [] 41 | paths = sorted(self.scan_folder(self.dataroot)) 42 | for path in paths: 43 | data_infos.append(dict(A_path=path)) 44 | 45 | return data_infos 46 | -------------------------------------------------------------------------------- /configs/cond_dcgan_mnist.yaml: -------------------------------------------------------------------------------- 1 | epochs: 200 2 | output_dir: output_dir 3 | 4 | model: 5 | name: GANModel 6 | generator: 7 | name: ConditionalDeepConvGenerator 8 | latent_dim: 128 9 | output_nc: 1 10 | size: 28 11 | ngf: 64 12 | n_class: 10 13 | discriminator: 14 | name: NLayerDiscriminatorWithClassification 15 | ndf: 16 16 | n_layers: 3 17 | input_nc: 1 18 | norm_type: batch 19 | n_class: 10 20 | use_sigmoid: True 21 | gan_criterion: 22 | name: GANLoss 23 | gan_mode: vanilla 24 | 25 | dataset: 26 | train: 27 | name: CommonVisionDataset 28 | dataset_name: MNIST 29 | num_workers: 4 30 | batch_size: 64 31 | return_label: True 32 | transforms: 33 | - name: Normalize 34 | mean: [127.5] 35 | std: [127.5] 36 | keys: [image] 37 | params: 38 | mode: train 39 | test: 40 | name: CommonVisionDataset 41 | dataset_name: MNIST 42 | num_workers: 0 43 | batch_size: 64 44 | return_label: True 45 | params: 46 | mode: train 47 | transforms: 48 | - name: Normalize 49 | mean: [127.5] 50 | std: [127.5] 51 | keys: [image] 52 | 53 | lr_scheduler: 54 | name: LinearDecay 55 | learning_rate: 0.0002 56 | start_epoch: 100 57 | decay_epochs: 100 58 | # will get from real dataset 59 | iters_per_epoch: 1 60 | 61 | optimizer: 62 | optimizer_G: 63 | name: Adam 64 | net_names: 65 | - netG 66 | beta1: 0.5 67 | optimizer_D: 68 | name: Adam 69 | net_names: 70 | - netD 71 | beta1: 0.5 72 | 73 | log_config: 74 | interval: 100 75 | visiual_interval: 500 76 | 77 | snapshot_config: 78 | interval: 5 79 | -------------------------------------------------------------------------------- /test_tipc/common_func.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | function func_parser_key(){ 4 | strs=$1 5 | IFS=":" 6 | array=(${strs}) 7 | tmp=${array[0]} 8 | echo ${tmp} 9 | } 10 | 11 | function func_parser_value(){ 12 | strs=$1 13 | IFS=":" 14 | array=(${strs}) 15 | tmp=${array[1]} 16 | echo ${tmp} 17 | } 18 | 19 | function func_set_params(){ 20 | key=$1 21 | value=$2 22 | if [ ${key}x = "null"x ];then 23 | echo " " 24 | elif [[ ${value} = "null" ]] || [[ ${value} = " " ]] || [ ${#value} -le 0 ];then 25 | echo " " 26 | else 27 | echo "${key}=${value}" 28 | fi 29 | } 30 | 31 | function func_parser_params(){ 32 | strs=$1 33 | IFS=":" 34 | array=(${strs}) 35 | key=${array[0]} 36 | tmp=${array[1]} 37 | IFS="|" 38 | res="" 39 | for _params in ${tmp[*]}; do 40 | IFS="=" 41 | array=(${_params}) 42 | mode=${array[0]} 43 | value=${array[1]} 44 | if [[ ${mode} = ${MODE} ]]; then 45 | IFS="|" 46 | #echo $(func_set_params "${mode}" "${value}") 47 | echo $value 48 | break 49 | fi 50 | IFS="|" 51 | done 52 | echo ${res} 53 | } 54 | 55 | function status_check(){ 56 | last_status=$1 # the exit code 57 | run_command=$2 58 | run_log=$3 59 | model_name=$4 60 | log_path=$5 61 | if [ $last_status -eq 0 ]; then 62 | echo -e "\033[33m Run successfully with command - ${model_name} - ${run_command} - ${log_path} \033[0m" | tee -a ${run_log} 63 | else 64 | echo -e "\033[33m Run failed with command - ${model_name} - ${run_command} - ${log_path} \033[0m" | tee -a ${run_log} 65 | fi 66 | } 67 | -------------------------------------------------------------------------------- /configs/dcgan_mnist.yaml: -------------------------------------------------------------------------------- 1 | epochs: 200 2 | output_dir: output_dir 3 | 4 | model: 5 | name: DCGANModel 6 | generator: 7 | name: DCGenerator 8 | norm_type: batch 9 | input_nz: 100 10 | input_nc: 1 11 | output_nc: 1 12 | ngf: 64 13 | discriminator: 14 | name: DCDiscriminator 15 | norm_type: batch 16 | ndf: 64 17 | input_nc: 1 18 | gan_criterion: 19 | name: GANLoss 20 | gan_mode: vanilla 21 | 22 | dataset: 23 | train: 24 | name: CommonVisionDataset 25 | dataset_name: MNIST 26 | num_workers: 0 27 | batch_size: 128 28 | return_label: False 29 | transforms: 30 | - name: Resize 31 | size: [64, 64] 32 | interpolation: 'bicubic' #cv2.INTER_CUBIC 33 | - name: Normalize 34 | mean: [127.5] 35 | std: [127.5] 36 | keys: [image] 37 | test: 38 | name: CommonVisionDataset 39 | dataset_name: MNIST 40 | num_workers: 0 41 | batch_size: 128 42 | return_label: False 43 | transforms: 44 | - name: Resize 45 | size: [64, 64] 46 | interpolation: 'bicubic' #cv2.INTER_CUBIC 47 | - name: Normalize 48 | mean: [127.5] 49 | std: [127.5] 50 | keys: [image] 51 | 52 | lr_scheduler: 53 | name: LinearDecay 54 | learning_rate: 0.0002 55 | start_epoch: 100 56 | decay_epochs: 100 57 | # will get from real dataset 58 | iters_per_epoch: 1 59 | 60 | optimizer: 61 | optimizer_G: 62 | name: Adam 63 | net_names: 64 | - netG 65 | beta1: 0.5 66 | optimizer_D: 67 | name: Adam 68 | net_names: 69 | - netD 70 | beta1: 0.5 71 | 72 | log_config: 73 | interval: 100 74 | visiual_interval: 500 75 | 76 | snapshot_config: 77 | interval: 5 78 | -------------------------------------------------------------------------------- /ppgan/datasets/paired_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .builder import DATASETS 16 | from .base_dataset import BaseDataset 17 | 18 | 19 | @DATASETS.register() 20 | class PairedDataset(BaseDataset): 21 | """A dataset class for paired image dataset. 22 | """ 23 | def __init__(self, dataroot, preprocess): 24 | """Initialize this dataset class. 25 | 26 | Args: 27 | dataroot (str): Directory of dataset. 28 | preprocess (list[dict]): A sequence of data preprocess config. 29 | 30 | """ 31 | super(PairedDataset, self).__init__(preprocess) 32 | self.dataroot = dataroot 33 | self.data_infos = self.prepare_data_infos() 34 | 35 | def prepare_data_infos(self): 36 | """Load paired image paths. 37 | 38 | Returns: 39 | list[dict]: List that contains paired image paths. 40 | """ 41 | data_infos = [] 42 | pair_paths = sorted(self.scan_folder(self.dataroot)) 43 | for pair_path in pair_paths: 44 | data_infos.append(dict(pair_path=pair_path)) 45 | 46 | return data_infos 47 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/ugatit.md: -------------------------------------------------------------------------------- 1 | # 1 U-GAT-IT 2 | 3 | ## 1.1 原理介绍 4 | 5 | 与CycleGAN类似,[U-GAT-IT](https://arxiv.org/abs/1907.10830)使用未配对的图片进行图像风格转换,输入两个不同风格的图像,U-GAT-IT自动执行风格转换。不同的是,U-GAT-IT在历史研究的基础上以端到端的方式引入了一个新的注意模块和一个新的可学习的归一化函数。 6 | 7 | ## 1.2 如何使用 8 | 9 | ### 1.2.1 数据准备 10 | 11 | U-GAT-IT使用的Selfie2anime数据集可以从[这里](https://www.kaggle.com/arnaud58/selfie2anime)下载,您也可以使用自己的数据集。 12 | 13 | 数据的组成形式为: 14 | 15 | ``` 16 | ├── dataset 17 | └── YOUR_DATASET_NAME 18 | ├── trainA 19 | ├── trainB 20 | ├── testA 21 | └── testB 22 | ``` 23 | 24 | ### 1.2.2 训练/测试 25 | 26 | 示例以selfie2anime数据集为例。如果您想使用自己的数据集,可以在配置文件中修改数据集为您自己的数据集。 27 | 28 | 训练模型: 29 | ``` 30 | python -u tools/main.py --config-file configs/ugatit_selfie2anime_light.yaml 31 | ``` 32 | 33 | 测试模型: 34 | ``` 35 | python tools/main.py --config-file configs/ugatit_selfie2anime_light.yaml --evaluate-only --load ${PATH_OF_WEIGHT} 36 | ``` 37 | 38 | ## 1.3 结果展示 39 | 40 | ![](../../imgs/ugatit.png) 41 | 42 | ## 1.4 模型下载 43 | | 模型 | 数据集 | 下载地址 | 44 | |---|---|---| 45 | | ugatit_light | selfie2anime | [ugatit_light](https://paddlegan.bj.bcebos.com/models/ugatit_light.pdparams) 46 | 47 | 48 | 49 | 50 | # 参考文献 51 | 52 | - 1. [U-GAT-IT: Unsupervised Generative Attentional Networks with Adaptive Layer-Instance Normalization for Image-to-Image Translation](https://arxiv.org/abs/1907.10830) 53 | 54 | ``` 55 | @article{kim2019u, 56 | title={U-GAT-IT: unsupervised generative attentional networks with adaptive layer-instance normalization for image-to-image translation}, 57 | author={Kim, Junho and Kim, Minjae and Kang, Hyeonwoo and Lee, Kwanghee}, 58 | journal={arXiv preprint arXiv:1907.10830}, 59 | year={2019} 60 | } 61 | ``` 62 | -------------------------------------------------------------------------------- /ppgan/models/criterions/builder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 16 | # 17 | # Licensed under the Apache License, Version 2.0 (the "License"); 18 | # you may not use this file except in compliance with the License. 19 | # You may obtain a copy of the License at 20 | # 21 | # http://www.apache.org/licenses/LICENSE-2.0 22 | # 23 | # Unless required by applicable law or agreed to in writing, software 24 | # distributed under the License is distributed on an "AS IS" BASIS, 25 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 | # See the License for the specific language governing permissions and 27 | # limitations under the License. 28 | 29 | from ...utils.registry import Registry 30 | 31 | CRITERIONS = Registry('CRITERION') 32 | 33 | 34 | def build_criterion(cfg): 35 | cfg_ = cfg.copy() 36 | name = cfg_.pop('name') 37 | try: 38 | criterion = CRITERIONS.get(name)(**cfg_) 39 | except Exception as e: 40 | cls_ = CRITERIONS.get(name) 41 | raise RuntimeError('class {} {}'.format(cls_.__name__, e)) 42 | return criterion 43 | -------------------------------------------------------------------------------- /ppgan/models/criterions/gradient_penalty.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import paddle 16 | from .builder import CRITERIONS 17 | 18 | 19 | @CRITERIONS.register() 20 | class GradientPenalty(): 21 | def __init__(self, loss_weight=1.0): 22 | self.loss_weight = loss_weight 23 | 24 | def __call__(self, net, real, fake): 25 | batch_size = real.shape[0] 26 | alpha = paddle.rand([batch_size]) 27 | for _ in range(real.ndim - 1): 28 | alpha = paddle.unsqueeze(alpha, -1) 29 | interpolate = alpha * real + (1 - alpha) * fake 30 | interpolate.stop_gradient = False 31 | interpolate_pred = net(interpolate) 32 | gradient = paddle.grad(outputs=interpolate_pred, 33 | inputs=interpolate, 34 | grad_outputs=paddle.ones_like(interpolate_pred), 35 | create_graph=True, 36 | retain_graph=True, 37 | only_inputs=True)[0] 38 | gradient_penalty = ((gradient.norm(2, 1) - 1) ** 2).mean() 39 | return gradient_penalty * self.loss_weight 40 | -------------------------------------------------------------------------------- /test_tipc/test_train_inference_python_npu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source test_tipc/common_func.sh 3 | 4 | function readlinkf() { 5 | perl -MCwd -e 'print Cwd::abs_path shift' "$1"; 6 | } 7 | 8 | function func_parser_config() { 9 | strs=$1 10 | IFS=" " 11 | array=(${strs}) 12 | tmp=${array[2]} 13 | echo ${tmp} 14 | } 15 | 16 | BASEDIR=$(dirname "$0") 17 | REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../) 18 | 19 | FILENAME=$1 20 | 21 | # change gpu to npu in tipc txt configs 22 | sed -i "s/state=GPU/state=NPU/g" $FILENAME 23 | sed -i "s/--device:gpu/--device:npu/g" $FILENAME 24 | sed -i "s/--benchmark:True/--benchmark:False/g" $FILENAME 25 | # python has been updated to version 3.9 for npu backend 26 | sed -i "s/python3.7/python3.9/g" $FILENAME 27 | dataline=`cat $FILENAME` 28 | 29 | # parser params 30 | IFS=$'\n' 31 | lines=(${dataline}) 32 | 33 | # change total iters/epochs for npu/xpu to accelaration 34 | modelname=$(func_parser_value "${lines[1]}") 35 | echo $modelname 36 | if [ $modelname == "Pix2pix" ]; then 37 | sed -i "s/lite_train_lite_infer=10/lite_train_lite_infer=1/g" $FILENAME 38 | sed -i "s/-o log_config.interval=1/-o log_config.interval=1 snapshot_config.interval=1/g" $FILENAME 39 | fi 40 | 41 | if [ $modelname == "edvr" ]; then 42 | sed -i "s/lite_train_lite_infer=100/lite_train_lite_infer=10/g" $FILENAME 43 | sed -i "s/snapshot_config.interval=25/snapshot_config.interval=5/g" $FILENAME 44 | fi 45 | 46 | # change gpu to npu in execution script 47 | sed -i 's/\"gpu\"/\"npu\"/g' test_tipc/test_train_inference_python.sh 48 | sed -i 's/--gpus/--npus/g' test_tipc/test_train_inference_python.sh 49 | 50 | # pass parameters to test_train_inference_python.sh 51 | cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2" 52 | echo $cmd 53 | eval $cmd 54 | -------------------------------------------------------------------------------- /test_tipc/test_train_inference_python_xpu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | source test_tipc/common_func.sh 3 | 4 | function readlinkf() { 5 | perl -MCwd -e 'print Cwd::abs_path shift' "$1"; 6 | } 7 | 8 | function func_parser_config() { 9 | strs=$1 10 | IFS=" " 11 | array=(${strs}) 12 | tmp=${array[2]} 13 | echo ${tmp} 14 | } 15 | 16 | BASEDIR=$(dirname "$0") 17 | REPO_ROOT_PATH=$(readlinkf ${BASEDIR}/../) 18 | 19 | FILENAME=$1 20 | 21 | # change gpu to npu in tipc txt configs 22 | sed -i "s/state=GPU/state=XPU/g" $FILENAME 23 | sed -i "s/--device:gpu/--device:xpu/g" $FILENAME 24 | sed -i "s/--benchmark:True/--benchmark:False/g" $FILENAME 25 | # python has been updated to version 3.9 for npu backend 26 | sed -i "s/python3.7/python3.9/g" $FILENAME 27 | dataline=`cat $FILENAME` 28 | 29 | # change gpu to npu in execution script 30 | sed -i 's/\"gpu\"/\"xpu\"/g' test_tipc/test_train_inference_python.sh 31 | sed -i 's/--gpus/--xpus/g' test_tipc/test_train_inference_python.sh 32 | 33 | # parser params 34 | IFS=$'\n' 35 | lines=(${dataline}) 36 | 37 | # change total iters/epochs for npu/xpu to accelaration 38 | modelname=$(func_parser_value "${lines[1]}") 39 | echo $modelname 40 | if [ $modelname == "Pix2pix" ]; then 41 | sed -i "s/lite_train_lite_infer=10/lite_train_lite_infer=1/g" $FILENAME 42 | sed -i "s/-o log_config.interval=1/-o log_config.interval=1 snapshot_config.interval=1/g" $FILENAME 43 | fi 44 | 45 | if [ $modelname == "edvr" ]; then 46 | sed -i "s/lite_train_lite_infer=100/lite_train_lite_infer=10/g" $FILENAME 47 | sed -i "s/snapshot_config.interval=25/snapshot_config.interval=5/g" $FILENAME 48 | fi 49 | 50 | # pass parameters to test_train_inference_python.sh 51 | cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} $2" 52 | echo $cmd 53 | eval $cmd 54 | -------------------------------------------------------------------------------- /configs/singan_animation.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 100000 2 | output_dir: output_dir 3 | export_model: null 4 | 5 | model: 6 | name: SinGANModel 7 | generator: 8 | name: SinGANGenerator 9 | nfc_init: 32 10 | min_nfc_init: 32 11 | noise_zero_pad: False 12 | discriminator: 13 | name: SinGANDiscriminator 14 | nfc_init: 32 15 | min_nfc_init: 32 16 | gan_criterion: 17 | name: GANLoss 18 | gan_mode: wgangp 19 | loss_weight: 1.0 20 | recon_criterion: 21 | name: MSELoss 22 | loss_weight: 10.0 23 | gp_criterion: 24 | name: GradientPenalty 25 | loss_weight: 0.1 26 | train_image: data/singan/stone.png 27 | scale_factor: 0.75 28 | min_size: 25 29 | is_finetune: False 30 | 31 | dataset: 32 | train: 33 | name: EmptyDataset 34 | test: 35 | name: SingleDataset 36 | dataroot: data/singan 37 | num_workers: 0 38 | batch_size: 1 39 | preprocess: 40 | - name: LoadImageFromFile 41 | key: A 42 | - name: Transforms 43 | input_keys: [A] 44 | pipeline: 45 | - name: Transpose 46 | - name: Normalize 47 | mean: [127.5, 127.5, 127.5] 48 | std: [127.5, 127.5, 127.5] 49 | 50 | lr_scheduler: 51 | name: MultiStepDecay 52 | learning_rate: 0.0005 53 | milestones: [9600] 54 | gamma: 0.1 55 | 56 | optimizer: 57 | optimizer_G: 58 | name: Adam 59 | beta1: 0.5 60 | beta2: 0.999 61 | optimizer_D: 62 | name: Adam 63 | beta1: 0.5 64 | beta2: 0.999 65 | 66 | log_config: 67 | interval: 100 68 | visiual_interval: 2000 69 | 70 | snapshot_config: 71 | interval: 10000 72 | 73 | validate: 74 | interval: -1 75 | save_img: True 76 | metrics: 77 | fid: # metric name, can be arbitrary 78 | name: FID 79 | batch_size: 1 80 | -------------------------------------------------------------------------------- /configs/singan_universal.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 100000 2 | output_dir: output_dir 3 | export_model: null 4 | 5 | model: 6 | name: SinGANModel 7 | generator: 8 | name: SinGANGenerator 9 | nfc_init: 32 10 | min_nfc_init: 32 11 | noise_zero_pad: True 12 | discriminator: 13 | name: SinGANDiscriminator 14 | nfc_init: 32 15 | min_nfc_init: 32 16 | gan_criterion: 17 | name: GANLoss 18 | gan_mode: wgangp 19 | loss_weight: 1.0 20 | recon_criterion: 21 | name: MSELoss 22 | loss_weight: 10.0 23 | gp_criterion: 24 | name: GradientPenalty 25 | loss_weight: 0.1 26 | train_image: data/singan/stone.png 27 | scale_factor: 0.75 28 | min_size: 25 29 | is_finetune: False 30 | 31 | dataset: 32 | train: 33 | name: EmptyDataset 34 | test: 35 | name: SingleDataset 36 | dataroot: data/singan 37 | num_workers: 0 38 | batch_size: 1 39 | preprocess: 40 | - name: LoadImageFromFile 41 | key: A 42 | - name: Transforms 43 | input_keys: [A] 44 | pipeline: 45 | - name: Transpose 46 | - name: Normalize 47 | mean: [127.5, 127.5, 127.5] 48 | std: [127.5, 127.5, 127.5] 49 | 50 | lr_scheduler: 51 | name: MultiStepDecay 52 | learning_rate: 0.0005 53 | milestones: [9600] 54 | gamma: 0.1 55 | 56 | optimizer: 57 | optimizer_G: 58 | name: Adam 59 | beta1: 0.5 60 | beta2: 0.999 61 | optimizer_D: 62 | name: Adam 63 | beta1: 0.5 64 | beta2: 0.999 65 | 66 | log_config: 67 | interval: 100 68 | visiual_interval: 2000 69 | 70 | snapshot_config: 71 | interval: 10000 72 | 73 | validate: 74 | interval: -1 75 | save_img: True 76 | metrics: 77 | fid: # metric name, can be arbitrary 78 | name: FID 79 | batch_size: 1 80 | -------------------------------------------------------------------------------- /docs/zh_CN/install.md: -------------------------------------------------------------------------------- 1 | # 安装文档 2 | 本文档包含了如何安装PaddleGAN以及相关依赖,更多产品简介请参考[README](https://github.com/PaddlePaddle/PaddleGAN/blob/develop/README_cn.md)。 3 | 4 | ## 环境依赖 5 | 6 | - PaddlePaddle >= 2.1.0 7 | - Python >= 3.6 8 | - CUDA >= 10.1 9 | 10 | 11 | ## 安装PaddlePaddle 12 | 13 | ``` 14 | 15 | # CUDA10.1 16 | python -m pip install paddlepaddle-gpu==2.1.0.post101 -f https://mirror.baidu.com/pypi/simple 17 | 18 | # CPU 19 | python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple 20 | 21 | ``` 22 | 23 | 更多安装方式例如conda或源码编译安装方法,请参考[PaddlePaddle安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/install/index_cn.html)。 24 | 25 | 请确保您的PaddlePaddle安装成功并且版本不低于需求版本。使用以下命令进行验证。 26 | 27 | ``` 28 | # 在您的Python解释器中确认PaddlePaddle安装成功 29 | >>> import paddle 30 | >>> paddle.utils.run_check() 31 | 32 | # 确认PaddlePaddle版本 33 | python -c "import paddle; print(paddle.__version__)" 34 | ``` 35 | 36 | ## 安装PaddleGAN 37 | 38 | ### 通过PIP安裝(只支持Python3) 39 | 40 | * 安装: 41 | ``` 42 | python3 -m pip install --upgrade ppgan 43 | ``` 44 | * 下载示例和配置文件: 45 | 46 | ``` 47 | git clone https://github.com/PaddlePaddle/PaddleGAN 48 | cd PaddleGAN 49 | ``` 50 | ### 通过源码安装 51 | 52 | ``` 53 | git clone https://github.com/PaddlePaddle/PaddleGAN 54 | cd PaddleGAN 55 | 56 | pip install -v -e . # or "python setup.py develop" 57 | 58 | # 安装其他依赖 59 | pip install -r requirements.txt 60 | ``` 61 | ## 其他第三方工具安装 62 | 63 | * 涉及视频的任务都需安装**ffmpeg**,这里推荐使用[conda](https://docs.conda.io/en/latest/miniconda.html)安装: 64 | 65 | ``` 66 | conda install x264=='1!152.20180717' ffmpeg=4.0.2 -c conda-forge 67 | ``` 68 | 69 | * 如需使用可视化工具监控训练过程,请安装[飞桨VisualDL](https://github.com/PaddlePaddle/VisualDL): 70 | ``` 71 | python -m pip install visualdl -i https://mirror.baidu.com/pypi/simple 72 | ``` 73 | *注意:VisualDL目前只维护Python3以上的安装版本 74 | -------------------------------------------------------------------------------- /configs/singan_sr.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 100000 2 | output_dir: output_dir 3 | export_model: null 4 | 5 | model: 6 | name: SinGANModel 7 | generator: 8 | name: SinGANGenerator 9 | nfc_init: 32 10 | min_nfc_init: 32 11 | noise_zero_pad: True 12 | discriminator: 13 | name: SinGANDiscriminator 14 | nfc_init: 32 15 | min_nfc_init: 32 16 | gan_criterion: 17 | name: GANLoss 18 | gan_mode: wgangp 19 | loss_weight: 1.0 20 | recon_criterion: 21 | name: MSELoss 22 | loss_weight: 100.0 23 | gp_criterion: 24 | name: GradientPenalty 25 | loss_weight: 0.1 26 | train_image: data/singan/stone.png 27 | scale_factor: 0.793701 # (1/2)^(1/3) 28 | min_size: 18 29 | is_finetune: False 30 | 31 | dataset: 32 | train: 33 | name: EmptyDataset 34 | test: 35 | name: SingleDataset 36 | dataroot: data/singan 37 | num_workers: 0 38 | batch_size: 1 39 | preprocess: 40 | - name: LoadImageFromFile 41 | key: A 42 | - name: Transforms 43 | input_keys: [A] 44 | pipeline: 45 | - name: Transpose 46 | - name: Normalize 47 | mean: [127.5, 127.5, 127.5] 48 | std: [127.5, 127.5, 127.5] 49 | 50 | lr_scheduler: 51 | name: MultiStepDecay 52 | learning_rate: 0.0005 53 | milestones: [9600] 54 | gamma: 0.1 55 | 56 | optimizer: 57 | optimizer_G: 58 | name: Adam 59 | beta1: 0.5 60 | beta2: 0.999 61 | optimizer_D: 62 | name: Adam 63 | beta1: 0.5 64 | beta2: 0.999 65 | 66 | log_config: 67 | interval: 100 68 | visiual_interval: 2000 69 | 70 | snapshot_config: 71 | interval: 10000 72 | 73 | validate: 74 | interval: -1 75 | save_img: True 76 | metrics: 77 | fid: # metric name, can be arbitrary 78 | name: FID 79 | batch_size: 1 80 | -------------------------------------------------------------------------------- /ppgan/datasets/repeat_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import paddle 16 | 17 | 18 | class RepeatDataset(paddle.io.Dataset): 19 | """A wrapper of repeated dataset. 20 | 21 | The length of repeated dataset will be `times` larger than the original 22 | dataset. This is useful when the data loading time is long but the dataset 23 | is small. Using RepeatDataset can reduce the data loading time between 24 | epochs. 25 | 26 | Args: 27 | dataset (:obj:`Dataset`): The dataset to be repeated. 28 | times (int): Repeat times. 29 | """ 30 | 31 | def __init__(self, dataset, times): 32 | self.dataset = dataset 33 | self.times = times 34 | 35 | self._ori_len = len(self.dataset) 36 | 37 | def __getitem__(self, idx): 38 | """Get item at each call. 39 | 40 | Args: 41 | idx (int): Index for getting each item. 42 | """ 43 | return self.dataset[idx % self._ori_len] 44 | 45 | def __len__(self): 46 | """Length of the dataset. 47 | 48 | Returns: 49 | int: Length of the dataset. 50 | """ 51 | return self.times * self._ori_len 52 | -------------------------------------------------------------------------------- /configs/invdn_denoising.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 150000 2 | output_dir: output_dir 3 | # tensor range for function tensor2img 4 | min_max: 5 | (0., 1.) 6 | 7 | model: 8 | name: InvDNModel 9 | generator: 10 | name: InvDN 11 | channel_in: 3 12 | channel_out: 3 13 | block_num: [8, 8] 14 | scale: 4 15 | down_num: 2 16 | 17 | dataset: 18 | train: 19 | name: InvDNDataset 20 | # TODO fix out of memory for val while training 21 | num_workers: 0 22 | batch_size: 14 # 4 GPUs 23 | opt: 24 | phase: train 25 | scale: 4 26 | crop_size: 144 27 | train_dir: data/SIDD_Medium_Srgb_Patches_512/train/ 28 | test: 29 | name: InvDNDataset 30 | # TODO fix out of memory for val while training 31 | num_workers: 0 32 | batch_size: 1 33 | opt: 34 | phase: test 35 | scale: 4 36 | val_dir: data/SIDD_Valid_Srgb_Patches_256/valid/ 37 | 38 | export_model: 39 | - {name: 'generator', inputs_num: 1} 40 | 41 | lr_scheduler: 42 | name: MultiStepDecay 43 | learning_rate: 8e-4 # num_gpu * 2e-4 44 | milestones: [25000, 50000, 75000, 100000, 125000, 135000, 145000] 45 | gamma: 0.5 46 | 47 | validate: 48 | interval: 500 49 | save_img: True 50 | 51 | metrics: 52 | psnr: # metric name, can be arbitrary 53 | name: PSNR 54 | crop_border: 4 55 | test_y_channel: True 56 | ssim: 57 | name: SSIM 58 | crop_border: 4 59 | test_y_channel: True 60 | 61 | optimizer: 62 | name: Adam 63 | # add parameters of net_name to optim 64 | # name should in self.nets 65 | net_names: 66 | - generator 67 | beta1: 0.9 68 | beta2: 0.99 69 | epsilon: 1e-8 70 | clip_grad_norm: 10 71 | 72 | log_config: 73 | interval: 100 74 | visiual_interval: 5000 75 | 76 | snapshot_config: 77 | interval: 500 78 | -------------------------------------------------------------------------------- /education/homework1.md: -------------------------------------------------------------------------------- 1 | # GAN基础理论客观题 2 | 3 | **1. (多选)GAN的基础结构包括()** 4 | 5 | A. 生成器 6 | 7 | B. 判别器 8 | 9 | C. 编码器 10 | 11 | D. 解码器 12 | 13 | 参考答案:AB 14 | 15 | **2.(多选)GAN的应用包括()** 16 | 17 | A. 换脸 18 | 19 | B. 动作迁移 20 | 21 | C. 图像翻译 22 | 23 | D. 超分辨率 24 | 25 | 参考答案:ABCD 26 | 27 | **3. (多选)生成对抗网络中的生成模型可以()** 28 | 29 | A. 输入噪声生成图像 30 | 31 | B. 输入噪声和标签生成图像 32 | 33 | C. 输入图像生成图像 34 | 35 | D. 输入文字描述生成图像 36 | 37 | 参考答案:ABCD 38 | 39 | **4. (单选)下列哪一项是GAN的判别器的损失函数()** 40 | 41 | A. ![img](https://user-images.githubusercontent.com/48054808/115173831-b1315300-a0fa-11eb-9616-c8bd39dd74eb.png) 42 | 43 | B. ![img](https://user-images.githubusercontent.com/48054808/115173874-c6a67d00-a0fa-11eb-8447-722a4d0993ca.png) 44 | 45 | C. ![img](https://user-images.githubusercontent.com/48054808/115173903-d1f9a880-a0fa-11eb-90bd-7c143367444b.png) 46 | 47 | D. ![img](https://user-images.githubusercontent.com/48054808/115173943-e178f180-a0fa-11eb-87ce-ba3e96e51572.png) 48 | 49 | 50 | 参考答案:C 51 | 52 | 53 | **5. (多选)下列关于GAN中对抗的描述正确的是()** 54 | 55 | A. 生成器与判别器互相对抗,在对抗中增强 56 | 57 | B. 两个神经网络通过相互博弈的方式进行学习 58 | 59 | C. 像警察与假钞,在对抗中增强警察的鉴别能力和小偷造假能力 60 | 61 | D. 像自然界中捕食者与被捕食者在对抗中的进化 62 | 63 | 参考答案:ABCD 64 | 65 | **6. (多选)下列关于GAN的描述正确的是()** 66 | 67 | A. 生成网络希望Fake image的score尽可能的大 68 | 69 | B. 生成网络希望Fake image的score尽可能的小 70 | 71 | C. 判别网络希望Fake image的score尽可能的大 72 | 73 | D. 判别网络希望Fake image的score尽可能的小 74 | 75 | 参考答案:AD 76 | 77 | **7. (多选)下列关于DCGAN的说法正确的有()** 78 | 79 | A. 使用卷积代替全连接层 80 | 81 | B. 添加BatchNorm 82 | 83 | C. 在生成器中使用Relu 84 | 85 | D. 在判别器中使用Relu 86 | 87 | 参考答案:ABC 88 | 89 | **8. GAN和auto encoder结构本质的区别是()** 90 | 91 | A. 网络结构不同 92 | 93 | B. 输入不同 94 | 95 | C. 对数据集的要求不同 96 | 97 | D. 损失函数不同 98 | 99 | 参考答案:D 100 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | 8 | # Distribution / packaging 9 | .Python 10 | build/ 11 | develop-eggs/ 12 | dist/ 13 | downloads/ 14 | eggs/ 15 | .eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | wheels/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | MANIFEST 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *.cover 46 | .hypothesis/ 47 | .pytest_cache/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | db.sqlite3 57 | 58 | # Flask stuff: 59 | instance/ 60 | .webassets-cache 61 | 62 | # Scrapy stuff: 63 | .scrapy 64 | 65 | # Sphinx documentation 66 | docs/_build/ 67 | 68 | # PyBuilder 69 | target/ 70 | 71 | # Jupyter Notebook 72 | .ipynb_checkpoints 73 | 74 | # pyenv 75 | .python-version 76 | 77 | # celery beat schedule file 78 | celerybeat-schedule 79 | 80 | # SageMath parsed files 81 | *.sage.py 82 | 83 | # Environments 84 | .env 85 | .venv 86 | env/ 87 | venv/ 88 | ENV/ 89 | env.bak/ 90 | venv.bak/ 91 | 92 | # Spyder project settings 93 | .spyderproject 94 | .spyproject 95 | 96 | # Rope project settings 97 | .ropeproject 98 | 99 | # mkdocs documentation 100 | /site 101 | 102 | # mypy 103 | .mypy_cache/ 104 | 105 | # data 106 | data/ 107 | output_dir/ 108 | 109 | # macos 110 | .DS_Store -------------------------------------------------------------------------------- /configs/lapstyle_rev_first.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 30000 2 | output_dir: output_dir 3 | checkpoints_dir: checkpoints 4 | min_max: 5 | (0., 1.) 6 | 7 | model: 8 | name: LapStyleRevFirstModel 9 | revnet_generator: 10 | name: RevisionNet 11 | revnet_discriminator: 12 | name: LapStyleDiscriminator 13 | draftnet_encode: 14 | name: Encoder 15 | draftnet_decode: 16 | name: DecoderNet 17 | calc_style_emd_loss: 18 | name: CalcStyleEmdLoss 19 | calc_content_relt_loss: 20 | name: CalcContentReltLoss 21 | calc_content_loss: 22 | name: CalcContentLoss 23 | calc_style_loss: 24 | name: CalcStyleLoss 25 | gan_criterion: 26 | name: GANLoss 27 | gan_mode: vanilla 28 | content_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 29 | style_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 30 | content_weight: 1.0 31 | style_weight: 3.0 32 | 33 | dataset: 34 | train: 35 | name: LapStyleDataset 36 | content_root: data/coco/train2017/ 37 | style_root: data/starrynew.png 38 | load_size: 280 39 | crop_size: 256 40 | num_workers: 16 41 | batch_size: 5 #1 GPUs 42 | test: 43 | name: LapStyleDataset 44 | content_root: data/coco/test2017/ 45 | style_root: data/starrynew.png 46 | load_size: 256 47 | crop_size: 256 48 | num_workers: 0 49 | batch_size: 1 50 | 51 | lr_scheduler: 52 | name: NonLinearDecay 53 | learning_rate: 1e-4 54 | lr_decay: 5e-5 55 | 56 | optimizer: 57 | optimG: 58 | name: Adam 59 | net_names: 60 | - net_rev 61 | beta1: 0.9 62 | beta2: 0.999 63 | optimD: 64 | name: Adam 65 | net_names: 66 | - netD 67 | beta1: 0.9 68 | beta2: 0.999 69 | 70 | validate: 71 | interval: 500 72 | save_img: false 73 | 74 | log_config: 75 | interval: 10 76 | visiual_interval: 500 77 | 78 | snapshot_config: 79 | interval: 5000 80 | -------------------------------------------------------------------------------- /configs/lapstyle_rev_second.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 30000 2 | output_dir: output_dir 3 | checkpoints_dir: checkpoints 4 | min_max: 5 | (0., 1.) 6 | 7 | model: 8 | name: LapStyleRevSecondModel 9 | revnet_generator: 10 | name: RevisionNet 11 | revnet_discriminator: 12 | name: LapStyleDiscriminator 13 | draftnet_encode: 14 | name: Encoder 15 | draftnet_decode: 16 | name: DecoderNet 17 | calc_style_emd_loss: 18 | name: CalcStyleEmdLoss 19 | calc_content_relt_loss: 20 | name: CalcContentReltLoss 21 | calc_content_loss: 22 | name: CalcContentLoss 23 | calc_style_loss: 24 | name: CalcStyleLoss 25 | gan_criterion: 26 | name: GANLoss 27 | gan_mode: vanilla 28 | content_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 29 | style_layers: ['r11', 'r21', 'r31', 'r41', 'r51'] 30 | content_weight: 1.0 31 | style_weight: 3.0 32 | 33 | dataset: 34 | train: 35 | name: LapStyleDataset 36 | content_root: data/coco/train2017/ 37 | style_root: data/starrynew.png 38 | load_size: 540 39 | crop_size: 512 40 | num_workers: 16 41 | batch_size: 2 #1 GPUs 42 | test: 43 | name: LapStyleDataset 44 | content_root: data/coco/test2017/ 45 | style_root: data/starrynew.png 46 | load_size: 512 47 | crop_size: 512 48 | num_workers: 0 49 | batch_size: 1 50 | 51 | lr_scheduler: 52 | name: NonLinearDecay 53 | learning_rate: 1e-4 54 | lr_decay: 5e-5 55 | 56 | optimizer: 57 | optimG: 58 | name: Adam 59 | net_names: 60 | - net_rev_2 61 | beta1: 0.9 62 | beta2: 0.999 63 | optimD: 64 | name: Adam 65 | net_names: 66 | - netD 67 | beta1: 0.9 68 | beta2: 0.999 69 | 70 | validate: 71 | interval: 500 72 | save_img: false 73 | 74 | log_config: 75 | interval: 10 76 | visiual_interval: 500 77 | 78 | snapshot_config: 79 | interval: 5000 80 | -------------------------------------------------------------------------------- /docs/zh_CN/industrial_solution/video_frame_cn.md: -------------------------------------------------------------------------------- 1 | # 视频补帧 2 | 3 | 针对老视频的流畅度提升,PaddleGAN提供了[DAIN](https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/en_US/apis/apps.md#ppganappsdainpredictor)模型接口。 4 | 5 | ## DAIN 6 | 7 | [DAIN](https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/en_US/apis/apps.md#ppganappsdainpredictor)模型通过探索深度的信息来显式检测遮挡。并且开发了一个深度感知的流投影层来合成中间流。在视频补帧方面有较好的效果。 8 | 9 |
10 | 11 |
12 | 13 | ``` 14 | ppgan.apps.DAINPredictor( 15 | output='output', 16 | weight_path=None, 17 | time_step=None, 18 | use_gpu=True, 19 | remove_duplicates=False) 20 | ``` 21 | ### 参数 22 | 23 | - `output (str,可选的)`: 输出的文件夹路径,默认值:`output`. 24 | - `weight_path (None,可选的)`: 载入的权重路径,如果没有设置,则从云端下载默认的权重到本地。默认值:`None`。 25 | - `time_step (int)`: 补帧的时间系数,如果设置为0.5,则原先为每秒30帧的视频,补帧后变为每秒60帧。 26 | - `remove_duplicates (bool,可选的)`: 是否删除重复帧,默认值:`False`. 27 | 28 | ### 使用方式 29 | **1. API预测** 30 | 31 | 除了定义输入视频路径外,此接口还需定义time_step,同时,目前API预测方式只支持在静态图下运行,需加上启动静态图命令,后续会支持动态图,敬请期待~ 32 | 33 | ``` 34 | paddle.enable_static() 35 | 36 | from ppgan.apps import DAINPredictor 37 | dain = DAINPredictor(output='output', time_step=0.5) 38 | # 测试一个视频文件 39 | dain.run("/home/aistudio/Peking_input360p_clip6_5s.mp4",) 40 | paddle.disable_static() 41 | 42 | paddle.disable_static() 43 | 44 | ``` 45 | 46 | **2. 命令行预测** 47 | 48 | ``` 49 | !python applications/tools/video-enhance.py --input /home/aistudio/Peking_input360p_clip6_5s.mp4 \ #原视频路径 50 | --process_order DAIN \ 51 | --output output_dir #成品视频所在的路径 52 | ``` 53 | ### 在线项目体验 54 | **1. [老北京城影像修复](https://aistudio.baidu.com/aistudio/projectdetail/1161285)** 55 | -------------------------------------------------------------------------------- /test_tipc/configs/msvsr/train_linux_gpu_normal_amp_infer_python_linux_gpu_cpu.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:msvsr 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:msvsr_reds*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/msvsr_reds/test 13 | null:null 14 | ## 15 | trainer:amp_train 16 | amp_train:tools/main.py --amp --amp_level O1 -c configs/msvsr_reds.yaml --seed 123 -o dataset.train.num_workers=0 log_config.interval=1 snapshot_config.interval=5 dataset.train.dataset.num_frames=2 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/msvsr_reds.yaml --inputs_size="1,2,3,180,320" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/msvsr/multistagevsrmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type msvsr -c configs/msvsr_reds.yaml --seed 123 -o dataset.test.num_frames=2 --output_path test_tipc/output/ 41 | --device:cpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================infer_benchmark_params========================== 53 | random_infer_input:[{float32,[2,3,180,320]}] 54 | -------------------------------------------------------------------------------- /configs/swinir_denoising.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 420000 2 | output_dir: output_dir 3 | 4 | model: 5 | name: SwinIRModel 6 | generator: 7 | name: SwinIR 8 | upscale: 1 9 | img_size: 128 10 | window_size: 8 11 | depths: [6, 6, 6, 6, 6, 6] 12 | embed_dim: 180 13 | num_heads: [6, 6, 6, 6, 6, 6] 14 | mlp_ratio: 2 15 | char_criterion: 16 | name: CharbonnierLoss 17 | eps: 0.000000001 18 | reduction: mean 19 | 20 | dataset: 21 | train: 22 | name: SwinIRDataset 23 | # TODO fix out of memory for val while training 24 | num_workers: 0 25 | batch_size: 2 # 4GPU 26 | opt: 27 | phase: train 28 | n_channels: 3 29 | H_size: 128 30 | sigma: 15 31 | sigma_test: 15 32 | dataroot_H: data/trainsets/trainH 33 | test: 34 | name: SwinIRDataset 35 | # TODO fix out of memory for val while training 36 | num_workers: 0 37 | batch_size: 1 38 | opt: 39 | phase: test 40 | n_channels: 3 41 | H_size: 128 42 | sigma: 15 43 | sigma_test: 15 44 | dataroot_H: data/trainsets/CBSD68 45 | 46 | export_model: 47 | - {name: 'generator', inputs_num: 1} 48 | 49 | lr_scheduler: 50 | name: MultiStepDecay 51 | learning_rate: 2e-4 52 | milestones: [210000, 305000, 345000, 385000, 420000] 53 | gamma: 0.5 54 | 55 | validate: 56 | interval: 200 57 | save_img: True 58 | 59 | metrics: 60 | psnr: # metric name, can be arbitrary 61 | name: PSNR 62 | crop_border: 4 63 | test_y_channel: True 64 | ssim: 65 | name: SSIM 66 | crop_border: 4 67 | test_y_channel: True 68 | 69 | optimizer: 70 | name: Adam 71 | # add parameters of net_name to optim 72 | # name should in self.nets 73 | net_names: 74 | - generator 75 | beta1: 0.9 76 | beta2: 0.999 77 | epsilon: 1e-8 78 | 79 | log_config: 80 | interval: 10 81 | visiual_interval: 5000 82 | 83 | snapshot_config: 84 | interval: 500 85 | -------------------------------------------------------------------------------- /tools/main.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | import sys 17 | 18 | cur_path = os.path.abspath(os.path.dirname(__file__)) 19 | root_path = os.path.split(cur_path)[0] 20 | sys.path.append(root_path) 21 | 22 | from ppgan.utils.options import parse_args 23 | from ppgan.utils.config import get_config 24 | from ppgan.utils.setup import setup 25 | from ppgan.engine.trainer import Trainer 26 | 27 | 28 | def main(args, cfg): 29 | # init environment, include logger, dynamic graph, seed, device, train or test mode... 30 | setup(args, cfg) 31 | # build trainer 32 | trainer = Trainer(cfg) 33 | 34 | # continue train or evaluate, checkpoint need contain epoch and optimizer info 35 | if args.resume: 36 | trainer.resume(args.resume) 37 | # evaluate or finute, only load generator weights 38 | elif args.load: 39 | trainer.load(args.load) 40 | 41 | if args.evaluate_only: 42 | trainer.test() 43 | return 44 | # training, when keyboard interrupt save weights 45 | try: 46 | trainer.train() 47 | except KeyboardInterrupt as e: 48 | trainer.save(trainer.current_epoch) 49 | trainer.close() 50 | 51 | 52 | if __name__ == '__main__': 53 | args = parse_args() 54 | cfg = get_config(args.config_file, args.opt) 55 | 56 | main(args, cfg) 57 | -------------------------------------------------------------------------------- /ppgan/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .unpaired_dataset import UnpairedDataset 16 | from .single_dataset import SingleDataset 17 | from .paired_dataset import PairedDataset 18 | from .base_sr_dataset import SRDataset 19 | from .makeup_dataset import MakeupDataset 20 | from .common_vision_dataset import CommonVisionDataset 21 | from .animeganv2_dataset import AnimeGANV2Dataset 22 | from .wav2lip_dataset import Wav2LipDataset 23 | from .starganv2_dataset import StarGANv2Dataset 24 | from .firstorder_dataset import FirstOrderDataset 25 | from .lapstyle_dataset import LapStyleDataset 26 | from .mpr_dataset import MPRTrain, MPRVal, MPRTest 27 | from .vsr_reds_dataset import VSRREDSDataset 28 | from .vsr_reds_multiple_gt_dataset import VSRREDSMultipleGTDataset 29 | from .vsr_vimeo90k_dataset import VSRVimeo90KDataset 30 | from .vsr_folder_dataset import VSRFolderDataset 31 | from .photopen_dataset import PhotoPenDataset 32 | from .empty_dataset import EmptyDataset 33 | from .gpen_dataset import GPENDataset 34 | from .swinir_dataset import SwinIRDataset 35 | from .gfpgan_datasets import FFHQDegradationDataset 36 | from .paired_image_datasets import PairedImageDataset 37 | from .invdn_dataset import InvDNDataset 38 | from .nafnet_dataset import NAFNetTrain, NAFNetVal, NAFNetTest 39 | from .aotgan_dataset import AOTGANDataset 40 | -------------------------------------------------------------------------------- /ppgan/modules/fused_act.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # code was heavily based on https://github.com/rosinality/stylegan2-pytorch 16 | # MIT License 17 | # Copyright (c) 2019 Kim Seonghyeon 18 | 19 | import paddle 20 | import paddle.nn as nn 21 | import paddle.nn.functional as F 22 | 23 | 24 | class FusedLeakyReLU(nn.Layer): 25 | def __init__(self, channel, bias=True, negative_slope=0.2, scale=2**0.5): 26 | super().__init__() 27 | 28 | if bias: 29 | self.bias = self.create_parameter( 30 | (channel, ), default_initializer=nn.initializer.Constant(0.0)) 31 | 32 | else: 33 | self.bias = None 34 | 35 | self.negative_slope = negative_slope 36 | self.scale = scale 37 | 38 | def forward(self, input): 39 | return fused_leaky_relu(input, self.bias, self.negative_slope, 40 | self.scale) 41 | 42 | 43 | def fused_leaky_relu(input, bias=None, negative_slope=0.2, scale=2**0.5): 44 | if bias is not None: 45 | rest_dim = [1] * (len(input.shape) - len(bias.shape) - 1) 46 | return (F.leaky_relu(input + bias.reshape( 47 | (1, bias.shape[0], *rest_dim)), 48 | negative_slope=0.2) * scale) 49 | 50 | else: 51 | return F.leaky_relu(input, negative_slope=0.2) * scale 52 | -------------------------------------------------------------------------------- /test_tipc/compare_results.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import subprocess 4 | import json 5 | import argparse 6 | import glob 7 | 8 | 9 | def init_args(): 10 | parser = argparse.ArgumentParser() 11 | # params for testing assert allclose 12 | parser.add_argument("--atol", type=float, default=1e-3) 13 | parser.add_argument("--rtol", type=float, default=1e-3) 14 | parser.add_argument("--gt_file", type=str, default="") 15 | parser.add_argument("--log_file", type=str, default="") 16 | parser.add_argument("--precision", type=str, default="fp32") 17 | return parser 18 | 19 | def parse_args(): 20 | parser = init_args() 21 | return parser.parse_args() 22 | 23 | def load_from_file(gt_file): 24 | if not os.path.exists(gt_file): 25 | raise ValueError("The log file {} does not exists!".format(gt_file)) 26 | with open(gt_file, 'r') as f: 27 | data = f.readlines() 28 | f.close() 29 | parser_gt = {} 30 | for line in data: 31 | metric_name, result = line.strip("\n").split(":") 32 | parser_gt[metric_name] = float(result) 33 | return parser_gt 34 | 35 | if __name__ == "__main__": 36 | # Usage: 37 | # python3.7 test_tipc/compare_results.py --gt_file=./test_tipc/results/*.txt --log_file=./test_tipc/output/*/*.txt 38 | 39 | args = parse_args() 40 | 41 | gt_collection = load_from_file(args.gt_file) 42 | pre_collection = load_from_file(args.log_file) 43 | 44 | for metric in pre_collection.keys(): 45 | try: 46 | np.testing.assert_allclose( 47 | np.array(pre_collection[metric]), np.array(gt_collection[metric]), atol=args.atol, rtol=args.rtol) 48 | print( 49 | "Assert allclose passed! The results of {} are consistent!". 50 | format(metric)) 51 | except Exception as E: 52 | print(E) 53 | raise ValueError( 54 | "The results of {} are inconsistent!". 55 | format(metric)) -------------------------------------------------------------------------------- /test_tipc/configs/esrgan/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:esrgan 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=100 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=2 10 | pretrained_model:null 11 | train_model_name:esrgan_psnr_x4_div2k*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/esrgan_psnr_x4_div2k.yaml --seed 123 -o log_config.interval=10 snapshot_config.interval=25 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/esrgan_psnr_x4_div2k.yaml --inputs_size="1,3,128,128" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/esrgan/esrganmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type esrgan -c configs/esrgan_psnr_x4_div2k.yaml --seed 123 --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================to_static_train_benchmark_params=========================== 53 | to_static_train:model.to_static=True 54 | ===========================train_benchmark_params========================== 55 | batch_size:32|64 56 | fp_items:fp32 57 | total_iters:500 58 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 59 | flags:FLAGS_cudnn_exhaustive_search=1 60 | -------------------------------------------------------------------------------- /test_tipc/configs/edvr/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:edvr 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=100 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=4 10 | pretrained_model:null 11 | train_model_name:edvr_m_wo_tsa*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/basicvsr_reds/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/edvr_m_wo_tsa.yaml --seed 123 -o log_config.interval=5 snapshot_config.interval=25 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/edvr_m_wo_tsa.yaml --inputs_size="1,5,3,180,320" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/edvr/edvrmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type edvr -c configs/edvr_m_wo_tsa.yaml --seed 123 -o dataset.test.num_frames=5 --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================to_static_train_benchmark_params=========================== 53 | to_static_train:model.to_static=True 54 | ===========================train_benchmark_params========================== 55 | batch_size:64 56 | fp_items:fp32|fp16 57 | total_iters:100 58 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 59 | flags:FLAGS_cudnn_exhaustive_search=1 60 | -------------------------------------------------------------------------------- /applications/tools/animeganv2.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import paddle 16 | import os 17 | import sys 18 | 19 | sys.path.insert(0, os.getcwd()) 20 | from ppgan.apps import AnimeGANPredictor 21 | import argparse 22 | 23 | if __name__ == "__main__": 24 | parser = argparse.ArgumentParser() 25 | parser.add_argument("--input_image", type=str, help="path to source image") 26 | 27 | parser.add_argument("--output_path", 28 | type=str, 29 | default='output_dir', 30 | help="path to output image dir") 31 | 32 | parser.add_argument("--weight_path", 33 | type=str, 34 | default=None, 35 | help="path to model checkpoint path") 36 | 37 | parser.add_argument("--use_adjust_brightness", 38 | action="store_false", 39 | help="adjust brightness mode.") 40 | 41 | parser.add_argument("--cpu", 42 | dest="cpu", 43 | action="store_true", 44 | help="cpu mode.") 45 | 46 | args = parser.parse_args() 47 | 48 | if args.cpu: 49 | paddle.set_device('cpu') 50 | 51 | predictor = AnimeGANPredictor(args.output_path, args.weight_path, 52 | args.use_adjust_brightness) 53 | predictor.run(args.input_image) 54 | -------------------------------------------------------------------------------- /ppgan/utils/filesystem.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | import six 17 | import pickle 18 | import paddle 19 | 20 | 21 | def makedirs(dir): 22 | if not os.path.exists(dir): 23 | # avoid error when train with multiple gpus 24 | try: 25 | os.makedirs(dir) 26 | except: 27 | pass 28 | 29 | 30 | def save(state_dicts, file_name): 31 | def convert(state_dict): 32 | model_dict = {} 33 | 34 | for k, v in state_dict.items(): 35 | if isinstance( 36 | v, 37 | (paddle.static.Variable, paddle.Tensor)): 38 | model_dict[k] = v.numpy() 39 | else: 40 | model_dict[k] = v 41 | 42 | return model_dict 43 | 44 | final_dict = {} 45 | for k, v in state_dicts.items(): 46 | if isinstance( 47 | v, 48 | (paddle.static.Variable, paddle.Tensor)): 49 | final_dict = convert(state_dicts) 50 | break 51 | elif isinstance(v, dict): 52 | final_dict[k] = convert(v) 53 | else: 54 | final_dict[k] = v 55 | 56 | paddle.save(final_dict, file_name) 57 | 58 | 59 | def load(file_name): 60 | with open(file_name, 'rb') as f: 61 | state_dicts = pickle.load(f) if six.PY2 else pickle.load( 62 | f, encoding='latin1') 63 | return state_dicts 64 | -------------------------------------------------------------------------------- /applications/tools/mprnet.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import paddle 16 | import os 17 | import sys 18 | 19 | sys.path.insert(0, os.getcwd()) 20 | from ppgan.apps import MPRPredictor 21 | import argparse 22 | 23 | if __name__ == "__main__": 24 | parser = argparse.ArgumentParser() 25 | parser.add_argument("--input_image", type=str, help="path to image") 26 | 27 | parser.add_argument("--output_path", 28 | type=str, 29 | default='output_dir', 30 | help="path to output image dir") 31 | 32 | parser.add_argument("--weight_path", 33 | type=str, 34 | default=None, 35 | help="path to model weight path") 36 | 37 | parser.add_argument( 38 | "--task", 39 | type=str, 40 | default='Deblurring', 41 | help="task can be chosen in 'Deblurring', 'Denoising', 'Deraining'") 42 | 43 | parser.add_argument("--cpu", 44 | dest="cpu", 45 | action="store_true", 46 | help="cpu mode.") 47 | 48 | args = parser.parse_args() 49 | 50 | if args.cpu: 51 | paddle.set_device('cpu') 52 | 53 | predictor = MPRPredictor(output_path=args.output_path, 54 | task=args.task, 55 | weight_path=args.weight_path) 56 | predictor.run(args.input_image) 57 | -------------------------------------------------------------------------------- /ppgan/models/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from .base_model import BaseModel, apply_to_static 16 | from .gan_model import GANModel 17 | from .cycle_gan_model import CycleGANModel 18 | from .pix2pix_model import Pix2PixModel 19 | from .sr_model import BaseSRModel 20 | from .makeup_model import MakeupModel 21 | from .esrgan_model import ESRGAN 22 | from .ugatit_model import UGATITModel 23 | from .dc_gan_model import DCGANModel 24 | from .drn_model import DRN 25 | from .animeganv2_model import AnimeGANV2Model, AnimeGANV2PreTrainModel 26 | from .styleganv2_model import StyleGAN2Model 27 | from .wav2lip_model import Wav2LipModel 28 | from .wav2lip_hq_model import Wav2LipModelHq 29 | from .starganv2_model import StarGANv2Model 30 | from .edvr_model import EDVRModel 31 | from .firstorder_model import FirstOrderModel 32 | from .lapstyle_model import LapStyleDraModel, LapStyleRevFirstModel, LapStyleRevSecondModel 33 | from .basicvsr_model import BasicVSRModel 34 | from .mpr_model import MPRModel 35 | from .photopen_model import PhotoPenModel 36 | from .msvsr_model import MultiStageVSRModel 37 | from .singan_model import SinGANModel 38 | from .rcan_model import RCANModel 39 | from .prenet_model import PReNetModel 40 | from .gpen_model import GPENModel 41 | from .swinir_model import SwinIRModel 42 | from .gfpgan_model import GFPGANModel 43 | from .invdn_model import InvDNModel 44 | from .nafnet_model import NAFNetModel 45 | from .aotgan_model import AOTGANModel 46 | -------------------------------------------------------------------------------- /tools/animegan_picmean.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import os 16 | import cv2 17 | import numpy as np 18 | from glob import glob 19 | from tqdm import tqdm 20 | import argparse 21 | 22 | 23 | def read_img(image_path): 24 | img = cv2.imread(image_path) 25 | assert len(img.shape) == 3 26 | B = img[..., 0].mean() 27 | G = img[..., 1].mean() 28 | R = img[..., 2].mean() 29 | return B, G, R 30 | 31 | 32 | def main(dataset): 33 | file_list = glob(os.path.join(dataset, '*.jpg')) 34 | image_num = len(file_list) 35 | print('image_num:', image_num) 36 | 37 | B_total = 0 38 | G_total = 0 39 | R_total = 0 40 | for f in tqdm(file_list): 41 | bgr = read_img(f) 42 | B_total += bgr[0] 43 | G_total += bgr[1] 44 | R_total += bgr[2] 45 | 46 | B_mean, G_mean, R_mean = B_total / image_num, G_total / image_num, R_total / image_num 47 | mean = (B_mean + G_mean + R_mean) / 3 48 | 49 | print('RGB mean diff') 50 | print( 51 | np.asfarray((mean - R_mean, mean - G_mean, mean - B_mean), 52 | dtype='float32')) 53 | 54 | 55 | if __name__ == '__main__': 56 | parser = argparse.ArgumentParser() 57 | parser.add_argument('--dataset', 58 | help="get the mean values of rgb from dataset", 59 | type=str, 60 | default='') 61 | 62 | args = parser.parse_args() 63 | 64 | main(args.dataset) 65 | -------------------------------------------------------------------------------- /test_tipc/configs/StyleGANv2/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:StyleGANv2 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=800 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=3|whole_train_whole_infer=3 10 | pretrained_model:null 11 | train_model_name:stylegan_v2_256_ffhq*/*checkpoint.pdparams 12 | train_infer_img_dir:null 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/stylegan_v2_256_ffhq.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=10 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/stylegan_v2_256_ffhq.yaml --inputs_size="1,1,512;1,1" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/stylegan2/stylegan2model_gen 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type stylegan2 --seed 123 -c configs/stylegan_v2_256_ffhq.yaml --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================train_benchmark_params========================== 53 | batch_size:8 54 | fp_items:fp32 55 | epoch:100 56 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 57 | flags:FLAGS_cudnn_exhaustive_search=1 58 | ===========================infer_benchmark_params========================== 59 | random_infer_input:[{float32,[1, 512]}, {float32,[1]}] 60 | -------------------------------------------------------------------------------- /test_tipc/configs/FOMM/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:FOMM 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | epochs:lite_train_lite_infer=1|lite_train_whole_infer=1|whole_train_whole_infer=100 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=8|whole_train_whole_infer=8 10 | pretrained_model:null 11 | train_model_name:firstorder_vox_256*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/firstorder_vox_256/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/firstorder_vox_256.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=1 dataset.train.num_repeats=1 dataset.train.id_sampling=False 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/firstorder_vox_256.yaml --inputs_size="1,3,256,256;1,3,256,256;1,10,2;1,10,2,2" --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:fom_dy2st 37 | train_model:./inference/fom_dy2st/ 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/fom_infer.py --driving_path data/first_order/Voxceleb/test --output_path test_tipc/output/fom/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================train_benchmark_params========================== 53 | batch_size:16 54 | fp_items:fp32 55 | epoch:15 56 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 57 | flags:FLAGS_cudnn_exhaustive_search=1 58 | ===========================infer_benchmark_params========================== 59 | random_infer_input:[{float32,[3,256,256]}] 60 | -------------------------------------------------------------------------------- /ppgan/utils/animate.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # code was heavily based on https://github.com/AliaksandrSiarohin/first-order-model 16 | 17 | import numpy as np 18 | from scipy.spatial import ConvexHull 19 | 20 | import paddle 21 | 22 | 23 | def normalize_kp(kp_source, 24 | kp_driving, 25 | kp_driving_initial, 26 | adapt_movement_scale=False, 27 | use_relative_movement=False, 28 | use_relative_jacobian=False): 29 | if adapt_movement_scale: 30 | source_area = ConvexHull(kp_source['value'][0].numpy()).volume 31 | driving_area = ConvexHull(kp_driving_initial['value'][0].numpy()).volume 32 | adapt_movement_scale = np.sqrt(source_area) / np.sqrt(driving_area) 33 | else: 34 | adapt_movement_scale = 1 35 | 36 | kp_new = {k: v for k, v in kp_driving.items()} 37 | 38 | if use_relative_movement: 39 | kp_value_diff = (kp_driving['value'] - kp_driving_initial['value']) 40 | kp_value_diff *= adapt_movement_scale 41 | kp_new['value'] = kp_value_diff + kp_source['value'] 42 | 43 | if use_relative_jacobian: 44 | jacobian_diff = paddle.matmul( 45 | kp_driving['jacobian'], 46 | paddle.inverse(kp_driving_initial['jacobian'])) 47 | kp_new['jacobian'] = paddle.matmul(jacobian_diff, 48 | kp_source['jacobian']) 49 | 50 | return kp_new 51 | -------------------------------------------------------------------------------- /test_tipc/configs/basicvsr/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:basicvsr 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:basicvsr_reds*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/basicvsr_reds/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/basicvsr_reds.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=5 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/basicvsr_reds.yaml --inputs_size="1,6,3,180,320" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/basicvsr/basicvsrmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type basicvsr -c configs/basicvsr_reds.yaml --seed 123 -o dataset.test.num_frames=6 --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================train_benchmark_params========================== 53 | batch_size:2|4 54 | fp_items:fp32 55 | total_iters:50 56 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 57 | flags:FLAGS_cudnn_exhaustive_search=1 58 | ===========================infer_benchmark_params========================== 59 | random_infer_input:[{float32,[6,3,180,320]}] 60 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from setuptools import setup 16 | from setuptools import find_packages 17 | from io import open 18 | 19 | from ppgan import __version__ 20 | 21 | with open('requirements.txt', encoding="utf-8-sig") as f: 22 | requirements = f.readlines() 23 | 24 | 25 | def readme(): 26 | with open('README.md', encoding="utf-8-sig") as f: 27 | README = f.read() 28 | return README 29 | 30 | 31 | setup( 32 | name='ppgan', 33 | packages=find_packages(), 34 | include_package_data=True, 35 | entry_points={"console_scripts": ["paddlegan= paddlegan.paddlegan:main"]}, 36 | author='PaddlePaddle Author', 37 | version=__version__, 38 | install_requires=requirements, 39 | license='Apache License 2.0', 40 | description='Awesome GAN toolkits based on PaddlePaddle', 41 | long_description=readme(), 42 | long_description_content_type='text/markdown', 43 | url='https://github.com/PaddlePaddle/PaddleGAN', 44 | download_url='https://github.com/PaddlePaddle/PaddleGAN.git', 45 | keywords=['gan paddlegan'], 46 | classifiers=[ 47 | 'Intended Audience :: Developers', 'Operating System :: OS Independent', 48 | 'Natural Language :: Chinese (Simplified)', 49 | 'Programming Language :: Python :: 3', 50 | 'Programming Language :: Python :: 3.5', 51 | 'Programming Language :: Python :: 3.6', 52 | 'Programming Language :: Python :: 3.7', 'Topic :: Utilities' 53 | ], 54 | ) 55 | -------------------------------------------------------------------------------- /configs/photopen.yaml: -------------------------------------------------------------------------------- 1 | total_iters: 1 2 | output_dir: output_dir 3 | checkpoints_dir: checkpoints 4 | 5 | model: 6 | name: PhotoPenModel 7 | generator: 8 | name: SPADEGenerator 9 | ngf: 24 10 | num_upsampling_layers: normal 11 | crop_size: 256 12 | aspect_ratio: 1.0 13 | norm_G: spectralspadebatch3x3 14 | semantic_nc: 14 15 | use_vae: False 16 | nef: 16 17 | discriminator: 18 | name: MultiscaleDiscriminator 19 | ndf: 128 20 | num_D: 4 21 | crop_size: 256 22 | label_nc: 12 23 | output_nc: 3 24 | contain_dontcare_label: True 25 | no_instance: False 26 | n_layers_D: 6 27 | criterion: 28 | name: PhotoPenPerceptualLoss 29 | crop_size: 224 30 | lambda_vgg: 1.6 31 | label_nc: 12 32 | contain_dontcare_label: True 33 | batchSize: 1 34 | crop_size: 256 35 | lambda_feat: 10.0 36 | 37 | dataset: 38 | train: 39 | name: PhotoPenDataset 40 | content_root: test/coco_stuff 41 | load_size: 286 42 | crop_size: 256 43 | num_workers: 0 44 | batch_size: 1 45 | test: 46 | name: PhotoPenDataset_test 47 | content_root: test/coco_stuff 48 | load_size: 286 49 | crop_size: 256 50 | num_workers: 0 51 | batch_size: 1 52 | 53 | lr_scheduler: # abundoned 54 | name: LinearDecay 55 | learning_rate: 0.0001 56 | start_epoch: 99999 57 | decay_epochs: 99999 58 | # will get from real dataset 59 | iters_per_epoch: 1 60 | 61 | optimizer: 62 | lr: 0.0001 63 | optimG: 64 | name: Adam 65 | net_names: 66 | - net_gen 67 | beta1: 0.9 68 | beta2: 0.999 69 | optimD: 70 | name: Adam 71 | net_names: 72 | - net_des 73 | beta1: 0.9 74 | beta2: 0.999 75 | 76 | log_config: 77 | interval: 1 78 | visiual_interval: 1 79 | 80 | snapshot_config: 81 | interval: 1 82 | 83 | predict: 84 | name: SPADEGenerator 85 | ngf: 24 86 | num_upsampling_layers: normal 87 | crop_size: 256 88 | aspect_ratio: 1.0 89 | norm_G: spectralspadebatch3x3 90 | semantic_nc: 14 91 | use_vae: False 92 | nef: 16 93 | contain_dontcare_label: True 94 | label_nc: 12 95 | batchSize: 1 96 | -------------------------------------------------------------------------------- /ppgan/utils/video.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import os 16 | import sys 17 | 18 | 19 | def video2frames(video_path, outpath, **kargs): 20 | def _dict2str(kargs): 21 | cmd_str = '' 22 | for k, v in kargs.items(): 23 | cmd_str += (' ' + str(k) + ' ' + str(v)) 24 | return cmd_str 25 | 26 | ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error '] 27 | vid_name = os.path.basename(video_path).split('.')[0] 28 | out_full_path = os.path.join(outpath, vid_name) 29 | 30 | if not os.path.exists(out_full_path): 31 | os.makedirs(out_full_path) 32 | 33 | # video file name 34 | outformat = os.path.join(out_full_path, '%08d.png') 35 | 36 | cmd = ffmpeg 37 | cmd = ffmpeg + [' -i ', video_path, ' -start_number ', ' 0 ', outformat] 38 | 39 | cmd = ''.join(cmd) + _dict2str(kargs) 40 | 41 | if os.system(cmd) != 0: 42 | raise RuntimeError('ffmpeg process video: {} error'.format(vid_name)) 43 | 44 | sys.stdout.flush() 45 | return out_full_path 46 | 47 | 48 | def frames2video(frame_path, video_path, r): 49 | ffmpeg = ['ffmpeg ', ' -y -loglevel ', ' error '] 50 | cmd = ffmpeg + [ 51 | ' -r ', r, ' -f ', ' image2 ', ' -i ', frame_path, ' -vcodec ', 52 | ' libx264 ', ' -pix_fmt ', ' yuv420p ', ' -crf ', ' 16 ', video_path 53 | ] 54 | cmd = ''.join(cmd) 55 | 56 | if os.system(cmd) != 0: 57 | raise RuntimeError('ffmpeg process video: {} error'.format(video_path)) 58 | 59 | sys.stdout.flush() 60 | -------------------------------------------------------------------------------- /test_tipc/configs/Pix2pix/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:Pix2pix 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | epochs:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:pix2pix_facades*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/facades/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/pix2pix_facades.yaml --seed 123 -o log_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/pix2pix_facades.yaml --inputs_size="-1,3,-1,-1" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/pix2pix_facade/pix2pixmodel_netG 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type pix2pix --seed 123 -c configs/pix2pix_facades.yaml --output_path test_tipc/output/ 41 | --device:cpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================to_static_train_benchmark_params=========================== 53 | to_static_train:model.to_static=True 54 | ===========================train_benchmark_params========================== 55 | batch_size:1 56 | fp_items:fp32 57 | epoch:10 58 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 59 | flags:null 60 | ===========================infer_benchmark_params========================== 61 | random_infer_input:[{float32,[3,256,256]}] 62 | -------------------------------------------------------------------------------- /ppgan/modules/conv.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | from paddle import nn 3 | from paddle.nn import functional as F 4 | 5 | 6 | class ConvBNRelu(nn.Layer): 7 | def __init__(self, 8 | cin, 9 | cout, 10 | kernel_size, 11 | stride, 12 | padding, 13 | residual=False, 14 | *args, 15 | **kwargs): 16 | super().__init__(*args, **kwargs) 17 | self.conv_block = nn.Sequential( 18 | nn.Conv2D(cin, cout, kernel_size, stride, padding), 19 | nn.BatchNorm2D(cout)) 20 | self.act = nn.ReLU() 21 | self.residual = residual 22 | 23 | def forward(self, x): 24 | out = self.conv_block(x) 25 | if self.residual: 26 | out += x 27 | return self.act(out) 28 | 29 | 30 | class NonNormConv2d(nn.Layer): 31 | def __init__(self, 32 | cin, 33 | cout, 34 | kernel_size, 35 | stride, 36 | padding, 37 | residual=False, 38 | *args, 39 | **kwargs): 40 | super().__init__(*args, **kwargs) 41 | self.conv_block = nn.Sequential( 42 | nn.Conv2D(cin, cout, kernel_size, stride, padding), ) 43 | self.act = nn.LeakyReLU(0.01) 44 | 45 | def forward(self, x): 46 | out = self.conv_block(x) 47 | return self.act(out) 48 | 49 | 50 | class Conv2dTransposeRelu(nn.Layer): 51 | def __init__(self, 52 | cin, 53 | cout, 54 | kernel_size, 55 | stride, 56 | padding, 57 | output_padding=0, 58 | *args, 59 | **kwargs): 60 | super().__init__(*args, **kwargs) 61 | self.conv_block = nn.Sequential( 62 | nn.Conv2DTranspose(cin, cout, kernel_size, stride, padding, 63 | output_padding), nn.BatchNorm2D(cout)) 64 | self.act = nn.ReLU() 65 | 66 | def forward(self, x): 67 | out = self.conv_block(x) 68 | return self.act(out) 69 | -------------------------------------------------------------------------------- /docs/en_US/tutorials/ugatit.md: -------------------------------------------------------------------------------- 1 | # 1 U-GAT-IT 2 | 3 | ## 1.1 Principle 4 | 5 | Similar to CycleGAN, [U-GAT-IT](https://arxiv.org/abs/1907.10830) uses unpaired pictures for image translation, input two different images with different styles, and automatically perform style transfer. Differently, U-GAT-IT is a novel method for unsupervised image-to-image translation, which incorporates a new attention module and a new learnable normalization function in an end-to-end manner. 6 | 7 | ## 1.2 How to use 8 | 9 | ### 1.2.1 Prepare Datasets 10 | 11 | Selfie2anime dataset used by U-GAT-IT can be download from [here](https://www.kaggle.com/arnaud58/selfie2anime). You can also use your own dataset. 12 | The structure of dataset is as following: 13 | ``` 14 | ├── dataset 15 | └── YOUR_DATASET_NAME 16 | ├── trainA 17 | ├── trainB 18 | ├── testA 19 | └── testB 20 | ``` 21 | 22 | ### 1.2.2 Train/Test 23 | 24 | Datasets used in example is selfie2anime, you can change it to your own dataset in the config file. 25 | 26 | Train a model: 27 | ``` 28 | python -u tools/main.py --config-file configs/ugatit_selfie2anime_light.yaml 29 | ``` 30 | 31 | Test the model: 32 | ``` 33 | python tools/main.py --config-file configs/ugatit_selfie2anime_light.yaml --evaluate-only --load ${PATH_OF_WEIGHT} 34 | ``` 35 | 36 | ## 1.3 Results 37 | 38 | ![](../../imgs/ugatit.png) 39 | 40 | ## 1.4 模型下载 41 | | 模型 | 数据集 | 下载地址 | 42 | |---|---|---| 43 | | ugatit_light | selfie2anime | [ugatit_light](https://paddlegan.bj.bcebos.com/models/ugatit_light.pdparams) 44 | 45 | 46 | 47 | 48 | # References 49 | 50 | - 1. [U-GAT-IT: Unsupervised Generative Attentional Networks with Adaptive Layer-Instance Normalization for Image-to-Image Translation](https://arxiv.org/abs/1907.10830) 51 | 52 | ``` 53 | @article{kim2019u, 54 | title={U-GAT-IT: unsupervised generative attentional networks with adaptive layer-instance normalization for image-to-image translation}, 55 | author={Kim, Junho and Kim, Minjae and Kang, Hyeonwoo and Lee, Kwanghee}, 56 | journal={arXiv preprint arXiv:1907.10830}, 57 | year={2019} 58 | } 59 | ``` 60 | 61 | -------------------------------------------------------------------------------- /test_tipc/docs/test_inference_cpp.md: -------------------------------------------------------------------------------- 1 | # C++预测功能测试 2 | 3 | C++预测功能测试的主程序为`test_inference_cpp.sh`,可以测试基于C++预测库的模型推理功能。 4 | 5 | ## 1. 测试结论汇总 6 | 7 | | 模型类型 |device | batchsize | tensorrt | mkldnn | cpu多线程 | 8 | | :----: | :----: | :----: | :----: | :----: | :----: | 9 | | 正常模型 | GPU | 1 | - | - | - | 10 | | 正常模型 | CPU | 1 | - | fp32 | 支持 | 11 | 12 | ## 2. 测试流程 13 | 运行环境配置请参考[文档](../../docs/zh_CN/install.md)的内容安装PaddleGAN,TIPC推荐的环境: 14 | - PaddlePaddle=2.3.1 15 | - CUDA=10.2 16 | - cuDNN=7.6.5 17 | 18 | ### 2.1 功能测试 19 | 先运行`prepare.sh`准备数据和模型,然后运行`test_inference_cpp.sh`进行测试,msvsr模型的具体测试如下 20 | 21 | ```bash 22 | # 准备模型和数据 23 | bash test_tipc/test_inference_cpp.sh test_tipc/configs/msvsr/inference_cpp.txt 24 | # cpp推理测试,可修改inference_cpp.txt配置累测试不同配置下的推理结果 25 | bash test_tipc/test_inference_cpp.sh test_tipc/configs/msvsr/inference_cpp.txt 26 | ``` 27 | 28 | 运行预测指令后,在`test_tipc/output`文件夹下自动会保存运行日志和输出结果,包括以下文件: 29 | 30 | ```shell 31 | test_tipc/output 32 | ├── infer_cpp/results_cpp_infer.log # 运行指令状态的日志 33 | ├── infer_cpp/infer_cpp_GPU.log # 使用GPU推理测试的日志 34 | ├── infer_cpp/infer_cpp_CPU_use_mkldnn_threads_1.log # 使用CPU开启mkldnn,thread为1的推理测试日志 35 | ├── output.mp4 # 视频超分预测结果 36 | ...... 37 | ``` 38 | 其中results_cpp_infer.log中包含了每条指令的运行状态,如果运行成功会输出: 39 | 40 | ``` 41 | Run successfully with command - ./deploy/cpp_infer/build/vsr --model_path=./inference/msvsr/multistagevsrmodel_generator.pdmodel --param_path=./inference/msvsr/multistagevsrmodel_generator.pdiparams --video_path=./data/low_res.mp4 --output_dir=./test_tipc/output/msvsr --frame_num=2 --device=GPU --gpu_id=1 --use_mkldnn=True --cpu_threads=1 > ./test_tipc/output/infer_cpp/infer_cpp_GPU.log 2>&1! 42 | ...... 43 | ``` 44 | 如果运行失败,会输出: 45 | ``` 46 | Run failed with command - ./deploy/cpp_infer/build/vsr --model_path=./inference/msvsr/multistagevsrmodel_generator.pdmodel --param_path=./inference/msvsr/multistagevsrmodel_generator.pdiparams --video_path=./data/low_res.mp4 --output_dir=./test_tipc/output/msvsr --frame_num=2 --device=GPU --gpu_id=1 --use_mkldnn=True --cpu_threads=1 > ./test_tipc/output/infer_cpp/infer_cpp_GPU.log 2>&1! 47 | ...... 48 | ``` 49 | 可以根据results_cpp_infer.log中的内容判定哪一个指令运行错误。 50 | -------------------------------------------------------------------------------- /test_tipc/docs/benchmark_train.md: -------------------------------------------------------------------------------- 1 | 2 | # TIPC Linux端Benchmark测试文档 3 | 4 | 该文档为Benchmark测试说明,Benchmark预测功能测试的主程序为`benchmark_train.sh`,用于验证监控模型训练的性能。 5 | 6 | # 1. 测试流程 7 | ## 1.1 准备数据和环境安装 8 | 运行`test_tipc/prepare.sh`,完成训练数据准备和安装环境流程。 9 | 10 | ```shell 11 | # 运行格式:bash test_tipc/prepare.sh train_benchmark.txt mode 12 | bash test_tipc/prepare.sh test_tipc/configs/msvsr/train_infer_python.txt benchmark_train 13 | ``` 14 | 15 | ## 1.2 功能测试 16 | 执行`test_tipc/benchmark_train.sh`,完成模型训练和日志解析 17 | 18 | ```shell 19 | # 运行格式:bash test_tipc/benchmark_train.sh train_benchmark.txt mode 20 | bash test_tipc/benchmark_train.sh test_tipc/configs/msvsr/train_infer_python.txt benchmark_train 21 | ``` 22 | 23 | `test_tipc/benchmark_train.sh`支持根据传入的第三个参数实现只运行某一个训练配置,如下: 24 | ```shell 25 | # 运行格式:bash test_tipc/benchmark_train.sh train_benchmark.txt mode 26 | bash test_tipc/benchmark_train.sh test_tipc/configs/msvsr/train_infer_python.txt benchmark_train dynamic_bs4_fp32_DP_N1C1 27 | ``` 28 | dynamic_bs4_fp32_DP_N1C1为test_tipc/benchmark_train.sh传入的参数,格式如下: 29 | `${modeltype}_${batch_size}_${fp_item}_${run_mode}_${device_num}` 30 | 包含的信息有:模型类型、batchsize大小、训练精度如fp32,fp16等、分布式运行模式以及分布式训练使用的机器信息如单机单卡(N1C1)。 31 | 32 | 33 | ## 2. 日志输出 34 | 35 | 运行后将保存模型的训练日志和解析日志,使用 `test_tipc/configs/basicvsr/train_benchmark.txt` 参数文件的训练日志解析结果是: 36 | 37 | ``` 38 | {"model_branch": "dygaph", "model_commit": "7c39a1996b19087737c05d883fd346d2f39dbcc0", "model_name": "basicvsr_bs4_fp32_SingleP_DP", "batch_size": 4, "fp_item": "fp32", "run_process_type": "SingleP", "run_mode": "DP", "convergence_value": "5.413110", "convergence_key": "loss:", "ips": 19.333, "speed_unit": "samples/s", "device_num": "N1C1", "model_run_time": "0", "frame_commit": "8cc09552473b842c651ead3b9848d41827a3dbab", "frame_version": "0.0.0"} 39 | ``` 40 | 41 | 训练日志和日志解析结果保存在benchmark_log目录下,文件组织格式如下: 42 | ``` 43 | train_log/ 44 | ├── index 45 | │ ├── PaddleGAN_msvsr_bs4_fp32_SingleP_DP_N1C1_speed 46 | │ └── PaddleGAN_msvsr_bs4_fp32_SingleP_DP_N1C4_speed 47 | ├── profiling_log 48 | │ └── PaddleGAN_msvsr_bs4_fp32_SingleP_DP_N1C1_profiling 49 | └── train_log 50 | ├── PaddleGAN_msvsr_bs4_fp32_SingleP_DP_N1C1_log 51 | └── PaddleGAN_msvsr_bs4_fp32_MultiP_DP_N1C4_log 52 | ``` 53 | -------------------------------------------------------------------------------- /configs/animeganv2_pretrain.yaml: -------------------------------------------------------------------------------- 1 | epochs: 2 2 | output_dir: output_dir 3 | 4 | model: 5 | name: AnimeGANV2PreTrainModel 6 | generator: 7 | name: AnimeGenerator 8 | discriminator: 9 | name: AnimeDiscriminator 10 | gan_criterion: 11 | name: GANLoss 12 | gan_mode: lsgan 13 | con_weight: 1 14 | pretrain_ckpt: null 15 | 16 | dataset: 17 | train: 18 | name: AnimeGANV2Dataset 19 | num_workers: 4 20 | batch_size: 4 21 | dataroot: data/animedataset 22 | style: Hayao 23 | transform_real: 24 | - name: Transpose 25 | - name: Normalize 26 | mean: [127.5, 127.5, 127.5] 27 | std: [127.5, 127.5, 127.5] 28 | transform_anime: 29 | - name: Add 30 | value: [-4.4346957, -8.665916, 13.100612] 31 | - name: Transpose 32 | - name: Normalize 33 | mean: [127.5, 127.5, 127.5] 34 | std: [127.5, 127.5, 127.5] 35 | transform_gray: 36 | - name: Grayscale 37 | num_output_channels: 3 38 | - name: Transpose 39 | - name: Normalize 40 | mean: [127.5, 127.5, 127.5] 41 | std: [127.5, 127.5, 127.5] 42 | test: 43 | name: SingleDataset 44 | dataroot: data/animedataset/test/test_photo 45 | preprocess: 46 | - name: LoadImageFromFile 47 | key: A 48 | - name: Transforms 49 | input_keys: [A] 50 | pipeline: 51 | - name: ResizeToScale 52 | size: [256, 256] 53 | scale: 32 54 | interpolation: bilinear 55 | - name: Transpose 56 | - name: Normalize 57 | mean: [127.5, 127.5, 127.5] 58 | std: [127.5, 127.5, 127.5] 59 | keys: [image, image] 60 | 61 | lr_scheduler: 62 | name: LinearDecay 63 | learning_rate: 0.0002 64 | start_epoch: 100 65 | decay_epochs: 100 66 | # will get from real dataset 67 | iters_per_epoch: 1 68 | 69 | optimizer: 70 | optimizer_G: 71 | name: Adam 72 | net_names: 73 | - netG 74 | beta1: 0.5 75 | optimizer_D: 76 | name: Adam 77 | net_names: 78 | - netD 79 | beta1: 0.5 80 | 81 | log_config: 82 | interval: 100 83 | visiual_interval: 100 84 | 85 | snapshot_config: 86 | interval: 5 87 | -------------------------------------------------------------------------------- /test_tipc/configs/CycleGAN/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:CycleGAN 3 | python:python3.7 4 | gpu_list:0|0,1 5 | ## 6 | auto_cast:null 7 | epochs:lite_train_lite_infer=1|lite_train_whole_infer=1|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:cyclegan_horse2zebra*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/horse2zebra/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/cyclegan_horse2zebra.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=1 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/cyclegan_horse2zebra.yaml --inputs_size="-1,3,-1,-1;-1,3,-1,-1" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/cyclegan_horse2zebra/cycleganmodel_netG_A 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type cyclegan --seed 123 -c configs/cyclegan_horse2zebra.yaml --output_path test_tipc/output/ 41 | --device:gpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================to_static_train_benchmark_params=========================== 53 | to_static_train:model.to_static=True 54 | ===========================train_benchmark_params========================== 55 | batch_size:1 56 | fp_items:fp32 57 | epoch:1 58 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 59 | flags:null 60 | ===========================infer_benchmark_params========================== 61 | random_infer_input:[{float32,[3,256,256]}] -------------------------------------------------------------------------------- /ppgan/models/discriminators/discriminator_lapstyle.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import paddle 16 | import paddle.nn as nn 17 | 18 | from .builder import DISCRIMINATORS 19 | 20 | 21 | @DISCRIMINATORS.register() 22 | class LapStyleDiscriminator(nn.Layer): 23 | def __init__(self): 24 | super(LapStyleDiscriminator, self).__init__() 25 | num_layer = 3 26 | num_channel = 32 27 | self.head = nn.Sequential( 28 | ('conv', 29 | nn.Conv2D(3, num_channel, kernel_size=3, stride=1, padding=1)), 30 | ('norm', nn.BatchNorm2D(num_channel)), 31 | ('LeakyRelu', nn.LeakyReLU(0.2))) 32 | self.body = nn.Sequential() 33 | for i in range(num_layer - 2): 34 | self.body.add_sublayer( 35 | 'conv%d' % (i + 1), 36 | nn.Conv2D(num_channel, 37 | num_channel, 38 | kernel_size=3, 39 | stride=1, 40 | padding=1)) 41 | self.body.add_sublayer('norm%d' % (i + 1), 42 | nn.BatchNorm2D(num_channel)) 43 | self.body.add_sublayer('LeakyRelu%d' % (i + 1), nn.LeakyReLU(0.2)) 44 | self.tail = nn.Conv2D(num_channel, 45 | 1, 46 | kernel_size=3, 47 | stride=1, 48 | padding=1) 49 | 50 | def forward(self, x): 51 | x = self.head(x) 52 | x = self.body(x) 53 | x = self.tail(x) 54 | return x 55 | -------------------------------------------------------------------------------- /ppgan/faceutils/face_detection/detection/blazeface/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import cv2 16 | import numpy as np 17 | 18 | 19 | def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA): 20 | dim = None 21 | (h, w) = image.shape[:2] 22 | 23 | if width is None and height is None: 24 | return image 25 | 26 | if width is None: 27 | r = height / float(h) 28 | dim = (int(w * r), height) 29 | else: 30 | r = width / float(w) 31 | dim = (width, int(h * r)) 32 | 33 | resized = cv2.resize(image, dim, interpolation=inter) 34 | 35 | return resized 36 | 37 | 38 | def resize_and_crop_image(image, dim): 39 | if image.shape[0] > image.shape[1]: 40 | img = image_resize(image, width=dim) 41 | yshift, xshift = (image.shape[0] - image.shape[1]) // 2, 0 42 | y_start = (img.shape[0] - img.shape[1]) // 2 43 | y_end = y_start + dim 44 | return img[y_start:y_end, :, :], (xshift, yshift) 45 | else: 46 | img = image_resize(image, height=dim) 47 | yshift, xshift = 0, (image.shape[1] - image.shape[0]) // 2 48 | x_start = (img.shape[1] - img.shape[0]) // 2 49 | x_end = x_start + dim 50 | return img[:, x_start:x_end, :], (xshift, yshift) 51 | 52 | 53 | def resize_and_crop_batch(frames, dim): 54 | smframes = [] 55 | xshift, yshift = 0, 0 56 | for i in range(len(frames)): 57 | smframe, (xshift, yshift) = resize_and_crop_image(frames[i], dim) 58 | smframes.append(smframe) 59 | smframes = np.stack(smframes) 60 | return smframes, (xshift, yshift) 61 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/pixel2style2pixel.md: -------------------------------------------------------------------------------- 1 | # Pixel2Style2Pixel 2 | 3 | ## Pixel2Style2Pixel 原理 4 | 5 | Pixel2Style2Pixel 的任务是image encoding。它主要是将图像编码为StyleGAN V2的风格向量,将StyleGAN V2当作解码器。 6 | 7 |
8 | 9 |
10 | 11 | Pixel2Style2Pixel使用相当大的模型对图像进行编码,将图像编码到StyleGAN V2的风格向量空间中,使编码前的图像和解码后的图像具有强关联性。 12 | 13 | 它的主要功能有: 14 | 15 | - 将图像转成隐藏编码 16 | - 将人脸转正 17 | - 根据草图或者分割结果生成图像 18 | - 将低分辨率图像转成高清图像 19 | 20 | 目前在PaddleGAN中实现了人像重建和人像卡通化的模型。 21 | 22 | ## 使用方法 23 | 24 | ### 生成 25 | 26 | 用户使用如下命令中进行生成,选择本地图像作为输入: 27 | 28 | ``` 29 | cd applications/ 30 | python -u tools/pixel2style2pixel.py \ 31 | --input_image <替换为输入的图像路径> \ 32 | --output_path <替换为生成图片存放的文件夹> \ 33 | --weight_path <替换为你的预训练模型路径> \ 34 | --model_type ffhq-inversion \ 35 | --seed 233 \ 36 | --size 1024 \ 37 | --style_dim 512 \ 38 | --n_mlp 8 \ 39 | --channel_multiplier 2 \ 40 | --cpu 41 | ``` 42 | 43 | **参数说明:** 44 | - input_image: 输入的图像路径 45 | - output_path: 生成图片存放的文件夹 46 | - weight_path: 预训练模型路径 47 | - model_type: PaddleGAN内置模型类型,若输入PaddleGAN已存在的模型类型,`weight_path`将失效。 48 | 当前可用: `ffhq-inversion`, `ffhq-toonify` 49 | - seed: 随机数种子 50 | - size: 模型参数,输出图片的分辨率 51 | - style_dim: 模型参数,风格z的维度 52 | - n_mlp: 模型参数,风格z所输入的多层感知层的层数 53 | - channel_multiplier: 模型参数,通道乘积,影响模型大小和生成图片质量 54 | - cpu: 是否使用cpu推理,若不使用,请在命令中去除 55 | 56 | ### 训练(TODO) 57 | 58 | 未来还将添加训练脚本方便用户训练出更多类型的 Pixel2Style2Pixel 图像编码器。 59 | 60 | 61 | ## 生成结果展示 62 | 63 | 输入人像: 64 | 65 |
66 | 67 |
68 | 69 | 裁剪人像-重建人像-卡通化人像: 70 | 71 |
72 | 73 | 74 | 75 |
76 | 77 | ## 参考文献 78 | 79 | ``` 80 | @article{richardson2020encoding, 81 | title={Encoding in Style: a StyleGAN Encoder for Image-to-Image Translation}, 82 | author={Richardson, Elad and Alaluf, Yuval and Patashnik, Or and Nitzan, Yotam and Azar, Yaniv and Shapiro, Stav and Cohen-Or, Daniel}, 83 | journal={arXiv preprint arXiv:2008.00951}, 84 | year={2020} 85 | } 86 | 87 | ``` 88 | -------------------------------------------------------------------------------- /docs/zh_CN/tutorials/starganv2.md: -------------------------------------------------------------------------------- 1 | # StarGAN V2 2 | 3 | ## 1 原理介绍 4 | 5 | [StarGAN V2](https://arxiv.org/pdf/1912.01865.pdf)是发布在CVPR2020上的一个图像转换模型。 6 | 一个好的图像到图像转换模型应该学习不同视觉域之间的映射,同时满足以下属性:1)生成图像的多样性和 2)多个域的可扩展性。 现有方法只解决了其中一个问题,领域的多样性有限或对所有领域用多个模型。 StarGAN V2是一个单一的框架,可以同时解决这两个问题,并在基线上显示出显着改善的结果。 CelebAHQ 和新的动物面孔数据集 (AFHQ) 上的实验验证了StarGAN V2在视觉质量、多样性和可扩展性方面的优势。 7 | 8 | ## 2 如何使用 9 | 10 | ### 2.1 数据准备 11 | 12 | StarGAN V2使用的CelebAHQ数据集可以从[这里](https://www.dropbox.com/s/f7pvjij2xlpff59/celeba_hq.zip?dl=0)下载,使用的AFHQ数据集可以从[这里](https://www.dropbox.com/s/t9l9o3vsx2jai3z/afhq.zip?dl=0)下载。将数据集下载解压后放到``PaddleGAN/data``文件夹下 。 13 | 14 | 数据的组成形式为: 15 | 16 | ``` 17 | ├── data 18 | ├── afhq 19 | | ├── train 20 | | | ├── cat 21 | | | ├── dog 22 | | | └── wild 23 | | └── val 24 | | ├── cat 25 | | ├── dog 26 | | └── wild 27 | └── celeba_hq 28 | ├── train 29 | | ├── female 30 | | └── male 31 | └── val 32 | ├── female 33 | └── male 34 | 35 | ``` 36 | 37 | ### 2.2 训练/测试 38 | 39 | 示例以AFHQ数据集为例。如果您想使用CelebAHQ数据集,可以在换一下配置文件。 40 | 41 | 训练模型: 42 | ``` 43 | python -u tools/main.py --config-file configs/starganv2_afhq.yaml 44 | ``` 45 | 46 | 测试模型: 47 | ``` 48 | python tools/main.py --config-file configs/starganv2_afhq.yaml --evaluate-only --load ${PATH_OF_WEIGHT} 49 | ``` 50 | 51 | ## 3 结果展示 52 | 53 | ![](https://user-images.githubusercontent.com/79366697/146308440-65259d70-d056-43d4-8cf5-a82530993910.jpg) 54 | 55 | ## 4 模型下载 56 | | 模型 | 数据集 | 下载地址 | 57 | |---|---|---| 58 | | starganv2_afhq | AFHQ | [starganv2_afhq](https://paddlegan.bj.bcebos.com/models/starganv2_afhq.pdparams) 59 | 60 | 61 | 62 | 63 | # 参考文献 64 | 65 | - 1. [StarGAN v2: Diverse Image Synthesis for Multiple Domains](https://arxiv.org/abs/1912.01865) 66 | 67 | ``` 68 | @inproceedings{choi2020starganv2, 69 | title={StarGAN v2: Diverse Image Synthesis for Multiple Domains}, 70 | author={Yunjey Choi and Youngjung Uh and Jaejun Yoo and Jung-Woo Ha}, 71 | booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition}, 72 | year={2020} 73 | } 74 | ``` 75 | -------------------------------------------------------------------------------- /test_tipc/configs/msvsr/train_infer_python.txt: -------------------------------------------------------------------------------- 1 | ===========================train_params=========================== 2 | model_name:msvsr 3 | python:python3.7 4 | gpu_list:0 5 | ## 6 | auto_cast:null 7 | total_iters:lite_train_lite_infer=10|lite_train_whole_infer=10|whole_train_whole_infer=200 8 | output_dir:./output/ 9 | dataset.train.batch_size:lite_train_lite_infer=1|whole_train_whole_infer=1 10 | pretrained_model:null 11 | train_model_name:msvsr_reds*/*checkpoint.pdparams 12 | train_infer_img_dir:./data/msvsr_reds/test 13 | null:null 14 | ## 15 | trainer:norm_train 16 | norm_train:tools/main.py -c configs/msvsr_reds.yaml --seed 123 -o log_config.interval=1 snapshot_config.interval=5 dataset.train.dataset.num_frames=15 17 | pact_train:null 18 | fpgm_train:null 19 | distill_train:null 20 | null:null 21 | null:null 22 | ## 23 | ===========================eval_params=========================== 24 | eval:null 25 | null:null 26 | ## 27 | ===========================infer_params=========================== 28 | --output_dir:./output/ 29 | load:null 30 | norm_export:tools/export_model.py -c configs/msvsr_reds.yaml --inputs_size="1,2,3,180,320" --model_name inference --load 31 | quant_export:null 32 | fpgm_export:null 33 | distill_export:null 34 | export1:null 35 | export2:null 36 | inference_dir:inference 37 | train_model:./inference/msvsr/multistagevsrmodel_generator 38 | infer_export:null 39 | infer_quant:False 40 | inference:tools/inference.py --model_type msvsr -c configs/msvsr_reds.yaml --seed 123 -o dataset.test.num_frames=2 --output_path test_tipc/output/ 41 | --device:cpu 42 | null:null 43 | null:null 44 | null:null 45 | null:null 46 | null:null 47 | --model_path: 48 | null:null 49 | null:null 50 | --benchmark:True 51 | null:null 52 | ===========================to_static_train_benchmark_params=========================== 53 | to_static_train:model.to_static=True 54 | ===========================train_benchmark_params========================== 55 | batch_size:2|4 56 | fp_items:fp32|fp16 57 | total_iters:60 58 | --profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile 59 | flags:FLAGS_cudnn_exhaustive_search=1 60 | ===========================infer_benchmark_params========================== 61 | random_infer_input:[{float32,[2,3,180,320]}] 62 | -------------------------------------------------------------------------------- /applications/tools/nafnet_denoising.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import os 16 | import sys 17 | import argparse 18 | 19 | sys.path.insert(0, os.getcwd()) 20 | import paddle 21 | from ppgan.apps import NAFNetPredictor 22 | 23 | if __name__ == "__main__": 24 | parser = argparse.ArgumentParser() 25 | parser.add_argument("--output_path", 26 | type=str, 27 | default='output_dir', 28 | help="path to output image dir") 29 | 30 | parser.add_argument("--weight_path", 31 | type=str, 32 | default=None, 33 | help="path to model checkpoint path") 34 | 35 | parser.add_argument("--seed", 36 | type=int, 37 | default=None, 38 | help="sample random seed for model's image generation") 39 | 40 | parser.add_argument('--images_path', 41 | default=None, 42 | required=True, 43 | type=str, 44 | help='Single image or images directory.') 45 | 46 | parser.add_argument("--cpu", 47 | dest="cpu", 48 | action="store_true", 49 | help="cpu mode.") 50 | 51 | args = parser.parse_args() 52 | 53 | if args.cpu: 54 | paddle.set_device('cpu') 55 | 56 | predictor = NAFNetPredictor(output_path=args.output_path, 57 | weight_path=args.weight_path, 58 | seed=args.seed) 59 | predictor.run(images_path=args.images_path) 60 | -------------------------------------------------------------------------------- /ppgan/faceutils/face_segmentation/layers.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import paddle 16 | import paddle.nn as nn 17 | import paddle.nn.functional as F 18 | 19 | 20 | def SyncBatchNorm(*args, **kwargs): 21 | """In cpu environment nn.SyncBatchNorm does not have kernel so use nn.BatchNorm instead""" 22 | if paddle.get_device() == 'cpu': 23 | return nn.BatchNorm(*args, **kwargs) 24 | else: 25 | return nn.SyncBatchNorm(*args, **kwargs) 26 | 27 | 28 | class ConvBNReLU(nn.Layer): 29 | def __init__(self, 30 | in_channels, 31 | out_channels, 32 | kernel_size, 33 | padding='same', 34 | **kwargs): 35 | super().__init__() 36 | 37 | self._conv = nn.Conv2D( 38 | in_channels, out_channels, kernel_size, padding=padding, **kwargs) 39 | 40 | self._batch_norm = SyncBatchNorm(out_channels) 41 | 42 | def forward(self, x): 43 | x = self._conv(x) 44 | x = self._batch_norm(x) 45 | x = F.relu(x) 46 | return x 47 | 48 | 49 | class ConvBN(nn.Layer): 50 | def __init__(self, 51 | in_channels, 52 | out_channels, 53 | kernel_size, 54 | padding='same', 55 | **kwargs): 56 | super().__init__() 57 | self._conv = nn.Conv2D( 58 | in_channels, out_channels, kernel_size, padding=padding, **kwargs) 59 | self._batch_norm = SyncBatchNorm(out_channels) 60 | 61 | def forward(self, x): 62 | x = self._conv(x) 63 | x = self._batch_norm(x) 64 | return x 65 | 66 | -------------------------------------------------------------------------------- /applications/tools/swinir_denoising.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import os 16 | import sys 17 | import argparse 18 | 19 | import paddle 20 | from ppgan.apps import SwinIRPredictor 21 | 22 | sys.path.insert(0, os.getcwd()) 23 | 24 | if __name__ == "__main__": 25 | parser = argparse.ArgumentParser() 26 | parser.add_argument("--output_path", 27 | type=str, 28 | default='output_dir', 29 | help="path to output image dir") 30 | 31 | parser.add_argument("--weight_path", 32 | type=str, 33 | default=None, 34 | help="path to model checkpoint path") 35 | 36 | parser.add_argument("--seed", 37 | type=int, 38 | default=None, 39 | help="sample random seed for model's image generation") 40 | 41 | parser.add_argument('--images_path', 42 | default=None, 43 | required=True, 44 | type=str, 45 | help='Single image or images directory.') 46 | 47 | parser.add_argument("--cpu", 48 | dest="cpu", 49 | action="store_true", 50 | help="cpu mode.") 51 | 52 | args = parser.parse_args() 53 | 54 | if args.cpu: 55 | paddle.set_device('cpu') 56 | 57 | predictor = SwinIRPredictor(output_path=args.output_path, 58 | weight_path=args.weight_path, 59 | seed=args.seed) 60 | predictor.run(images_path=args.images_path) 61 | -------------------------------------------------------------------------------- /applications/tools/photopen.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve. 2 | # 3 | #Licensed under the Apache License, Version 2.0 (the "License"); 4 | #you may not use this file except in compliance with the License. 5 | #You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | #Unless required by applicable law or agreed to in writing, software 10 | #distributed under the License is distributed on an "AS IS" BASIS, 11 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | #See the License for the specific language governing permissions and 13 | #limitations under the License. 14 | 15 | import paddle 16 | import os 17 | import sys 18 | 19 | sys.path.insert(0, os.getcwd()) 20 | from ppgan.apps import PhotoPenPredictor 21 | import argparse 22 | from ppgan.utils.config import get_config 23 | 24 | if __name__ == "__main__": 25 | parser = argparse.ArgumentParser() 26 | 27 | parser.add_argument("--semantic_label_path", 28 | type=str, 29 | default=None, 30 | help="path to input semantic label") 31 | 32 | parser.add_argument("--output_path", 33 | type=str, 34 | default=None, 35 | help="path to output image dir") 36 | 37 | parser.add_argument("--weight_path", 38 | type=str, 39 | default=None, 40 | help="path to model weight") 41 | 42 | parser.add_argument("--config-file", 43 | type=str, 44 | default=None, 45 | help="path to yaml file") 46 | 47 | parser.add_argument("--cpu", 48 | dest="cpu", 49 | action="store_true", 50 | help="cpu mode.") 51 | 52 | args = parser.parse_args() 53 | 54 | if args.cpu: 55 | paddle.set_device('cpu') 56 | 57 | cfg = get_config(args.config_file) 58 | predictor = PhotoPenPredictor(output_path=args.output_path, 59 | weight_path=args.weight_path, 60 | gen_cfg=cfg.predict) 61 | predictor.run(semantic_label_path=args.semantic_label_path) 62 | --------------------------------------------------------------------------------