Update README.md
Browse files
README.md
CHANGED
@@ -9,4 +9,95 @@ pipeline_tag: image-to-image
|
|
9 |
|
10 |
## Metrics
|
11 |
PSNR
|
12 |
-
- Validation set: 21.70
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
## Metrics
|
11 |
PSNR
|
12 |
+
- Validation set: 21.70
|
13 |
+
|
14 |
+
## Usage
|
15 |
+
|
16 |
+
### Download Model
|
17 |
+
```bash
|
18 |
+
git clone https://huggingface.co/danhtran2mind/autoencoder-grayscale2color-landscape
|
19 |
+
```
|
20 |
+
```bash
|
21 |
+
cd autoencoder-grayscale2color-landscape
|
22 |
+
git lfs pull
|
23 |
+
```
|
24 |
+
### Import Libraries
|
25 |
+
```bash
|
26 |
+
from PIL import Image
|
27 |
+
import os
|
28 |
+
import numpy as np
|
29 |
+
import tensorflow as tf
|
30 |
+
import requests
|
31 |
+
from skimage.color import lab2rgb
|
32 |
+
|
33 |
+
from models.auto_encoder_gray2color import SpatialAttention
|
34 |
+
|
35 |
+
```
|
36 |
+
### Load Model file
|
37 |
+
```bash
|
38 |
+
# Load the saved model once at startup
|
39 |
+
load_model_path = "./ckpts/best_model.h5"
|
40 |
+
|
41 |
+
print(f"Loading model from {load_model_path}...")
|
42 |
+
loaded_autoencoder = tf.keras.models.load_model(
|
43 |
+
load_model_path,
|
44 |
+
custom_objects={'SpatialAttention': SpatialAttention}
|
45 |
+
)
|
46 |
+
```
|
47 |
+
|
48 |
+
### Inferenc Step
|
49 |
+
```python
|
50 |
+
# Assuming process_image function is defined as provided
|
51 |
+
# and required imports (tensorflow, skimage.color for lab2rgb) are available
|
52 |
+
|
53 |
+
def display_images_pil(input_path):
|
54 |
+
# Read input image
|
55 |
+
input_img = Image.open(input_path)
|
56 |
+
|
57 |
+
# Process the image using the provided function
|
58 |
+
output_img = process_image(input_img)
|
59 |
+
|
60 |
+
# Display input image
|
61 |
+
input_img.show(title="Input Image")
|
62 |
+
|
63 |
+
# Display output image
|
64 |
+
output_img.show(title="Output Image")
|
65 |
+
|
66 |
+
def process_image(input_img):
|
67 |
+
# Store original input dimensions
|
68 |
+
original_width, original_height = input_img.size
|
69 |
+
|
70 |
+
# Convert PIL Image to grayscale and resize to model input size
|
71 |
+
img = input_img.convert("L") # Convert to grayscale (single channel)
|
72 |
+
img = img.resize((WIDTH, HEIGHT)) # Resize to 512x512 for model
|
73 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0 # Normalize to [0, 1]
|
74 |
+
img_array = img_array[None, ..., 0:1] # Add batch dimension, shape: (1, 512, 512, 1)
|
75 |
+
|
76 |
+
# Run inference (assuming loaded_autoencoder predicts a*b* channels)
|
77 |
+
output_array = loaded_autoencoder.predict(img_array) # Shape: (1, 512, 512, 2) for a*b*
|
78 |
+
print("output_array shape: ", output_array.shape)
|
79 |
+
|
80 |
+
# Extract L* (grayscale input) and a*b* (model output)
|
81 |
+
L_channel = img_array[0, :, :, 0] * 100.0 # Denormalize L* to [0, 100]
|
82 |
+
ab_channels = output_array[0] * 128.0 # Denormalize a*b* to [-128, 128]
|
83 |
+
|
84 |
+
# Combine L*, a*, b* into a 3-channel L*a*b* image
|
85 |
+
lab_image = np.stack([L_channel, ab_channels[:, :, 0], ab_channels[:, :, 1]], axis=-1) # Shape: (512, 512, 3)
|
86 |
+
|
87 |
+
# Convert L*a*b* to RGB
|
88 |
+
rgb_array = lab2rgb(lab_image) # Convert to RGB, output in [0, 1]
|
89 |
+
rgb_array = np.clip(rgb_array, 0, 1) * 255.0 # Scale to [0, 255]
|
90 |
+
rgb_image = Image.fromarray(rgb_array.astype(np.uint8), mode="RGB") # Create RGB PIL image
|
91 |
+
|
92 |
+
# Resize output image to match input image resolution
|
93 |
+
rgb_image = rgb_image.resize((original_width, original_height), Image.Resampling.LANCZOS)
|
94 |
+
|
95 |
+
return rgb_image
|
96 |
+
|
97 |
+
|
98 |
+
# Example usage
|
99 |
+
if __name__ == "__main__":
|
100 |
+
# Replace 'input_image.jpg' with the path to your image
|
101 |
+
image_path = "<input_image.jpg>"
|
102 |
+
display_images_pil(image_path)
|
103 |
+
```
|