Gray-scale to Color Conversion
Author: Soham Mukherjee
Date: December 2023
This project aims to colorize grayscale images using Convolutional Neural Networks (CNNs) in the YUV color space. It includes functions for training and inference on multi-output-channel CNNs, as well as utilities for YUV to RGB conversion and display, and neural model complexity analysis based on FLOPs and MACCs.
The colab file included in this project includes all the details.
Ensure you have the required libraries installed. You can install them using the following command: pip install numpy tensorflow matplotlib requests scipy
To use this project, follow these steps:
- Download images (DIV2K dataset). Images 001-800 are used as the training set and the images 801-900 are used as the testing set.
- Convert the images in each set to YUV420p using FFmpeg.
- Get random p x p patches from images in each set. Each patch comprises a p x p Y channel and two corresponding U, V channels at half resolution p/2 x p/2. The Y patches are written out to a Y file, while the U, V channels are output interleaved as 2 channels to a separate file.
- Randomly sort the patches from the Y and UV patch files.
- Create numpy NPZ array files to store the sorted Y and UV patches for each set for convenience. Note that each NPZ file contains one multi-dimensional Y array of size (#patches x p x p), and one corresponding multi-dimensional UV array of size (#patches x p/2 x p/2 x 2). There will be two NPZ files produced one for the training set and another for the testing set.
- Load YUV data NPZ files for training and testing using Google Drive paths.
- Split the training set into training and validation sets.
- Normalize and reshape the data using the
preprocess
function. - Generate a CNN model with the
generate_cnn
function. - Train the CNN model with the training and validation data.
- Save the trained model to Google Drive.
- Test the trained model on the test dataset.
The project utilizes YUV data stored in NPZ files. The training and testing data paths are defined as follows:
- Training Data:
/content/drive/MyDrive/train64.npz
- Testing Data:
/content/drive/MyDrive/valid64.npz
For each inference pass of the CNN:
- Model inputs are the Y channels.
- Model outputs (labels) are the U and V channels.
- Together we have the predicted color YUV image.
The CNN model is designed for image colorization. It includes convolutional layers with varying filter sizes and strides. The architecture is defined in the generate_cnn
function.
To train the model, execute the following steps directly in the Colab notebook:
- Load training and testing data.
- Split the training set into training and validation sets.
- Normalize and reshape the data.
- Generate a CNN model using
generate_cnn
. - Display the model summary and estimate FLOPS/MACCs.
- Train the model using the
fit
method. - Save the trained model to Google Drive.
Example training code:
my_cnn.fit( x=y_train_norm, y=uv_train_norm, epochs=25, batch_size=1024, shuffle=True, validation_data=(y_valid_norm, uv_valid_norm), )
The evaluation of the trained model can be performed using a custom testing function on the test set. The function test_my_cnn
preprocesses the test data, generates UV colors using the trained model, and displays the results using the display3
function directly in the Colab notebook.
Example evaluation code:
model_out = '/content/drive/MyDrive/Colab Notebooks/colorizeA.keras' my_cnn = tf.keras.models.load_model(model_out)
test_my_cnn(my_cnn, y_test, uv_test)
Provide details and visualizations of the results obtained from training and evaluation.
- The project makes use of TensorFlow, NumPy, FFmpeg and other open-source libraries.
- The project uses the public DIV2K Dataset for images.