-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataloader.py
137 lines (97 loc) · 4.42 KB
/
dataloader.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset
import torchvision
from torchvision.transforms import Resize, Compose
import numpy as np
from utils import load_img, np_psnr
class MIT5KData(Dataset):
def __init__(self, originals, enhanced):
self.originals = originals
self.enhanced = enhanced
def __len__(self):
return len(self.originals)
def __getitem__(self, idx):
inp_img = load_img (self.originals[idx])
inp = torch.from_numpy(inp_img)
inp = inp.reshape((inp.shape[0]*inp.shape[1],3))
enh_img = load_img (self.enhanced[idx])
return inp, inp_img, enh_img
class LUTFitting(Dataset):
def __init__(self, inp_img, out_img, resize=False):
super().__init__()
img = load_img(inp_img)
lut = load_img(out_img)
self.error = np_psnr(img,lut)
assert img.shape == lut.shape
assert (img.max() <= 1) and (lut.max() <= 1)
if resize:
self.resize = Compose([Resize(img.shape[0] // 2, interpolation=torchvision.transforms.InterpolationMode.NEAREST)])
# Convert images to pytorch tensors
img = torch.from_numpy(img)
if resize: img = self.resize(img)
lut = torch.from_numpy(lut)
if resize: lut = self.resize(lut)
self.shape = img.shape
#self.pixels = img.permute(1, 2, 0).view(-1, 1)
self.intensities = img.reshape((img.shape[0]*img.shape[1],3))
self.outputs = lut.reshape((img.shape[0]*img.shape[1],3))
self.dim = self.intensities.shape
del img, lut
def __dim__(self):
return self.dim
def __shape__(self):
return self.shape
def __len__(self):
return 1
def __error__(self):
return self.error
def __getitem__(self, idx):
if idx > 0: raise IndexError
return self.intensities, self.outputs
class EvalMultiLUTBlending (Dataset):
"""
Dataloader to load the input image <inp_img> and the reference target images <list_out_imgs>.
The order of the target images must be: ground-truth 3D LUT outputs (the first <nluts> elements in the list), following by gt blending results.
We will load each reference, and include the corresponding style vector a sinput to the network
Example:
test_images = EvalMultiLUTFitting('./DatasetLUTs_100images/001.png',
['./DatasetLUTs_100images/001_LUT01.png',
'./DatasetLUTs_100images/001_LUT03.png',
'./DatasetLUTs_100images/001_LUT04.png',
'./DatasetLUTs_100images/001_blend.png'], nluts=3)
test_dataloader = DataLoader(test_images, batch_size=1, pin_memory=True, num_workers=0)
"""
def __init__(self, inp_img, list_out_img, nluts):
super().__init__()
self.inp_imgs = load_img(inp_img)
self.out_imgs = []
self.error = []
self.shape = self.inp_imgs.shape
self.nluts = nluts
for fout in list_out_img:
lut = load_img(fout)
assert self.inp_imgs.shape == lut.shape
assert (self.inp_imgs.max() <= 1) and (lut.max() <= 1)
self.out_imgs.append(lut)
self.error.append(np_psnr(self.inp_imgs,lut))
del lut
self.references = len(list_out_img)
def __len__(self):
return self.references
def __getitem__(self, idx):
if idx > self.references: raise IndexError
style_vector = np.zeros(self.nluts).astype(np.float32)
if idx < self.nluts:
style_vector[idx] = 1.
else:
style_vector = np.array([0.33, 0.33, 0.33]).astype(np.float32)
# Convert images to pytorch tensors
img = torch.from_numpy(self.inp_imgs)
lut = torch.from_numpy(self.out_imgs[idx])
img = img.reshape((img.shape[0]*img.shape[1],3)) # [hw, 3]
lut = lut.reshape((lut.shape[0]*lut.shape[1],3)) # [hw, 3]
style_vector = torch.from_numpy(style_vector)
style_vector_re = style_vector.repeat(img.shape[0]).view(img.shape[0],self.nluts)
img = torch.cat([img,style_vector_re], dim=-1)
return img, lut, style_vector