-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
219 lines (164 loc) · 7.09 KB
/
util.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
import csv
import os
import random
import shutil
import numpy as np
from keras.utils import to_categorical
from skimage import exposure
from skimage import io
from skimage import transform
from sklearn.metrics import classification_report
from tensorflow import math
def makeTop5Stats(name):
rows = open(name).read().strip().split("\n")
classes_dict = {}
for (i, row) in enumerate(rows):
if i > 0 and i % 100 == 0:
print("[INFO] processed {} total rows".format(i))
first, second, third = row.strip().split(",")[:3]
classId = row.strip().split(",")[-1:]
id = int(classId[0])
if str(id) == first or str(id) == second or str(id) == third:
if id not in classes_dict:
classes_dict[id] = 1
else:
classes_dict[id] += 1
return classes_dict
def makeLabelCsv(basePath, csvName):
root_dir = os.path.abspath(basePath)
writer = csv.writer(open(os.path.sep.join([basePath, csvName + ".csv"]), "w"), delimiter=",", lineterminator="\n")
header = ["Width", "Height", "Roi.X1", "Roi.Y1", "Roi.X2", "Roi.Y2", "ClassId", "Path"]
writer.writerow(header)
for item in os.listdir(root_dir):
item_full_path = os.path.join(root_dir, item)
if not os.path.isdir(item_full_path):
continue
for image in os.listdir(item_full_path):
row = ["X", "X", "X", "X", "X", "X", item, item + "/" + image]
writer.writerow(row)
def augmentLabels(basePath, csvName, forTest=False):
root_dir = os.path.abspath(basePath)
writer = csv.writer(open(os.path.sep.join([basePath, csvName + '.csv']), 'a'), delimiter=",", lineterminator="\n")
for item in os.listdir(os.path.sep.join([basePath, csvName])):
if item == '-1':
item_full_path = os.path.sep.join([root_dir, csvName, item])
if not os.path.isdir(item_full_path):
continue
for image in os.listdir(item_full_path):
if not forTest:
row = ["X", "X", "X", "X", "X", "X", item, csvName + "/" + item + "/" + image]
else:
row = ["X", "X", "X", "X", "X", "X", item, csvName + "/" + image]
writer.writerow(row)
def split_to_train_and_eval(basePath, csvPath, newPath, evalsize=20):
# count the number of images
rows = open(csvPath).read().strip().split("\n")[1:]
random.shuffle(rows)
eval_dict = {}
writerTrain = csv.writer(open(os.path.sep.join([newPath, "Train.csv"]), "w"), delimiter=",", lineterminator="\n")
writerEval = csv.writer(open(os.path.sep.join([newPath, "Eval.csv"]), "w"), delimiter=",", lineterminator="\n")
header = ["Width", "Height", "Roi.X1", "Roi.Y1", "Roi.X2", "Roi.Y2", "ClassId", "Path"]
writerEval.writerow(header)
writerTrain.writerow(header)
# for each image
for (i, row) in enumerate(rows):
if i > 0 and i % 1000 == 0:
print("[INFO] processed {} total images".format(i))
# find path and id
(label, imagePath) = row.strip().split(",")[-2:]
dstTrainPath = os.path.sep.join([newPath, "Train", imagePath.partition("/")[2]])
dstEvalPath = os.path.sep.join([newPath, "Eval", imagePath.partition("/")[2]])
imagePath = os.path.sep.join([basePath, imagePath])
# jpgfile = Image.open(imagePath)
intaux = int(label)
if intaux not in eval_dict:
eval_dict[intaux] = 1
x = dstEvalPath.partition("/")[0]
os.makedirs(dstEvalPath.partition("/")[0], exist_ok=True)
shutil.copy(imagePath, dstEvalPath)
row = row.replace("Train", "Eval")
writerEval.writerow(row.strip().split(","))
elif eval_dict[intaux] < evalsize:
eval_dict[intaux] += 1
shutil.copy(imagePath, dstEvalPath)
row = row.replace("Train", "Eval")
writerEval.writerow(row.strip().split(","))
else:
os.makedirs(dstTrainPath.partition("/")[0], exist_ok=True)
shutil.copy(imagePath, dstTrainPath)
writerTrain.writerow(row.strip().split(","))
def writeTopToCSV(name, list):
with open(name, mode='w') as top_file:
top_writer = csv.writer(top_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC,
lineterminator="\n")
for top in list:
top_writer.writerow(top)
def evaluate(model, evalX, evalY, labelNames):
stats = {0: 0, 1: 0, 2: 0, 3: 0}
top5 = []
for i, image in enumerate(evalX):
if i % 10 == 0:
print("[INFO] Evaluated using {} images".format(i))
image = image.astype("float32") / 255.0
image = np.expand_dims(image, axis=0)
preds = model.predict(image)
top = np.argsort(-preds, axis=1)
if evalY[i] == top[0][0]:
stats[0] += 1
elif evalY[i] == top[0][1]:
stats[1] += 1
elif evalY[i] == top[0][2]:
stats[2] += 1
else:
stats[3] += 1
top5.append([top[0][0], top[0][1], top[0][2], top[0][3], top[0][4], evalY[i]])
numLabels = len(np.unique(evalY))
evalY = to_categorical(evalY, numLabels)
evalX = np.array(evalX, dtype=np.float32) / 255.0
predictions = model.predict(evalX)
report = classification_report(evalY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames)
confusion = math.confusion_matrix(evalY.argmax(axis=1), predictions.argmax(axis=1))
return stats, top5, report, confusion
# split training data again into TRAINING and FINAL EVALUATION/TESTING - DISJOINT
def load_data_and_labels(basePath, csvPath, evaluation_split=False, evalsize=20):
data = []
labels = []
# count the number of images
rows = open(csvPath).read().strip().split("\n")[1:]
random.shuffle(rows)
if evaluation_split:
eval_dict = {}
# for each image
for (i, row) in enumerate(rows):
if i > 0 and i % 1000 == 0:
print("[INFO] processed {} total images".format(i))
# find path and id
(label, imagePath) = row.strip().split(",")[-2:]
imagePath = os.path.sep.join([basePath, imagePath])
image = io.imread(imagePath)
image = transform.resize(image, (32, 32))
image = exposure.equalize_adapthist(image, clip_limit=0.1)
intaux = int(label)
if evaluation_split:
if intaux not in eval_dict:
eval_dict[intaux] = [image]
elif len(eval_dict[intaux]) < evalsize - 1:
eval_dict[intaux].append(image)
else:
data.append(image)
labels.append(intaux)
else:
data.append(image)
labels.append(intaux)
if evaluation_split:
eval_data = []
eval_labels = []
for key in eval_dict:
for value in eval_dict[key]:
eval_data.append(value)
eval_labels.append(key)
data = np.array(data)
labels = np.array(labels)
if evaluation_split:
return ((eval_data, eval_labels), (data, labels))
return data, labels