-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
265 lines (214 loc) · 6.6 KB
/
utilities.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
import numpy as np
import pathlib
import PIL
import PIL.Image
import os
import gc
from matplotlib import pyplot as plt
from pathlib import Path, PurePath
import tensorflow as tf
from keras.layers import *
from keras.models import *
from keras.losses import *
from keras.optimizers import *
from keras.utils import *
from keras.callbacks import *
import CNNModels
DATASET_DIRECTORY = pathlib.Path("downloads", "CatsDogs")
def ready_to_be_used_dataset(
image_size,
color_mode="rgb",
seed=42,
):
"""
Normalize and split in half `DATASET_DIRECTORY` images.
"""
training_dataset = image_dataset_from_directory(
DATASET_DIRECTORY,
validation_split=0.7,
color_mode=color_mode,
subset="training",
seed=seed,
image_size=(image_size, image_size),
)
validation_dataset = image_dataset_from_directory(
DATASET_DIRECTORY,
color_mode=color_mode,
validation_split=0.7,
subset="validation",
seed=seed,
image_size=(image_size, image_size),
)
# normalization_layer = Rescaling(1.0 / 255)
# normalized_training = training_dataset.map(lambda x, y: (normalization_layer(x), y))
# normalized_validation = validation_dataset.map(
# lambda x, y: (normalization_layer(x), y)
# )
# return normalized_training, normalized_validation
return training_dataset, validation_dataset
# https://cloudxlab.com/assessment/displayslide/5658/converting-tensor-to-image
def tensor_to_image(tensor):
tensor = tensor * 255
tensor = np.array(tensor, dtype=np.uint8)
if np.ndim(tensor) > 3:
assert tensor.shape[0] == 1
tensor = tensor[0]
return PIL.Image.fromarray(tensor)
# plot_model(
# model,
# show_shapes=True,
# show_dtype=False,
# show_layer_names=True,
# rankdir="TB",
# expand_nested=False,
# dpi=96,
# layer_range=None,
# show_layer_activations=True,
# )
def performance_plot(results, tag: str = None, figsize=(16, 6)):
plt.figure(figsize=figsize)
# Plot loss
plt.subplot(1, 2, 1)
plt.plot(results.history["loss"])
plt.plot(results.history["val_loss"])
plt.ylabel("loss", size=12)
plt.xlabel("epoch", size=12)
plt.legend(["train", "val"], fontsize=10)
# Plot accuracy
plt.subplot(1, 2, 2)
plt.plot(results.history["accuracy"])
plt.plot(results.history["val_accuracy"])
plt.ylabel("accuracy", size=12)
plt.xlabel("epoch", size=12)
plt.legend(["train", "val"], fontsize=10)
if tag is not None:
plt.savefig(PurePath(tag, "loss_accuracy.png"))
else:
plt.show()
def delete_from_list(filename="files.to.delete.txt"):
count = 0
with open(filename, "rt") as f:
lines = f.readlines()
for pth in lines:
try:
os.remove(pth.strip())
count += 1
except Exception as inst:
print(inst.args)
print(
count,
"of",
len(lines),
"files deleted",
)
def validate_tag(model, color_mode, image_size):
tag = f"{model}_model_{color_mode}_x{image_size}_img"
Path(tag).mkdir(parents=True, exist_ok=True)
return tag
def auto_train(
model_name,
image_size=128,
color_mode="rgb",
epochs=24,
):
# REF TAG
tag = validate_tag(model_name, image_size=image_size, color_mode=color_mode)
# DATA
train_val, test = ready_to_be_used_dataset(
image_size=image_size,
color_mode=color_mode,
)
valid_size = len(train_val) // 5
valid, train = train_val.take(valid_size), train_val.skip(valid_size)
# CREATE MODEL
model = CNNModels.get_model(model_name)
model.compile(
optimizer=Adam(), # NOTE learning rate?
loss=SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"],
)
# FITTING
results = model.fit(
x=train,
validation_data=valid,
epochs=epochs,
callbacks=[EarlyStopping(monitor="val_accuracy", min_delta=0.01, patience=4)],
)
performance_plot(results, tag)
# SAVE MODEL
with open(PurePath(tag, "model_summary.txt"), "wt") as f:
model.summary(print_fn=lambda x: f.write(x + "\n"))
# THAT's ALL FOLKS
del model
gc.collect()
return tag, test
def k_fold_cross_validation(
tag,
dataset,
model_name,
k=5,
epochs=24,
learning_rate=1e-3,
):
"""
https://github.com/christianversloot/machine-learning-articles/blob/main/how-to-use-k-fold-cross-validation-with-keras.md
"""
# per-fold score containers
accuracies = []
losses = []
for fold_counter in range(k):
# ROTATE SPLITTING OF TESTING
N = len(dataset)
fold_size = N // k
train_lx_size = N * fold_counter // k
train_lx = dataset.take(train_lx_size)
test = dataset.skip(train_lx_size).take(fold_size)
validation = dataset.skip(train_lx_size + fold_size).take(fold_size)
train_rx = dataset.skip(train_lx_size + fold_size + fold_size)
if fold_counter == k-1:
validation, train_lx = train_lx.take(fold_size), train_lx.skip(fold_size)
train = train_rx.concatenate(train_lx)
print("FSIZE:", len(train), len(validation), len(test))
# CREATE MODEL
if type(model_name) is list:
model = Sequential(model_name)
optimizer = Adam(learning_rate=learning_rate)
else:
model = CNNModels.get_model(model_name)
optimizer = Adam()
model.compile(
optimizer=optimizer,
loss=SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"],
)
# FITTING
print("-" * 35)
print(f">> Train fold {fold_counter+1} ...")
print("-" * 35)
model.fit(
x=train,
validation_data=validation,
epochs=epochs,
callbacks=[
EarlyStopping(monitor="val_accuracy", min_delta=0.01, patience=4)
],
verbose=2,
)
# EVALUATING
print("-" * 35)
print(f"<< Eval fold {fold_counter+1} ...")
print("-" * 35)
scores = model.evaluate(test)
print(f"Score for fold {fold_counter+1}:")
for i, s in enumerate(model.metrics_names):
print(f">> {s} of {scores[i]}")
# SAVE STUFF
accuracies.append(scores[1])
losses.append(scores[0])
# THAT's ALL FOLKS
del model
gc.collect()
with open(f"accuracies_{tag}.txt", "wt") as f:
for e in accuracies:
f.write(f"{e}\n")
return accuracies