-
Notifications
You must be signed in to change notification settings - Fork 2
/
makedataset.py
254 lines (215 loc) · 7.03 KB
/
makedataset.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 12 20:00:46 2020
@author: Administrator
"""
import os
import os.path
import random
import numpy as np
import cv2
import h5py
import torch
import torch.utils.data as udata
class Dataset(udata.Dataset):
r"""Implements torch.utils.data.Dataset
"""
def __init__(self, trainrgb=True,trainsyn = True, shuffle=False):
super(Dataset, self).__init__()
self.trainrgb = trainrgb
self.trainsyn = trainsyn
self.train_haze = 'train_ImageEdge.h5'
if self.trainrgb:
if self.trainsyn:
h5f = h5py.File(self.train_haze, 'r')
else:
h5f = h5py.File(self.train_real_rgb, 'r')
else:
if self.trainsyn:
h5f = h5py.File(self.train_syn_gray, 'r')
else:
h5f = h5py.File(self.train_real_gray, 'r')
self.keys = list(h5f.keys())
if shuffle:
random.shuffle(self.keys)
h5f.close()
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
if self.trainrgb:
if self.trainsyn:
h5f = h5py.File(self.train_haze, 'r')
else:
h5f = h5py.File(self.train_real_rgb, 'r')
else:
if self.trainsyn:
h5f = h5py.File(self.train_syn_gray, 'r')
else:
h5f = h5py.File(self.train_real_gray, 'r')
key = self.keys[index]
data = np.array(h5f[key])
h5f.close()
return torch.Tensor(data)
def data_augmentation(image, mode):
r"""Performs dat augmentation of the input image
Args:
image: a cv2 (OpenCV) image
mode: int. Choice of transformation to apply to the image
0 - no transformation
1 - flip up and down
2 - rotate counterwise 90 degree
3 - rotate 90 degree and flip up and down
4 - rotate 180 degree
5 - rotate 180 degree and flip
6 - rotate 270 degree
7 - rotate 270 degree and flip
"""
out = np.transpose(image, (1, 2, 0))
if mode == 0:
# original
out = out
elif mode == 1:
# flip up and down
out = np.flipud(out)
elif mode == 2:
# rotate counterwise 90 degree
out = np.rot90(out)
elif mode == 3:
# rotate 90 degree and flip up and down
out = np.rot90(out)
out = np.flipud(out)
elif mode == 4:
# rotate 180 degree
out = np.rot90(out, k=2)
elif mode == 5:
# rotate 180 degree and flip
out = np.rot90(out, k=2)
out = np.flipud(out)
elif mode == 6:
# rotate 270 degree
out = np.rot90(out, k=3)
elif mode == 7:
# rotate 270 degree and flip
out = np.rot90(out, k=3)
out = np.flipud(out)
else:
raise Exception('Invalid choice of image transformation')
return np.transpose(out, (2, 0, 1))
def img_to_patches(img,win,stride,Syn=True):
chl,raw,col = img.shape
chl = int(chl)
num_raw = np.ceil((raw-win)/stride+1).astype(np.uint8)
num_col = np.ceil((col-win)/stride+1).astype(np.uint8)
count = 0
total_process = int(num_col)*int(num_raw)
img_patches = np.zeros([chl,win,win,total_process])
if Syn:
for i in range(num_raw):
for j in range(num_col):
if stride * i + win <= raw and stride * j + win <=col:
img_patches[:,:,:,count] = img[:,stride*i : stride*i + win, stride*j : stride*j + win]
elif stride * i + win > raw and stride * j + win<=col:
img_patches[:,:,:,count] = img[:,raw-win : raw,stride * j : stride * j + win]
elif stride * i + win <= raw and stride*j + win>col:
img_patches[:,:,:,count] = img[:,stride*i : stride*i + win, col-win : col]
else:
img_patches[:,:,:,count] = img[:,raw-win : raw,col-win : col]
count +=1
return img_patches
def readfiles(filepath):
'''Get dataset images names'''
files = os.listdir(filepath)
return files
def normalize(data):
return np.float32(data/255.0)
def samesize(img,size):
img = cv2.resize(img,size)
return img
def concatenate2imgs(img,depth):
c,w,h = img.shape
conimg = np.zeros((c+c,w,h))
conimg[0:c,:,:] = img
conimg[c:2*c,:,:] = depth
return conimg
def Edge_TrainSynRGB(img_filepath, depth_filepath, patch_size, stride):
'''synthetic ImageEdge images'''
train_haze = 'train_ImageEdge.h5'
img_files = readfiles(img_filepath)
count = 0
scales = [1.0]#[0.6,0.8,1.0]
with h5py.File(train_haze, 'w') as h5f:
for i in range(len(img_files)):
filename = img_files[i][:-4]
print(filename)
oimg = cv2.imread(img_filepath + '/' + filename + '.jpg')
#odepth = cv2.imread(depth_filepath + '/' + filename.replace('High', 'Low') + '.jpg')
odepth = cv2.imread(depth_filepath + '/' + filename + '.jpg')
# oimg = cv2.imread(img_filepath + '/' + img_filepath[i])
# odepth = cv2.imread(depth_filepath + '/' + depth_filepath[i])
for sca in scales:
#img = cv2.resize(oimg, (0, 0), fx=sca, fy=sca, interpolation=cv2.INTER_CUBIC)
#depth = cv2.resize(odepth, (0, 0), fx=sca, fy=sca, interpolation=cv2.INTER_CUBIC)
img = oimg.transpose(2, 0, 1)
depth = odepth.transpose(2, 0, 1)
#depth = depth.transpose((1,0))
print(img.shape,depth.shape)
img = normalize(img)
depth = normalize(depth)
img_depth = concatenate2imgs(img,depth)
img_patches = img_to_patches(img_depth, win=patch_size, stride=stride)
print("\tfile: %s scale %.1f # samples: %d" %(img_files[i], sca,img_patches.shape[3]))
for nx in range(img_patches.shape[3]):
data = data_augmentation(img_patches[:, :, :, nx].copy(), np.random.randint(0, 7))
h5f.create_dataset(str(count), data=data)
count += 1
i += 1
print(data.shape)
h5f.close()
def TrainSynRGB(img_filepath, patch_size, stride):
'''synthetic Haze images'''
train_haze = 'train_haze.h5'
img_files = readfiles(img_filepath)
count = 0
scales = [0.6,0.8,1.0]
with h5py.File(train_haze, 'w') as h5f:
for i in range(len(img_files)):
filename = img_files[i]
o_img = cv2.imread(img_filepath + '/' + filename)
o_img = cv2.resize(o_img,(360,360))
#img= samesize(img,(360,360))
for sca in scales:
img = o_img
img = cv2.resize(o_img, (0, 0), fx=sca, fy=sca, interpolation=cv2.INTER_CUBIC)
img = img.transpose(2, 0, 1)
img = normalize(img)
img_patches = img_to_patches(img, win=patch_size, stride=stride)
print("\tfile: %s scale %.1f # samples: %d" %(img_files[i], sca,img_patches.shape[3]))
for nx in range(img_patches.shape[3]):
data = data_augmentation(img_patches[:, :, :, nx].copy(), np.random.randint(0, 7))
h5f.create_dataset(str(count), data=data)
count += 1
i += 1
print(data.shape)
h5f.close()
def TrainSynRGB_NA(img_filepath, patch_size, stride):
'''synthetic Haze images'''
train_haze = 'train_haze.h5'
img_files = readfiles(img_filepath)
count = 0
scales = [1.0]#[0.6,0.8,1.0]
with h5py.File(train_haze, 'w') as h5f:
for i in range(len(img_files)):
filename = img_files[i]
oooimg = cv2.imread(img_filepath + '/' + filename)
img = cv2.resize(oooimg,(256,256))
for sca in scales:
img = cv2.resize(img, (0, 0), fx=sca, fy=sca, interpolation=cv2.INTER_CUBIC)
img = img.transpose(2, 0, 1)
img = normalize(img)
print("\tfile: %s scale %.1f" %(img_files[i], sca))
data = data_augmentation(img.copy(), np.random.randint(0, 7))
h5f.create_dataset(str(count), data=data)
count += 1
i += 1
print(data.shape)
h5f.close()