Lab 2
In lab 2, you will perform K-nearest-neighbor classification of face images. You will use two different feature vectors: the discrete cosine transform (dct), and the principal component analysis (pca). You will use two different values of K: K=1, and K=4. Finally, you will use two different feature vector dimensions: nfeats=9, and nfeats=36. Solutions are provided to you for the nfeats=9 case.
- Code, test code, data, and solutions.
- Images of the solutions, created using make_cool_plots.py.
- Autograder submission site.
The ZIP Archive, and the recommended Code-Writing Process
In the ZIP archive, you will find the following directory hierarchy:
-
submitted.py -- this is the ONLY file that you
will submit, and the only file on which you will be
graded. It defines a python class called KNN
that computes the KNN classification of each face image,
with respect to the other face images.
The
__init__
function (provided) reads in all of the image files. Your job, then, is to convert compute the DCT projection of each image, convert each image to a raw pixel vector, find the PCA transform for this dataset, compute the PCA projection of each image, then perform KNN using each type of feature.- set_vectors - convert each image into a raw pixel vector
- set_mean - find the global mean vector
- set_centered - subtract the mean from each image vector
- set_transform - compute the feature transform matrix (DCT or PCA)
- set_features - compute the feature vector for each image
- set_energyspectrum - cumulative fraction of variance, vs. number of features
- set_neighbors - compute the K nearest neighbors of each image
- set_hypotheses - use neighbors to classify each image
- set_confusion - compute the confusion matrix
- set_metrics - compute the accuracy, recall, and precision. Warning: this is actually a pretty tough image set; you should expect that, for every parameter setting, your total accuracy will be around 50 percent.
- data/PERSON_NUM.ppm -- these are the image files; they are extracted from the Labeled Faces in the Wild dataset. PERSON is one of {Arnold_Schwarzenegger, George_HW_Bush, George_W_Bush, Jiang_Zemin}. NUM is one of [0001,0012].
- requirements.txt lists the python packages that are used by the autograder. You can install them by typing pip3 install -r requirements.txt.
- make_cool_plots.py --- generates plots that might be useful to debug your code. After you have written some functions in submitted.py, you can generate the corresponding plots, e.g., for the nfeats=36, transformtype=dct, K=4 case by typing python3 make_cool_plots.py 36 dct 4. Outputs will be placed in the directory make_cool_plots_outputs; they should look like the images provided in the solution image set
- run_tests.py --- once your code is producing reasonable output plots using make_cool_plots.py, try running python3 run_tests.py. This will call the test routines in tests/test_sequence.py, using scoring utility functions in score.py, and compare the results to the hashed solution criteria in solutions/*.json.
How to Submit
When you're ready to submit, go to Gradescope.
- Submit only the one file, submitted.py. Any other files you submit will be ignored.
- You may submit as many times as you like, until the deadline. Only your last submission will count toward your course grade.