-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
92 lines (68 loc) · 3.61 KB
/
train.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
import matplotlib
from tensorflow.core.framework.dataset_options_pb2 import AUTO
from model import *
import model_resnet as modelres
import model_new as model_new_cnn
import tensorflow as tf
from data import *
from settings import *
########################################################################################################################
# LOADING DATA
########################################################################################################################
import matplotlib.pyplot as plt
plt.clf()
categories, train_data, test_data, x_train, y_train, x_test, y_test = load_data(DATA_PATH)
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
########################################################################################################################
# BUILDING MODEL
########################################################################################################################
# Model CNN
# model = create_model(x_train, x_test, classes=len(categories), trainable_encoder=False)
# Model ResNet
x_train_pp, x_test_pp, model = modelres.create_model(x_train, x_test, classes=len(categories), trainable_encoder=False)
# Model CNN New
#model = model_new_cnn.create_model(x_train, x_test, classes=len(categories), trainable_encoder=False)
########################################################################################################################
# TRAINING MODEL
########################################################################################################################
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_sparse_categorical_accuracy', mode='max',
patience=30, restore_best_weights=True, verbose=1)
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_sparse_categorical_accuracy', mode='max',
factor=0.1, patience=10, verbose=1)
csv_logger = tf.keras.callbacks.CSVLogger(os.path.join(TMP_PATH, 'training.csv'))
# Model CNN
# hist = model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=1000,
# validation_data=(x_test, y_test), verbose=2,
# callbacks=[early_stopping, reduce_lr, csv_logger])
# Model Resnet
hist = model.fit(x_train_pp, y_train, batch_size=BATCH_SIZE, epochs=1000,
validation_data=(x_test_pp, y_test), verbose=2,
callbacks=[early_stopping, reduce_lr, csv_logger])
path = os.path.join(TMP_PATH, 'trained_model.h5')
model.save(path, include_optimizer=False)
plt.clf()
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.savefig(os.path.join(TMP_PATH, 'training_loss.png'))
plt.clf()
plt.plot(hist.history['sparse_categorical_accuracy'])
plt.plot(hist.history['val_sparse_categorical_accuracy'])
plt.savefig(os.path.join(TMP_PATH, 'training_accuracy.png'))
########################################################################################################################
# EVALUATE MODEL
########################################################################################################################
res = model.evaluate(x_test, y_test, batch_size=BATCH_SIZE)
print(res)
y_out = model.predict(x_test, batch_size=BATCH_SIZE)
y_out = np.argmax(y_out, axis=1)
i = 0
plt.figure(figsize=(4, 4))
for img, out, exp in zip(x_test, y_out, y_test):
if out != exp:
# plt.clf()
# plt.imshow(img)
title = '{} misclassified as {}'.format(categories[exp], categories[out])
print(title)
# plt.title(title)
i += 1
# plt.savefig(os.path.join(TMP_PATH, '{} ({}).png'.format(i, title)))