Hammad712 commited on
Commit
bc52b6d
·
verified ·
1 Parent(s): 8760604

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +129 -0
README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.
4
+
5
+ Model Details
6
+ Model Name: GAN Colorization Model
7
+ Model Architecture: The model uses a ResNet-34 backbone as the encoder in the generator network and a PatchGAN discriminator network.
8
+ Framework: PyTorch
9
+ Repository: Hammad712/GAN-Colorization-Model
10
+ Model Training
11
+ Dataset
12
+ Dataset Used: COCO 2017
13
+ Training Size: 8000 images
14
+ Validation Size: 2000 images
15
+ Image Size: 256x256 pixels
16
+ Training Configuration
17
+ Batch Size: 16
18
+ Number of Epochs: 5
19
+ Optimizer for Generator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
20
+ Optimizer for Discriminator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
21
+ 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
+
107
+ Model Performance
108
+ Qualitative Results
109
+ The model generates visually plausible colorizations for grayscale images. Here are some examples of colorized outputs:
110
+
111
+
112
+
113
+ Limitations
114
+ The model may not always produce accurate colors for objects with complex or unusual color distributions.
115
+ Performance may degrade for images that significantly differ from the training dataset.
116
+ Citation
117
+ If you use this model in your research, please cite the original repository:
118
+
119
+ bibtex
120
+ Copy code
121
+ @misc{Hammad7122023GANColorization,
122
+ title={GAN-Colorization-Model},
123
+ author={Hammad712},
124
+ year={2023},
125
+ publisher={Hugging Face},
126
+ howpublished={\url{https://huggingface.co/Hammad712/GAN-Colorization-Model}},
127
+ }
128
+ Contact
129
+ For any issues or inquiries, please reach out to the model author through the Hugging Face repository.