-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataloader.py
156 lines (112 loc) · 4.41 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import torch
import torch.utils.data as data
import glob
import random
import h5py
import os
import numpy as np
from PIL import Image
from natsort import natsorted
class Dataset_test(data.Dataset):
def __init__(self, src_path, sigma, transform=None):
self.src_path = natsorted(glob.glob(os.path.join(src_path, '*.png')))
self.src_path_noise = natsorted(glob.glob(os.path.join(src_path, f'noisy_sig{sigma}', '*.png')))
self.transform = transform
def __getitem__(self, index):
clean = Image.open(self.src_path[index])
noisy = Image.open(self.src_path_noise[index])
clean = self.transform(clean)
noisy = self.transform(noisy)
return (clean, noisy)
def __len__(self):
return len(self.src_path)
class real_Dataset_test(data.Dataset):
def __init__(self, src_path, transform=None):
self.src_path = natsorted(glob.glob(os.path.join(src_path, 'GT','*.png')))
self.src_path_noise = natsorted(glob.glob(os.path.join(src_path, 'Noisy', '*.png')))
self.transform = transform
def __getitem__(self, index):
clean = Image.open(self.src_path[index])
noisy = Image.open(self.src_path_noise[index])
clean = self.transform(clean)
noisy = self.transform(noisy)
return (clean, noisy)
def __len__(self):
return len(self.src_path)
class Dataset_from_h5(data.Dataset):
def __init__(self, src_path_N, src_path_C, sigma, transform=None):
self.path_N = src_path_N
self.path_C = src_path_C
# load generator dataset
h5f_N = h5py.File(self.path_N, 'r')
self.keys_G = list(h5f_N.keys())
random.shuffle(self.keys_G)
h5f_N.close()
####################################
self.sigma = sigma
# load clean dataset
h5f_C = h5py.File(self.path_C, 'r')
self.keys_D = list(h5f_C.keys())
random.shuffle(self.keys_D)
h5f_C.close()
####################################
self.transform = transform
def __getitem__(self, index):
h5f_N = h5py.File(self.path_N, 'r')
key_N = self.keys_G[index]
data = np.array(h5f_N[key_N]).reshape(h5f_N[key_N].shape)
gt = Image.fromarray(np.uint8(data*255))
h5f_N.close()
if self.transform:
gt = self.transform(gt)
noise = torch.normal(torch.zeros(gt.size()), self.sigma/255.0)
noisy = gt + noise
noisy = torch.clamp(noisy, 0.0, 1.0)
# for clean data
h5f_C = h5py.File(self.path_C, 'r')
key_C = self.keys_D[index]
data = np.array(h5f_C[key_C]).reshape(h5f_C[key_C].shape)
clean_input = Image.fromarray(np.uint8(data*255))
h5f_C.close()
if self.transform:
clean_input = self.transform(clean_input)
return (noisy, clean_input)
def __len__(self):
return min(len(self.keys_G), len(self.keys_D))
class realDataset_from_h5(data.Dataset):
def __init__(self, src_path_N, src_path_C, transform=None):
self.path_N = src_path_N
self.path_C = src_path_C
# load generator dataset
h5f_N = h5py.File(self.path_N, 'r')
self.keys_G = list(h5f_N.keys())
random.shuffle(self.keys_G)
h5f_N.close()
####################################
# load clean dataset
h5f_C = h5py.File(self.path_C, 'r')
self.keys_D = list(h5f_C.keys())
random.shuffle(self.keys_D)
h5f_C.close()
####################################
self.transform = transform
def __getitem__(self, index):
h5f_N = h5py.File(self.path_N, 'r')
key_N = self.keys_G[index]
data = np.array(h5f_N[key_N]).reshape(h5f_N[key_N].shape)
noisy = Image.fromarray(np.uint8(data*255))
h5f_N.close()
if self.transform:
noisy = self.transform(noisy)
noisy = torch.clamp(noisy, 0.0, 1.0)
# for clean data
h5f_C = h5py.File(self.path_C, 'r')
key_C = self.keys_D[index]
data = np.array(h5f_C[key_C]).reshape(h5f_C[key_C].shape)
clean_input = Image.fromarray(np.uint8(data*255))
h5f_C.close()
if self.transform:
clean_input = self.transform(clean_input)
return (noisy, clean_input)
def __len__(self):
return min(len(self.keys_G), len(self.keys_D))