-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLoader.py
177 lines (157 loc) · 8.11 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
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
import os
import Eye
import matplotlib.image as mp_i
class DataLoader:
"""Loades images from given directory."""
_data_path = os.path.join(".", "data")
_raw_path = os.path.join(_data_path, "images")
_manual_path = os.path.join(_data_path, "manual1")
_mask_path = os.path.join(_data_path, "mask")
_load_healthy = False
_load_glaucomatous = False
_load_diabetic = False
_healthy_ends_with = "_h"
_glaucomatous_ends_with = "_g"
_diabetic_ends_with = "_dr"
_mask_ends_with = "_mask.tif"
_manual_ends_with = ".tif"
files_raw = []
files_manual = []
files_mask = []
x_patch_size = int()
_startLearning = int()
_endLearning = int()
_startProcessing = int()
_endProcessing = int()
def __init__(self, healthy=True, glaucomatous=False, diabetic=False,
start_learning=1, end_learning=1, start_processing=2, end_processing=2,
x_patch_size=15, y_patch_size=15):
self._load_healthy = healthy
self._load_glaucomatous = glaucomatous
self._load_diabetic = diabetic
self._startLearning = start_learning - 1
self._endLearning = end_learning - 1
self._startProcessing = start_processing - 1
self._endProcessing = end_processing - 1
self.x_patch_size = x_patch_size
self.y_patch_size = y_patch_size
def loadData(self, verbose):
if self._load_healthy:
if not self.__validate_number_of_files(self._healthy_ends_with):
raise FileNotFoundError("Missing files of healthy images")
if self._load_glaucomatous:
if not self.__validate_number_of_files(self._glaucomatous_ends_with):
raise FileNotFoundError("Missing files of glaucomatous images")
if self._load_diabetic:
if not self.__validate_number_of_files(self._diabetic_ends_with):
raise FileNotFoundError("Missing files of diabetic images")
eyes_to_train = {"h": [], # healthy
"g": [], # glaucomatous
"d": []} # diabetic
eyes_to_process = {"h": [], # healthy
"g": [], # glaucomatous
"d": []} # diabetic
self.files_raw = os.listdir(self._raw_path)
self.files_manual = os.listdir(self._manual_path)
self.files_mask = os.listdir(self._mask_path)
if self._load_healthy:
files_raw_healthy = self.filter_files(self.files_raw,
self._healthy_ends_with,
self._startLearning,
self._endLearning)
eyes_to_train["h"] = self.load_list_of_eyes(files_raw_healthy,
self._startLearning,
self._endLearning,
verbose)
eyes_to_process["h"] = self.load_list_of_eyes(files_raw_healthy,
self._startProcessing,
self._endProcessing,
verbose)
if self._load_diabetic:
files_raw_diabetic = self.filter_files(self.files_raw,
self._diabetic_ends_with,
self._startLearning,
self._endLearning)
eyes_to_train["d"] = self.load_list_of_eyes(files_raw_diabetic,
self._startLearning,
self._endLearning,
verbose)
eyes_to_process["d"] = self.load_list_of_eyes(files_raw_diabetic,
self._startProcessing,
self._endProcessing,
verbose)
if self._load_glaucomatous:
files_raw_glaucomatous = self.filter_files(self.files_raw,
self._glaucomatous_ends_with,
self._startLearning,
self._endLearning)
eyes_to_train["g"] = self.load_list_of_eyes(files_raw_glaucomatous,
self._startLearning,
self._endLearning,
verbose)
eyes_to_process["g"] = self.load_list_of_eyes(files_raw_glaucomatous,
self._startProcessing,
self._endProcessing,
verbose)
print("Loaded " + str(len(eyes_to_train.get("h"))) + " healthy images to train")
print("Loaded " + str(len(eyes_to_train.get("g"))) + " glaucomatous images to train")
print("Loaded " + str(len(eyes_to_train.get("d"))) + " diabetic images to train")
print("Loaded " + str(len(eyes_to_process.get("h"))) + " healthy images to process")
print("Loaded " + str(len(eyes_to_process.get("g"))) + " glaucomatous images to process")
print("Loaded " + str(len(eyes_to_process.get("d"))) + " diabetic images to process")
return eyes_to_train, eyes_to_process
@staticmethod
def filter_files(list_of_files, filename_ends_with, start, end):
files = []
numbers = DataLoader.generate_list_of_numbers(start+1, end+1)
for file in list_of_files:
if filename_ends_with in file and True in [num in file for num in numbers]:
files.append(file)
return files
def load_list_of_eyes(self, list_of_files, start, end, verbose):
list_of_eyes = []
for i in range(start, end):
path = list_of_files[i]
if verbose:
print("Loading: " + path)
eye = self.loadEye(path, i % 2 != 0)
if eye is not None:
list_of_eyes.append(eye)
return list_of_eyes
@staticmethod
def generate_list_of_numbers(start, end):
numbers = []
for i in range(start, end + 1):
s = "%02d" % i
numbers.append(s)
return numbers
@staticmethod
def count_files(path, path_ends_with):
counter = 0
for file in os.listdir(path):
if file.endswith(path_ends_with):
counter += 1
return counter
def __validate_number_of_files(self, filename_ending):
images = self.count_files(self._raw_path, filename_ending)
manuals = self.count_files(self._manual_path, filename_ending)
masks = self.count_files(self._mask_path, filename_ending)
if images == manuals == masks:
return True
else:
return False
def loadEye(self, file, reverse=False, plot_loaded_images=False):
if not reverse:
img_raw = mp_i.imread(os.path.join(self._raw_path, file))
img_manual = mp_i.imread(os.path.join(self._manual_path, file.split('.')[0] + self._manual_ends_with))
img_mask = mp_i.imread(os.path.join(self._mask_path, file.split('.')[0] + self._mask_ends_with))
else:
img_raw = mp_i.imread(os.path.join(self._raw_path, file))[:, ::-1]
img_manual = mp_i.imread(os.path.join(self._manual_path, file.split('.')[0] + self._manual_ends_with))[:, ::-1]
img_mask = mp_i.imread(os.path.join(self._mask_path, file.split('.')[0] + self._mask_ends_with))[:, ::-1]
eye = Eye.Eye(img_raw, img_manual, img_mask, self.x_patch_size, self.y_patch_size)
if plot_loaded_images:
eye.plot_raw()
eye.plot_manual()
eye.plot_image(eye.get_mask())
return eye