Traffic Sign Classification with Keras

Keras exists to make coding deep neural networks simpler. To demonstrate just how easy it is, you’re going to use Keras to build a convolutional neural network in a few dozen lines of code.

You’ll be connecting the concepts from the previous lessons to the methods that Keras provides.

Dataset

The network you'll build with Keras is similar to the example that you can find in Keras’s GitHub repository that builds out a convolutional neural network for MNIST.

However, instead of using the MNIST dataset, you're going to use the German Traffic Sign Recognition Benchmark dataset that you've used previously.

You can download pickle files with sanitized traffic sign data here.

Overview

Here are the steps you'll take to build the network:

  1. First load the training data and do a train/validation split.
  2. Preprocess data.
  3. Build a feedforward neural network to classify traffic signs.
  4. Build a convolutional neural network to classify traffic signs.
  5. Evaluate performance of final neural network on testing data.

Keep an eye on the network’s accuracy over time. Once the accuracy reaches the 98% range, you can be confident that you’ve built and trained an effective model.

In [1]:
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
import math

Load the Data

Start by importing the data from the pickle file.

In [83]:
# TODO: Implement load the data here.
with open('./data/train.p', 'rb') as f:
    data = pickle.load(f)
In [84]:
data['features'].shape
Out[84]:
(39209, 32, 32, 3)

Validate the Network

Split the training data into a training and validation set.

Measure the validation accuracy of the network after two training epochs.

Hint: Use the train_test_split() method from scikit-learn.

In [85]:
# TODO: Use `train_test_split` here.
X_train, X_val, y_train, y_val = train_test_split(data['features'], data['labels'], test_size=0.20, random_state=42)
In [86]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
assert(X_train.shape[0] == y_train.shape[0]), "The number of images is not equal to the number of labels."
assert(X_train.shape[1:] == (32,32,3)), "The dimensions of the images are not 32 x 32 x 3."
assert(X_val.shape[0] == y_val.shape[0]), "The number of images is not equal to the number of labels."
assert(X_val.shape[1:] == (32,32,3)), "The dimensions of the images are not 32 x 32 x 3."

Preprocess the Data

Now that you've loaded the training data, preprocess the data such that it's in the range between -0.5 and 0.5.

In [87]:
# TODO: Implement data normalization here.
def norm_data(X, range_max=0.5, range_min=-0.5):   
    X_min = np.min(X, axis=0)
    X_max = np.max(X, axis=0)
    X = (X - X_min) / (X_max - X_min)
    return X * (range_max - range_min) + range_min
X_train = norm_data(X_train)
X_val = norm_data(X_val)
In [88]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
assert(math.isclose(np.min(X_train), -0.5, abs_tol=1e-5) and math.isclose(np.max(X_train), 0.5, abs_tol=1e-5)), "The range of the training data is: %.1f to %.1f" % (np.min(X_train), np.max(X_train))
assert(math.isclose(np.min(X_val), -0.5, abs_tol=1e-5) and math.isclose(np.max(X_val), 0.5, abs_tol=1e-5)), "The range of the validation data is: %.1f to %.1f" % (np.min(X_val), np.max(X_val))

Build a Two-Layer Feedfoward Network

The code you've written so far is for data processing, not specific to Keras. Here you're going to build Keras-specific code.

Build a two-layer feedforward neural network, with 128 neurons in the fully-connected hidden layer.

To get started, review the Keras documentation about models and layers.

The Keras example of a Multi-Layer Perceptron network is similar to what you need to do here. Use that as a guide, but keep in mind that there are a number of differences.

In [64]:
# TODO: Build a two-layer feedforward neural network with Keras here.
from keras.models import Sequential
from keras.layers.core import Dense, Activation

model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(3072,)))
model.add(Dense(43))
model.add(Activation('softmax'))
In [65]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
dense_layers = []
for l in model.layers:
    if type(l) == Dense:
        dense_layers.append(l)
