-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimage_folder.py
330 lines (271 loc) · 11.8 KB
/
image_folder.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""A modified image folder class
We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py)
so that this class can load images from both current directory and its subdirectories.
"""
import torch.utils.data as data
from PIL import Image
import os
IMG_EXTENSIONS = [
'.jpg', '.JPG', '.jpeg', '.JPEG',
'.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
'.tif', '.TIF', '.tiff', '.TIFF',
]
def is_image_file(filename):
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
def make_dataset(dir, max_dataset_size=float("inf")):
images = []
# assert os.path.isdir(dir), '%s is not a valid directory' % dir
if 'rain1400' in dir:
for root, _, fnames in sorted(os.walk(dir)):
if 'ground_truth' in root:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
for i in range(14):
images.append(path)
else:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
elif 'RESIDE' in dir:
for root, _, fnames in sorted(os.walk(dir)):
if 'clear' in root:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
for i in range(35):
images.append(path)
else:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
elif len(dir)==4:
for di in dir:
for root, _, fnames in sorted(os.walk(di)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
else:
for root, _, fnames in sorted(os.walk(dir)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_all(dirA, dirB, dirC, dirD, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirA), '%s is not a valid directory' % dirA
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirC), '%s is not a valid directory' % dirC
assert os.path.isdir(dirD), '%s is not a valid directory' % dirD
for root, _, fnames in sorted(os.walk(dirA)):
if 'ground_truth' in root:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
for i in range(14):
images.append(path)
else:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirC)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirD)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_3(dirB, dirC, dirD, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirC), '%s is not a valid directory' % dirC
assert os.path.isdir(dirD), '%s is not a valid directory' % dirD
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirC)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirD)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_2(dirB, dirC, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirC), '%s is not a valid directory' % dirC
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirC)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_5(dirB, dirC, dirD, dirE, dirF, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirC), '%s is not a valid directory' % dirC
assert os.path.isdir(dirD), '%s is not a valid directory' % dirD
assert os.path.isdir(dirE), '%s is not a valid directory' % dirE
assert os.path.isdir(dirF), '%s is not a valid directory' % dirF
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirC)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirD)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirE)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirF)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_6(dirB, dirD, dirE, dirF, dirA, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirD), '%s is not a valid directory' % dirD
assert os.path.isdir(dirE), '%s is not a valid directory' % dirE
assert os.path.isdir(dirF), '%s is not a valid directory' % dirF
assert os.path.isdir(dirA), '%s is not a valid directory' % dirA
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirD)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirE)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirF)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirA)):
if 'clear' in root:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
for i in range(35):
images.append(path)
else:
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_4(dirB, dirC, dirD, dirE, max_dataset_size=float("inf")):
images = []
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
assert os.path.isdir(dirC), '%s is not a valid directory' % dirC
assert os.path.isdir(dirD), '%s is not a valid directory' % dirD
assert os.path.isdir(dirE), '%s is not a valid directory' % dirE
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirC)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirD)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
for root, _, fnames in sorted(os.walk(dirE)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(root, fname)
images.append(path)
return images[:min(max_dataset_size, len(images))]
def make_dataset_all_text(dirA, dirB, max_dataset_size=float("inf")):
text = []
negative_text = []
assert os.path.isdir(dirA), '%s is not a valid directory' % dirA
assert os.path.isdir(dirB), '%s is not a valid directory' % dirB
for root, _, fnames in sorted(os.walk(dirA)):
for fname in fnames:
if is_image_file(fname):
for i in range(14):
# text.append('rAn image without rain')
text.append('rRemove the rain in the image.')
negative_text.append('rrain')
for root, _, fnames in sorted(os.walk(dirB)):
for fname in fnames:
if is_image_file(fname):
# text.append('lAn image with more light')
text.append('lStrengthen the light in the image.')
negative_text.append('l')
return text[:min(max_dataset_size, len(text))], negative_text[:min(max_dataset_size, len(negative_text))]
def default_loader(path):
return Image.open(path).convert('RGB')
class ImageFolder(data.Dataset):
def __init__(self, root, transform=None, return_paths=False,
loader=default_loader):
imgs = make_dataset(root)
if len(imgs) == 0:
raise(RuntimeError("Found 0 images in: " + root + "\n"
"Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))
self.root = root
self.imgs = imgs
self.transform = transform
self.return_paths = return_paths
self.loader = loader
def __getitem__(self, index):
path = self.imgs[index]
img = self.loader(path)
if self.transform is not None:
img = self.transform(img)
if self.return_paths:
return img, path
else:
return img
def __len__(self):
return len(self.imgs)