- Data-driven approaches
- Linear classification
- K-Nearest Neighbor
A core task in computer vision
- The image classification task 给图像分类
- Two basic data-driven approaches to image classificaton 两个基本的给图像分类的数据驱动方法
- K-nearest neighbor and linear classifier K最近邻居与线性分类器
那么什么是 Image Classification?
Given an image and a number of predefined labels, the job of the system is to assigned the label to the specific image.
Images are often defined by matrices of data, generally tensers of data.
So this is the gap between machine and human to understand an image.
Some challenges:
- Systematic
- camera's position
- illumination
- background clutter
- scale
- Objective
- occlusion
- deformation
- intraclass variation
- context
- ...
# An image classifier
def classify_image(image):
# some magic haha..
return class_label- Find edges of image first
- find the corners of the edges
- use the corners' logic to label the class
- Very hard to scale
- Find the logic of each images
- Collect a dataset of images and labels
- use internet to get datasets
- Use ML algorithms to train a classifier
def train (images, labels):
# ML!!!
return model- Evaluate the classifier on new images
def predict(model, test_images):
# Use the model that we trained!
return test_labelsInstead of builing a logic, we built a data-driven approach
def train (images, labels):
# ML!!!
return modelMemorize all data and labels
def predict(model, test_images):
# Use the model that we trained!
return test_labelsPredict the label of the most similar training images
To find the nearest neighbor, we need to have a Distance function
import numpy as np
class NearestNeighbor:
def __init__(self):
pass
def train(self, X, y):
'''
X is N x D where each row is an example.
Y is 1-dimension of size N
'''
self.Xtr = X
self.ytr = y
def predict(self, X):
'''
X is N x D where each row is an example we wish to predict label for
'''
num_test = X.shape[0]
Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
# loop over all test rows
for i in range(num_test):
# find the nearest training image to eht i'th test image
# using the L1 distance
distance = np.sum(np.abs(self.Xtr - X[i, :]), axis = 1)
min_index = np.argmin(distance) # get the index with smallest distance
Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
return Ypred
Q: With N examples, how fast are training and prediction?
A: Train:
So consider this kind of situation, it seems that the prediction is slower than training.
This is bad, but why?
In my opinion, the training process is ok to be long, because nobody cares about how much time does the training process take, people only care about the result, which is the prediction time
So we should find some ways that predict is faster than training
Given this space that we have five classes of colors, each dot represents one training sample.
So this graph shows that if you have a test sample, you put the point in the graph, the diffrent partitions just shows you what the nearest neighbor for that sample will be.
But there is a problem in this kind of approach
Two distance functions:
$$ L1(Manhattan) distance \
d_{1}(I_1,I_2)= \Sigma_P |I_1^p - I_2^p|
$$ $$ L2(Euclidean) distance \
d_{1}(I_1,I_2)= \Sigma_P \sqrt{(I_1^p - I_2^p)^2} \ $$
You will see the graph of L1 is a square and L2 is a circle.
So the L1 is sensitive to feature but L2 isn't.
![[Pasted image 20260511142326.png]]
Why do we talk about KNN in the begining?
First, it's easy to understand.
But moreover, is to understand the concept of Hyperparameters:
- Best value of
$k$ - Best distance to use
Bad:
Bad: No idea how algorithm will perform on new data
Useful to small dataset, but not for big dataset, it's too tricky
