File size: 7,285 Bytes
8a6c301 869c037 8a6c301 869c037 8a6c301 869c037 70e8f53 869c037 12e1d9e 869c037 70e8f53 869c037 70e8f53 12e1d9e 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 56d5114 70e8f53 56d5114 869c037 70e8f53 869c037 56d5114 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 70e8f53 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 70e8f53 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 56d5114 869c037 12e1d9e 869c037 12e1d9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
---
library_name: keras
license: mit
language:
- en
pipeline_tag: image-to-image
---
# Autoencoder Grayscale2Color Landscape π‘οΈ
[](https://huggingface.co/docs/hub)
[](https://pypi.org/project/pillow/)
[](https://numpy.org/)
[](https://www.tensorflow.org/)
[](https://gradio.app/)
[](https://opensource.org/licenses/MIT)
## Introduction
Transform grayscale landscape images into vibrant, full-color visuals with this autoencoder model. Built from scratch, this project leverages deep learning to predict color channels (a*b* in L*a*b* color space) from grayscale inputs, delivering impressive results with a sleek, minimalist design. π
## Key Features
- πΈ Converts grayscale landscape images to vivid RGB.
- π§ Custom autoencoder with spatial attention for enhanced detail.
- β‘ Optimized for high-quality inference at 512x512 resolution.
- π Achieves a PSNR of 21.70 on the validation set.
## Notebook
Explore the implementation in our Jupyter notebook:
[](https://colab.research.google.com/#fileId=https://huggingface.co/danhtran2mind/autoencoder-grayscale2color-landscape/blob/main/notebooks/autoencoder-grayscale-to-color-landscape.ipynb)
[](https://huggingface.co/danhtran2mind/autoencoder-grayscale2color-landscape/blob/main/notebooks/autoencoder-grayscale-to-color-landscape.ipynb)
## Dataset
Details about the dataset are available in the [README Dataset](./dataset/README.md). π
## From Scratch Model
Custom-built autoencoder with a spatial attention mechanism, trained **FROM SCRATCH** to predict a*b* color channels from grayscale (L*) inputs. π§©
## Demonstration
Experience the brilliance of our cutting-edge technology! Transform grayscale landscapes into vibrant colors with our interactive demo.
[](https://huggingface.co/spaces/danhtran2mind/autoencoder-grayscale2color-landscape)

## Installation
### Step 1: Clone the Repository
```bash
git clone https://huggingface.co/danhtran2mind/autoencoder-grayscale2color-landscape
cd ./autoencoder-grayscale2color-landscape
git lfs pull
```
### Step 2: Install Dependencies
```bash
pip install -r requirements.txt
```
## Usage
Follow these steps to colorize images programmatically using Python.
### 1. Import Required Libraries
Install and import the necessary libraries for image processing and model inference.
```python
from PIL import Image
import os
import numpy as np
import tensorflow as tf
import requests
import matplotlib.pyplot as plt
from skimage.color import lab2rgb
from models.auto_encoder_gray2color import SpatialAttention
```
### 2. Load the Pre-trained Model
Download and load the autoencoder model from a remote source if itβs not already available locally.
```python
load_model_path = "./ckpts/best_model.h5"
os.makedirs(os.path.dirname(load_model_path), exist_ok=True)
print(f"Loading model from {load_model_path}...")
loaded_autoencoder = tf.keras.models.load_model(
load_model_path, custom_objects={"SpatialAttention": SpatialAttention}
)
print("Model loaded successfully.")
```
### 3. Define Image Processing Functions
These functions handle image preprocessing, colorization, and visualization.
```python
def process_image(input_img):
"""Convert a grayscale image to color using the autoencoder."""
# Store original dimensions
original_width, original_height = input_img.size
# Preprocess: Convert to grayscale, resize, and normalize
img = input_img.convert("L").resize((512, 512))
img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0
img_array = img_array[None, ..., 0:1] # Add batch dimension
# Predict color channels
output_array = loaded_autoencoder.predict(img_array)
# Reconstruct LAB image
L_channel = img_array[0, :, :, 0] * 100.0 # Scale L channel
ab_channels = output_array[0] * 128.0 # Scale ab channels
lab_image = np.stack([L_channel, ab_channels[:, :, 0], ab_channels[:, :, 1]], axis=-1)
# Convert to RGB and clip values
rgb_array = lab2rgb(lab_image)
rgb_array = np.clip(rgb_array, 0, 1) * 255.0
# Create and resize output image
rgb_image = Image.fromarray(rgb_array.astype(np.uint8), mode="RGB")
return rgb_image.resize((original_width, original_height), Image.Resampling.LANCZOS)
def process_and_save_image(image_path):
"""Process an image and save the colorized result."""
input_img = Image.open(image_path)
output_img = process_image(input_img)
output_img.save("output.jpg")
return input_img, output_img
def plot_images(input_img, output_img):
"""Display input and output images side by side."""
plt.figure(figsize=(17, 8), dpi=300)
# Plot input grayscale image
plt.subplot(1, 2, 1)
plt.imshow(input_img, cmap="gray")
plt.title("Input Grayscale Image")
plt.axis("off")
# Plot output colorized image
plt.subplot(1, 2, 2)
plt.imshow(output_img)
plt.title("Colorized Output Image")
plt.axis("off")
# Save and display the plot
plt.savefig("output.jpg", dpi=300, bbox_inches="tight")
plt.show()
```
### 4. Perform Inference
Run the colorization process on a sample image.
```python
# Set image dimensions and path
WIDTH, HEIGHT = 512, 512
image_path = "<path_to_input_image.jpg>" # Replace with your image path
# Process and visualize the image
input_img, output_img = process_and_save_image(image_path)
plot_images(input_img, output_img)
```
### 5. Example Output
The output will be a side-by-side comparison of the input grayscale image and the colorized result, saved as `output.jpg`. For a sample result, see the example below:

## Training Hyperparameters
- **Resolution**: 512x512 pixels
- **Color Space**: L*a*b*
- **Custom Layer**: SpatialAttention
- **Model File**: `best_model.h5`
- **Epochs**: 100
## Callbacks
- **Early Stopping**: Monitors `val_loss`, patience of 20 epochs, restores best weights.
- **ReduceLROnPlateau**: Monitors `val_loss`, reduces learning rate by 50% after 5 epochs, minimum learning rate of 1e-6.
- **BackupAndRestore**: Saves checkpoints to `./ckpts/backup`.
## Metrics
- **PSNR (Validation)**: 21.70 π
## Environment
- Python 3.11.11
- Libraies
```
numpy==1.26.4
tensorflow==2.18.0
opencv-python==4.11.0.86
scikit-image==0.25.2
matplotlib==3.7.2
scikit-image==0.25.2
```
## Contact
For questions or issues, reach out via the [HuggingFace Community](https://huggingface.co/danhtran2mind/autoencoder-grayscale2color-landscape/discussions) tab. π
|