-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersona_trainer.py
305 lines (252 loc) · 10.5 KB
/
persona_trainer.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
import glob
import shutil
import matplotlib.pyplot as plt
import numpy as np
import os
import pathlib
import cv2
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
# from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
print("Tensorflow version:", tf.__version__)
data_dir = pathlib.Path("./Bulk Dataset/")
image_count = len(list(data_dir.glob('*/*.jpg')))
print("Num Images: %s" % image_count)
batch_size = 32
img_height = 64
img_width = 32
def train_model(persona, epochs=10, skip_preprocess=False):
if persona == "Tom":
print("Retraining Tom")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print("Loaded Training Classes: %s" % class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
model = create_model()
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
print("Saving Tom weights...")
model.save_weights("./Personalities/Tom/Tom")
print("Tom weights saved.")
print("Saving Tom model...")
tf.keras.models.save_model(model, "./Personalities/Tom/")
print("Tom model saved.")
elif persona == "Jerry":
print("Retraining Jerry")
# make the temporary folder that clones the dataset and performs edge detection on them
os.makedirs("./temp/occ")
os.makedirs("./temp/nocc")
for image in glob.glob("Bulk Dataset/occ/*.jpg"):
n = cv2.imread(image)
n = edgify_image(n)
cv2.imwrite(os.path.join("./temp/occ", os.path.basename(image)), n)
print(os.path.join("./temp/occ", os.path.basename(image)))
for image in glob.glob("Bulk Dataset/nocc/*.jpg"):
n = cv2.imread(image)
n = edgify_image(n)
cv2.imwrite(os.path.join("./temp/nocc", os.path.basename(image)), n)
print(os.path.join("./temp/nocc", os.path.basename(image)))
# Override Jerry's datapath to be the canny edge detected images
edgydir = pathlib.Path("./temp/")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
edgydir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
edgydir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print("Loaded Training Classes: %s" % class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
model = create_model()
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
model.save_weights("./Personalities/Jerry/Jerry")
print("Saving Jerry model...")
tf.keras.models.save_model(model, "./Personalities/Jerry/")
print("Jerry model saved.")
elif persona == "Tweety":
print("Retraining Tweety")
# make the temporary folder that clones the dataset and performs edge detection on them
if not skip_preprocess:
print("Preprocessing...")
os.makedirs("./temp/occ")
os.makedirs("./temp/nocc")
for image in glob.glob("Bulk Dataset/occ/*.jpg"):
n = cv2.imread(image)
print("Edgifying...")
n = edgify_image(n, "Sobel")
cv2.imwrite(os.path.join("./temp/occ", os.path.basename(image)), n)
print(os.path.join("./temp/occ", os.path.basename(image)))
for image in glob.glob("Bulk Dataset/nocc/*.jpg"):
n = cv2.imread(image)
print("Edgifying...")
n = edgify_image(n, "Sobel")
cv2.imwrite(os.path.join("./temp/nocc", os.path.basename(image)), n)
else:
print("Skipping preprocess. WARNING: If this was unintentional, Tweety will not be trained on the correct data.")
# Override Tweety's datapath to be the canny edge detected images
edgydir = pathlib.Path("./temp/")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
edgydir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
edgydir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print("Loaded Training Classes: %s" % class_names)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
model = create_model()
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
model.save_weights("./Personalities/Tweety/Tweety")
print("Saving Tweety model...")
tf.keras.models.save_model(model, "./Personalities/Tweety/")
print("Tweety model saved.")
shutil.rmtree('./temp')
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
if os.path.exists('./temp'):
shutil.rmtree('./temp')
print("Result should be occ")
test_persona("./Processed Images/19_17_38_56.jpg", persona, class_names)
print("Result should be nocc")
test_persona("./Processed Images/20_17_38_56.jpg", persona, class_names)
print("Result should be nocc")
test_persona("./Processed Images/25_17_38_56.jpg", persona, class_names)
def edgify_image(image, style="Canny"):
# Blur the image for better edge detection
image = cv2.GaussianBlur(image, (3, 3), 0)
if style == "Canny":
image = cv2.Canny(image=image, threshold1=20, threshold2=80) # Canny Edge Detection
elif style == "Sobel":
image = cv2.Sobel(src=image, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X and Y Sobel Edge Detection
return image
def edgify_image_from_path(image_path, output_path, style="Canny"):
if not os.path.exists(output_path):
os.makedirs(output_path)
# Blur the image for better edge detection
image = cv2.imread(image_path)
image = edgify_image(image, style)
cv2.imwrite(os.path.join(output_path, '{}.jpg'.format(style)), image)
return os.path.join(output_path, '{}.jpg'.format(style))
def test_persona(image_path, persona, class_names):
if persona == "Jerry":
n = cv2.imread(image_path)
n = edgify_image(n)
# Check if temp exists, if not make it - Useful if we ever call this function from outside of this script
if not os.path.exists("./temp"):
os.makedirs("./temp")
cv2.imwrite("./temp/testcase.jpg", n)
image_path = "./temp/testcase.jpg"
elif persona == "Tweety":
n = cv2.imread(image_path)
n = edgify_image(n, "Sobel")
# Check if temp exists, if not make it - Useful if we ever call this function from outside of this script
if not os.path.exists("./temp"):
os.makedirs("./temp")
cv2.imwrite("./temp/testcase.jpg", n)
image_path = "./temp/testcase.jpg"
img = tf.keras.preprocessing.image.load_img(
image_path, target_size=(img_height, img_width)
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
model = create_model()
model.load_weights('./Personalities/{}/{}'.format(persona, persona))
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"{} thinks that this image most likely belongs to {} with a {:.2f} percent confidence."
.format(persona, class_names[np.argmax(score)], 100 * np.max(score))
)
# Display the testing image
disp = cv2.imread(image_path)
cv2.imshow("This is what {} saw.".format(persona), disp)
cv2.waitKey(0)
cv2.destroyAllWindows()
if os.path.exists('./temp'):
shutil.rmtree('./temp')
def create_model():
num_classes = 2
model = Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(8, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(num_classes)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
# Test Cases
# train_model("Tom")
# train_model("Jerry")
# train_model("Tweety")