Skip to content

Latest commit

 

History

History
222 lines (145 loc) · 4.98 KB

File metadata and controls

222 lines (145 loc) · 4.98 KB

Topics

  1. Data-driven approaches
  2. Linear classification
  3. K-Nearest Neighbor

Image Classification

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

Attemps have been made

1. Edge detectors

  1. Find edges of image first
  2. find the corners of the edges
  3. use the corners' logic to label the class

Why don't we use it?

  1. Very hard to scale
  2. Find the logic of each images

ML: Data-driven Approach

  1. Collect a dataset of images and labels
    • use internet to get datasets
  2. Use ML algorithms to train a classifier
def train (images, labels):
    # ML!!!
    return model
  1. Evaluate the classifier on new images
def predict(model, test_images):
    # Use the model that we trained!
    return test_labels

Instead of builing a logic, we built a data-driven approach

First classifier: Nearest Neighbor

def train (images, labels):
    # ML!!!
    return model

Memorize all data and labels

def predict(model, test_images):
    # Use the model that we trained!
    return test_labels

Predict the label of the most similar training images

To find the nearest neighbor, we need to have a Distance function

$$ L1 \ distance: \ d_{1}(I_{1}, I_{2}) = \Sigma_{P} |I_{1}^{P} - I_{2}^{P}| $$

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: $O(1)$, Predict: $O(n)$

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

image1.png

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

Setting Hyperparameters

1. Choose Hyperpara that work best on the training data

Bad: $K = 1$ always works perfectly on training data

2. Choose Hyperpara that work best on the test data

Bad: No idea how algorithm will perform on new data

3. Split the data into train, val; choose hpp on val to validate

4. Cross-ValidationL Split data into folds, try each fold as validation and average the results

Useful to small dataset, but not for big dataset, it's too tricky

Linear classifier

$$ f(x, W) = Wx + b $$

Softmax Classifier

$$ s = f(x_{i};W) $$ $$ P(Y=k|X=x_{i})=\frac{e^sk}{\Sigma_{j}e^sj} $$