In this blog, we are going to use Neural Network to do image classification. The following figure shows a simple example of Neural Network. If you are interested in this field, please see this review or recently this review.
1. A simple Neural Network on MNIST dataset as an example.
The MNIST database is a large database of handwritten digits. It contains 60,000 training images and 10,000 testing images. The Keras provides a convenience method for loading the MNIST dataset.
# In this demo we design a Neural Network with 1 hidden layers:
# Input layer (784 dimensions)
# layer 1 (1000 hidden neurons)
# layer 2 (10 outputs)
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# flatten 28*28 images to a 784 vector for each image
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
# design model
model = Sequential()
model.add(Dense(1000, input_dim=num_pixels, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
# print out summary of the model
print(model.summary())
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-scores[1]*100))
2. Assignment
Run the above code to double check your environment and get some sense about how it looks like while training a model. Design your own Neural Network to do Image Classification on Boat Dataset. Boat Dataset consists of 5 different types of boats:
-
Training Dataset (249.6 MB) Download
Class Number of images aircraft_carrier 500 banana_boat 500 oil_tanker 500 passenger_ship 500 yacht 500 In total 2500 -
Testing Dataset (97.1 MB, 1000 images) Download
Train your model on training dataset and test the trained model on testing dataset.
Hint Try to understand the following API(s):
# All images in the new dataset are colorful images (consist of Red, Blue, and Green channels) with different sizes. You may would like to use the following API to resize all images into the same size before doing flatten (line 15 in the above code).
image = cv2.imread(path) # load an image
resized_image = cv2.resize(image, (new_height, new_width)) # resize an image
To improve the performance of your model, you can try different values of the following parameters:
- number of hidden layers (network structure)
- number of neurons in each layer (network structure)
- image size (preprocessing)
- batch_size (training phase, model.compile)
- optimizer (training phase, model.compile)
- other settings you can dig out.
3. Submite your sulotion:
- Your final python code. Please name it using your Lehigh ID. (<your_LehighID>.py)
- A short <your_LehighID>.pdf file. Simply describe what you did, what you got, and other things you want to report, e.g. what you have learned.