Old photos carry every meaning in life, which is the memory of generations. Today, Jack Cui teaches you two algorithms to help you get rid of old photos that are not clear, wrinkled, black and white to color.
One, foreword
In your family, do you have old photos of elders with high appearance level?
Or it is black and white, or it is fuzzy, even drape damage.
Old photos carry every meaning in life, which is the memory of generations.
Today, Jack Cui teaches you two algorithms to help you get rid of old photos that are not clear, wrinkled, black and white to color.
Blurred photos, one click hd:
Fold photos, one-click restoration:
Black and white photo, one key color:
In this age of artificial intelligence, algorithms can do it for you!
Second, the algorithm
Blurred, wrinkled photo repair, is Microsoft 2020’s latest CVPR article:
“Bringing Old Photo Back to Life”
Black and white photo coloring, is a classic coloring algorithm (2018) :
“DeOldify”
1, Bringing Old Photo Back to Life
The authors are from City University of Hong Kong, Microsoft Research Asia, Microsoft Cloud + AI and University of Science and Technology of China.
The authors use variational automatic encoders (VAE) to transform images to hidden space and perform image recovery in hidden space.
I don’t understand. Speak English!
For example, a chess master can look at a board for five seconds and remember the positions of all the pieces in a way that a normal person can’t.
But the placement of the pieces must be the actual chess game (that is, the internal rules of the existence of the pieces), the random placement of the pieces can not be.
Chess masters don’t have better memories than we do, but they have more experience and are very good at recognizing patterns and memorizing games efficiently.
VAE are “chess masters”, they find the internal rules (hidden space) of pictures and then express them in their own way.
The author used two VAE:
The first VAE is used to encode synthesized old photos (blurred, worn) into the hidden space.
The second VAE is used to encode the corresponding clean old photos.
Then, in hidden space, learn the mapping from dirty old photos to clean photos.
In this way, the realization of an old photo repair algorithm.
This is a bit like learning to control the image’s clear, worn representation of a feature, and by controlling this feature, you can achieve the purpose of repairing damaged photos.
Restoration effect of the paper:
Open source algorithm, and a pre-training model, you can directly test the effect.
Project Address:
Github.com/microsoft/B…
The project relies on “Synchronized- batchnorm-pytorch”, which can be configured according to the tutorial.
Step 1: Clone Project:
git clone https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life
Copy the code
Step 2: Enter the project directory and clone dependent projects:
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 .. /.. /Copy the code
Step 3: Download the pre-training model.
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 .. /Copy the code
Then download the trained models of Global and Face_Enhancement respectively, decompress them and put them in the corresponding directories:
Global:
Facevc.blob.core.windows.net/zhanbo/old_…
Face_Enhancement:
Facevc.blob.core.windows.net/zhanbo/old_…
Download speed is slow, or too troublesome, you can download my packaged project, take direct use!
Model, code, material, project download address (extraction code: Jack) :
A network backup link: pan.baidu.com/s/1jVjd8dS0…
Usage:
Image repair without cracks, that is, the picture is not clear, you can use the following command:
python run.py --input_folder [test_image_folder_path] \
--output_folder [output_path] \
--GPU 0
Copy the code
Place the image you want to fix in the [test_image_folder_path] directory (self-specified) and the resulting image will be placed in the [output_path] directory.
For cracked images, you need to add an additional parameter, and the command is as follows:
python run.py --input_folder [test_image_folder_path] \
--output_folder [output_path] \
--GPU 0 \
--with_scratch
Copy the code
Note here that the specified path needs to use an absolute path.
Operation effect:
2, DeOldify
“DeOldify” is an image coloring algorithm.
Once on overheat search repair 100 years ago old Beijing image, is to use this algorithm.
Everything is ready-made and easy to use.
DeOldify is an app that works against generative networks.
The principle is the use of NoGAN technology, which combines the advantages of GAN training, such as excellent coloring, while eliminating some side effects, such as unstable coloring, flickering phenomenon.
Algorithm out for a long time, algorithm principle tutorial should be a lot, here is no longer tired, we directly see how to use it.
Project Address:
Github.com/jantic/DeOl…
Need to configure as above, install some libraries, and then download the model weights file.
The project project page details what files to download and what to install.
After configuration, write the following code in the project directory:
#NOTE: This must be the first call in order to work properly! from deoldify import device from deoldify.device_id import DeviceId #choices: CPU, GPU0... GPU7 device.set(device=DeviceId.GPU0) import torch if not torch.cuda.is_available(): print('GPU not available.') import fastai from deoldify.visualize import * import warnings warnings.filterwarnings("ignore", category=UserWarning, message=".*? Your .*? set is empty.*?" ) colorizer = get_image_colorizer(artistic=True) colorizer.plot_transformed_image("test_images/1.png", render_factor=10, compare=True)Copy the code
Test_images /1.png is the image to color, run the program, you can achieve black and white photo color.
Still, the model, code and materials have been packaged, and the project download address (extraction code: Jack) :
Web disk link: pan.baidu.com/s/17sma_a1I…
Open Jupyter and directly run the run.ipynb file. The generated results are saved in the result_images folder.
In addition to manipulating images, it’s also easy to color videos.
Third, summary
At home, are there some old black and white photos?
Repair their old photos for the elderly, is a surprise, but also the intention. Time gone never return, the past can only aftertaste ~
Elders such as amazing, remember to come back to forward, like oh!
I’m Jack Cui, a programmer who likes to do technical stuff, and we’ll see you next time