assert(len(dense_layers) == 2), "There should be 2 Dense layers."
d1 = dense_layers[0]
d2 = dense_layers[1]
assert(d1.input_shape == (None, 3072))
assert(d1.output_shape == (None, 128))
assert(d2.input_shape == (None, 128))
assert(d2.output_shape == (None, 43))

last_layer = model.layers[-1]
assert(last_layer.activation.__name__ == 'softmax'), "Last layer should be softmax activation, is {}.".format(last_layer.activation.__name__)
In [66]:
# Debugging
for l in model.layers:
    print(l.name, l.input_shape, l.output_shape, l.activation)
dense_9 (None, 3072) (None, 128) <function relu at 0x12b90e8c8>
dense_10 (None, 128) (None, 43) <function linear at 0x12b90eae8>
activation_3 (None, 43) (None, 43) <function softmax at 0x12b90e6a8>

Train the Network

Compile and train the network for 2 epochs. Use the adam optimizer, with categorical_crossentropy loss.

Hint 1: In order to use categorical cross entropy, you will need to one-hot encode the labels.

Hint 2: In order to pass the input images to the fully-connected hidden layer, you will need to reshape the input.

Hint 3: Keras's .fit() method returns a History.history object, which the tests below use. Save that to a variable named history.

In [67]:
# TODO: Compile and train the model here.
from keras.optimizers import Adam
from keras.utils import np_utils

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])
In [68]:
y_train = np_utils.to_categorical(y_train, 43)
y_val = np_utils.to_categorical(y_val, 43)
In [69]:
X_train = X_train.reshape(-1, 3072)
X_val = X_val.reshape(-1, 3072)
In [72]:
history = model.fit(X_train, y_train,
                    batch_size=128, nb_epoch=20,
                    verbose=1, validation_data=(X_val, y_val))
Train on 31367 samples, validate on 7842 samples
Epoch 1/20
31367/31367 [==============================] - 2s - loss: 0.1488 - acc: 0.9597 - val_loss: 0.3835 - val_acc: 0.8843
Epoch 2/20
31367/31367 [==============================] - 2s - loss: 0.1550 - acc: 0.9567 - val_loss: 0.7967 - val_acc: 0.8268
Epoch 3/20
31367/31367 [==============================] - 2s - loss: 0.2056 - acc: 0.9455 - val_loss: 0.3512 - val_acc: 0.9115
Epoch 4/20
31367/31367 [==============================] - 2s - loss: 0.1403 - acc: 0.9606 - val_loss: 0.2089 - val_acc: 0.9440
Epoch 5/20
31367/31367 [==============================] - 2s - loss: 0.1204 - acc: 0.9666 - val_loss: 0.2139 - val_acc: 0.9429
Epoch 6/20
31367/31367 [==============================] - 2s - loss: 0.1443 - acc: 0.9588 - val_loss: 0.2468 - val_acc: 0.9300
Epoch 7/20
31367/31367 [==============================] - 2s - loss: 0.1238 - acc: 0.9653 - val_loss: 0.4114 - val_acc: 0.8859
Epoch 8/20
31367/31367 [==============================] - 2s - loss: 0.1533 - acc: 0.9574 - val_loss: 0.2240 - val_acc: 0.9376
Epoch 9/20
31367/31367 [==============================] - 2s - loss: 0.1139 - acc: 0.9682 - val_loss: 0.2750 - val_acc: 0.9229
Epoch 10/20
31367/31367 [==============================] - 2s - loss: 0.1073 - acc: 0.9696 - val_loss: 0.2531 - val_acc: 0.9314
Epoch 11/20
31367/31367 [==============================] - 2s - loss: 0.1168 - acc: 0.9675 - val_loss: 0.3990 - val_acc: 0.8971
Epoch 12/20
31367/31367 [==============================] - 2s - loss: 0.1181 - acc: 0.9654 - val_loss: 0.2416 - val_acc: 0.9332
Epoch 13/20
31367/31367 [==============================] - 2s - loss: 0.1195 - acc: 0.9656 - val_loss: 0.4115 - val_acc: 0.8958
Epoch 14/20
31367/31367 [==============================] - 2s - loss: 0.1173 - acc: 0.9661 - val_loss: 0.2742 - val_acc: 0.9291
Epoch 15/20
31367/31367 [==============================] - 2s - loss: 0.1076 - acc: 0.9681 - val_loss: 0.3000 - val_acc: 0.9272
Epoch 16/20
31367/31367 [==============================] - 2s - loss: 0.1032 - acc: 0.9703 - val_loss: 0.2307 - val_acc: 0.9357
Epoch 17/20
31367/31367 [==============================] - 2s - loss: 0.1295 - acc: 0.9638 - val_loss: 0.2680 - val_acc: 0.9308
Epoch 18/20
31367/31367 [==============================] - 2s - loss: 0.0992 - acc: 0.9700 - val_loss: 0.2253 - val_acc: 0.9320
Epoch 19/20
31367/31367 [==============================] - 2s - loss: 0.1114 - acc: 0.9689 - val_loss: 0.2616 - val_acc: 0.9271
Epoch 20/20
31367/31367 [==============================] - 2s - loss: 0.0966 - acc: 0.9727 - val_loss: 0.3534 - val_acc: 0.9137
In [73]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
assert(history.history['acc'][-1] > 0.92), "The training accuracy was: %.3f" % history.history['acc'][-1]
assert(history.history['val_acc'][-1] > 0.9), "The validation accuracy is: %.3f" % history.history['val_acc'][-1]

