项目源码:

https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life

简介

旧照片修复(官方PyTorch实现) 让老照片复活,CVPR2020(口头)

通过深层潜在空间平移修复旧照片,TPAMI 2022

该框架现在支持恢复高分辨率输入。

提供培训代码,欢迎尝试并了解培训详细信息。

您现在可以玩我们的Colab并在您的照片上尝试。

要求

该代码在安装了Nvidia GPU和CUDA的Ubuntu上进行了测试。运行代码需要 Python>=3.6。

安装

克隆 Syncd-BatchNorm-PyTorch 存储库

1
2
3
4
5
6
7
8
cd Face_Enhancement/models/networks/
git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
cp -rf Synchronized-BatchNorm-PyTorch/sync_batchnorm .
cd ../../../
cd Global/detection_models
git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
cp -rf Synchronized-BatchNorm-PyTorch/sync_batchnorm .
cd ../../

下载地标检测预训练模型

1
2
3
4
cd Face_Detection/
wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
bzip2 -d shape_predictor_68_face_landmarks.dat.bz2
cd ../

下载预训练模型,将文件放在 下,将文件放在 下。然后分别解压缩它们。

Face_Enhancement/checkpoints.zip./Face_EnhancementGlobal/checkpoints.zip./Global

1
2
3
4
5
6
7
8
cd Face_Enhancement/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/face_checkpoints.zip
unzip face_checkpoints.zip
cd ../
cd Global/
wget https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/global_checkpoints.zip
unzip global_checkpoints.zip
cd ../

安装依赖项:

1
pip install -r requirements.txt

🚀 怎么用?

注意:GPU 可以设置为 0 或 0,1,2 或 0,2;使用 -1 作为 CPU

1) 全管道

安装和下载预训练模型后,您可以使用一个简单的命令轻松恢复旧照片。

对于没有划痕的图像:

1
2
3
python run.py --input_folder [test_image_folder_path] \
              --output_folder [output_path] \
              --GPU 0

对于划痕图像:

1
2
3
4
python run.py --input_folder [test_image_folder_path] \
              --output_folder [output_path] \
              --GPU 0 \
              --with_scratch

对于带有划痕的高分辨率图像:

1
2
3
4
5
python run.py --input_folder [test_image_folder_path] \
              --output_folder [output_path] \
              --GPU 0 \
              --with_scratch \
              --HR

注意:请尝试使用绝对路径。最终结果将保存在 中。您还可以在 中查看不同步骤的生成结果。./output_path/final_output/output_path

2) 划痕检测

目前,我们不打算直接发布带有标签的划痕旧照片数据集。如果您想获取配对数据,您可以使用我们的预训练模型来测试收集的图像以获得标签。

1
2
3
4
cd Global/
python detection.py --test_path [test_image_folder_path] \
                    --output_dir [output_path] \
                    --input_size [resize_256|full_size|scale_256]

3) 全球恢复

提出一种三重域翻译网络来解决老照片的结构化退化和非结构化退化问题。

1
2
3
4
5
6
7
8
9
cd Global/
python test.py --Scratch_and_Quality_restore \
               --test_input [test_image_folder_path] \
               --test_mask [corresponding mask] \
               --outputs_dir [output_path]

python test.py --Quality_restore \
               --test_input [test_image_folder_path] \
               --outputs_dir [output_path]

4) 面部增强

我们使用渐进式生成器来细化旧照片的面部区域。更多详细信息可以在我们的期刊提交和文件夹中找到。./Face_Enhancement

注: 此回购主要用于研究目的,我们尚未优化运行性能。

由于模型是使用 256*256 张图像预训练的,因此该模型可能无法理想地用于任意分辨率。

5) 图形用户界面

一个用户友好的GUI,它接受用户输入的图像并在相应的窗口中显示结果。

工作原理:

运行 GUI.py 文件。

单击浏览并从test_images/old_w_scratch文件夹中选择您的图像以去除划痕。 单击修改照片按钮。 稍等片刻,然后在 GUI 窗口中查看结果。 通过单击退出窗口并在输出文件夹中获取结果图像。

如何训练?

1) 创建培训文件

将VOC数据集的文件夹,收集的旧照片(例如,Real_L_old和Real_RGB_old)放入一个共享文件夹中。然后

1
2
cd Global/data/
python Create_Bigfile.py

注意:请记住根据您自己的环境修改代码。

2)分别训练域A和域B的VAE

1
2
3
4
cd ..
python train_domain_A.py --use_v2_degradation --continue_train --training_dataset domain_A --name domainA_SR_old_photos --label_nc 0 --loadSize 256 --fineSize 256 --dataroot [your_data_folder] --no_instance --resize_or_crop crop_only --batchSize 100 --no_html --gpu_ids 0,1,2,3 --self_gen --nThreads 4 --n_downsample_global 3 --k_size 4 --use_v2 --mc 64 --start_r 1 --kl 1 --no_cgan --outputs_dir [your_output_folder] --checkpoints_dir [your_ckpt_folder]

