Week 2 Progress Update

Week 2 Progress Update.

        I spent the majority of this week learning how to use the TensorFlow Framework. In order to do this I built a very simple neural network with it. All this neural network does is sort 28 x 28 pictures of street signs into 62 different categories, below is the code, the sections that are commented out (surrounded by ''' or proceeded by #) are either notes or remnants of the tutorial I was following. Below the code is an image which randomly samples some of the signs in the testing dataset once the network has been trained. It then attempts to predict what category it belongs to, if the predicted category matches the actual category it shows green text, if not, it shows red text.



# imports
import tensorflow as tf
import os
import skimage
import numpy as np
import matplotlib.pyplot as plt
import random as r
from skimage import transform


'''

#init constants
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])

#multiply
result = tf.multiply(x1,x2)

#
#Session Run Option 1
#

#session
sess = tf.Session()

#print
print(sess.run(result))


#close sess
sess.close()


#
#Session Option 2
#

with tf.Session() as sess:
    output = sess.run(result)
    print(output)


'''
###
# parse data
###

def load_data(data_directory):
    #search root dir for new directories and put into var "directories"
    directories = [d for d in os.listdir(data_directory)
                   if os.path.isdir(os.path.join(data_directory, d))]
    #init lists
    labels = []
    images = []
    #search for image files (ppm extension) in found directories
    for d in directories:
        label_directory = os.path.join(data_directory, d)
        file_names = [os.path.join(label_directory, f)
                      for f in os.listdir(label_directory)
                      if f.endswith(".ppm")]
        #add image files to files list and labels to label list
        for f in file_names:
            images.append(skimage.data.imread(f))
            labels.append(int(d))
    return images, labels

ROOT_PATH = "C:/users/emmet/Desktop/python/NNresources"
train_data_directory = os.path.join(ROOT_PATH, "TrafficSigns/Training")
test_data_directory = os.path.join(ROOT_PATH, "TrafficSigns/Testing")

images, labels = load_data(train_data_directory)

'''
plt.hist(labels, 62)
plt.show()
'''


images28 = [transform.resize(image, (28,28)) for image in images]
images28 = np.array(images28)
images28 = skimage.color.rgb2gray(images28)


traffic_signs = [r.randint(0,4574),r.randint(0,4574),r.randint(0,4574),r.randint(0,4574)]
'''
for i in range(len(traffic_signs)):
    plt.subplot(1,4,i+1)
    plt.axis('off')
    plt.imshow(images28[traffic_signs[i]], cmap="gray")
    plt.subplots_adjust(wspace=0.5)

plt.show()
'''

#####
## Model
#####

#placeholders
x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
y = tf.placeholder(dtype = tf.int32, shape = [None])

#Flatten
images_flat = tf.contrib.layers.flatten(x)

#Fully Connected Layer
logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)

#Loss Function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,
                                                                     logits = logits))
#Optimizer
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

#equate logits to labels
correct_pred = tf.argmax(logits, 1)

#Define an accuracy metric
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

print("images_flat: ", images_flat)
print("logits: ", logits)
print("loss: ", loss)
print("predicted_labels: ", correct_pred)

tf.set_random_seed(1234)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

for i in range(201):
    print('EPOCH', i)

    _, accuracy_val, loss_val = sess.run([train_op, accuracy, loss], feed_dict={x: images28, y: labels})
    if i % 10 == 0:
        print("Loss:", loss_val)
    print('Done with Epoch')

sample_indexes = r.sample(range(len(images28)), 10)
sample_images = [images28[i] for i in sample_indexes]
sample_labels = [labels[i] for i in sample_indexes]

predicted = sess.run([correct_pred], feed_dict = {x: sample_images})[0]

sess.close()
print(sample_labels)
print(predicted)

fig = plt.figure(figsize = (10,10))
for i in range(len(sample_images)):
    truth = sample_labels[i]
    prediction = predicted[i]
    plt.subplot(5, 2, 1+i)
    plt.axis('off')
    color = 'green' if truth == prediction else 'red'
    plt.text(40, 10, "Truth: {0}\nPrediction:{1}".format(truth, prediction),
             fontsize=12, color=color)
    plt.imshow(sample_images[i], cmap = "gray")
plt.show()

 

Comments