Image Recognition with TensorFlow

TensorFlow (TF) has been released and explored by Machine Learning (ML) researchers for a couple of years. Today I experiment the performance of TF on image recognition by using pixel color profiles and logistic regression (logit) to train and test CIFAR-10 images using the tutorial [1]. I found only a couple of errors that are fixed easily, so thank so much Wolfgang Beyer for a great blog. Thus, the plots below show that increasing the training steps do not help improve the accuracy of the model, and fortunately, we calculate training accuracy at every 100 steps to guide when to stop. A consequence of increasing steps is training time that grows approximately linearly.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf
import time
import data_helpers

beginTime = time.time()

# Parameter definitions
batch_size = 100
learning_rate = 0.005
max_steps = 2000

# Prepare data
data_sets = data_helpers.load_data()

# Define input placeholders
images_placeholder = tf.placeholder(tf.float32, shape=[None, 3072])
labels_placeholder = tf.placeholder(tf.int64, shape=[None])

# Define variables to be optimized
weights = tf.Variable(tf.zeros([3072, 10]))
biases = tf.Variable(tf.zeros([10]))

# Define classifier's result (logistic regression)
logits = tf.matmul(images_placeholder, weights) + biases

# Define loss function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels_placeholder, logits=logits))

# Define the training operation
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

# Operation comparing prediction with true label
correct_prediction = tf.equal(tf.argmax(logits, 1), labels_placeholder)

# Operation calculating the accuracy of our prediction
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# ------------------------------------------------------
# Run the TensorFlow graph
# ------------------------------------------------------
with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.initialize_all_variables())

    # Repeat max_steps times
    for i in range(max_steps):
        indices = np.random.choice(data_sets['images_train'].shape[0], batch_size)
        images_batch = data_sets['images_train'][indices]
        labels_batch = data_sets['labels_train'][indices]

        # Periodically print out the model's current accuracy
        if i % 100 == 0:
            train_accuracy = sess.run(accuracy, feed_dict={
                images_placeholder: images_batch, labels_placeholder: labels_batch})
            print('Step {:5d}: training accuracy {:g}'.format(i, train_accuracy))
        # Perform a single training step
        sess.run(train_step, feed_dict={images_placeholder: images_batch, labels_placeholder: labels_batch})
    
    # After finishing the training, evaluate the test set
    test_accuracy = sess.run(accuracy, feed_dict={
        images_placeholder: data_sets['images_test'],
        labels_placeholder: data_sets['labels_test']})
    print('Test accuracy {:g}'.format(test_accuracy))

endTime = time.time()
print('Total time: {:5.2f}s'.format(endTime - beginTime))

 

 

[1] http://www.wolfib.com/Image-Recognition-Intro-Part-1/