Colorize old photos with NoGAN's image enhancement technique.

DeOldify

Original image

DeOldify

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

DeOldify

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.

Related articles

python reverse Dict

A very common dictionary task is if we have a dictionary and want to flip its keys and values, the keys will become the values and the values will become the keys

Pyecharts Draws Visual Earth

Here we use the global dataset of the number of new crown infections as our test data, let's first look at the data as a whole import pandas as pd df = pd.read_csv("owid-covid-data.csv") df_0608 = df[df['date'] == '2022-06-08']

Python Retry Mechanism

To avoid functional problems caused by some network or other uncontrollable factors, such as. For example, when sending a request, there will be a request timeout problem often due to network instability.

How do you make Python code more professional?

Write your own code only for yourself to see, in fact, how to write. Once you have a team to work with, or to share your code, you have to write it properly, and professional code can accumulate technical influence for yourself.

Merge two or more dict

Suppose we have two or more dict and we want to merge them all into a single dict with a unique key

Mapping lists to dictionaries

The last task of the list code snippet, if given a list and mapping it to a dictionary, that is, we want to convert our list to a dictionary with numeric keys

Sorting a list of strings

We are often faced with lists containing strings that we need to sort alphabetically, by length, or any other factor we want or need for our application

Sorting the dictionary list

This set of daily list tasks are sorting tasks, and depending on the data types of the elements contained in the list, we will sort them in a slightly different way.