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 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:


In [2]:
from urllib.request import urlretrieve
from os.path import isfile
from tqdm import tqdm

class DLProgress(tqdm):
    last_block = 0

    def hook(self, block_num=1, block_size=1, total_size=None):
        self.total = total_size
        self.update((block_num - self.last_block) * block_size)
        self.last_block = block_num

if not isfile('train.p'):
    with DLProgress(unit='B', unit_scale=True, miniters=1, desc='Train Dataset') as pbar:
        urlretrieve(
            'https://s3.amazonaws.com/udacity-sdc/datasets/german_traffic_sign_benchmark/train.p',
            'train.p',
            pbar.hook)

if not isfile('test.p'):
    with DLProgress(unit='B', unit_scale=True, miniters=1, desc='Test Dataset') as pbar:
        urlretrieve(
            'https://s3.amazonaws.com/udacity-sdc/datasets/german_traffic_sign_benchmark/test.p',
            'test.p',
            pbar.hook)

print('Training and Test data downloaded.')


Training and Test data downloaded.

Overview

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

  1. Load the training data.
  2. Preprocess the data.
  3. Build a feedforward neural network to classify traffic signs.
  4. Build a convolutional neural network to classify traffic signs.
  5. Evaluate the 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
import math

# Fix error with TF and Keras
import tensorflow as tf
tf.python.control_flow_ops = tf

print('Modules loaded.')


Modules loaded.

Load the Data

Start by importing the data from the pickle file.


In [5]:
"""
Commented out to test CIFAR10 dataset

with open('train.p', 'rb') as f:
    data = pickle.load(f)

# TODO: Load the feature data to the variable X_train
X_train = data['features']

# TODO: Load the label data to the variable y_train
y_train = data['labels']
"""


from keras.datasets import cifar10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

print("Complete")


Complete

In [5]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
assert np.array_equal(X_train, data['features']), 'X_train not set to data[\'features\'].'
assert np.array_equal(y_train, data['labels']), 'y_train not set to data[\'labels\'].'
print('Tests passed.')


Tests passed.

Preprocess the Data

  1. Shuffle the data
  2. Normalize the features using Min-Max scaling between -0.5 and 0.5
  3. One-Hot Encode the labels

Shuffle the data

Hint: You can use the scikit-learn shuffle function to shuffle the data.


In [6]:
# TODO: Shuffle the data
from sklearn.utils import shuffle
X_train, y_train = shuffle(X_train, y_train)

In [7]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
assert X_train.shape == data['features'].shape, 'X_train has changed shape. The shape shouldn\'t change when shuffling.'
assert y_train.shape == data['labels'].shape, 'y_train has changed shape. The shape shouldn\'t change when shuffling.'
assert not np.array_equal(X_train, data['features']), 'X_train not shuffled.'
assert not np.array_equal(y_train, data['labels']), 'y_train not shuffled.'
print('Tests passed.')


Tests passed.

Normalize the features

Hint: You solved this in TensorFlow lab Problem 1.


In [7]:
# TODO: Normalize the data features to the variable X_normalized

def normalize_to_grayscale(data):
    a = -.5
    b = .5
    grayscale_min = 0
    grayscale_max = 255
    return a + (((data - grayscale_min) * (b-a) ) / (grayscale_max - grayscale_min) )
    

X_normalized = normalize_to_grayscale(X_train)

In [9]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
assert math.isclose(np.min(X_normalized), -0.5, abs_tol=1e-5) and math.isclose(np.max(X_normalized), 0.5, abs_tol=1e-5), 'The range of the training data is: {} to {}.  It must be -0.5 to 0.5'.format(np.min(X_normalized), np.max(X_normalized))
print('Tests passed.')


Tests passed.

One-Hot Encode the labels

Hint: You can use the scikit-learn LabelBinarizer function to one-hot encode the labels.


In [8]:
# TODO: One Hot encode the labels to the variable y_one_hot
from sklearn.preprocessing import LabelBinarizer

label_binarizer = LabelBinarizer()
y_one_hot = label_binarizer.fit_transform(y_train)

In [11]:
# STOP: Do not change the tests below. Your implementation should pass these tests. 
import collections

assert y_one_hot.shape == (39209, 43), 'y_one_hot is not the correct shape.  It\'s {}, it should be (39209, 43)'.format(y_one_hot.shape)
assert next((False for y in y_one_hot if collections.Counter(y) != {0: 42, 1: 1}), True), 'y_one_hot not one-hot encoded.'
print('Tests passed.')


Tests passed.

