File size: 11,477 Bytes
cc6c676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c20dc8
cc6c676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torch.utils.data import Dataset
import torch
from configparser import ConfigParser
import matplotlib.pyplot as plt
import os
import torch as th
from PIL import Image
import numpy as np
import random
from PIL import ImageMath
import random

def dataloader(dataset, input_size, batch_size,dim,split='train', trans=False):
    #transform = transforms.Compose([transforms.Resize((input_size, input_size)), transforms.ToTensor(),
    #                                transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
    if dataset == 'mnist':
        data_loader = DataLoader(
            datasets.MNIST('data/mnist', train=True, download=True, transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == 'fashion-mnist':
        data_loader = DataLoader(
            datasets.FashionMNIST('data/fashion-mnist', train=True, download=True, transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == 'cifar10':
        data_loader = DataLoader(
            datasets.CIFAR10('data/cifar10', train=True, download=True, transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == 'svhn':
        data_loader = DataLoader(
            datasets.SVHN('data/svhn', split=split, download=True, transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == 'stl10':
        data_loader = DataLoader(
            datasets.STL10('data/stl10', split=split, download=True, transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == 'lsun-bed':
        data_loader = DataLoader(
            datasets.LSUN('data/lsun', classes=['bedroom_train'], transform=transform),
            batch_size=batch_size, shuffle=True)
    elif dataset == '4cam':
        if split == 'score':
            cams = ScoreDataset(root_dir=os.getcwd() + '/Images/Score-Test', dim=dim, name=split, cant_images=300) #hardcode is bad but quick
            return DataLoader(cams, batch_size=batch_size, shuffle=False, num_workers=0)
        if split != 'test':
            cams = ImagesDataset(root_dir=os.getcwd() + '/Images/ActualDataset', dim=dim, name=split, transform=trans)
            return DataLoader(cams, batch_size=batch_size, shuffle=True, num_workers=0)
        else:
            cams = TestingDataset(root_dir=os.getcwd() + '/Images/Input-Test', dim=dim, name=split)
            return DataLoader(cams, batch_size=batch_size, shuffle=False, num_workers=0)

    return data_loader


class ImagesDataset(Dataset):
    """My dataset."""

    def __init__(self, root_dir, dim, name, transform):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.root_dir = root_dir
        self.nCameras = 2
        self.imageDim = dim
        self.name = name
        self.parser = ConfigParser()
        self.parser.read('config.ini')
        self.transform = transform

    def __len__(self):

        return self.parser.getint(self.name, 'total')
        #oneCameRoot = self.root_dir + '\CAM1'
        #return int(len([name for name in os.listdir(oneCameRoot) if os.path.isfile(os.path.join(oneCameRoot, name))])/2) #por el depth


    def __getitem__(self, idx):
        if th.is_tensor(idx):
            idx = idx.tolist()
        idx = self.parser.get(self.name, str(idx))
        if self.transform:
            brighness = random.uniform(0.7, 1.2)
            saturation = random.uniform(0, 2)
            contrast = random.uniform(0.4, 2)
            gamma = random.uniform(0.7, 1.3)
            hue = random.uniform(-0.3, 0.3)  # 0.01

        oneCameRoot = self.root_dir + '/CAM0'

        # foto normal
        img_name = os.path.join(oneCameRoot, "n_" + idx + ".png")
        img = Image.open(img_name).convert('RGB')  # .convert('L')
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        if self.transform:
            img = transforms.functional.adjust_gamma(img, gamma)
            img = transforms.functional.adjust_brightness(img, brighness)
            img = transforms.functional.adjust_contrast(img, contrast)
            img = transforms.functional.adjust_saturation(img, saturation)
            img = transforms.functional.adjust_hue(img, hue)
        x1 = transforms.ToTensor()(img)
        x1 = (x1 * 2) - 1

        # foto produndidad
        img_name = os.path.join(oneCameRoot, "d_" + idx + ".png")
        img = Image.open(img_name).convert('I')
        img = convert_I_to_L(img)
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x1_dep = transforms.ToTensor()(img)
        x1_dep = (x1_dep * 2) - 1

        oneCameRoot = self.root_dir + '/CAM1'

        # foto normal
        img_name = os.path.join(oneCameRoot, "n_" + idx + ".png")
        img = Image.open(img_name).convert('RGB')  # .convert('L')
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        if self.transform:
            img = transforms.functional.adjust_gamma(img, gamma)
            img = transforms.functional.adjust_brightness(img, brighness)
            img = transforms.functional.adjust_contrast(img, contrast)
            img = transforms.functional.adjust_saturation(img, saturation)
            img = transforms.functional.adjust_hue(img, hue)
        x2 = transforms.ToTensor()(img)
        x2 = (x2 * 2) - 1

        # foto produndidad
        img_name = os.path.join(oneCameRoot, "d_" + idx + ".png")
        img = Image.open(img_name).convert('I')
        img = convert_I_to_L(img)
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x2_dep = transforms.ToTensor()(img)
        x2_dep = (x2_dep * 2) - 1


        #random izq o derecha
        if (bool(random.getrandbits(1))):
            sample = {'x_im': x1, 'x_dep': x1_dep, 'y_im': x2, 'y_dep': x2_dep, 'y_': torch.ones(1, self.imageDim, self.imageDim)}
        else:
            sample = {'x_im': x2, 'x_dep': x2_dep, 'y_im': x1, 'y_dep': x1_dep, 'y_': torch.zeros(1, self.imageDim, self.imageDim)}

        return sample

    def __iter__(self):

        for i in range(this.__len__()):
            list.append(this.__getitem__(i))
        return iter(list)

class TestingDataset(Dataset):
    """My dataset."""

    def __init__(self, root_dir, dim, name):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.root_dir = root_dir
        self.imageDim = dim
        self.name = name
        files = os.listdir(self.root_dir)
        self.files = [ele for ele in files if not ele.endswith('_d.png')]

    def __len__(self):

        #return self.parser.getint(self.name, 'total')
        #oneCameRoot = self.root_dir + '\CAM1'
        #return int(len([name for name in os.listdir(self.root_dir) if os.path.isfile(os.path.join(self.root_dir, name))])/2) #por el depth
        return len(self.files)


    def __getitem__(self, idx):
        if th.is_tensor(idx):
            idx = idx.tolist()

        # foto normal
        img_name = os.path.join(self.root_dir, self.files[idx])
        img = Image.open(img_name).convert('RGB')  # .convert('L')
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x1 = transforms.ToTensor()(img)
        x1 = (x1 * 2) - 1


        # foto produndidad
        img_name = os.path.join(self.root_dir , self.files[idx][:-4] + "_d.png")
        img = Image.open(img_name)
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x1_dep = transforms.ToTensor()(img)
        x1_dep = (x1_dep * 2) - 1

        sample = {'x_im': x1, 'x_dep': x1_dep}

        return sample

    def __iter__(self):

        for i in range(this.__len__()):
            list.append(this.__getitem__(i))
        return iter(list)


def show_image(t_data, grey=False):

    #from numpy
    t_data2 = t_data.transpose(1, 2, 0)
    t_data2 = t_data2 * 255.0
    t_data2 = t_data2.astype(np.uint8)
    if (not grey):
        outIm = Image.fromarray(t_data2, mode='RGB')
    else:
        t_data2 = np.squeeze(t_data2, axis=2)
        outIm = Image.fromarray(t_data2, mode='L')
    outIm.show()

def convert_I_to_L(img):
    array = np.uint8(np.array(img) / 256) #el numero esta bien, sino genera espacios en negro en la imagen
    return Image.fromarray(array)

class ScoreDataset(Dataset):
    """My dataset."""

    def __init__(self, root_dir, dim, name, cant_images):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.root_dir = root_dir
        self.nCameras = 2
        self.imageDim = dim
        self.name = name
        self.size = cant_images

    def __len__(self):

        return self.size 


    def __getitem__(self, idx):

        oneCameRoot = self.root_dir + '/CAM0'

        idx = "{:04d}".format(idx)
        # foto normal
        img_name = os.path.join(oneCameRoot, "n_" + idx + ".png")
        img = Image.open(img_name).convert('RGB')  # .convert('L')
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x1 = transforms.ToTensor()(img)
        x1 = (x1 * 2) - 1

        # foto produndidad
        img_name = os.path.join(oneCameRoot, "d_" + idx + ".png")
        img = Image.open(img_name).convert('I')
        img = convert_I_to_L(img)
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x1_dep = transforms.ToTensor()(img)
        x1_dep = (x1_dep * 2) - 1

        oneCameRoot = self.root_dir + '/CAM1'

        # foto normal
        img_name = os.path.join(oneCameRoot, "n_" + idx + ".png")
        img = Image.open(img_name).convert('RGB')  # .convert('L')
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x2 = transforms.ToTensor()(img)
        x2 = (x2 * 2) - 1

        # foto produndidad
        img_name = os.path.join(oneCameRoot, "d_" + idx + ".png")
        img = Image.open(img_name).convert('I')
        img = convert_I_to_L(img)
        if (img.size[0] != self.imageDim or img.size[1] != self.imageDim):
            img = img.resize((self.imageDim, self.imageDim))
        x2_dep = transforms.ToTensor()(img)
        x2_dep = (x2_dep * 2) - 1


        sample = {'x_im': x1, 'x_dep': x1_dep, 'y_im': x2, 'y_dep': x2_dep, 'y_': torch.ones(1, self.imageDim, self.imageDim)}
        return sample

    def __iter__(self):

        for i in range(self.__len__()):
            list.append(self.__getitem__(i))
        return iter(list)