Document 13484800

advertisement
The Mahalanobis Distance in Character
Recognition
Authored by Dan Frey 31 July 98
This sheet explores the use of the Mahalanobis Distance in character recognition. It defines a letters A, B, C, and D.
It creates a population of distorted letters to train a classifier.
It creates a test population of letters to test the classifier.
It allows you to compete against the Mahalanobis classifier.
ORIGIN := 1
Every matrix and vector in this sheet will begin with index number 1.
Here are the Canonical letters A B C and
D defined as matrices of 1s and 0s
�0
�0
�
A := � 1
�1
�
Ł1
0 1 0 0�
1 0 1
0 0 0
1 1 1
0 0 0
�1
�1
�
B := � 1
�1
�
Ł1
�
0
�
1�
1�
�
1ł
1 1 1 0�
0 0 1
1 1 1
0 0 1
1 1 1
�0
�1
�
C := � 1
�1
�
Ł0
�
1
�
0�
1�
�
0ł
1 1 1 0�
0 0 0
0 0 0
0 0 0
1 1 1
�
1
�
0�
1�
�
0ł
�1
�1
�
D := � 1
�1
�
Ł1
Stretch the letters out into a vector of "features".
k := 1.. 25
Alin := A
k
Clin := C
k
Blin := B
� k � , ceil� k �
�
� �
Ł 5ł
Ł 5ł
k
k+ 5-5� ceil�
� k � , ceil� k �
�
� �
Ł 5ł
Ł 5ł
k+ 5-5� ceil�
Dlin := D
� k � , ceil� k �
�
� �
Ł 5ł
Ł 5ł
k
k+ 5-5� ceil�
� k � , ceil� k �
�
� �
Ł 5ł
Ł 5ł
k+ 5-5�ceil�
Reverse the letters. This makes the plots easier to read.
Alin := Alin = 0
k
k
Blin := Blin = 0 Clin := Clin = 0
k
k
k
1
k
Dlin := Dlin = 0
k
k
1 1 1 0�
�
�
0 0 0 1�
0 0 0 1�
�
1 1 1 0ł
0 0 0 1
Define a function that will display the vector of
features as a matrix
DISPLAY(Llin) :=
for i ˛ 1.. 5
for j ˛ 1.. 5
L
i, j
‹ Llin
i+ 5�( j-1)
L
Here are a couple of plots that show what the Cononical letters look like.
DISPLAY( Alin )
DISPLAY( Clin )
DISPLAY( Blin )
DISPLAY( Dlin )
2
Create a population of fuzzed up letters
training_population := 300
Size of the training population. Has to be bigger than
the number of pixels.
pop := 1.. training_population
st := 0.01 How much random noise is superposed onto the pixels.
�������������
fi
Æ popæ
APOP
:= Ø 2�floor runif(1, 0, 2) - 1 �Alinø + rnorm 25, 0, st
1
º
ß
�������������
fi
Æ popæ
BPOP
:= Ø 2�floor runif(1, 0, 2) - 1 �Blinø + rnorm 25, 0, st
(
º(
(
(
) )
)
1)
ß
(
)
(
)
�������������
fi
Æ popæ
:= Ø 2�floor runif(1, 0, 2) - 1 �Clinø + rnorm 25, 0, st
CPOP
1
º
ß
�������������
fi
Æ popæ
DPOP
:= Ø 2�floor runif(1, 0, 2) - 1 �Dlinø + rnorm 25, 0, st
(
º(
(
(
) )
)
1)
(
ß
These populations have
been switched from
positive to negative with
a 50/50 probability, then
superposed with white
noise.
)
(
)
Here is an example of a fuzzed up A and a fuzzed up B.
(
Æ 5æ
DISPLAY APOP
)
(
Æ 3æ
DISPLAY BPOP
3
)
Compute Statistics of the Training Population
To create a Mahananobis distanace classifier, we need to deternime the mean and
cavariance matrices from the population of letters.
n := 1.. 25
m := 1.. 25
Ø(
T
Æ æ
Æ æ
) n , (APOPT) m øß
n, m
:= cvarº APOP
COVB
Ø(
T
n, m
:= cvarº BPOP
) n , (BPOPT) m øß
COVC
:= cvarº CPOP
Ø(
T
) n , (CPOPT) m øß
COVD
:= cvarº DPOP
Ø(
T
COVA
n, m
n, m
Æ æ
Æ æ
Æ æ
Ø(
T
MEANA := meanº APOP
n
Æ æ
Æ æ
T
) n øß
Ø(
T
) n øß
Ø(
T
MEANC := meanº CPOP
n
Æ æ
) n , (DPOPT) m øß
MEAND := meanº DPOP
n
Æ æ
Ø(
MEANB := meanº BPOP
n
Æ æ
) n øß
Æ æ
Æ æ
) n øß
The classifier will use the inverse of the covariance matrix, so let's compute it ahead of time.
INVCOVA := COVA
-1
-1
INVCOVC := COVC
-1
INVCOVB := COVB
INVCOVD := COVD
4
-1
I think it's interesting to note that the "mean" appearance of the letter A is incomprehensible
as an A. The reason is that we had an equal number of positive images and negative
images of A. The same applies to all the other letters. And yet the classifier successfully
identifies the letter A because it has a map of the correlations within the letter A.
DISPLAY( MEANA )
5
Define the Classifier
Here is the classifier function. It is very simple. You send it a letter (L) as an argument. It
computes the Mahalanobis distance from the letter to the class A, B, C, and D. Finally, it
classifies L as the letter with the smallest Mahalanobis distance.
Class(L) :=
T
MDA ‹ (L - MEANA) �INVCOVA �(L - MEANA)
T
MDB ‹ (L - MEANB) �INVCOVB�(L - MEANB)
T
MDC ‹ (L - MEANC) �INVCOVC�(L - MEANC)
T
MDD ‹ (L - MEAND) �INVCOVD�(L - MEAND)
"A" if (MDA < MDB)�(MDA < MDC)�(MDA < MDD)
otherwise
"B" if (MDB < MDC)�(MDB < MDD)
otherwise
"C" if (MDC < MDD)
"D" otherwise
6
Test the Classifier
s := 0.6
How badly should the letters be fuzzed up.
tests := 10000 How many times should you test the classifier
test := 1.. tests
Answer
test
:=
rand ‹ runif(1, 0, 4)
1
"A" if 0 £ rand £ 1
Make the four letters equally likely to appear
within the test batch.
"B" if 1 < rand £ 2
"C" if 2 < rand £ 3
"D" if 3 < rand £ 4
Create a batch of letters corresponding to the desired correct answers and appropriately
fuzzed up. This will be the batch of data to test our classifier.
L
test
:=
�������������
fi
غ( 2�floor( runif(1, 0, 2) 1) - 1) �Alinøß + rnorm( 25, 0, s) if Answertest = "A"
�������������
fi
غ( 2�floor( runif(1, 0, 2) 1) - 1) �Blinøß + rnorm( 25, 0, s) if Answertest = "B"
�������������
fi
غ( 2�floor( runif(1, 0, 2) 1) - 1) �Clinøß + rnorm( 25, 0, s) if Answertest = "C"
�������������
fi
غ( 2�floor( runif(1, 0, 2) 1) - 1) �Dlinøß + rnorm( 25, 0, s) if Answertest = "D"
RIGHT
test
(
(
:= Class L
test
) = Answertest)
mean(RIGHT) = 0.905
At sigma=0.6, the Mahalanobis classifier is right about 94% of the time!
Can you do as well in classifying the letters?
Also note how fast the Mahalanobis classifier operates.
7
Try Your Hand at Classification
Answ := 1
Take a guess at which letter is represented in the following four graphs. When you're done,
change the varaible "Answ" to 1 and the answers will display. Did you get it right? Did the MD
classifier?
( 1)
( 2)
DISPLAY L
DISPLAY L
C
Answer
1�(Answ=1)
ClassØ L
º
D
= "C"
1� (Answ=1) ø
ß
Answer
2�(Answ=1)
= "C"
ClassØ L
º
( 3)
3�(Answ=1)
ClassØ L
º
2� (Answ=1) ø
ß
= "D"
( 4)
DISPLAY L
Answer
= "D"
DISPLAY L
= "A" A
3� (Answ=1) ø
ß
Answer
4�(Answ=1)
ClassØ L
= "A"
º
8
= "A"
4� (Answ=1) ø
ß
= "A"
A
Download