-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainNeuralNetwork.py
97 lines (75 loc) · 2.76 KB
/
trainNeuralNetwork.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
from keras import layers
from keras import models
from keras import optimizers
import numpy as np
import cv2
import glob
from matplotlib import pyplot as plt
from keras.utils import plot_model
from keras import backend
import sklearn
from sklearn import metrics
VALIDATION_SPLIT = 0.2
def reset_weights(model):
session = backend.get_session()
for layer in model.layers:
if hasattr(layer, 'kernel_initializer'):
layer.kernel.initializer.run(session=session)
def to_one_hot(char):
ind = ord(char)
ind = ind - 48 if ind < 58 else ind - 55
row = [0] * 36
row[ind] = 1
return row
paths = np.array(glob.glob('/home/fizzer/enph353_cnn_lab/data/*.png'))
np.random.shuffle(paths)
x_data = np.array([cv2.imread(path)[:,:,2][:,:,np.newaxis]/255.0 for path in paths])
y_data = np.array([to_one_hot(path[-8]) for path in paths])
print(y_data.shape)
print(x_data.shape)
# actual code xd
conv_model = models.Sequential()
conv_model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(85, 53, 1)))
conv_model.add(layers.MaxPooling2D((2, 2)))
conv_model.add(layers.Conv2D(64, (3, 3), activation='relu'))
conv_model.add(layers.MaxPooling2D((2, 2)))
conv_model.add(layers.Conv2D(128, (3, 3), activation='relu'))
conv_model.add(layers.MaxPooling2D((2, 2)))
conv_model.add(layers.Conv2D(128, (3, 3), activation='relu'))
conv_model.add(layers.MaxPooling2D((2, 2)))
conv_model.add(layers.Flatten())
conv_model.add(layers.Dropout(0.5))
conv_model.add(layers.Dense(512, activation='relu'))
conv_model.add(layers.Dense(36, activation='softmax'))
conv_model.summary()
LEARNING_RATE = 1e-4
conv_model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=LEARNING_RATE),
metrics=['acc'])
reset_weights(conv_model)
history_conv = conv_model.fit(x_data, y_data,
validation_split=VALIDATION_SPLIT,
epochs=5,
batch_size=16)
conv_model.save('showMiti.h5')
y_pred = conv_model.predict(x_data)
y_pred = [np.argmax(i) for i in y_pred]
y_data = [np.argmax(i) for i in y_data]
confusion_matrix = sklearn.metrics.confusion_matrix(y_data, y_pred)
confusion_matrix = confusion_matrix.astype("float") / confusion_matrix.sum(axis=1)[:, np.newaxis]
print(confusion_matrix)
plt.plot(history_conv.history['loss'])
plt.plot(history_conv.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train loss', 'val loss'], loc='upper left')
plt.show()
plt.plot(history_conv.history['acc'])
plt.plot(history_conv.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy (%)')
plt.xlabel('epoch')
plt.legend(['train accuracy', 'val accuracy'], loc='upper left')
plt.show()