# Neural Networks Crash Tutorial - Keras version¶

## Classifying images with Neural Networks¶

This notebook is inspired by lots of tutorials on the web. It's first part uses the same data and shares the same objectives as with:

https://www.datacamp.com/community/tutorials/tensorflow-tutorial

http://btsd.ethz.ch/shareddata/

Look for the two zip files, BelgiumTSC_Training.zip and BelgiumTSC_Testing.zip in the section Anotations next to the label "BelgiumTS for Classification (cropped images)".

This notebook assumes that the images will be in a directory named BelgiumTrafficSigns with subdirectories Training and Testing.

You will need jupyter notebook and tensorflow to be installed. You will also need the following packages:

• numpy
• scipy
• matplotlib
• scikit-image
• keras

Most if not all of these packages may already be installed for you.

### Preliminaries¶

Here are some useful functions:

In [1]:
import os, skimage.data

directories = [d for d in os.listdir(data_directory)
if os.path.isdir(os.path.join(data_directory, d))]
labels = []
images = []
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")]
for f in file_names:
labels.append(int(d))
return images, labels

In [2]:
def plot_images(images, labels, color_map = "brg"):
# Get the unique labels
unique_labels = set(labels)

# Initialize the figure
plt.figure(figsize=(15, 15))

# Set a counter
i = 1

# For each unique label,
for label in unique_labels:
# You pick the first image for each label
image = images[labels.index(label)]
# Define 64 subplots
plt.subplot(8, 8, i)
# Don't include axes
plt.axis('off')
# Add a title to each subplot
plt.title("Label {0} ({1})".format(label, labels.count(label)))
# Add 1 to the counter
i += 1
# And you plot this first image
plt.imshow(image, cmap=color_map)

# Show the plot
plt.show()


In [3]:
train_data_directory = "./BelgiumTrafficSigns/Training"


Plot histogram of labels to see how the images are distributed

In [4]:
# Import the pyplot module of matplotlib
import matplotlib.pyplot as plt

# Make a histogram with 62 bins of the labels data
plt.hist(labels, 62)

# Show the plot
plt.show()


Check out some random images and their shapes:

In [5]:
import random

# Determine the (random) indexes of the images that you want to see
traffic_signs = random.sample(range(len(images)), 4)

# Fill out the subplots with the random images and add shape
for i in range(len(traffic_signs)):
plt.subplot(1, 4, i+1)
plt.axis('off')
plt.imshow(images[traffic_signs[i]])
plt.show()
print("shape: {0}".format(images[traffic_signs[i]].shape))

shape: (136, 142, 3)

shape: (116, 115, 3)

shape: (72, 71, 3)

shape: (60, 56, 3)


Plot first sample from each category:

In [6]:
plot_images(images,labels)