Photo_Colorizer / utils.py
matikosowy's picture
new model
dcc6440
from torchvision import transforms
def normalize_lab(L, ab):
"""
Normalize the L and ab channels of an image in Lab color space.
(Even though ab channels are in [-128, 127] range, we divide them by 110 because higher values are very rare.
This makes the distribution closer to [-1, 1] in most cases.)
"""
L = L / 50. - 1.
ab = ab / 110.
return L, ab
def denormalize_lab(L, ab):
"""
Denormalize the L and ab channels of an image in Lab color space.
(reverse of normalize_lab function)
"""
L = (L + 1) * 50.
ab = ab * 110.
return L, ab
def decide_size(image):
height = image.size[1]
width = image.size[0]
new_height = 256
new_width = 256
while new_height < height:
new_height *= 2
while new_width < width:
new_width *= 2
return new_height, new_width
def pad_image(image):
height = image.size[1]
width = image.size[0]
new_height, new_width = decide_size(image)
pad_height = new_height - height
pad_width = new_width - width
padding = (0, 0, pad_width, pad_height)
image = transforms.Pad(padding, padding_mode='reflect')(image)
return image