-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDataLoader.py
67 lines (48 loc) · 1.79 KB
/
DataLoader.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
import Debugger
import os
from os import listdir
from os.path import isfile, join
import re
import pickle
import numpy as np
from skimage import io
from tqdm import tqdm
import h5py
class DataLoader(object):
"""
Will handle loading and parsing the data.
"""
def __init__(self, settings):
self.settings = settings
self.debugger = Debugger.Debugger(settings)
### h5 save and load
def mkdir(self, directory):
if not os.path.exists(directory):
os.makedirs(directory)
def save_images_to_h5(self, lefts, rights, labels, savepath):
SIZE = lefts[0].shape
SUBSET = len(lefts)
self.mkdir(self.settings.large_file_folder+"datasets")
hdf5_path = self.settings.large_file_folder+"datasets/"+savepath + str(SUBSET) + "_res" + str(SIZE[0]) + "x" + str(SIZE[1]) + ".h5"
hdf5_file = h5py.File(hdf5_path, mode='w')
hdf5_file.create_dataset("lefts", data=lefts, dtype="float32")
hdf5_file.create_dataset("rights", data=rights, dtype="float32")
hdf5_file.create_dataset("labels", data=labels, dtype="float32")
hdf5_file.close()
#print("Saved", SUBSET, "images successfully to:", hdf5_path)
return hdf5_path
def load_images_from_h5(self, hdf5_path):
hdf5_file = h5py.File(hdf5_path, "r")
lefts = hdf5_file['lefts'][:]
rights = hdf5_file['rights'][:]
labels = hdf5_file['labels'][:]
hdf5_file.close()
return lefts, rights, labels
### Loading file paths from already computed files:
def load_paths_from_pickle(self, name):
with open(name, 'rb') as fp:
paths = pickle.load(fp)
return paths
def save_paths(self, paths, name):
with open(name, 'wb') as fp:
pickle.dump(paths, fp)