-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.1_convolutional_example.py
56 lines (43 loc) · 1.35 KB
/
1.1_convolutional_example.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
from util import readDatabase, showConfusionMatrix
from keras.models import Sequential
from keras.layers.core import Dense, Flatten
from keras.layers.convolutional import Conv2D
from keras.optimizers import Adam
xTrain, yTrain, xTest, yTestCategorical, yTest = readDatabase()
print(xTrain.shape)
print(xTest.shape)
print(yTestCategorical.shape)
# Network parameters
firstConvLayerDepth = 2
numberOfNeurons = 200
# Training hyper-parameters
learningRate = 0.001
noOfEpochs = 20
batchSize = 32
numberOfClasses = yTrain.shape[1]
featureSize = xTrain.shape[1]
# Program parameters
verbose = 1
showPlot = True
# Network architecture
model = Sequential()
model.add(Conv2D(firstConvLayerDepth, kernel_size=(5, 5),
activation='relu',
strides=(2, 2),
padding='same',
input_shape=(32, 32, 3)))
# output is 16x16
model.add(Flatten())
model.add(Dense(numberOfNeurons, activation='relu'))
model.add(Dense(numberOfClasses, activation='softmax'))
sgd = Adam(lr=learningRate)
model.compile(optimizer=sgd,
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=xTrain,
y=yTrain,
epochs=noOfEpochs,
batch_size=batchSize,
verbose=verbose)
predictedValues = model.predict(xTest, batch_size=1)
showConfusionMatrix(yTest, predictedValues)