CS231n笔记--图片线性分类

目录

 

图像分类面临的问题

Machine Learning: Data-Driven Approach

 K-Nearest Neighbor

原理

代码

结果

 L1与L2距离

应用情况

数据集的划分

cross-validation

Linear Classifier

模型

实际意义

对权重矩阵的解释

 对bias的处理

 损失函数SVM loss

代码

Softmax classifier

Softmax vs. SVM


图像分类面临的问题

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

<
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值