Colorize old photos with NoGAN's image enhancement technique.

Original image

After coloring
NoGAN is a new type of GAN that takes the least amount of time for GAN training.
1. Preparation
First, download the source code with the git clone command
git clone https://github.com/jantic/DeOldify.git
Go to the root of the project and install the Python dependencies
pip3 install -r requirements.txt
Before writing the code to run the project, the pre-trained models need to be downloaded. The project provides three models

Models
The difference is as follows:
-
ColorizeArtistic_gen.pth:Achieving the highest quality image coloring results in terms of interesting details and vibrancy, the model was trained on UNet using resnet34 as the backbone with 5 iterations of the reviewer pre-training/GAN cycle via NoGAN
-
ColorizeStable_gen.pth:The best results were achieved in landscapes and portraits, and the model was trained on UNet using resnet101 as the backbone with 3 iterations of the critic pre-training/GAN cycle via NoGAN
-
ColorizeVideo_gen.pth:Optimized for smooth videos, it uses only the initial generator/reviewer pre-training/GAN NoGAN training. It has less color than the first two due to the pursuit of smooth speed.
Simply place the downloaded model files in the models directory of the project root.
2. Writing code
Create a Python file in the same level of the project root directory and write code to load the model file you just downloaded.
from DeOldify.deoldify.generators import gen_inference_wide
from DeOldify.deoldify.filters import MasterFilter, ColorizerFilter
# Specify the model file
learn = gen_inference_wide(root_folder=Path('./DeOldify'), weights_name='ColorizeVideo_gen')
# Loading Models
deoldfly_model = MasterFilter([ColorizerFilter(learn=learn)], render_factor=10)
root_folder specifies the project root directory, and weights_name specifies which model to use next for coloring the photo.
Reading old photos for coloring
import cv2
import numpy as np
from PIL import Image
img = cv2.imread('./images/origin.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pil_img = Image.fromarray(img)
filtered_image = deoldfly_model.filter(
pil_img, pil_img, render_factor=35, post_process=True
)
result_img = np.asarray(filtered_image)
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
cv2.imwrite('deoldify.jpg', result_img)
Use cv2 to read old photos and use PIL.Image module to convert the images into the format needed for model input, send them to the model for coloring, and save them when finished.