You are on page 1of 5

Image Analysis: Assignment 2

Ali Kadhem

1 Your own classifier


A dataset of images consisting of faces and not faces was trained and classified using nearest
neighbour classification. Each image has 200 features. The MATLAB–code used is shown below
in listing 1.

Listing 1: Nearest neighbour classifier


function classification_data = class_train(X, Y)
classification_data{1}=X;% Features
classification_data{2}=Y;% Ground truth
end

function y = classify(x, classification_data)


for i=1:size(classification_data{1},2)
dist(i)=min(norm(classification_data{1}(:,i)-x));
end
[val,ind]=min(dist);
y=classification_data{2}(ind);
end

The dataset was split into a training–set and a test–set, 80% for training and 20% for test. Error
for training–set for nearest neighbour classifier is irrelevant since it will always be zero. The error
for the test–set for 100 trials of different partitions of training– and test–sets was 15%. To print a
face image and a non–face image the code in listing 2 was used. The images chosen were the first
and last images among the test images. First image is a face and the last image is not a face.

Listing 2: Printing a face and a non–face with respective prediction.


ind=[1 length(Y_test)];
imF=X_test(:,ind(1));
if Y_test(ind(1))==classify(imF,classification_data)
display(’SAME!’)
else
display(’NOT SAME!’)
end
imF=reshape(X_test(:,ind(1)),[19 19]);
figure
imagesc(imF)

1
colormap(gray)

imNotF=X_test(:,ind(2));
if Y_test(ind(2))==classify(imNotF,classification_data)
display(’SAME!’)
else
display(’NOT SAME!’)
end
imNotF=reshape(X_test(:,ind(2)),[19 19]);
figure
imagesc(imNotF)
colormap(gray)

For the images in figure 1 the classifier predicted correct classes.

2 2

4 4

6 6

8 8

10 10

12 12

14 14

16 16

18 18

2 4 6 8 10 12 14 16 18 2 4 6 8 10 12 14 16 18

(a) Face image. (b) Non–face image.

Figure 1: Image of face and non–face.

2 Use pre–coded machine learning techniques


MATLAB also has built–in classifiers that were looked into. Classifiers that were considered are re-
gression tree classifier fitctree, support vector machine fitcsvm and nearest neighbour fitcknn.
A mean error of these classfiers and the nearst neighbour classifier that was coded was found for
100 trials of different subsets of training and test images.

Mean error rates for the test–set were:

Own nearest neighbour: 0.1498


Regression tree: 0.1570
Support vector machine: 0.0555
Built–in nearest neighbour: 0.1498.

2
Mean error rates for the training–set were:

Own nearest neighbour: 0


Regression tree: 0.0123
Support vector machine: 0
Built–in nearest neighbour: 0.

For the test set the nearest neighbour classifier and the regression tree have the same performance,
while support vector machine is much better. Support vector machine performs much better since it
has a good algorithm for producing a (hyper)plane that separates different classes (by for example
mapping to a higher dimensional non–linear feature space), as compared to a simple distance
function for nearest neighbour. Training set is zero for nearest neighbour, since this method does
not use any real training, and zero for support vector machine means that it was possible to
completely separate the dataset into their classes with a hyperplane.

3 Testing a simple CNN model


A simple CNN model was used to classify faces and non–faces. Average error test of 100 trials
resulted in no error for the training set and error of 0.05 for the test set. A graph of training
progress is shown in figure 2 that shows that training error goes to zero.

Figure 2: Accuracy of training of the simple CNN model for one trial.

The CNN model performs much like the SVM for this face classification problem.

3
4 Linefit
A line fit on a set of data can be done in different ways. Least square method is a method that
minimizes the vertical distance from data points to the fitted line. This method produces fits that
are susceptible to outlier data and therefore the fitted line can misrepresent the data, as seen in
figure 3.

35

30 data points

least-squares

25 RANSAC

20

15
y

10

-5
0 2 4 6 8 10 12
x

Figure 3: Line fit using least squares and RANSAC. Least square equation is y = 1.7422x + 5.4217
and of RANSAC it is y = 3.2671x − 3.0998

A method that tries to solve this problem is RANSAC. For this problem, RANSAC chooses two
random points and makes a fit for these two points. The number of points close to this fit is
calculated and if there are enough points this line is taken as the fitted line. In case not enough
points are close to the line, a new set of points are randomly chosen and again the number of close
points to the fitted line is considered. These steps are however only done for a limited number of
times. The least square errors (sum of square vertical errors) and the total least square errors (sum
of square orthogonal errors) were found to be

Least square errors:


Least squares method: 1484
RANSAC method: 2146

and the total least square errors are:

Least squares method: 368


RANSAC method: 184.

The code of how the errors are found is seen in listing 3. Least squares minimize the vertical errors,

4
which explains why the error for least square is less than RANSAC. The total least squares was
less for RANSAC since this method tries to find a line that is close to as many points as possible
(or as one desires). In general, to consider only vertical errors can be result in very large errors
depending on the slope of the line fit. This is seen in the difference between the values for the two
different error types.

Listing 3: Code for calculating least squares error and total least squares error for least squares fit
and RANSAC.
ls_error=sum(abs(y_leastVal-ym).^2)
a=-p_ls(1);
b=1;
c=-p_ls(2);
d=(1/sqrt(a^2+b^2).*abs(a.*xm+b.*ym+c));
tls_error_least=sum(d.^2)

y_RANVal=p_ransac(1) * xm + p_ransac(2);
ransac_error=sum(abs(y_RANVal-ym).^2)
a=-p_ransac(1);
b=1;
c=-p_ransac(2);
d2=(1/sqrt(a^2+b^2).*abs(a.*xm+b.*ym+c));
tls_error_ransac=sum(d2.^2)

5 OCR system construction and system testing


To construct the OCR system, the classification function, nearest neighbour classifier, from the
first task was used. The hitrate with easier test images (short1) was 62% and for more difficult
images (home1) was 26.6%. The main reasons for low hitrate score is bad segmentation and not
good enough features. The choice of classifier affects the score, but segmentation score was around
0.8 and is thought to be the main issue with this OCR system.

You might also like