How to do it...

  1. Download the dataset, as follows (the code file is available as Traffic_signal_detection.ipynb in GitHub). The dataset is available through the paper: J. Stallkamp, M. Schlipsing, J. Salmen, C. Igel, Man vs. computer: Benchmarking machine learning algorithms for traffic sign recognition:
$ wget http://benchmark.ini.rub.de/Dataset/GTSRB_Final_Training_Images.zip
$ unzip GTSRB_Final_Training_Images.zip
  1. Read the image paths into a list, as follows:
from skimage import io
import os
import glob

root_dir = '/content/GTSRB/Final_Training/Images/'
all_img_paths = glob.glob(os.path.join(root_dir, '*/*.ppm'))

A sample of the images looks as follows:

Note that certain images have a smaller shape when compared to others and also that certain images have more lighting when compared to others. Thus, we'll have to preprocess the images so that all images are normalized per exposure to lighting as well as shape.

  1. Perform histogram normalization on top of the input dataset, as follows:
import numpy as np
from skimage import color, exposure, transform

NUM_CLASSES = 43
IMG_SIZE = 48

def preprocess_img(img):
hsv = color.rgb2hsv(img)
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
img = color.hsv2rgb(hsv)
img = transform.resize(img, (IMG_SIZE, IMG_SIZE))
return img

In the preceding code, we are first converting an image that is in RGB format into a Hue Saturation Value (HSV) format. By transforming the image from RGB to HSV format, we are essentially converting the combined RGB values into an array that can then be transformed into an array of single dimension.

Post that, we are normalizing the values obtained in HSV format so that they belong to the same scale by using the equalize_hist method.

Once the images are normalized in the last channel of the HSV format, we convert them back in to RGB format.

Finally, we resize the images to a standard size.

  1. Check the image prior to passing it through histogram normalization and contrast that with post histogram normalization (post passing the image through the preprocess_img function), as follows:

From the preceding pictures, we can see that there is a considerable change in the visibility of the image (the image on the left) post histogram normalization (the image on the right).

  1. Prepare the input and output arrays, as follows:
count = 0
imgs = []
labels = []
for img_path in all_img_paths:
img = preprocess_img(io.imread(img_path))
label = img_path.split('/')[-2]
imgs.append(img)
labels.append(label)

X = np.array(imgs)
Y = to_categorical(labels, num_classes = NUM_CLASSES)
  1. Build the training and test datasets, as follows:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state= 42)
  1. Build and compile the model, as follows:
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',input_shape=(IMG_SIZE, IMG_SIZE, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same',activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), padding='same',activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
model.summary()

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

A summary of the model is as follows:

  1. Fit the model, as follows:
model.fit(X_train, y_train,batch_size=32,epochs=5,validation_data = (X_test, y_test))

The preceding code, results in a model that has an accuracy of ~99%:

Additionally, if you perform the exact same analysis like we did, but without histogram normalization (correcting for exposure), the accuracy of the model is ~97%.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset