目录
Machine Learning: Data-Driven Approach
图像分类面临的问题
Viewpoint variation | ![]() |
Illumination | ![]() |
Background Clutter | ![]() |
Occlusion | ![]() |
Intraclass variation | ![]() |
Occlusion | ![]() |
Context | ![]() |
Machine Learning: Data-Driven Approach
我对Data-Driven的理解是:由算法自己从数据集中提取特征,用于分类
19
1. Collect a dataset of images and labels
2. Use Machine Learning algorithms to train a classifier
3. Evaluate the classifier on new images
K-Nearest Neighbor
原理
比较目标图片和每一个label集的图片每个位置对应的像素,相差值(求和)最小的那个label就是目标图片所属label
- During training, the classifier takes the training data and simply remembers it
- During testing, kNN classifies every test image by comparing to all training images and transfering the labels of the k most similar training examples
代码
以下代码出自官网CS231n Convolutional Neural Networks for Visual Recognition
import numpy as np
class NearestNeighbor(object):
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
"""
# the nearest neighbor classifier simply remembers all the training data
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]
# lets make sure that the output type matches the input type
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 the i'th test image
# using the L1 distance (sum of absolute value differences)
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
min_index = np.argmin(distances) # get the index with smallest distance
Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
return Ypred
L2距离:
# 辅助函数
import numpy as np
class KNearestNeighbor(object):
""" a kNN classifier with L2 distance """
def __init__(self):
pass
def train(self, X, y):
"""
Train the classifier. For k-nearest neighbors this is just memorizing the training data.
Inputs:
- X: A numpy array of shape (num_train, D) containing the training data
consisting of num_train samples each of dimension D.
- y: A numpy array of shape (N,) containing the training labels, where
y[i] is the label for X[i].
"""
self.X_train = X
self.y_train = y
def predict(self, X, k=1, num_loops=0):
"""
Inputs:
- X: A numpy array of shape (num_test, D) containing test data consisting
of num_test samples each of dimension D.
- k: The number of nearest neighbors that vote for the predicted labels.
- num_loops: Determines which implementation to use to compute distances
between training points and testing points.
Returns:
- y: A numpy array of shape (num_test,) containing predicted labels for the
test data, where y[i] is the predicted label for the test point X[i].
"""
dists = self.compute_distances(X)
return self.predict_labels(dists, k=k)
def compute_distances(self, X):
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
# (x - y)^2 = x^2 + y^2 - 2xy
dists = np.sqrt(
np.sum(X**2, axis=1,keepdims = True)
- 2 * np.dot(X, self.X_train.T)
+ np.sum(X_train**2, axis = 1, keepdims = True).T
)
return dists
Q:
With N examples,how fast are training and prediction?
Ans
: Train O(1), predict O(N)
This is bad: we want classifiers that are
fast at prediction;
slow
for training is ok
<