-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
381 lines (298 loc) · 13.9 KB
/
dataset.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import os
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import exifread
from PIL import Image
import rawpy
# import imageio
import numpy as np
import h5py
import cv2
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import pickle
# import tkinter as tk
# import libraw
class ImageFolder(Dataset):
def __init__(self, root_dir):
self.root_dir = root_dir
self.eps = 1e-7
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
self.files = os.listdir(root_dir)
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
name = self.files[idx]
image_path = os.path.join(self.root_dir, name)
if image_path.split(".")[-1] in ["jpg", "JPG"]:
with Image.open(image_path) as f:
img = np.array(f)
else:
with rawpy.imread(image_path) as raw:
try:
img = raw.postprocess()
except:
raise Exception("Couldn't handle this file type{}".format(image_path))
h, w, _ = img.shape
img_raw = img
if h<w:
img = np.transpose(img, (1,0,2))
h, w, _ = img.shape
img = img[(h-w)//2:-(h-w)//2, :]
res = cv2.resize(img, dsize=(256, 256), interpolation=cv2.INTER_CUBIC)
# reshape
res = res.astype(np.float32)
# res = np.transpose(res.astype(np.float32),(1,2,0))
# noramlize
v_min, v_max = res.min(), res.max()
new_min, new_max = 0.0, 1.0
res = (res - v_min)/(v_max - v_min + self.eps)*(new_max - new_min) + new_min
# apply transforms and augmentations
if self.transform:
res = self.transform(res)
return {'img': res, "path": image_path, "raw_img": img_raw}
class FocalLengthDataset(Dataset):
def __init__(self, root_dir, transform=None, hdf5_path=None, focal_length_path=None, force_recompute=False, mode="train", split_mode="rand",
append_new_data=False, recompute_split=False, in_memory=False, force_focal_length=None):
self.root_dir = root_dir
self.transform = transform
self.hdf5_path = hdf5_path
self.eps = 1e-7
self.in_memory = in_memory
self.force_focal_length = force_focal_length
# check existence
if not append_new_data:
with h5py.File(hdf5_path, 'r') as hf:
if "imgs" not in hf.keys() or "focal_length" not in hf.keys() or force_recompute:
doprep = True
else:
if hf["imgs"].shape[0] != hf["focal_length"].shape[0]:
doprep = True
else:
doprep = False
if doprep:
self.doprep()
recompute_split = True
else:
self.append_data()
recompute_split = True
# prepare variables
with h5py.File(hdf5_path, 'r') as hf:
self.focal_length = hf["focal_length"][:]
if in_memory:
self.imgs = hf["imgs"][:]
# organize splitting of samples
if force_recompute or recompute_split:
# samples with focal length 0
invalid_mask = np.ones_like(self.focal_length)
invalid_mask *= self.focal_length!=0
# generate split file
idx = np.arange(len(self.focal_length))
# mask invalid data
idx = idx[invalid_mask==1]
valid_focal_length = self.focal_length[invalid_mask==1]
if split_mode=="rand":
X_train_idx, X_test_idx, y_train, y_test = train_test_split(idx, valid_focal_length, test_size=0.2, random_state=1)
X_train_idx, X_val_idx, y_train, y_val = train_test_split(X_train_idx, y_train, test_size=0.25, random_state=1) # 0.25 x 0.8 = 0.2
elif split_mode=="time":
n = len(idx)
X_test_idx, X_val_idx, X_train_idx = idx[:int(n*0.1)], idx[int(n*0.1):int(n*0.2)], idx[int(n*0.3):]
y_test, y_val, y_train = idx[:int(n*0.1)], idx[int(n*0.1):int(n*0.2)], idx[int(n*0.3):]
split_dict = { "train": X_train_idx, "test": X_test_idx, "val": X_val_idx }
with open('data/split_file.pickle', 'wb') as handle:
pickle.dump(split_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
else:
with open('data/split_file.pickle', 'rb') as handle:
split_dict = pickle.load(handle)
# decise on the split
if mode=="train":
self.X_idx = split_dict["train"]
elif mode=="test":
self.X_idx = split_dict["test"]
elif mode=="val":
self.X_idx = split_dict["val"]
self.X_idx.sort()
self.y = self.focal_length[self.X_idx]
# print("init finished")
def append_data(self):
Focal_lengths = []
self.images = []
valid = False
count = 0
for path, subdirs, files in os.walk(self.root_dir):
for name in files:
image_path = os.path.join(path, name)
if image_path.__contains__("."):
if image_path.split(".")[1] not in ["xmp", "MOV", "tif", "TIF", "tiff", "TIFF"]:
count += 1
with h5py.File(self.hdf5_path, 'a') as hf:
# resize to new shape
old_shape = hf["imgs"].shape[0]
hf["imgs"].resize((old_shape + count), axis = 0)
Focal_lengths = list(hf["focal_length"])
i = 0
dset = hf["imgs"]
for path, subdirs, files in os.walk(self.root_dir):
for name in files:
image_path = os.path.join(path, name)
# print(image_path)
if image_path.__contains__("."):
if image_path.split(".")[1] not in ["xmp", "MOV", "tif", "TIF", "tiff", "TIFF"]:
with open(image_path, 'rb') as f:
tags = exifread.process_file(f)
if "EXIF FocalLengthIn35mmFilm" in tags:
focal_length = int(str(tags["EXIF FocalLengthIn35mmFilm"]))
valid = True
elif self.force_focal_length is not None:
focal_length = self.force_focal_length
valid = True
else:
print("No Tag")
valid = False
if valid:
if image_path.split(".")[1] in ["jpg", "JPG"]:
with Image.open(image_path) as f:
img = np.array(f)
else:
with rawpy.imread(image_path) as raw:
try:
img = raw.postprocess()
except:
continue
h, w, _ = img.shape
if h<w:
img = np.transpose(img, (1,0,2))
h, w, _ = img.shape
if h!=w:
img = img[(h-w)//2:-(h-w)//2, :]
res = cv2.resize(img, dsize=(256, 256), interpolation=cv2.INTER_CUBIC)
dset[old_shape+i] = np.transpose(res,(2,0,1))
Focal_lengths.append(focal_length)
i+=1
print(old_shape+i)
# if i ==10:
# break
else:
# Continue if the inner loop wasn't broken.
continue
break
dset.resize((i+old_shape,3,256,256))
del hf["focal_length"]
flengths = hf.create_dataset('focal_length', data=Focal_lengths)
# hf["focal_length"].resize((i + hf["focal_length"].shape[0]), axis = 0)
# hf["focal_length"][-len(Focal_lengths):] = Focal_lengths
def doprep(self):
Focal_lengths = []
self.images = []
valid = False
count = 0
for path, subdirs, files in os.walk(self.root_dir):
for name in files:
image_path = os.path.join(path, name)
if image_path.split(".")[1] not in ["xmp", "MOV", "tif", "TIF", "tiff", "TIFF"]:
count += 1
# print(image_path)
# hf = h5py.File('data.h5', 'w')
# f = h5py.File(hdf5_path, 'w')
with h5py.File(self.hdf5_path, 'w') as hf:
# hf.create_dataset
dset = hf.create_dataset('imgs', shape=(count,3,256,256),
maxshape=(None,3,256,256), chunks=(8,3,256,256), dtype=np.int8, compression="gzip")
i = 0
with h5py.File(self.hdf5_path, 'a') as hf:
dset = hf["imgs"]
for path, subdirs, files in os.walk(self.root_dir):
for name in files:
image_path = os.path.join(path, name)
# print(image_path)
if image_path.split(".")[1] not in ["xmp", "MOV", "tif", "TIF", "tiff", "TIFF"]:
with open(image_path, 'rb') as f:
tags = exifread.process_file(f)
if "EXIF FocalLengthIn35mmFilm" in tags:
focal_length = int(str(tags["EXIF FocalLengthIn35mmFilm"]))
valid = True
else:
print("No Tag")
valid = False
if valid:
if image_path.split(".")[1] in ["jpg", "JPG"]:
with Image.open(image_path) as f:
img = np.array(f)
else:
with rawpy.imread(image_path) as raw:
try:
img = raw.postprocess()
except:
continue
h, w, _ = img.shape
if h<w:
img = np.transpose(img, (1,0,2))
h, w, _ = img.shape
if h!=w:
img = img[(h-w)//2:-(h-w)//2, :]
res = cv2.resize(img, dsize=(256, 256), interpolation=cv2.INTER_CUBIC)
# im_resized = img.resize((256, 256))
dset[i] = np.transpose(res,(2,0,1))
Focal_lengths.append(focal_length)
i+=1
print(i)
# if i ==30:
# break
else:
# Continue if the inner loop wasn't broken.
continue
break
# else:
# print("No Tag found:", image_path)
dset.resize((i,3,256,256))
flengths = hf.create_dataset('focal_length', data=Focal_lengths)
print("finished with", str(i), "samples")
def __len__(self):
return len(self.X_idx)
def __getitem__(self, idx):
# pick from the train, test, val ordering
dataidx = self.X_idx[idx]
# access data
if self.in_memory:
img = self.imgs[dataidx]
else:
with h5py.File(self.hdf5_path, 'r') as hf:
img = hf["imgs"][dataidx]
focal_length = self.focal_length[dataidx]
# reshape
img = np.transpose(img.astype(np.float32),(1,2,0))
# noramlize
v_min, v_max = img.min(), img.max()
new_min, new_max = 0.0, 1.0
img = (img - v_min)/(v_max - v_min + self.eps)*(new_max - new_min) + new_min
# apply transforms and augmentations
if self.transform:
img = self.transform(img)
return {'img': img, 'y': focal_length}
if __name__=='__main__':
# Create a transform to preprocess the data
data_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Load the dataset
# dataset = FocalLengthDataset(root_dir=r'C:\Users\nando\Pictures\Lightroom_backuped\Lightroom Catalog-v12 Smart Previews.lrdata\E', transform=data_transform)
# dataset = FocalLengthDataset(
# root_dir=r'D:\Photo_collection_ssd',
# transform=data_transform, hdf5_path="data/imgdataset3.h5", focal_length_path='data/split_file3.pickle',
# force_recompute=False, split_mode="time", append_new_data=True)
dataset = FocalLengthDataset(
root_dir=r'data/raws/xx',
transform=data_transform, hdf5_path="data/imgdataset4.h5", focal_length_path='data/split_file4.pickle',
force_recompute=False, split_mode="time", force_focal_length=12, append_new_data=False, recompute_split=True)
# Create a dataloader to feed the dataset into a model
dataloader = DataLoader(dataset, batch_size=4, shuffle=True, num_workers=4)
a = dataset[0]
# Iterate through the dataloader
for batch in tqdm(dataloader):
images = batch['img']
focal_lengths = batch['y']