Keras Sequential Model

from keras.models import Sequential

# Create the Sequential model
model = Sequential()

The keras.models.Sequential class is a wrapper for the neural network model. Just like many of the class models in scikit-learn, it provides common functions like fit(), evaluate(), and compile(). We'll cover these functions as we get to them. Let's start looking at the layers of the model.

Keras Layer

A Keras layer is just like a neural network layer. It can be fully connected, max pool, activation, etc. You can add a layer to the model using the model's add() function. For example, a simple model would look like this:

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten

# Create the Sequential model
model = Sequential()

# 1st Layer - Add a flatten layer
model.add(Flatten(input_shape=(32, 32, 3)))

# 2nd Layer - Add a fully connected layer
model.add(Dense(100))

# 3rd Layer - Add a ReLU activation layer
model.add(Activation('relu'))

# 4th Layer - Add a fully connected layer
model.add(Dense(60))

# 5th Layer - Add a ReLU activation layer
model.add(Activation('relu'))

Keras will automatically infer the shape of all layers after the first layer. This means you only have to set the input dimensions for the first layer.

The first layer from above, model.add(Flatten(input_shape=(32, 32, 3))), sets the input dimension to (32, 32, 3) and output dimension to (3072=32*32*3). The second layer takes in the output of the first layer and sets the output dimenions to (100). This chain of passing output to the next layer continues until the last layer, which is the output of the model.

Build a Multi-Layer Feedforward Network

Build a multi-layer feedforward neural network to classify the traffic sign images.

  1. Set the first layer to a Flatten layer with the input_shape set to (32, 32, 3)
  2. Set the second layer to Dense layer width to 128 output.
  3. Use a ReLU activation function after the second layer.
  4. Set the output layer width to 43, since there are 43 classes in the dataset.
  5. Use a softmax activation function after the output 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 [23]:
from keras.models import Sequential
model = Sequential()
# TODO: Build a Multi-layer feedforward neural network with Keras here.

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten

model.add(Flatten(input_shape=(32, 32, 3)))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

In [24]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
from keras.layers.core import Dense, Activation, Flatten
from keras.activations import relu, softmax

def check_layers(layers, true_layers):
    assert len(true_layers) != 0, 'No layers found'
    for layer_i in range(len(layers)):
        assert isinstance(true_layers[layer_i], layers[layer_i]), 'Layer {} is not a {} layer'.format(layer_i+1, layers[layer_i].__name__)
    assert len(true_layers) == len(layers), '{} layers found, should be {} layers'.format(len(true_layers), len(layers))

check_layers([Flatten, Dense, Activation, Dense, Activation], model.layers)

assert model.layers[0].input_shape == (None, 32, 32, 3), 'First layer input shape is wrong, it should be (32, 32, 3)'
assert model.layers[1].output_shape == (None, 128), 'Second layer output is wrong, it should be (128)'
assert model.layers[2].activation == relu, 'Third layer not a relu activation layer'
assert model.layers[3].output_shape == (None, 43), 'Fourth layer output is wrong, it should be (43)'
assert model.layers[4].activation == softmax, 'Fifth layer not a softmax activation layer'
print('Tests passed.')


Tests passed.

Training a Sequential Model

You built a multi-layer neural network in Keras, now let's look at training a neural network.

from keras.models import Sequential
from keras.layers.core import Dense, Activation

model = Sequential()
...

# Configures the learning process and metrics
model.compile('sgd', 'mean_squared_error', ['accuracy'])

# Train the model
# History is a record of training loss and metrics
history = model.fit(x_train_data, Y_train_data, batch_size=128, nb_epoch=2, validation_split=0.2)

# Calculate test score
test_score = model.evaluate(x_test_data, Y_test_data)

The code above configures, trains, and tests the model. The line model.compile('sgd', 'mean_squared_error', ['accuracy']) configures the model's optimizer to 'sgd'(stochastic gradient descent), the loss to 'mean_squared_error', and the metric to 'accuracy'.

You can find more optimizers here, loss functions here, and more metrics here.

To train the model, use the fit() function as shown in model.fit(x_train_data, Y_train_data, batch_size=128, nb_epoch=2, validation_split=0.2). The validation_split parameter will split a percentage of the training dataset to be used to validate the model. The model can be further tested with the test dataset using the evaluation() function as shown in the last line.

Train the Network

  1. Compile the network using adam optimizer and categorical_crossentropy loss function.
  2. Train the network for ten epochs and validate with 20% of the training data.

