Semantic Gap : represent image to numbers
Challenges
An image classifier (API in python)
def classify_image(image):
# Write magical Some codes
return class_label
import numpy as np
class NearestNeighbor :
def __init__(self):
pass
# Memorize training data
def train(self, X, y):
self.Xtr = X
self.Ytr = y
def predict(self, X):
num_test = X.shape[0]
Ypred = np.zeros(num_test, dtype = self.ytr.dtype) #output data type matches input type
# For each test image: Find closest train image, Predict label of nearest image
for i in xrange(num_test):
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1) #using the L1 distance (sum of absolute value differences)
min_index = np.argmin(distances) #get index with smallest distance
Ypred[i] = self.ytr[min_index]
return Ypred
What is the best value of k and distance to use?
We call this k and distance hyperparameters.
Must try them all out and see what works best.
Setting Hyperparameters
K-nearest Neighbor is work quite exactly. However, it can not used on images.
Summary
- In Image classification we start with a training set of images and labes, and must predict labels on the test set.
- K-Nearest Neighbors : predicts labels based on nearest training examples
- Distance matric(L1,L2) and K(in K-Nearest Neighbors) are hyperparameters
- Choose hyperparameters using the validation set; test set is used only once at the end!
more detail will be introduced lecture03