Top Banner
Eigenfaces in Scala A quick walkthrough and sample implementation of the Eigenfaces algorithm in Scala. Justin Long
12

Eigenfaces In Scala

Jul 17, 2015

Download

Technology

Justin Long
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Eigenfaces In Scala

Eigenfaces inScalaA quick walkthrough and sample implementation of the Eigenfaces algorithm in Scala.

Justin Long

Page 2: Eigenfaces In Scala

What is Eigenfaces?

Need to do quick and dirty object or facial recognition? Eigenfaces may be for you.

Page 3: Eigenfaces In Scala

The essence of Eigenfaces

Eigenfaces is the name given to a set of eigenvectors when they are used for facial recognition.

A typical use for calculating Eigenfaces works as such:

1. Obtain a training set of faces and convert to a pixel matrix

2. Compute the mean image (which is an average of pixel intensity across each image).

3. Compute a differential matrix by subtracting the mean from each training image, pixel by pixel

4. Compute covariance matrix of the differential matrix

Page 4: Eigenfaces In Scala

The essence of Eigenfaces

Your “average face” may look a little uncanny…

4. Calculate eigenvectors from covariance matrix

5. Compute Eigenfaces by multiplying eigenvectors and covariance matrices together, and normalizing them

Page 5: Eigenfaces In Scala

Putting it together

def computeEigenFaces(pixelMatrix: Array[Array[Double]], meanColumn: Array[Double]): DoubleMatrix2D = {

val diffMatrix = MatrixHelpers.computeDifferenceMatrixPixels(pixelMatrix, meanColumn)

val covarianceMatrix = MatrixHelpers.computeCovarianceMatrix(pixelMatrix, diffMatrix)

val eigenVectors = MatrixHelpers.computeEigenVectors(covarianceMatrix)

computeEigenFaces(eigenVectors, diffMatrix)

}

Page 6: Eigenfaces In Scala

Multiplying eigenvectors/differential

(0 to (rank-1)).foreach { i =>

var sumSquare = 0.0

(0 to (pixelCount-1)).foreach { j =>

(0 to (imageCount-1)).foreach { k =>

eigenFaces(j)(i) += diffMatrix(j)(k) * eigenVectors.get(i,k)

}

sumSquare += eigenFaces(j)(i) * eigenFaces(j)(i)

}

var norm = Math.sqrt(sumSquare)

(0 to (pixelCount-1)).foreach { j =>

eigenFaces(j)(i) /= norm

}

}

Page 7: Eigenfaces In Scala

Preprocessing is key

You need to preprocess your images!

- Grayscale: important for calculating proper pixel intensity values

- Normalization: very helpful if your images were taken in different lighting conditions

- Cropping: Very important to focus only on facial features

Without preprocessing, you’re gonna have a bad time.

Page 8: Eigenfaces In Scala

Potential for Eigenfaces?

Facial recognition is most obvious use.

Eigenfaces and eigenvectors are also useful for other applications of computer vision:

- Subjects that can be read 2-dimensionally, from same angle

- Optical Character Recognition (OCR)

- Image segmentation

- http://www.cs.huji.ac.il/~yweiss/iccv99.pdf

Page 9: Eigenfaces In Scala

What about this author?

Well… I had a more creative use for Eigenfaces…

Page 10: Eigenfaces In Scala

Tinderbox used Eigenfaces

What happened when I got fed up with all the swiping involved in Tinder? I automated it with Eigenfaces (a slightly modified strategy).

- Added a simple step of averaging all faces together for yes/no faces

- The “average” face was used for Eigenfaces selection (k-nearest neighbor between 2 models)

Page 12: Eigenfaces In Scala

Questions?