Image features exercise

(Adapted from Stanford University's CS231n Open Courseware)

Complete and hand in this completed worksheet (including its outputs and any supporting code outside of the worksheet) with your assignment submission. For more details see the HW page on the course website.

We have seen that we can achieve reasonable performance on an image classification task by training a linear classifier on the pixels of the input image. In this exercise we will show that we can improve our classification performance by training linear classifiers not on raw pixels but on features that are computed from the raw pixels.

All of your work for this exercise will be done in this notebook.


In [1]:
import random
import numpy as np
from cs231n.data_utils import load_CIFAR10
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# for auto-reloading extenrnal modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2

In [2]:
# Load the CIFAR10 data
from cs231n.features import color_histogram_hsv, hog_feature

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
  # Load the raw CIFAR-10 data
  cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
  X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
  
  # Subsample the data
  mask = range(num_training, num_training + num_validation)
  X_val = X_train[mask]
  y_val = y_train[mask]
  mask = range(num_training)
  X_train = X_train[mask]
  y_train = y_train[mask]
  mask = range(num_test)
  X_test = X_test[mask]
  y_test = y_test[mask]

  return X_train, y_train, X_val, y_val, X_test, y_test

X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()

In [3]:
from cs231n.features import *

# Extract features. For each image we will compute a Histogram of Oriented
# Gradients (HOG) as well as a color histogram using the hue channel in HSV
# color space. We form our final feature vector for each image by concatenating
# the HOG and color histogram feature vectors.
#
# Roughly speaking, HOG should capture the texture of the image while ignoring
# color information, and the color histogram represents the color of the input
# image while ignoring texture. As a result, we expect that using both together
# ought to work better than using either alone. Verifying this assumption would
# be a good thing to try for the bonus section.

# The hog_feature and color_histogram_hsv functions both operate on a single
# image and return a feature vector for that image. The extract_features
# function takes a set of images and a list of feature functions and evaluates
# each feature function on each image, storing the results in a matrix where
# each column is the concatenation of all feature vectors for a single image.

num_color_bins = 10 # Number of bins in the color histogram
feature_fns = [hog_feature, lambda img: color_histogram_hsv(img, nbin=num_color_bins)]
X_train_feats = extract_features(X_train, feature_fns, verbose=True)
X_val_feats = extract_features(X_val, feature_fns)
X_test_feats = extract_features(X_test, feature_fns)

# Preprocessing: Subtract the mean feature
mean_feat = np.mean(X_train_feats, axis=1)
mean_feat = np.expand_dims(mean_feat, axis=1)
X_train_feats -= mean_feat
X_val_feats -= mean_feat
X_test_feats -= mean_feat

# Preprocessing: Divide by standard deviation. This ensures that each feature
# has roughly the same scale.
std_feat = np.std(X_train_feats, axis=1)
std_feat = np.expand_dims(std_feat, axis=1)
X_train_feats /= std_feat
X_val_feats /= std_feat
X_test_feats /= std_feat

# Preprocessing: Add a bias dimension
X_train_feats = np.vstack([X_train_feats, np.ones((1, X_train_feats.shape[1]))])
X_val_feats = np.vstack([X_val_feats, np.ones((1, X_val_feats.shape[1]))])
X_test_feats = np.vstack([X_test_feats, np.ones((1, X_test_feats.shape[1]))])


Done extracting features for 1000 / 49000 images
Done extracting features for 2000 / 49000 images
Done extracting features for 3000 / 49000 images
Done extracting features for 4000 / 49000 images
Done extracting features for 5000 / 49000 images
Done extracting features for 6000 / 49000 images
Done extracting features for 7000 / 49000 images
Done extracting features for 8000 / 49000 images
Done extracting features for 9000 / 49000 images
Done extracting features for 10000 / 49000 images
Done extracting features for 11000 / 49000 images
Done extracting features for 12000 / 49000 images
Done extracting features for 13000 / 49000 images
Done extracting features for 14000 / 49000 images
Done extracting features for 15000 / 49000 images
Done extracting features for 16000 / 49000 images
Done extracting features for 17000 / 49000 images
Done extracting features for 18000 / 49000 images
Done extracting features for 19000 / 49000 images
Done extracting features for 20000 / 49000 images
Done extracting features for 21000 / 49000 images
Done extracting features for 22000 / 49000 images
Done extracting features for 23000 / 49000 images
Done extracting features for 24000 / 49000 images
Done extracting features for 25000 / 49000 images
Done extracting features for 26000 / 49000 images
Done extracting features for 27000 / 49000 images
Done extracting features for 28000 / 49000 images
Done extracting features for 29000 / 49000 images
Done extracting features for 30000 / 49000 images
Done extracting features for 31000 / 49000 images
Done extracting features for 32000 / 49000 images
Done extracting features for 33000 / 49000 images
Done extracting features for 34000 / 49000 images
Done extracting features for 35000 / 49000 images
Done extracting features for 36000 / 49000 images
Done extracting features for 37000 / 49000 images
Done extracting features for 38000 / 49000 images
Done extracting features for 39000 / 49000 images
Done extracting features for 40000 / 49000 images
Done extracting features for 41000 / 49000 images
Done extracting features for 42000 / 49000 images
Done extracting features for 43000 / 49000 images
Done extracting features for 44000 / 49000 images
Done extracting features for 45000 / 49000 images
Done extracting features for 46000 / 49000 images
Done extracting features for 47000 / 49000 images
Done extracting features for 48000 / 49000 images

In [4]:
# Use the validation set to tune the learning rate and regularization strength

from cs231n.classifiers.linear_classifier import LinearSVM

learning_rates = np.arange(5)*2e-8+1.3e-7#[5e-8, 1e-7, 5e-7]
regularization_strengths = np.arange(5)*1e4+8.3e4#[1e5, 5e5, 1e6]

results = {}
best_val = -1
best_svm = None
best_stats = None

################################################################################
# TODO:                                                                        #
# Use the validation set to set the learning rate and regularization strength. #
# This should be identical to the validation that you did for the SVM; save    #
# the best trained softmax classifer in best_svm. You might also want to play  #
# with different numbers of bins in the color histogram. If you are careful    #
# you should be able to get accuracy of near 0.44 on the validation set.       #
################################################################################
for lr in learning_rates:
    for rs in regularization_strengths:
        smc = LinearSVM()
        stats=smc.train(X_train_feats, y_train, lr, rs, 1000)
        results[(lr, rs)] = (np.mean(smc.predict(X_train_feats)==y_train),
                             np.mean(smc.predict(X_val_feats)==y_val))
        if results[(lr,rs)][1]>best_val:
            best_val = results[(lr,rs)][1]
            best_svm = smc
            best_stats = stats
            print best_val
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print 'lr %e reg %e train accuracy: %f val accuracy: %f' % (
                lr, reg, train_accuracy, val_accuracy)
    
print 'best validation accuracy achieved during cross-validation: %f' % best_val
plt.plot(best_stats)
plt.show()


0.413
0.416
0.419
0.423
lr 1.300000e-07 reg 8.300000e+04 train accuracy: 0.413510 val accuracy: 0.413000
lr 1.300000e-07 reg 9.300000e+04 train accuracy: 0.415265 val accuracy: 0.416000
lr 1.300000e-07 reg 1.030000e+05 train accuracy: 0.411102 val accuracy: 0.402000
lr 1.300000e-07 reg 1.130000e+05 train accuracy: 0.415286 val accuracy: 0.419000
lr 1.300000e-07 reg 1.230000e+05 train accuracy: 0.414878 val accuracy: 0.419000
lr 1.500000e-07 reg 8.300000e+04 train accuracy: 0.412939 val accuracy: 0.413000
lr 1.500000e-07 reg 9.300000e+04 train accuracy: 0.411878 val accuracy: 0.414000
lr 1.500000e-07 reg 1.030000e+05 train accuracy: 0.406857 val accuracy: 0.405000
lr 1.500000e-07 reg 1.130000e+05 train accuracy: 0.414286 val accuracy: 0.417000
lr 1.500000e-07 reg 1.230000e+05 train accuracy: 0.414857 val accuracy: 0.423000
lr 1.700000e-07 reg 8.300000e+04 train accuracy: 0.415551 val accuracy: 0.423000
lr 1.700000e-07 reg 9.300000e+04 train accuracy: 0.416306 val accuracy: 0.420000
lr 1.700000e-07 reg 1.030000e+05 train accuracy: 0.416143 val accuracy: 0.419000
lr 1.700000e-07 reg 1.130000e+05 train accuracy: 0.415776 val accuracy: 0.420000
lr 1.700000e-07 reg 1.230000e+05 train accuracy: 0.413347 val accuracy: 0.416000
lr 1.900000e-07 reg 8.300000e+04 train accuracy: 0.415367 val accuracy: 0.416000
lr 1.900000e-07 reg 9.300000e+04 train accuracy: 0.408286 val accuracy: 0.403000
lr 1.900000e-07 reg 1.030000e+05 train accuracy: 0.415510 val accuracy: 0.410000
lr 1.900000e-07 reg 1.130000e+05 train accuracy: 0.419000 val accuracy: 0.422000
lr 1.900000e-07 reg 1.230000e+05 train accuracy: 0.413306 val accuracy: 0.421000
lr 2.100000e-07 reg 8.300000e+04 train accuracy: 0.414245 val accuracy: 0.418000
lr 2.100000e-07 reg 9.300000e+04 train accuracy: 0.412531 val accuracy: 0.406000
lr 2.100000e-07 reg 1.030000e+05 train accuracy: 0.413551 val accuracy: 0.418000
lr 2.100000e-07 reg 1.130000e+05 train accuracy: 0.418571 val accuracy: 0.423000
lr 2.100000e-07 reg 1.230000e+05 train accuracy: 0.412265 val accuracy: 0.414000
best validation accuracy achieved during cross-validation: 0.423000

In [5]:
# Evaluate your classifier on the test set
y_test_pred = best_svm.predict(X_test_feats)
test_accuracy = np.mean(y_test == y_test_pred)
print test_accuracy


0.429

In [6]:
# An important way to gain intuition about how an algorithm works is to
# visualize the mistakes that it makes. In this visualization, we show examples
# of images that are misclassified by our current system. The first column
# shows images that our system labeled as "plane" but whose true label is
# something other than "plane".

examples_per_class = 8
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
for cls, cls_name in enumerate(classes):
    idxs = np.where((y_test != cls) & (y_test_pred == cls))[0]
    idxs = np.random.choice(idxs, examples_per_class, replace=False)
    for i, idx in enumerate(idxs):
        plt.subplot(examples_per_class, len(classes), i * len(classes) + cls + 1)
        plt.imshow(X_test[idx].astype('uint8'))
        plt.axis('off')
        if i == 0:
            plt.title(cls_name)
plt.show()


Inline question 1:

Describe the misclassification results that you see. Do they make sense?

They do not make sense to human eye, but if we interpret them considering our futures, they all have similar texture and color distributions within their class.

Bonus: Design your own features!

You have seen that simple image features can improve classification performance. So far we have tried HOG and color histograms, but other types of features may be able to achieve even better classification performance.

For bonus points, design and implement a new type of feature and use it for image classification on CIFAR-10. Explain how your feature works and why you expect it to be useful for image classification. Implement it in this notebook, cross-validate any hyperparameters, and compare its performance to the HOG + Color histogram baseline.