and next to each we show the top 10 nearest neighbors in the training set according to
pixel-wise difference.
Suppose now that we are given the CIFAR-10 training set of 50,000 images (5,000
images for every one of the labels), and we wish to label the remaining 10,000.
The nearest neighbor classifier will take a test image, compare it to every single
one of the training images, and predict the label of the closest training image.
In the image above and on the right you can see an example result of such a
procedure for 10 example test images. Notice that in only about 3 out of 10
examples an image of the same class is retrieved, while in the other 7 examples
this is not the case. For example, in the 8th row the nearest training image to
the horse head is a red car, presumably due to the strong black background. As a
result, this image of a horse would in this case be mislabeled as a car.
You may have noticed that we left unspecified the details of exactly how we
compare two images, which in this case are just two blocks of 32 x 32 x 3. One of
the simplest possibilities is to compare the images pixel by pixel and add up all
the differences. In other words, given two images and representing them as vectors
, a reasonable choice for comparing them might be the L1 distance:
Where the sum is taken over all pixels. Here is the procedure visualized:
An example of using pixel-wise differences to compare two images with L1 distance (for one
color channel in this example). Two images are subtracted elementwise and then all
differences are added up to a single number. If two images are identical the result will be
zero. But if the images are very different the result will be large.
Let’s also look at how we might implement the classifier in code. First, let’s
load the CIFAR-10 data into memory as 4 arrays: the training data/labels and the
test data/labels. In the code below, Xtr (of size 50,000 x 32 x 32 x 3) holds
all the images in the training set, and a corresponding 1-dimensional array Ytr
(of length 50,000) holds the training labels (from 0 to 9):
- 1
- 2
- 3
- 4
前往页