File size: 3,722 Bytes
52758c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Copyright (c) 2022 Samsung Electronics Co., Ltd.

Author:
Abhijith Punnappurath (abhijith.p@samsung.com)

Licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License, (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://creativecommons.org/licenses/by-nc/4.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
For conditions of distribution and use, see the accompanying LICENSE.md file.

"""

# no need to run this code separately

import os
import torch
import torchvision
from torchvision import transforms
import utils.data_generator as dg

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


class DatasetRAW(object):
    def __init__(self, root, batch_size, patch_size, stride, wb_illum, on_cuda, which_input):

        print('Inside raw data generator')
        rawimgs = dg.datagenerator_raw(data_dir=os.path.join(root, which_input),
                                                    meta_dir=os.path.join(root, 'metadata_raw'),
                                                    wb_illum=wb_illum,
                                                    batch_size=batch_size, patch_size=patch_size, stride=stride)
        rawimgs = torch.from_numpy(rawimgs)
        rawimgs = rawimgs.permute(0, 3, 1, 2)

        rawimgs = rawimgs ** (1 / 2.2)
        if on_cuda:
            rawimgs = rawimgs.to(device)

        print('Number of patches '+str(rawimgs.shape[0]))
        print('Outside raw data generator \n')
        self.rawimgs = rawimgs

        print('Inside sRGB data generator')
        srgbimgs = dg.datagenerator_sRGB(data_dir=os.path.join(root, 'clean'), batch_size=batch_size, patch_size=patch_size,
                                    stride=stride)
        print('Number of patches '+str(srgbimgs.shape[0]))
        print('Outside sRGB data generator \n')
        srgbimgs = torch.from_numpy(srgbimgs)
        srgbimgs = srgbimgs.permute(0, 3, 1, 2)
        srgbimgs = srgbimgs / 255.0
        if on_cuda:
            srgbimgs = srgbimgs.to(device)
        self.srgbimgs = srgbimgs

    def __getitem__(self, idx):
        # load images

        img = self.rawimgs[idx]
        target = self.srgbimgs[idx]

        return img, target

    def __len__(self):
        return len(self.rawimgs)


if __name__ == '__main__':
    data_dir = 'synth_night'
    batch_size, patch_size, stride = 4, 480, 480
    wb_illum = 'avg'
    on_cuda = False
    which_input = 'noisy_raw'

    image_datasets = {
        x: DatasetRAW(os.path.join(data_dir, x), batch_size, patch_size, stride, wb_illum,
                      on_cuda, which_input)
        for x in ['val']}

    dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size,
                                                  shuffle=True, num_workers=0)
                   for x in ['val']}

    dataset_sizes = {x: len(image_datasets[x]) for x in ['val']}

    # Get a batch of training data
    inputs, targets = next(iter(dataloaders['val']))

    # Make a grid from batch
    targets_grid = torchvision.utils.make_grid(targets)
    inputs_grid = torchvision.utils.make_grid(inputs)

    targets_grid = transforms.ToPILImage()(targets_grid.cpu())
    inputs_grid = transforms.ToPILImage()(inputs_grid.cpu())

    targets_grid.save("debug_targets_grid.png")
    inputs_grid.save("debug_inputs_grid.png")

    print('Done!')