ialhashim commited on
Commit
00b1139
·
0 Parent(s):

Removed binary

Browse files
Files changed (15) hide show
  1. .gitattributes +27 -0
  2. README.md +13 -0
  3. app.py +74 -0
  4. colorizer.py +133 -0
  5. ka0001.jpg +0 -0
  6. ka0002.jpg +0 -0
  7. ka0003.jpg +0 -0
  8. ka0004.jpg +0 -0
  9. ka0005.jpg +0 -0
  10. ka0006.jpg +0 -0
  11. ka0007.jpg +0 -0
  12. ka0008.jpg +0 -0
  13. ka0009.jpg +0 -0
  14. ka0010.jpg +0 -0
  15. ka0011.jpg +0 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Colorizer
3
+ emoji: 🏃
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 2.9.4
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import torch
4
+
5
+ import skimage
6
+ import skimage.io
7
+ from skimage.transform import rescale, resize
8
+ from skimage import io, color
9
+
10
+ import cv2
11
+ from colorizer import normalize_lab_channels, torch_normalized_lab_to_rgb
12
+
13
+ model = torch.load('colorizer.pth')
14
+ model = model.eval()
15
+
16
+ def colorize_img( img_float32, model, res=512, border=0.2, apply_blur=True):
17
+ img = img_float32
18
+
19
+ # Add white border
20
+ border = int(img.shape[0] * border)
21
+ img2 = cv2.copyMakeBorder(img, border, border, border, border, cv2.BORDER_CONSTANT, value=(1.,1.,1.))
22
+
23
+ # Resize to expected resolution
24
+ img_resized = resize(img2, (res,res), anti_aliasing=True)
25
+
26
+ # Blur image a bit
27
+ if apply_blur:
28
+ img2 = skimage.filters.gaussian( img_resized, sigma=1, channel_axis=-1 )
29
+ else:
30
+ img2 = img_resized
31
+
32
+ # Convert to Lab color space and normalize between 0 and 1 all channels
33
+ img2 = normalize_lab_channels(color.rgb2lab(img2))
34
+
35
+ # Keep copy of unblured + resized image
36
+ img_resized = normalize_lab_channels(color.rgb2lab(img_resized))
37
+ img_resized = torch.from_numpy(img_resized)
38
+ img_resized = img_resized.permute(2,0,1).unsqueeze(dim=0)
39
+
40
+ # Convert to expected tensor of 'LLL'
41
+ x = torch.from_numpy(img2)
42
+ x = x.permute(2,0,1).unsqueeze(dim=0)
43
+ x[:,1,:,:] = x[:,0,:,:]
44
+ x[:,2,:,:] = x[:,0,:,:]
45
+
46
+ x_hat_ab = model( x )
47
+
48
+ x_hat = img_resized.clone()
49
+ x_hat[:,1:,:,:] = x_hat_ab.clone()
50
+
51
+ colored_img = torch_normalized_lab_to_rgb( x_hat )
52
+
53
+ return colored_img.detach().cpu().squeeze().permute(1,2,0).numpy()
54
+
55
+ def process_image(img):
56
+ return colorize_img( (img / 255).astype('float32'), model )
57
+
58
+ image = gr.inputs.Image()
59
+ label = gr.outputs.Label()
60
+ title = "Colorizer"
61
+ description = "A model that colorizes b&w images."
62
+ interpretation='default'
63
+ enable_queue=True
64
+
65
+ examples = ['ka0001.jpg', 'ka0003.jpg', 'ka0009.jpg', 'ka0010.jpg']
66
+ css = ".h-60 {min-height: 512px !important;}"
67
+
68
+ gr.Interface(fn=process_image,
69
+ inputs=gr.inputs.Image(),
70
+ outputs=gr.outputs.Image(),
71
+ title=title,
72
+ description=description,
73
+ css=css,
74
+ examples=examples).launch()
colorizer.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import glob
5
+ import random
6
+ import skimage
7
+ import skimage.io
8
+ import numpy as np
9
+ from skimage import io, color
10
+
11
+ import skimage
12
+ import skimage.io
13
+ from PIL import Image
14
+ import cv2
15
+
16
+ import torch
17
+ import torch.nn as nn
18
+ from torch.nn import functional as F
19
+
20
+ import timm
21
+ import torchvision
22
+ from torchvision.models.feature_extraction import create_feature_extractor
23
+
24
+ L_range = 100
25
+ ab_min = -128
26
+ ab_max = 127
27
+ ab_range = ab_max - ab_min
28
+
29
+ def extract_zip(input_zip):
30
+ input_zip=ZipFile(input_zip)
31
+ return {name: input_zip.read(name) for name in input_zip.namelist()}
32
+
33
+ def normalize_lab_channels(x):
34
+ # Normalize L
35
+ x[:,:,0] = x[:,:,0] / L_range
36
+
37
+ # Normalize AB
38
+ x[:,:,1] = (x[:,:,1]-ab_min) / ab_range
39
+ x[:,:,2] = (x[:,:,2]-ab_min) / ab_range
40
+ return x
41
+
42
+ def normalized_lab_to_rgb(lab):
43
+ lab[:,:,0] = (lab[:,:,0] * L_range)
44
+ lab[:,:,1] = (lab[:,:,1] * ab_range) + ab_min
45
+ lab[:,:,2] = (lab[:,:,2] * ab_range) + ab_min
46
+ return color.lab2rgb(lab)
47
+
48
+ def torch_normalized_lab_to_rgb(lab):
49
+ for i in range(lab.shape[0]):
50
+ lab[i,0,:,:] = torch.clip(lab[i,0,:,:] * L_range, 0, L_range)
51
+ lab[i,1,:,:] = torch.clip((lab[i,1,:,:] * ab_range) + ab_min, ab_min, ab_max)
52
+ lab[i,2,:,:] = torch.clip((lab[i,2,:,:] * ab_range) + ab_min, ab_min, ab_max)
53
+
54
+ for i in range(lab.shape[0]):
55
+ lab[i] = torch.from_numpy( color.lab2rgb(lab[i].permute(1,2,0).detach().cpu().numpy()) ).permute(2,0,1)
56
+
57
+ return lab
58
+
59
+ class Encoder(nn.Module):
60
+ def __init__(self):
61
+ super(Encoder, self).__init__()
62
+ self.backend_model = timm.create_model('efficientnetv2_rw_s', pretrained=True)
63
+ self.backend = create_feature_extractor(self.backend_model,
64
+ return_nodes=['blocks.0', 'blocks.1', 'blocks.2', 'blocks.3', 'act2'])
65
+
66
+ def forward(self, x):
67
+ features = self.backend(x)
68
+ return list(features.values())
69
+
70
+ class UpSample(nn.Sequential):
71
+ def __init__(self, in_channels, out_channels):
72
+ skip_input, output_features = in_channels, out_channels
73
+
74
+ super(UpSample, self).__init__()
75
+ self.convA = nn.Conv2d(skip_input, output_features, kernel_size=3, stride=1, padding=1, padding_mode='reflect', bias=False)
76
+ self.leakyreluA = nn.LeakyReLU(0.2)
77
+ self.convB = nn.Conv2d(output_features, output_features, kernel_size=3, stride=1, padding=1, padding_mode='reflect', bias=False)
78
+ self.leakyreluB = nn.LeakyReLU(0.2)
79
+
80
+ self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False)
81
+
82
+ def forward(self, x, concat_with=None):
83
+ up_x = self.upsample(x)
84
+
85
+ if concat_with is not None:
86
+ up_x = torch.cat([up_x, concat_with], dim=1)
87
+
88
+ return self.leakyreluB( self.convB( self.leakyreluA( self.convA( up_x ) ) ) )
89
+
90
+ class Decoder(nn.Module):
91
+ def __init__(self, num_features=1792 * 1, decoder_width=None):
92
+ super(Decoder, self).__init__()
93
+ features = int(num_features * decoder_width)
94
+
95
+ self.conv2 = nn.Sequential(
96
+ nn.Conv2d(num_features, features, kernel_size=1, stride=1, padding=0, bias=False),
97
+ nn.LeakyReLU(0.2),
98
+ )
99
+
100
+ self.up1 = UpSample(in_channels=features//1 + 152 - 24, out_channels=features//2)
101
+ self.up2 = UpSample(in_channels=features//2 + 80 - 16, out_channels=features//4)
102
+ self.up3 = UpSample(in_channels=features//4 + 56 - 8, out_channels=features//8)
103
+ self.up4 = UpSample(in_channels=features//8 + 32 - 8, out_channels=features//16)
104
+
105
+ self.up5 = UpSample(in_channels=features//16, out_channels=features//16)
106
+
107
+ self.conv3 = nn.Conv2d(features//16, 2, kernel_size=1, stride=1, padding=0, bias=False)
108
+
109
+ def forward(self, features):
110
+ blocks0, blocks1, blocks2, blocks3, x = features
111
+
112
+ x = self.conv2(x)
113
+
114
+ x = self.up1(x, blocks3)
115
+ x = self.up2(x, blocks2)
116
+ x = self.up3(x, blocks1)
117
+ x = self.up4(x, blocks0)
118
+
119
+ x = self.up5(x)
120
+
121
+ x_final = self.conv3(x)
122
+
123
+ return x_final
124
+
125
+ class ColorizeNet(nn.Module):
126
+ def __init__(self, decoder_width):
127
+ super(ColorizeNet, self).__init__()
128
+ self.encoder = Encoder()
129
+ self.decoder = Decoder(decoder_width=decoder_width)
130
+
131
+ def forward(self, x):
132
+ features_x = self.encoder(x)
133
+ return self.decoder( features_x )
ka0001.jpg ADDED
ka0002.jpg ADDED
ka0003.jpg ADDED
ka0004.jpg ADDED
ka0005.jpg ADDED
ka0006.jpg ADDED
ka0007.jpg ADDED
ka0008.jpg ADDED
ka0009.jpg ADDED
ka0010.jpg ADDED
ka0011.jpg ADDED