-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
189 lines (132 loc) · 5.49 KB
/
cnn.py
File metadata and controls
189 lines (132 loc) · 5.49 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import tensorflow as tf
import numpy as np
import time
from datetime import timedelta
import math
#import cifar10
# Convolutional Layer 1
filter_size1 = 5 # 5x5
num_filters1 = 16
# Convolutional Layer 2
filter_size2 = 5
num_filters2 = 36
# Fully-connected layer
fc_size = 128 # Number of neurons
#import data
#mnist handwritten dataset can be imported directly from tensorflow library
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets('data/MNIST/', one_hot=True)
data.test.cls = np.argmax(data.test.labels, axis=1)
img_size = 28
# Size of image as 1-d vector
img_vector_size = img_size * img_size
# Height and Width
img_shape = (img_size, img_size)
# Number of colour channels: 1 = gray-scale , 3 = RGB
num_channels = 1
# Number of classes
num_classes = 10
def create_weights(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
def create_biases(length):
return tf.Variable(tf.constant(0.05, shape=[length]))
def conv_layer(input, num_input_channels, filter_size, num_filters, use_pooling=True):
shape = [filter_size, filter_size, num_input_channels, num_filters]
# Create new weights-filters and biases
weights = create_weights(shape=shape)
biases = create_biases(length=num_filters)
# Convolution operation
layer = tf.nn.conv2d(input=input, filter=weights, strides=[1, 1, 1, 1], padding='SAME')
layer += biases
# Use pooling to down-sample the image resolution
if use_pooling:
layer = tf.nn.max_pool(value=layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# ReLU - calculates max(x, 0) for each input pixel x.
layer = tf.nn.relu(layer)
return layer, weights
def flatten_layer(layer):
layer_shape = layer.get_shape()
# Number of features = img_height * img_width * num_channels
num_features = layer_shape[1:4].num_elements()
layer_flat = tf.reshape(layer, [-1, num_features])
return layer_flat, num_features
def fully_connected_layer(input,num_inputs, num_outputs, use_relu=True):
# Create new weights and biases.
weights = new_weights(shape=[num_inputs, num_outputs])
biases = new_biases(length=num_outputs)
# w * X + b
layer = tf.matmul(input, weights) + biases
if use_relu:
layer = tf.nn.relu(layer)
return layer
#Tensorflow placeholders for the variables
#The variables needed in order TensorFlow to create the computational graph
x = tf.placeholder(tf.float32, shape=[None, img_vector_size], name='x')
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
y_true = tf.placeholder(tf.float32, shape=[None, 10], name='y_true')
y_true_cls = tf.argmax(y_true, axis=1)
#Conv Layer 1
layer_conv1, weights_conv1 = new_conv_layer(input=x_image,
num_input_channels=num_channels,
filter_size=filter_size1,
num_filters=num_filters1,
use_pooling=True)
#Conv Layer 2
layer_conv2, weights_conv2 = new_conv_layer(input=layer_conv1,
num_input_channels=num_filters1,
filter_size=filter_size2,
num_filters=num_filters2,
use_pooling=True)
#Flatten Layer - Reshape to 2d-tensors
layer_flat, num_features = flatten_layer(layer_conv2)
#Fully Connected Layer
layer_fc1 = new_fc_layer(input=layer_flat,
num_inputs=num_features,
num_outputs=fc_size,
use_relu=True)
#Second fully connected Layer - Output the 10 classes
layer_fc2 = new_fc_layer(input=layer_fc1,
num_inputs=fc_size,
num_outputs=num_classes,
use_relu=False)
#Softmax
y_pred = tf.nn.softmax(layer_fc2)
y_pred_cls = tf.argmax(y_pred, axis=1)
#Cost Function Optimization
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,
labels=y_true)
cost = tf.reduce_mean(cross_entropy)
#Optimization Method - Gradient Descent
optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
#Performance Measures
correct_prediction = tf.equal(y_pred_cls, y_true_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#Run TensorFlow
session = tf.Session()
# Initialize Variables
session.run(tf.global_variables_initializer())
train_batch_size = 64
total_iterations = 0
def optimize(num_iterations):
global total_iterations
start_time = time.time()
for i in range(total_iterations, total_iterations + num_iterations):
# Get a batch of training examples.
x_batch, y_true_batch = data.train.next_batch(train_batch_size)
# Create a dict with batch images and corresponding classes
feed_dict_train = {x: x_batch, y_true: y_true_batch}
# Run the optimizer using this batch of training data.
session.run(optimizer, feed_dict=feed_dict_train)
# Print accuracy status every 10 iterations
if i % 10 == 0:
# Calculate the accuracy on the training-set.
acc = session.run(accuracy, feed_dict=feed_dict_train)
msg = "Optimization Iteration: {0:>6}, Training Accuracy: {1:>6.1%}"
print(msg.format(i + 1, acc))
total_iterations += num_iterations
end_time = time.time()
time_dif = end_time - start_time
print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))
if __name__ == "__main__":
num_iterations = 10000
optimize(num_iterations=num_iterations)