python train_domain_B.py --continue_train --training_dataset domain_B --name domainB_old_photos --label_nc 0 --loadSize 256 --fineSize 256 --dataroot [your_data_folder]  --no_instance --resize_or_crop crop_only --batchSize 120 --no_html --gpu_ids 0,1,2,3 --self_gen --nThreads 4 --n_downsample_global 3 --k_size 4 --use_v2 --mc 64 --start_r 1 --kl 1 --no_cgan --outputs_dir [your_output_folder]  --checkpoints_dir [your_ckpt_folder]

注意:对于 –name 选项,请确保您的实验名称包含“domainA”或“domainB”,它们将用于选择不同的数据集。

3) 训练域之间的映射网络

训练映射无划痕:

1
python train_mapping.py --use_v2_degradation --training_dataset mapping --use_vae_which_epoch 200 --continue_train --name mapping_quality --label_nc 0 --loadSize 256 --fineSize 256 --dataroot [your_data_folder] --no_instance --resize_or_crop crop_only --batchSize 80 --no_html --gpu_ids 0,1,2,3 --nThreads 8 --load_pretrainA [ckpt_of_domainA_SR_old_photos] --load_pretrainB [ckpt_of_domainB_old_photos] --l2_feat 60 --n_downsample_global 3 --mc 64 --k_size 4 --start_r 1 --mapping_n_block 6 --map_mc 512 --use_l1_feat --niter 150 --niter_decay 100 --outputs_dir [your_output_folder] --checkpoints_dir [your_ckpt_folder]

用 scraches 跟踪映射:

1
python train_mapping.py --no_TTUR --NL_res --random_hole --use_SN --correlation_renormalize --training_dataset mapping --NL_use_mask --NL_fusion_method combine --non_local Setting_42 --use_v2_degradation --use_vae_which_epoch 200 --continue_train --name mapping_scratch --label_nc 0 --loadSize 256 --fineSize 256 --dataroot [your_data_folder] --no_instance --resize_or_crop crop_only --batchSize 36 --no_html --gpu_ids 0,1,2,3 --nThreads 8 --load_pretrainA [ckpt_of_domainA_SR_old_photos] --load_pretrainB [ckpt_of_domainB_old_photos] --l2_feat 60 --n_downsample_global 3 --mc 64 --k_size 4 --start_r 1 --mapping_n_block 6 --map_mc 512 --use_l1_feat --niter 150 --niter_decay 100 --outputs_dir [your_output_folder] --checkpoints_dir [your_ckpt_folder] --irregular_mask [absolute_path_of_mask_file]

用 scraches 跟踪映射(HR 输入的多尺度补丁注意力):

1
python train_mapping.py --no_TTUR --NL_res --random_hole --use_SN --correlation_renormalize --training_dataset mapping --NL_use_mask --NL_fusion_method combine --non_local Setting_42 --use_v2_degradation --use_vae_which_epoch 200 --continue_train --name mapping_Patch_Attention --label_nc 0 --loadSize 256 --fineSize 256 --dataroot [your_data_folder] --no_instance --resize_or_crop crop_only --batchSize 36 --no_html --gpu_ids 0,1,2,3 --nThreads 8 --load_pretrainA [ckpt_of_domainA_SR_old_photos] --load_pretrainB [ckpt_of_domainB_old_photos] --l2_feat 60 --n_downsample_global 3 --mc 64 --k_size 4 --start_r 1 --mapping_n_block 6 --map_mc 512 --use_l1_feat --niter 150 --niter_decay 100 --outputs_dir [your_output_folder] --checkpoints_dir [your_ckpt_folder] --irregular_mask [absolute_path_of_mask_file] --mapping_exp 1

引文

如果您发现我们的工作对您的研究有用,请考虑引用以下论文:)

@inproceedings{wan2020bringing, title={Bringing Old Photos Back to Life}, author={Wan, Ziyu and Zhang, Bo and Chen, Dongdong and Zhang, Pan and Chen, Dong and Liao, Jing and Wen, Fang}, booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition}, pages={2747–2757}, year={2020} } @article{wan2020old, title={Old Photo Restoration via Deep Latent Space Translation}, author={Wan, Ziyu and Zhang, Bo and Chen, Dongdong and Zhang, Pan and Chen, Dong and Liao, Jing and Wen, Fang}, journal={arXiv preprint arXiv:2009.07047}, year={2020} }

如果您也对旧照片/视频着色感兴趣,请参阅此作品。

维护

本项目目前由万子玉维护,仅供学术研究使用。如果您有任何疑问,请随时与 raywzy@gmail.com 联系。

许可证

此存储库中的代码和预训练模型位于 LICENSE 文件指定的 MIT 许可证下。我们使用标记的数据集来训练划痕检测模型。

该项目采用了Microsoft开源行为准则。有关更多信息,请参阅行为准则常见问题解答或联系 opencode@microsoft.com 提出任何其他问题或意见。