Validation Accuracy: 0.9137

Congratulations

You've built a feedforward neural network in Keras!

Don't stop here! Next, you'll add a convolutional layer to drive.py.

Convolutions

Build a new network, similar to your existing network. Before the hidden layer, add a 3x3 convolutional layer with 32 filters and valid padding.

Then compile and train the network.

Hint 1: The Keras example of a convolutional neural network for MNIST would be a good example to review.

Hint 2: Now that the first layer of the network is a convolutional layer, you no longer need to reshape the input images before passing them to the network. You might need to reload your training data to recover the original shape.

Hint 3: Add a Flatten() layer between the convolutional layer and the fully-connected hidden layer.

In [94]:
from keras.layers import Convolution2D, Flatten
In [96]:
y_train = np_utils.to_categorical(y_train, 43)
y_val = np_utils.to_categorical(y_val, 43)
In [99]:
# TODO: Re-construct the network and add a convolutional layer before the first fully-connected layer.
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(32, 32, 3)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

# TODO: Compile and train the model here.

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])

history = model.fit(X_train, y_train,
                    batch_size=128, nb_epoch=4,
                    verbose=1, validation_data=(X_val, y_val))
Train on 31367 samples, validate on 7842 samples
Epoch 1/4
31367/31367 [==============================] - 24s - loss: 0.9126 - acc: 0.7682 - val_loss: 0.3572 - val_acc: 0.9061
Epoch 2/4
31367/31367 [==============================] - 25s - loss: 0.2107 - acc: 0.9465 - val_loss: 0.2438 - val_acc: 0.9411
Epoch 3/4
31367/31367 [==============================] - 26s - loss: 0.1222 - acc: 0.9687 - val_loss: 0.1861 - val_acc: 0.9561
Epoch 4/4
31367/31367 [==============================] - 26s - loss: 0.0832 - acc: 0.9793 - val_loss: 0.1706 - val_acc: 0.9617
In [100]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
assert(history.history['val_acc'][-1] > 0.95), "The validation accuracy is: %.3f" % history.history['val_acc'][-1]

Validation Accuracy: 0.9617

Pooling

Re-construct your network and add a 2x2 pooling layer immediately following your convolutional layer.

Then compile and train the network.

