CONVOLUTIONAL NEURAL NETWORK WITH CUSTOM DATA
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
print ("Packages loaded")
Packages loaded
LOAD DATA
cwd = os.getcwd()
loadpath = cwd + "/data/custom_data.npz"
l = np.load(loadpath)
print (l.files)
trainimg = l['trainimg']
trainlabel = l['trainlabel']
testimg = l['testimg']
testlabel = l['testlabel']
imgsize = l['imgsize']
use_gray = l['use_gray']
ntrain = trainimg.shape[0]
nclass = trainlabel.shape[1]
dim = trainimg.shape[1]
ntest = testimg.shape[0]
print ("%d train images loaded" % (ntrain))
print ("%d test images loaded" % (ntest))
print ("%d dimensional input" % (dim))
print ("Image size is %s" % (imgsize))
print ("%d classes" % (nclass))
['trainlabel', 'imgsize', 'trainimg', 'testimg', 'testlabel', 'use_gray']
408 train images loaded
273 test images loaded
4096 dimensional input
Image size is [64 64]
4 classes
DEFINE NETWORK
tf.set_random_seed(0)
n_input = dim
n_output = nclass
if use_gray:
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 128], stddev=0.1)),
'wc2': tf.Variable(tf.random_normal([5, 5, 128, 128], stddev=0.1)),
'wd1': tf.Variable(tf.random_normal(
[(int)(imgsize[0]/4*imgsize[1]/4)*128, 128], stddev=0.1)),
'wd2': tf.Variable(tf.random_normal([128, n_output], stddev=0.1))
}
else:
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 3, 128], stddev=0.1)),
'wc2': tf.Variable(tf.random_normal([5, 5, 128, 128], stddev=0.1)),
'wd1': tf.Variable(tf.random_normal(
[(int)(imgsize[0]/4*imgsize[1]/4)*128, 128], stddev=0.1)),
'wd2': tf.Variable(tf.random_normal([128, n_output], stddev=0.1))
}
biases = {
'bc1': tf.Variable(tf.random_normal([128], stddev=0.1)),
'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)),
'bd1': tf.Variable(tf.random_normal([128], stddev=0.1)),
'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1))
}
def conv_basic(_input, _w, _b, _keepratio, _use_gray):
if _use_gray:
_input_r = tf.reshape(_input, shape=[-1, imgsize[0], imgsize[1], 1])
else:
_input_r = tf.reshape(_input, shape=[-1, imgsize[0], imgsize[1], 3])
_conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(_input_r
, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME'), _b['bc1']))
_pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1]
, strides=[1, 2, 2, 1], padding='SAME')
_pool_dr1 = tf.nn.dropout(_pool1, _keepratio)
_conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(_pool_dr1
, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME'), _b['bc2']))
_pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1]
, strides=[1, 2, 2, 1], padding='SAME')
_pool_dr2 = tf.nn.dropout(_pool2, _keepratio)
_dense1 = tf.reshape(_pool_dr2
, [-1, _w['wd1'].get_shape().as_list()[0]])
_fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1']))
_fc_dr1 = tf.nn.dropout(_fc1, _keepratio)
_out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2'])
out = {
'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1
, 'pool1_dr1': _pool_dr1, 'conv2': _conv2, 'pool2': _pool2
, 'pool_dr2': _pool_dr2, 'dense1': _dense1, 'fc1': _fc1
, 'fc_dr1': _fc_dr1, 'out': _out
}
return out
print ("NETWORK READY")
NETWORK READY
DEFINE FUNCTIONS
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output])
keepratio = tf.placeholder(tf.float32)
_pred = conv_basic(x, weights, biases, keepratio, use_gray)['out']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(_pred, y))
WEIGHT_DECAY_FACTOR = 0.0001
l2_loss = tf.add_n([tf.nn.l2_loss(v)
for v in tf.trainable_variables()])
cost = cost + WEIGHT_DECAY_FACTOR*l2_loss
optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
_corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1))
accr = tf.reduce_mean(tf.cast(_corr, tf.float32))
init = tf.initialize_all_variables()
print ("FUNCTIONS READY")
FUNCTIONS READY
OPTIMIZE
training_epochs = 400
batch_size = 100
display_step = 40
sess = tf.Session()
sess.run(init)
for epoch in range(training_epochs):
avg_cost = 0.
num_batch = int(ntrain/batch_size)+1
for i in range(num_batch):
randidx = np.random.randint(ntrain, size=batch_size)
batch_xs = trainimg[randidx, :]
batch_ys = trainlabel[randidx, :]
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys
, keepratio:0.7})
avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys
, keepratio:1.})/num_batch
if epoch % display_step == 0 or epoch == training_epochs-1:
print ("Epoch: %03d/%03d cost: %.9f" %
(epoch, training_epochs, avg_cost))
train_acc = sess.run(accr, feed_dict={x: batch_xs
, y: batch_ys, keepratio:1.})
print (" Training accuracy: %.3f" % (train_acc))
test_acc = sess.run(accr, feed_dict={x: testimg
, y: testlabel, keepratio:1.})
print (" Test accuracy: %.3f" % (test_acc))
print ("Optimization Finished!")
Epoch: 000/400 cost: 10.054866600
Training accuracy: 0.750
Test accuracy: 0.755
Epoch: 040/400 cost: 2.086551237
Training accuracy: 0.980
Test accuracy: 0.901
Epoch: 080/400 cost: 1.829108810
Training accuracy: 1.000
Test accuracy: 0.930
CLOSE SESSION
sess.close()
print ("Session closed.")