Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
Model Card: GAN Colorization Model
|
2 |
Model Description
|
3 |
This GAN-based model performs image colorization, transforming grayscale images into color images. It leverages a generator network to predict the color channels and a discriminator network to improve the colorization quality through adversarial training.
|
@@ -22,85 +30,6 @@ Loss Functions:
|
|
22 |
GAN Loss: Binary Cross-Entropy Loss with Logits
|
23 |
L1 Loss: L1 Loss for pixel-wise comparison between generated and real color channels
|
24 |
|
25 |
-
|
26 |
-
Model Inference
|
27 |
-
|
28 |
-
torch
|
29 |
-
torchvision
|
30 |
-
fastai
|
31 |
-
skimage
|
32 |
-
matplotlib
|
33 |
-
PIL
|
34 |
-
numpy
|
35 |
-
|
36 |
-
Inference Code
|
37 |
-
|
38 |
-
from huggingface_hub import hf_hub_download
|
39 |
-
import torch
|
40 |
-
from PIL import Image
|
41 |
-
from torchvision import transforms
|
42 |
-
from skimage.color import rgb2lab, lab2rgb
|
43 |
-
import numpy as np
|
44 |
-
import matplotlib.pyplot as plt
|
45 |
-
|
46 |
-
# Download the model from Hugging Face Hub
|
47 |
-
repo_id = "Hammad712/GAN-Colorization-Model"
|
48 |
-
model_filename = "generator.pt"
|
49 |
-
model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
|
50 |
-
|
51 |
-
# Define the generator model (same architecture as used during training)
|
52 |
-
from fastai.vision.learner import create_body
|
53 |
-
from torchvision.models import resnet34
|
54 |
-
from fastai.vision.models.unet import DynamicUnet
|
55 |
-
|
56 |
-
def build_generator(n_input=1, n_output=2, size=256):
|
57 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
58 |
-
backbone = create_body(resnet34(), pretrained=True, n_in=n_input, cut=-2)
|
59 |
-
G_net = DynamicUnet(backbone, n_output, (size, size)).to(device)
|
60 |
-
return G_net
|
61 |
-
|
62 |
-
# Initialize and load the model
|
63 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
64 |
-
G_net = build_generator(n_input=1, n_output=2, size=256)
|
65 |
-
G_net.load_state_dict(torch.load(model_path, map_location=device))
|
66 |
-
G_net.eval()
|
67 |
-
|
68 |
-
# Preprocessing function
|
69 |
-
def preprocess_image(img_path):
|
70 |
-
img = Image.open(img_path).convert("RGB")
|
71 |
-
img = transforms.Resize((256, 256), Image.BICUBIC)(img)
|
72 |
-
img = np.array(img)
|
73 |
-
img_to_lab = rgb2lab(img).astype("float32")
|
74 |
-
img_to_lab = transforms.ToTensor()(img_to_lab)
|
75 |
-
L = img_to_lab[[0], ...] / 50. - 1.
|
76 |
-
return L.unsqueeze(0).to(device)
|
77 |
-
|
78 |
-
# Inference function
|
79 |
-
def colorize_image(img_path, model):
|
80 |
-
L = preprocess_image(img_path)
|
81 |
-
with torch.no_grad():
|
82 |
-
ab = model(L)
|
83 |
-
L = (L + 1.) * 50.
|
84 |
-
ab = ab * 110.
|
85 |
-
Lab = torch.cat([L, ab], dim=1).permute(0, 2, 3, 1).cpu().numpy()
|
86 |
-
rgb_imgs = []
|
87 |
-
for img in Lab:
|
88 |
-
img_rgb = lab2rgb(img)
|
89 |
-
rgb_imgs.append(img_rgb)
|
90 |
-
return np.stack(rgb_imgs, axis=0)
|
91 |
-
|
92 |
-
# Example image path
|
93 |
-
img_path = "/path/to/your/image.jpg" # Replace with your image path
|
94 |
-
|
95 |
-
# Perform inference
|
96 |
-
colorized_images = colorize_image(img_path, G_net)
|
97 |
-
|
98 |
-
# Display the result
|
99 |
-
plt.imshow(colorized_images[0])
|
100 |
-
plt.axis("off")
|
101 |
-
plt.show()
|
102 |
-
|
103 |
-
|
104 |
Usage
|
105 |
To use the model for image colorization, ensure that the dependencies are installed and run the inference code provided. You will need to replace the image path with your own image for colorization.
|
106 |
|
|
|
1 |
+
---
|
2 |
+
datasets:
|
3 |
+
- phiyodr/coco2017
|
4 |
+
language:
|
5 |
+
- en
|
6 |
+
library_name: fastai
|
7 |
+
pipeline_tag: image-to-image
|
8 |
+
---
|
9 |
Model Card: GAN Colorization Model
|
10 |
Model Description
|
11 |
This GAN-based model performs image colorization, transforming grayscale images into color images. It leverages a generator network to predict the color channels and a discriminator network to improve the colorization quality through adversarial training.
|
|
|
30 |
GAN Loss: Binary Cross-Entropy Loss with Logits
|
31 |
L1 Loss: L1 Loss for pixel-wise comparison between generated and real color channels
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
Usage
|
34 |
To use the model for image colorization, ensure that the dependencies are installed and run the inference code provided. You will need to replace the image path with your own image for colorization.
|
35 |
|