In [101]:
from keras.layers.pooling import MaxPooling2D
In [102]:
# TODO: Re-construct the network and add a pooling layer after the convolutional layer.

model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=None, border_mode='valid'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

# TODO: Compile and train the model here.

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])

history = model.fit(X_train, y_train,
                    batch_size=128, nb_epoch=4,
                    verbose=1, validation_data=(X_val, y_val))
Train on 31367 samples, validate on 7842 samples
Epoch 1/4
31367/31367 [==============================] - 16s - loss: 1.2208 - acc: 0.6880 - val_loss: 0.4505 - val_acc: 0.8898
Epoch 2/4
31367/31367 [==============================] - 16s - loss: 0.2886 - acc: 0.9332 - val_loss: 0.2456 - val_acc: 0.9416
Epoch 3/4
31367/31367 [==============================] - 17s - loss: 0.1561 - acc: 0.9632 - val_loss: 0.1970 - val_acc: 0.9509
Epoch 4/4
31367/31367 [==============================] - 18s - loss: 0.1010 - acc: 0.9775 - val_loss: 0.1549 - val_acc: 0.9656
In [103]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
assert(history.history['val_acc'][-1] > 0.95), "The validation accuracy is: %.3f" % history.history['val_acc'][-1]

Validation Accuracy: 0.9656

Dropout

Re-construct your network and add dropout after the pooling layer. Set the dropout rate to 50%.

In [104]:
# TODO: Re-construct the network and add dropout after the pooling layer.
from keras.layers import Dropout

model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=None, border_mode='valid'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

# TODO: Compile and train the model here.

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])

history = model.fit(X_train, y_train,
                    batch_size=128, nb_epoch=4,
                    verbose=1, validation_data=(X_val, y_val))
Train on 31367 samples, validate on 7842 samples
Epoch 1/4
31367/31367 [==============================] - 18s - loss: 1.3831 - acc: 0.6357 - val_loss: 0.5173 - val_acc: 0.8669
Epoch 2/4
31367/31367 [==============================] - 18s - loss: 0.3799 - acc: 0.9024 - val_loss: 0.2737 - val_acc: 0.9399
Epoch 3/4
31367/31367 [==============================] - 18s - loss: 0.2331 - acc: 0.9409 - val_loss: 0.2021 - val_acc: 0.9532
Epoch 4/4
31367/31367 [==============================] - 18s - loss: 0.1668 - acc: 0.9571 - val_loss: 0.1567 - val_acc: 0.9654
In [105]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
assert(history.history['val_acc'][-1] > 0.95), "The validation accuracy is: %.3f" % history.history['val_acc'][-1]

Validation Accuracy: 0.9654

Optimization

Congratulations! You've built a neural network with convolutions, pooling, dropout, and fully-connected layers, all in just a few lines of code.

Have fun with the model and see how well you can do! Add more layers, or regularization, or different padding, or batches, or more training epochs.

What is the best validation accuracy you can achieve?

In [ ]:
 

Best Validation Accuracy: (fill in here)

Testing

Once you've picked out your best model, it's time to test it.

Load up the test data and use the evaluate() method to see how well it does.

Hint 1: The evaluate() method should return an array of numbers. Use the metrics_names() method to get the labels.

In [106]:
# TODO: Load test data
with open('./data/test.p', 'rb') as f:
    test_data = pickle.load(f)
    
# TODO: Preprocess data & one-hot encode the labels
X_test = norm_data(test_data['features'])
y_test = np_utils.to_categorical(test_data['labels'])

# TODO: Evaluate model on test data
test_eval = model.evaluate(X_test, y_test)
12630/12630 [==============================] - 2s     
In [111]:
model.metrics_names
Out[111]:
['loss', 'acc']
In [109]:
test_eval
Out[109]:
[0.53311918069706588, 0.87965162308180234]

Test Accuracy: 0.8796

Summary

Keras is a great tool to use if you want to quickly build a neural network and evaluate performance.