Hammad712 commited on
Commit
c58fabe
·
verified ·
1 Parent(s): 11acb1c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +78 -15
README.md CHANGED
@@ -6,47 +6,110 @@ language:
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.
12
 
13
- Model Details
14
  Model Name: GAN Colorization Model
15
  Model Architecture: The model uses a ResNet-34 backbone as the encoder in the generator network and a PatchGAN discriminator network.
16
  Framework: PyTorch
17
  Repository: Hammad712/GAN-Colorization-Model
18
- Model Training
19
- Dataset
20
  Dataset Used: COCO 2017
21
  Training Size: 8000 images
22
  Validation Size: 2000 images
23
  Image Size: 256x256 pixels
24
- Training Configuration
25
  Batch Size: 16
26
  Number of Epochs: 5
27
  Optimizer for Generator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
28
  Optimizer for Discriminator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
29
- Loss Functions:
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
 
36
- Model Performance
37
  Qualitative Results
38
  The model generates visually plausible colorizations for grayscale images. Here are some examples of colorized outputs:
39
 
40
-
41
-
42
- Limitations
43
  The model may not always produce accurate colors for objects with complex or unusual color distributions.
44
  Performance may degrade for images that significantly differ from the training dataset.
45
  Citation
46
  If you use this model in your research, please cite the original repository:
47
 
48
- bibtex
49
- Copy code
50
  @misc{Hammad7122023GANColorization,
51
  title={GAN-Colorization-Model},
52
  author={Hammad712},
@@ -54,5 +117,5 @@ Copy code
54
  publisher={Hugging Face},
55
  howpublished={\url{https://huggingface.co/Hammad712/GAN-Colorization-Model}},
56
  }
57
- Contact
58
  For any issues or inquiries, please reach out to the model author through the Hugging Face repository.
 
6
  library_name: fastai
7
  pipeline_tag: image-to-image
8
  ---
9
+ # Model Description
 
10
  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.
11
 
12
+ # Model Details
13
  Model Name: GAN Colorization Model
14
  Model Architecture: The model uses a ResNet-34 backbone as the encoder in the generator network and a PatchGAN discriminator network.
15
  Framework: PyTorch
16
  Repository: Hammad712/GAN-Colorization-Model
17
+ # Model Training
18
+ ## Dataset
19
  Dataset Used: COCO 2017
20
  Training Size: 8000 images
21
  Validation Size: 2000 images
22
  Image Size: 256x256 pixels
23
+ ## Training Configuration
24
  Batch Size: 16
25
  Number of Epochs: 5
26
  Optimizer for Generator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
27
  Optimizer for Discriminator: Adam (learning rate: 0.0004, betas: 0.5, 0.999)
28
+ ## Loss Functions:
29
  GAN Loss: Binary Cross-Entropy Loss with Logits
30
  L1 Loss: L1 Loss for pixel-wise comparison between generated and real color channels
31
 
32
+ # Inference Code
33
+
34
+ from huggingface_hub import hf_hub_download
35
+ import torch
36
+ from PIL import Image
37
+ from torchvision import transforms
38
+ from skimage.color import rgb2lab, lab2rgb
39
+ import numpy as np
40
+ import matplotlib.pyplot as plt
41
+
42
+ #Download the model from Hugging Face Hub
43
+ repo_id = "Hammad712/GAN-Colorization-Model"
44
+ model_filename = "generator.pt"
45
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
46
+
47
+ #Define the generator model (same architecture as used during training)
48
+ from fastai.vision.learner import create_body
49
+ from torchvision.models import resnet34
50
+ from fastai.vision.models.unet import DynamicUnet
51
+
52
+ def build_generator(n_input=1, n_output=2, size=256):
53
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
54
+ backbone = create_body(resnet34(), pretrained=True, n_in=n_input, cut=-2)
55
+ G_net = DynamicUnet(backbone, n_output, (size, size)).to(device)
56
+ return G_net
57
+
58
+ #Initialize and load the model
59
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
60
+ G_net = build_generator(n_input=1, n_output=2, size=256)
61
+ G_net.load_state_dict(torch.load(model_path, map_location=device))
62
+ G_net.eval()
63
+
64
+ #Preprocessing function
65
+ def preprocess_image(img_path):
66
+ img = Image.open(img_path).convert("RGB")
67
+ img = transforms.Resize((256, 256), Image.BICUBIC)(img)
68
+ img = np.array(img)
69
+ img_to_lab = rgb2lab(img).astype("float32")
70
+ img_to_lab = transforms.ToTensor()(img_to_lab)
71
+ L = img_to_lab[[0], ...] / 50. - 1.
72
+ return L.unsqueeze(0).to(device)
73
+
74
+ #Inference function
75
+ def colorize_image(img_path, model):
76
+ L = preprocess_image(img_path)
77
+ with torch.no_grad():
78
+ ab = model(L)
79
+ L = (L + 1.) * 50.
80
+ ab = ab * 110.
81
+ Lab = torch.cat([L, ab], dim=1).permute(0, 2, 3, 1).cpu().numpy()
82
+ rgb_imgs = []
83
+ for img in Lab:
84
+ img_rgb = lab2rgb(img)
85
+ rgb_imgs.append(img_rgb)
86
+ return np.stack(rgb_imgs, axis=0)
87
+
88
+ #Example image path
89
+ img_path = "/path/to/your/image.jpg" # Replace with your image path
90
+
91
+ #Perform inference
92
+ colorized_images = colorize_image(img_path, G_net)
93
+
94
+ #Display the result
95
+ plt.imshow(colorized_images[0])
96
+ plt.axis("off")
97
+ plt.show()
98
+
99
+ # Usage
100
  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.
101
 
102
+ # Model Performance
103
  Qualitative Results
104
  The model generates visually plausible colorizations for grayscale images. Here are some examples of colorized outputs:
105
 
106
+ # Limitations
 
 
107
  The model may not always produce accurate colors for objects with complex or unusual color distributions.
108
  Performance may degrade for images that significantly differ from the training dataset.
109
  Citation
110
  If you use this model in your research, please cite the original repository:
111
 
112
+ # bibtex
 
113
  @misc{Hammad7122023GANColorization,
114
  title={GAN-Colorization-Model},
115
  author={Hammad712},
 
117
  publisher={Hugging Face},
118
  howpublished={\url{https://huggingface.co/Hammad712/GAN-Colorization-Model}},
119
  }
120
+ # Contact
121
  For any issues or inquiries, please reach out to the model author through the Hugging Face repository.