/
neural_network.py
76 lines (59 loc) · 2.27 KB
/
neural_network.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
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, Lambda
from keras.utils import np_utils
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
import numpy as np
from data import loadImages
def predict(model):
print("\n>>>PREDICTING ON TEST DATA<<<")
# testSimpleX,testSimpleY = loadImages("images/TEST_SIMPLE");
print("\n\tLoading images...")
testX,testY = loadImages("processed_images/TEST");
print("\tDONE! Images are loaded.")
encoder = LabelEncoder()
encoder.fit(testY)
encoded_y_test = encoder.transform(testY)
testY = np_utils.to_categorical(encoded_y_test)
prediction_matching = np.rint(model.predict(testX))
print(accuracy_score(testY, prediction_matching))
def fitModel(epochs, batch_size):
print("\n>>>TRAINING MODEL<<<")
print("\n\tLoading images...")
trainX, trainY = loadImages("processed_images/TRAIN")
print("\tDONE! Images are loaded.")
encoder = LabelEncoder()
encoder.fit(trainY)
encoded_y_train = encoder.transform(trainY)
trainY = np_utils.to_categorical(encoded_y_train)
model = neural_network_model()
model.fit(trainX, trainY, epochs=epochs, batch_size=batch_size)
model.save_weights('network.h5')
return model
def loadModel(path):
model = neural_network_model()
model.load_weights(path)
return model
def neural_network_model():
model = Sequential()
model.add(Lambda(lambda x: x / 127.5 - 1., input_shape=(240, 320, 3,), output_shape=(240, 320, 3,)))
model.add(Conv2D(32, (3, 3), input_shape=(240, 320, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.7))
model.add(Dense(4))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model