-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
60 lines (51 loc) · 1.87 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
from torch.nn import functional as F
import numpy as np
import torchvision
from torchvision import transforms
class Map:
"""
Maps every pixel to the respective object in the dictionary
Input:
mapper: dict, dictionary of the mapping
"""
def __init__(self, mapper):
self.mapper = mapper
def __call__(self, input):
return np.vectorize(self.mapper.__getitem__, otypes=[np.float32])(input)
class Map2:
"""
Maps every pixel to the respective object in the dictionary
Input:
mapper: dict, dictionary of the mapping
"""
def __init__(self, mapper):
self.mapper = mapper
def __call__(self, input):
return np.array([[self.mapper[element] for element in row]for row in input], dtype=np.float32)
class ToTensor:
"""
Convert into a tensor of float32: differently from transforms.ToTensor() this function does not normalize the values in [0,1] and does not swap the dimensions
"""
def __call__(self, input):
return torch.as_tensor(input, dtype=torch.float32)
class ToNumpy:
"""
Convert into a tensor into a numpy array
"""
def __call__(self, input):
return input.numpy()
class ToTensorSwap:
"""
Convert into a tensor of float32: differently from transforms.ToTensor() this function does not normalize the values in [0,1] and does not swap the dimensions
"""
def __call__(self, input):
return torch.as_tensor(input, dtype=torch.uint8).permute(2,0,1)
def colorLabel(label, palette):
composed = torchvision.transforms.Compose([ToNumpy(), Map2(palette), ToTensorSwap(), transforms.ToPILImage()])
label = composed(label)
return label
def save_images(palette, predict, path_to_save):
predict = torch.tensor(predict.copy(), dtype=torch.uint8).squeeze()
predict = colorLabel(predict, palette)
predict.save(path_to_save)