what the project does:
- Load and preprocess image datasets.
- Convert images into the Lab color space.
- Use a deep learning model with an encoder-decoder architecture for colorization.
- Train the model to predict the
ABcolor channels from theL(lightness) channel. - Save and visualize the trained model's results.
The load_image_dataset function loads all images from a given folder path, applies preprocessing transformations, and converts them to tensors.
- Input: Path to image folder.
- Output: A PyTorch tensor containing all preprocessed images.
train_folderandval_folderpoint to the respective directories in the ImageNet dataset.
- Resize: Images are resized to 128x128 pixels.
- ToTensor: Images are converted into tensors scaled to a
[0, 1]range.
The training and validation datasets are wrapped into PyTorch DataLoader objects, enabling efficient batch processing.
The Lab color space separates lightness (L) and color (AB) channels. The preprocess_lab function converts RGB images to Lab and extracts:
X: TheL(lightness) channel as input.Y: TheABcolor channels as target labels, scaled to [-1, 1].
The colorization model consists of:
[!Important] Architecture
- A pre-trained VGG-16 model is used as the encoder.
- Only the first 23 layers (up to the last convolutional layer) are retained.
- The encoder's weights are frozen to prevent updates during training.
- A custom CNN is used as the decoder, which progressively upsamples the encoded feature maps to the original image size (128x128).
- The decoder uses:
- Convolutional layers with ReLU activation.
- Upsampling layers using bilinear interpolation.
- A final Tanh activation layer to scale the output.
The ColorizationModel class integrates the encoder and decoder into a single model.
The training loop optimizes the model using:
- Loss Function: Mean Squared Error (MSE) loss between predicted and true
ABchannels. - Optimizer: Adam optimizer with a learning rate of 0.0002.
- Load a batch of grayscale images and preprocess them.
- Repeat the
Lchannel to create a 3-channel input for the encoder. - Forward pass through the model.
- Compute the loss and backpropagate to update the decoder weights.
- Print the average loss after each epoch.
The trained model's parameters are saved to a file (colorization_model0.pt) for future use.
torch.save(model.state_dict(), "colorization_model0.pt")The test_and_visualize function evaluates the model on a sample image and visualizes the colorization results.
Steps:
- Load the model and set it to evaluation mode.
- Convert the image to the Lab color space and extract the
Lchannel. - Forward pass through the model to predict
ABchannels. - Combine the
Land predictedABchannels to reconstruct the color image.
- The original and colorized images are displayed side-by-side using Matplotlib.
The ImageNet dataset (7GB) is used for training and validation. Images are organized into separate folders for training and validation data.
The code is designed to run on Kaggle, which provides 30 hours of free GPU usage per week. It leverages CUDA if available for faster training and inference.
- Main Script: Contains all preprocessing, model definition, training, and testing code.
- Saved Model: The trained model parameters (
colorization_model0.pt). - Dataset Folders: Organized into
trainandvaldirectories.
[!faq] Requirements
- Python 3.x
- PyTorch
- torchvision
- scikit-image
- numpy
- matplotlib
[!example] INSTRUCTIONS
Set Up the Environment Ensure all dependencies are installed. Use Kaggle or a local machine with GPU support.
Dataset Preparation Download and extract the ImageNet dataset. Place it in the appropriate folder structure:
/imagenet /train /valRun the Code Execute the script in an environment with GPU support for faster training. Use Kaggle for free GPU hours if needed.
Testing Use the
test_and_visualizefunction to evaluate the model on a sample image.
- we have a gui file called
gui.pyin the repository, download it along with themodel.ptfile in the same directory - type in the terminal
streamlit run [absolute path to the gui.py file here] - or use our web app here !