-
Notifications
You must be signed in to change notification settings - Fork 4
/
DerainDataset.py
167 lines (134 loc) · 5.92 KB
/
DerainDataset.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
161
162
163
164
165
166
167
import os
import os.path
import numpy as np
import random
import h5py
import torch
import cv2
import glob
import torch.utils.data as udata
from utils import data_augmentation
def normalize(data):
return data / 255.
def Im2Patch(img, win, stride=1):
k = 0
endc = img.shape[0]
endw = img.shape[1]
endh = img.shape[2]
patch = img[:, 0:endw - win + 0 + 1:stride, 0:endh - win + 0 + 1:stride]
TotalPatNum = patch.shape[1] * patch.shape[2]
Y = np.zeros([endc, win * win, TotalPatNum], np.float32)
for i in range(win):
for j in range(win):
patch = img[:, i:endw - win + i + 1:stride, j:endh - win + j + 1:stride]
Y[:, k, :] = np.array(patch[:]).reshape(endc, TotalPatNum)
k = k + 1
return Y.reshape([endc, win, win, TotalPatNum])
def prepare_data_Rain100H(data_path, patch_size, stride, aug_times=1):
# train
print('process training data')
input_path = os.path.join(data_path)
target_path = os.path.join(data_path)
save_target_path = os.path.join(data_path,'train_target.h5')
save_input_path = os.path.join(data_path,'train_input.h5')
target_h5f = h5py.File(save_target_path, 'w')
input_h5f = h5py.File(save_input_path, 'w')
train_num = 0
for i in range(1800):
target_file = "norain-%d.png" % (i + 1)
if os.path.exists(os.path.join(target_path,target_file)):
target = cv2.imread(os.path.join(target_path,target_file))
b, g, r = cv2.split(target)
target = cv2.merge([r, g, b])
input_file = "rain-%d.png" % (i + 1)
if os.path.exists(os.path.join(input_path,input_file)):
input_img = cv2.imread(os.path.join(input_path,input_file))
b, g, r = cv2.split(input_img)
input_img = cv2.merge([r, g, b])
target_img = target
target_img = np.float32(normalize(target_img))
target_patches = Im2Patch(target_img.transpose(2,0,1), win=patch_size, stride=stride)
input_img = np.float32(normalize(input_img))
input_patches = Im2Patch(input_img.transpose(2, 0, 1), win=patch_size, stride=stride)
print("target file: %s # samples: %d" % (
input_file, target_patches.shape[3] * aug_times))
for n in range(target_patches.shape[3]):
target_data = target_patches[:, :, :, n].copy()
target_h5f.create_dataset(str(train_num), data=target_data)
input_data = input_patches[:, :, :, n].copy()
input_h5f.create_dataset(str(train_num), data=input_data)
train_num += 1
target_h5f.close()
input_h5f.close()
print('training set, # samples %d\n' % train_num)
def prepare_data_Rain100L(data_path, patch_size, stride, aug_times=1):
# train
print('process training data')
input_path = os.path.join(data_path)
target_path = os.path.join(data_path)
save_target_path = os.path.join(data_path,'train_target.h5')
save_input_path = os.path.join(data_path,'train_input.h5')
target_h5f = h5py.File(save_target_path, 'w')
input_h5f = h5py.File(save_input_path, 'w')
train_num = 0
for i in range(200):
target_file = "norain-%d.png" % (i + 1)
target = cv2.imread(os.path.join(target_path,target_file))
b, g, r = cv2.split(target)
target = cv2.merge([r, g, b])
for j in range(2):
input_file = "rain-%d.png" % (i+1)
input_img = cv2.imread(os.path.join(input_path,input_file))
b, g, r = cv2.split(input_img)
input_img = cv2.merge([r, g, b])
target_img = target
if j == 1:
target_img = cv2.flip(target_img,1)
input_img = cv2.flip(input_img,1)
target_img = np.float32(normalize(target_img))
target_patches = Im2Patch(target_img.transpose(2,0,1), win=patch_size, stride=stride)
input_img = np.float32(normalize(input_img))
input_patches = Im2Patch(input_img.transpose(2, 0, 1), win=patch_size, stride=stride)
print("target file: %s # samples: %d" % (
input_file, target_patches.shape[3] * aug_times))
for n in range(target_patches.shape[3]):
target_data = target_patches[:, :, :, n].copy()
target_h5f.create_dataset(str(train_num), data=target_data)
input_data = input_patches[:, :, :, n].copy()
input_h5f.create_dataset(str(train_num), data=input_data)
train_num += 1
target_h5f.close()
input_h5f.close()
print('training set, # samples %d\n' % train_num)
class Dataset(udata.Dataset):
def __init__(self, train=True, data_path='.'):
super(Dataset, self).__init__()
self.train = train
self.data_path = data_path
if self.train:
target_path = os.path.join(self.data_path,'train_target.h5')
input_path = os.path.join(self.data_path,'train_input.h5')
target_h5f = h5py.File(target_path, 'r')
input_h5f = h5py.File(input_path,'r')
else:
h5f = h5py.File('val.h5', 'r')
self.keys = list(target_h5f.keys())
random.shuffle(self.keys)
target_h5f.close()
input_h5f.close()
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
if self.train:
target_path = os.path.join(self.data_path, 'train_target.h5')
input_path = os.path.join(self.data_path, 'train_input.h5')
target_h5f = h5py.File(target_path, 'r')
input_h5f = h5py.File(input_path, 'r')
else:
h5f = h5py.File('val.h5', 'r')
key = self.keys[index]
target = np.array(target_h5f[key])
input = np.array(input_h5f[key])
target_h5f.close()
input_h5f.close()
return torch.Tensor(input), torch.Tensor(target)