Letter Classification by Convolutional Neural Network


October 5, 2018

by Shihang Feng

PIC

Figure 1: Input letters and their classification

1 Objective

In this lab, we will go through convolutional neural network (CNN) to classify the letter in a image.

2 Prerequisties

3 Procedure of CNN

  1. Download CNN_lab.zip and unzip it

  2. Add the path of CNN functions and data into the working path by typing

    clear all,close all

    addpath ./CNN

    addpath ./util

    addpath ./data

  3. Load the training data and test data by typing

    load mnist_uint8;

    train_x = double(reshape(train_x',28,28,60000))/255;

    test_x = double(reshape(test_x',28,28,10000))/255;

    train_y = double(train_y');

    test_y = double(test_y');

  4. Visualizingthe data by typing

    train_n=reshape(train_x,784,60000)';

    sel = randperm(size(train_n, 1));

    sel = sel(1:100);

    displayData(train_n(sel,:));

  5. Setup CNN parameters by typing

    rand('state',0);

    opts.alpha = 1; %learning rate

    opts.batchsize = 50; %minibatch size

    opts.numepochs = 2; %iteration number

  6. Setup CNN structure by typing

    cnn.layers = { struct('type', 'i') %input layer

    struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer

    struct('type', 's', 'scale', 2) %sub sampling layer

    struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer

    struct('type', 's', 'scale', 2)}; %subsampling layer

  7. Check the input parameters and train CNN by typing

    cnn = cnnsetup(cnn, train_x, train_y);

    cnn = cnntrain(cnn, train_x, train_y, opts);

  8. Apply trained CNN on the test data by typing

    [er, bad, nn] = cnntest(cnn, test_x, test_y);

  9. Plot the residue curve by typing

    figure;plot(cnn.rL);

  10. Visualizing test data and its classification by typing

    sel = randperm(size(test_x, 3));

    sel= sel(1:6);

    figure('rend','painters','pos',[10 10 1200 250]);

    for ii=1:6

    subplot(1,6,ii);

    imagesc(test_x(:,:,sel(ii)));

    axis off;

    [num,letter]=max(nn.o(:,sel(ii)));

    title(['letter is ', num2str(letter-1)],'fontsize',24);

    end

  11. Visualizing the weights in the first convolution layer

    figure('rend','painters','pos',[10 10 1200 250]);

    for ii=1:6

    subplot(1,6,ii)

    imagesc(cnn.layers{2,1}.k{1,1}{1,ii})

    axis off;

    end

PIC

Figure 2: The convolutional neural network architecture with two convolutional layer and a fully connected layer.

4 Questions

  1. Modify the structure of CNN. See how the CNN parameter will change the result.
  2. Modify minibatch size and learning rate. See how these parameter will change the convergence rate and the result.

5 PS

If there are any errors in this Lab, please contact: shihang.feng@kaust.edu.sa