In [27]:
# Compile model using Stochastic gradient descent, mean squared error for loss
# and a target metric of accuracy

model.compile('adam', 'categorical_crossentropy', ['accuracy'])

history = model.fit(X_normalized, y_one_hot, nb_epoch=10, validation_split=.2)


Train on 31367 samples, validate on 7842 samples
Epoch 1/10
31367/31367 [==============================] - 7s - loss: 1.4924 - acc: 0.5984 - val_loss: 0.7442 - val_acc: 0.7934
Epoch 2/10
31367/31367 [==============================] - 7s - loss: 0.6880 - acc: 0.8046 - val_loss: 0.5576 - val_acc: 0.8458
Epoch 3/10
31367/31367 [==============================] - 7s - loss: 0.4966 - acc: 0.8591 - val_loss: 0.4889 - val_acc: 0.8554
Epoch 4/10
31367/31367 [==============================] - 7s - loss: 0.4250 - acc: 0.8798 - val_loss: 0.3754 - val_acc: 0.8865
Epoch 5/10
31367/31367 [==============================] - 7s - loss: 0.3831 - acc: 0.8895 - val_loss: 0.3671 - val_acc: 0.8897
Epoch 6/10
31367/31367 [==============================] - 6s - loss: 0.3184 - acc: 0.9081 - val_loss: 0.3627 - val_acc: 0.9037
Epoch 7/10
31367/31367 [==============================] - 6s - loss: 0.3015 - acc: 0.9136 - val_loss: 0.3190 - val_acc: 0.9081
Epoch 8/10
31367/31367 [==============================] - 8s - loss: 0.2814 - acc: 0.9180 - val_loss: 0.3681 - val_acc: 0.8930
Epoch 9/10
31367/31367 [==============================] - 6s - loss: 0.2579 - acc: 0.9256 - val_loss: 0.3035 - val_acc: 0.9139
Epoch 10/10
31367/31367 [==============================] - 6s - loss: 0.2655 - acc: 0.9229 - val_loss: 0.3605 - val_acc: 0.8967

In [28]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
from keras.optimizers import Adam

assert model.loss == 'categorical_crossentropy', 'Not using categorical_crossentropy loss function'
assert isinstance(model.optimizer, Adam), 'Not using adam optimizer'
assert len(history.history['acc']) == 10, 'You\'re using {} epochs when you need to use 10 epochs.'.format(len(history.history['acc']))

assert history.history['acc'][-1] > 0.92, 'The training accuracy was: %.3f. It shoud be greater than 0.92' % history.history['acc'][-1]
assert history.history['val_acc'][-1] > 0.85, 'The validation accuracy is: %.3f. It shoud be greater than 0.85' % history.history['val_acc'][-1]
print('Tests passed.')


Tests passed.

Convolutions

  1. Re-construct the previous network
  2. Add a convolutional layer with 32 filters, a 3x3 kernel, and valid padding before the flatten layer.
  3. Add a ReLU activation after the convolutional layer.

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


In [31]:
# TODO: Re-construct the network and add a convolutional layer before the flatten layer.

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten
from keras.layers.convolutional import Convolution2D

model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(32, 32, 3)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

In [32]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
from keras.layers.core import Dense, Activation, Flatten
from keras.layers.convolutional import Convolution2D

check_layers([Convolution2D, Activation, Flatten, Dense, Activation, Dense, Activation], model.layers)

assert model.layers[0].input_shape == (None, 32, 32, 3), 'First layer input shape is wrong, it should be (32, 32, 3)'
assert model.layers[0].nb_filter == 32, 'Wrong number of filters, it should be 32'
assert model.layers[0].nb_col == model.layers[0].nb_row == 3, 'Kernel size is wrong, it should be a 3x3'
assert model.layers[0].border_mode == 'valid', 'Wrong padding, it should be valid'

model.compile('adam', 'categorical_crossentropy', ['accuracy'])
history = model.fit(X_normalized, y_one_hot, batch_size=128, nb_epoch=2, validation_split=0.2)
assert(history.history['val_acc'][-1] > 0.91), "The validation accuracy is: %.3f.  It should be greater than 0.91" % history.history['val_acc'][-1]
print('Tests passed.')


Train on 31367 samples, validate on 7842 samples
Epoch 1/2
31367/31367 [==============================] - 4s - loss: 1.1119 - acc: 0.7156 - val_loss: 0.4181 - val_acc: 0.8838
Epoch 2/2
31367/31367 [==============================] - 4s - loss: 0.2664 - acc: 0.9345 - val_loss: 0.2221 - val_acc: 0.9461
Tests passed.

