Repository navigation
Expand file tree
/
Copy path06_functionalCNN.py
More file actions
108 lines (94 loc) · 2.45 KB
/
Copy path06_functionalCNN.py
File metadata and controls
108 lines (94 loc) · 2.45 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Convoluted neural network (CNN) implementation using
functional API rather than using sequential api
on solving the MNIST dataset classification.
"""
# to supress tensorflow-gpu debug information
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
from tensorflow.keras.layers import Dense, Dropout, Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical, plot_model
# load MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# from sparse label to categorical
num_labels = len(np.unique(y_train))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# reshape and normalize input images
image_size = x_train.shape[1]
x_train = np.reshape(x_train,[-1, image_size, image_size, 1])
x_test = np.reshape(x_test,[-1, image_size, image_size, 1])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
# network parameters
input_shape = (image_size, image_size, 1)
batch_size = 128
kernel_size = 3
filters = 64
dropout = 0.3
# use functional API to build cnn layers
inputs = Input(shape=input_shape)
y = Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu'
)(inputs)
y = MaxPooling2D()(y)
y = Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu'
)(y)
y = MaxPooling2D()(y)
y = Conv2D(
filters=filters,
kernel_size=kernel_size,
activation='relu'
)(y)
# image to vector before connecting to dense layer
y = Flatten()(y)
# dropout regularization
y = Dropout(dropout)(y)
outputs = Dense(
num_labels,
activation='softmax'
)(y)
# build the model by supplying inputs/outputs
model = Model(
inputs=inputs,
outputs=outputs
)
# network model in text
model.summary()
plot_model(model, to_file='functional-cnn.png', show_shapes=True)
# classifier loss, Adam optimizer, classifier accuracy
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# train the model with input images and labels
model.fit(
x_train,
y_train,
validation_data=(
x_test,
y_test
),
epochs=20,
batch_size=batch_size
)
# model accuracy on test dataset
score = model.evaluate(
x_test,
y_test,
batch_size=batch_size,
verbose=0
)
print("\nTest accuracy: %.2f%%" % (100.0 * score[1]))