-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset_Rain200H.py
160 lines (129 loc) · 4.8 KB
/
dataset_Rain200H.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
157
158
159
160
import os
import cv2
import numpy as np
from numpy.random import RandomState
from torch.utils.data import Dataset
import settings_Rain200H
class TrainValDataset(Dataset):
def __init__(self, name):
super().__init__()
self.rand_state = RandomState(66)
self.root_dir = os.path.join(settings_Rain200H.data_dir, name)
self.mat_files = os.listdir(self.root_dir)
self.patch_size = settings_Rain200H.patch_size
self.file_num = len(self.mat_files)
def __len__(self):
return self.file_num * 100
def __getitem__(self, idx):
file_name = self.mat_files[idx % self.file_num]
img_file = os.path.join(self.root_dir, file_name)
img_pair = cv2.imread(img_file).astype(np.float32) / 255
if settings_Rain200H.aug_data:
O, B = self.crop(img_pair, aug=True)
O, B = self.flip(O, B)
O, B = self.rotate(O, B)
else:
O, B = self.crop(img_pair, aug=False)
O = np.transpose(O, (2, 0, 1))
B = np.transpose(B, (2, 0, 1))
sample = {'O': O, 'B': B}
return sample
def crop(self, img_pair, aug):
patch_size = self.patch_size
h, ww, c = img_pair.shape
w = int(ww / 2)
if aug:
mini = - 1 / 4 * self.patch_size
maxi = 1 / 4 * self.patch_size + 1
p_h = patch_size + self.rand_state.randint(mini, maxi)
p_w = patch_size + self.rand_state.randint(mini, maxi)
else:
p_h, p_w = patch_size, patch_size
r = self.rand_state.randint(0, h - p_h)
c = self.rand_state.randint(0, w - p_w)
O = img_pair[r: r+p_h, c+w: c+p_w+w]
B = img_pair[r: r+p_h, c: c+p_w]
if aug:
O = cv2.resize(O, (patch_size, patch_size))
B = cv2.resize(B, (patch_size, patch_size))
return O, B
def flip(self, O, B):
if self.rand_state.rand() > 0.5:
O = np.flip(O, axis=1)
B = np.flip(B, axis=1)
return O, B
def rotate(self, O, B):
angle = self.rand_state.randint(-30, 30)
patch_size = self.patch_size
center = (int(patch_size / 2), int(patch_size / 2))
M = cv2.getRotationMatrix2D(center, angle, 1)
O = cv2.warpAffine(O, M, (patch_size, patch_size))
B = cv2.warpAffine(B, M, (patch_size, patch_size))
return O, B
class TestDataset(Dataset):
def __init__(self, name):
super().__init__()
self.rand_state = RandomState(66)
self.root_dir = os.path.join(settings_Rain200H.data_dir, name)
self.mat_files = os.listdir(self.root_dir)
self.patch_size = settings_Rain200H.patch_size
self.file_num = len(self.mat_files)
def __len__(self):
return self.file_num
def __getitem__(self, idx):
file_name = self.mat_files[idx % self.file_num]
img_file = os.path.join(self.root_dir, file_name)
img_pair = cv2.imread(img_file).astype(np.float32) / 255
h, ww, c = img_pair.shape
w = ww // 2
O = np.transpose(img_pair[:, w:], (2, 0, 1))
B = np.transpose(img_pair[:, :w], (2, 0, 1))
sample = {'O': O, 'B': B}
return sample
class ShowDataset(Dataset):
def __init__(self, name):
super().__init__()
self.rand_state = RandomState(66)
self.root_dir = os.path.join(settings_Rain200H.data_dir, name)
self.img_files = sorted(os.listdir(self.root_dir))
self.file_num = len(self.img_files)
def __len__(self):
return self.file_num
def __getitem__(self, idx):
file_name = self.img_files[idx % self.file_num]
img_file = os.path.join(self.root_dir, file_name)
print(img_file)
img_pair = cv2.imread(img_file).astype(np.float32) / 255
h, ww, c = img_pair.shape
w = int(ww / 2)
#h_8 = h % 8
#w_8 = w % 8
if settings_Rain200H.pic_is_pair:
O = np.transpose(img_pair[:, w:], (2, 0, 1))
B = np.transpose(img_pair[:, :w], (2, 0, 1))
else:
O = np.transpose(img_pair[:, :], (2, 0, 1))
B = np.transpose(img_pair[:, :], (2, 0, 1))
sample = {'O': O, 'B': B,'file_name':file_name[:-4]}
return sample
if __name__ == '__main__':
dt = TrainValDataset('val')
print('TrainValDataset')
for i in range(10):
smp = dt[i]
for k, v in smp.items():
print(k, v.shape, v.dtype, v.mean())
print()
dt = TestDataset('test')
print('TestDataset')
for i in range(10):
smp = dt[i]
for k, v in smp.items():
print(k, v.shape, v.dtype, v.mean())
print()
print('ShowDataset')
dt = ShowDataset('test')
for i in range(10):
smp = dt[i]
for k, v in smp.items():
print(k, v.shape, v.dtype, v.mean())