Pooling

  1. Re-construct the network
  2. Add a 2x2 max pooling layer immediately following your convolutional layer.

In [38]:
# TODO: Re-construct the network and add a pooling layer after the convolutional layer.

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten 
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

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

In [39]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
from keras.layers.core import Dense, Activation, Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

check_layers([Convolution2D, MaxPooling2D, Activation, Flatten, Dense, Activation, Dense, Activation], model.layers)
assert model.layers[1].pool_size == (2, 2), 'Second layer must be a max pool layer with pool size of 2x2'

model.compile('adam', 'categorical_crossentropy', ['accuracy'])
history = model.fit(X_normalized, y_one_hot, batch_size=128, nb_epoch=2, validation_split=0.2)
assert(history.history['val_acc'][-1] > 0.91), "The validation accuracy is: %.3f.  It should be greater than 0.91" % history.history['val_acc'][-1]
print('Tests passed.')


Train on 31367 samples, validate on 7842 samples
Epoch 1/2
31367/31367 [==============================] - 4s - loss: 1.4806 - acc: 0.6123 - val_loss: 0.6191 - val_acc: 0.8360
Epoch 2/2
31367/31367 [==============================] - 3s - loss: 0.4059 - acc: 0.9033 - val_loss: 0.2896 - val_acc: 0.9313
Tests passed.

Dropout

  1. Re-construct the network
  2. Add a dropout layer after the pooling layer. Set the dropout rate to 50%.

In [46]:
# TODO: Re-construct the network and add dropout after the pooling layer.

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten, Dropout 
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

model = Sequential()

model.add(Convolution2D(32, 3, 3, input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2,2), border_mode='valid'))
model.add(Dropout(.55))

model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(43))
model.add(Activation('softmax'))

In [47]:
# STOP: Do not change the tests below. Your implementation should pass these tests.
from keras.layers.core import Dense, Activation, Flatten, Dropout
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

check_layers([Convolution2D, MaxPooling2D, Dropout, Activation, Flatten, Dense, Activation, Dense, Activation], model.layers)
assert model.layers[2].p == 0.55, 'Third layer should be a Dropout of 50%'

model.compile('adam', 'categorical_crossentropy', ['accuracy'])
history = model.fit(X_normalized, y_one_hot, batch_size=128, nb_epoch=2, validation_split=0.2)
assert(history.history['val_acc'][-1] > 0.91), "The validation accuracy is: %.3f.  It should be greater than 0.91" % history.history['val_acc'][-1]
print('Tests passed.')


Train on 31367 samples, validate on 7842 samples
Epoch 1/2
31367/31367 [==============================] - 4s - loss: 1.6610 - acc: 0.5627 - val_loss: 0.6893 - val_acc: 0.8280
Epoch 2/2
31367/31367 [==============================] - 3s - loss: 0.5552 - acc: 0.8532 - val_loss: 0.3466 - val_acc: 0.9260
Tests passed.

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 [9]:
# TODO: Build a model
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten, Dropout 
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

model = Sequential()

model.add(Convolution2D(32, 5, 5, input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2,2), border_mode='valid'))
model.add(Dropout(.5))
model.add(Activation('relu'))

model.add(Convolution2D(32, 3, 3, input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2,2), border_mode='valid'))
model.add(Dropout(.5))
model.add(Activation('relu'))

model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))

### Important! Changed to 10 for CIFAR10 test
model.add(Dense(10))
model.add(Activation('softmax'))

# TODO: Compile and train the model
model.compile('Nadam', 'categorical_crossentropy', ['accuracy'])

history = model.fit(X_normalized, y_one_hot, batch_size=128, nb_epoch=25, validation_split=.2)


Train on 40000 samples, validate on 10000 samples
Epoch 1/25
40000/40000 [==============================] - 7s - loss: 1.6340 - acc: 0.4052 - val_loss: 1.3742 - val_acc: 0.5266
Epoch 2/25
40000/40000 [==============================] - 4s - loss: 1.3362 - acc: 0.5192 - val_loss: 1.1985 - val_acc: 0.5830
Epoch 3/25
40000/40000 [==============================] - 4s - loss: 1.2219 - acc: 0.5650 - val_loss: 1.1876 - val_acc: 0.5911
Epoch 4/25
40000/40000 [==============================] - 4s - loss: 1.1506 - acc: 0.5916 - val_loss: 1.1147 - val_acc: 0.6218
Epoch 5/25
40000/40000 [==============================] - 4s - loss: 1.0946 - acc: 0.6146 - val_loss: 1.0260 - val_acc: 0.6476
Epoch 6/25
40000/40000 [==============================] - 4s - loss: 1.0530 - acc: 0.6280 - val_loss: 0.9977 - val_acc: 0.6598
Epoch 7/25
40000/40000 [==============================] - 4s - loss: 1.0124 - acc: 0.6430 - val_loss: 0.9358 - val_acc: 0.6807
Epoch 8/25
40000/40000 [==============================] - 4s - loss: 0.9856 - acc: 0.6511 - val_loss: 0.9510 - val_acc: 0.6725
Epoch 9/25
40000/40000 [==============================] - 4s - loss: 0.9577 - acc: 0.6634 - val_loss: 0.8885 - val_acc: 0.6904
Epoch 10/25
40000/40000 [==============================] - 4s - loss: 0.9329 - acc: 0.6712 - val_loss: 0.8740 - val_acc: 0.7016
Epoch 11/25
40000/40000 [==============================] - 4s - loss: 0.9189 - acc: 0.6776 - val_loss: 0.8555 - val_acc: 0.7039
Epoch 12/25
40000/40000 [==============================] - 4s - loss: 0.8984 - acc: 0.6840 - val_loss: 0.8461 - val_acc: 0.7051
Epoch 13/25
40000/40000 [==============================] - 4s - loss: 0.8748 - acc: 0.6927 - val_loss: 0.8406 - val_acc: 0.7066
Epoch 14/25
40000/40000 [==============================] - 4s - loss: 0.8660 - acc: 0.6952 - val_loss: 0.8298 - val_acc: 0.7140
Epoch 15/25
40000/40000 [==============================] - 4s - loss: 0.8526 - acc: 0.6990 - val_loss: 0.8257 - val_acc: 0.7121
Epoch 16/25
40000/40000 [==============================] - 4s - loss: 0.8465 - acc: 0.7014 - val_loss: 0.7963 - val_acc: 0.7224
Epoch 17/25
40000/40000 [==============================] - 4s - loss: 0.8297 - acc: 0.7091 - val_loss: 0.7983 - val_acc: 0.7156
Epoch 18/25
40000/40000 [==============================] - 4s - loss: 0.8273 - acc: 0.7060 - val_loss: 0.8194 - val_acc: 0.7150
Epoch 19/25
40000/40000 [==============================] - 4s - loss: 0.8162 - acc: 0.7135 - val_loss: 0.8021 - val_acc: 0.7163
Epoch 20/25
40000/40000 [==============================] - 4s - loss: 0.8043 - acc: 0.7148 - val_loss: 0.7767 - val_acc: 0.7300
Epoch 21/25
40000/40000 [==============================] - 4s - loss: 0.8002 - acc: 0.7200 - val_loss: 0.7812 - val_acc: 0.7289
Epoch 22/25
40000/40000 [==============================] - 4s - loss: 0.7908 - acc: 0.7209 - val_loss: 0.7834 - val_acc: 0.7295
Epoch 23/25
40000/40000 [==============================] - 4s - loss: 0.7852 - acc: 0.7246 - val_loss: 0.7770 - val_acc: 0.7355
Epoch 24/25
40000/40000 [==============================] - 4s - loss: 0.7858 - acc: 0.7238 - val_loss: 0.7773 - val_acc: 0.7272
Epoch 25/25
40000/40000 [==============================] - 4s - loss: 0.7763 - acc: 0.7280 - val_loss: 0.7644 - val_acc: 0.7314

In [16]:
from keras.models import load_model

model.save('my_model.h5')

Best Validation Accuracy: (fill in here)

99.29 %

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 property to get the labels.


In [15]:
# Load test data
with open('test.p', 'rb') as f:
    data_test = pickle.load(f)

X_test = data_test['features']
y_test = data_test['labels']


print('hello')

# Preprocess data & one-hot encode the labels
X_normalized_test = normalize_to_grayscale(X_test)
y_one_hot_test = label_binarizer.fit_transform(y_test)

# TODO: Evaluate model on test data
metrics = model.evaluate(X_normalized_test, y_one_hot_test)

for metric_i in range(len(model.metrics_names)):
    metric_name = model.metrics_names[metric_i]
    metric_value = metrics[metric_i]
    print('{}: {}'.format(metric_name, metric_value))


hello
12630/12630 [==============================] - 1s     
loss: 0.2003572868891149
acc: 0.9496437055575687

Test Accuracy:

94.96%

Summary

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