课程1(CS231N)
1、目标识别进展:
CS231n——图像分类(KNN实现)
GitHub
The CIFAR-10 dataset
1、根据物体的边缘和曲线进行识别
2、传统机器学习进行分类和识别
3、SIFT特征匹配整个目标。(结论:某些特征能够在变化中具有表现性和不变性)
4、深度学习(卷积神经网络)
传统机器学习存在的问题:可视化的数据非常复杂,由于数据复杂,所以模型的维度高,同时还存在许多需要调优的参数。当训练数据不够时,很快会产生过拟合。
2、图像分割:把一张图中的像素点归类到有意义的区域。
3、数据集:
PASCAL VOC 20个类别
IMAGE NET 2200个类别
KNN_CIFAR-10案例
1、距离公式总结与实现
1、欧式矩阵距离公式
计算两个矩阵之间的欧式距离(参考博客)
运算效率最高的算法是将训练集和测试集都使用矩阵表示,然后使用矩阵运算的方法替代之前的循环操作。但此操作需要我们对矩阵的运算规则非常熟悉。接下来着重记录如何计算两个矩阵之间的欧式距离。
记录测试集矩阵P的大小为MD,训练集矩阵C的大小为ND(测试集中共有M个点,每个点为D维特征向量。训练集中共有N个点,每个点为D维特征向量)
def compute_distances_no_loops(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using no explicit loops.
Input / Output: Same as compute_distances_two_loops
"""
num_test = X.shape[0]
print(X.shape[1])
num_train = self.X_train.shape[0]
print(self.X_train.shape[1])
dists = np.zeros((num_test, num_train))
#########################################################################
# TODO: #
# Compute the l2 distance between all test points and all training #
# points without using any explicit loops, and store the result in #
# dists. 在不使用任何显式循环的情况下,计算所有测试点和所有训练点之间的12个距离,并将结果存储在#dist中。 #
# #
# You should implement this function using only basic array operations; #
# in particular you should not use functions from scipy. #
# #
# HINT: Try to formulate the l2 distance using matrix multiplication #
# and two broadcast sums. #
#########################################################################
M = np.dot(X , self.X_train.T)
te = np.square(X).sum(axis = 1)
tr = np.square(self.X_train).sum(axis = 1)
print(tr.shape[0])
print((M+tr).shape)
dists = np.sqrt(-2*M +tr+np.matrix(te).T)
2、欧式距离循环公式
def cal_dis2(self,X):
test_num=X.shape[0]
train_num=self.X_train.shape[0]
dists=np.zeros((test_num,train_num))
for i in range(test_num):
for j in range(train_num):
dists[i][j]=np.sqrt(np.sum(np.square(X[i,:]-self.X_train[j,:])))
3、曼哈顿距离
def cal_distMHD(self,X):
test_num=X.shape[0]
train_num=self.X_train.shape[0]
dists=np.zeros((test_num,train_num))
for i in range(test_num):
for j in range(train_num):
dists[i][j]=np.sum(np.abs(X[i,:]-self.X_train[j,:]))
return dists
4、切比雪夫距离
def cal_distQX(self,X):
test_num=X.shape[0]
train_num=self.X_train.shape[0]
dists=np.zeros((test_num,train_num))
for i in range(test_num):
for j in range(train_num):
dists[i][j]=np.max(np.abs(X[i,:]-self.X_train[j,:]))
return dists
整个代码
#深度学习过程中,需要制作训练集和验证集、测试集。
#import os, random, shutil
#def moveFile(fileDir):
# pathDir = os.listdir(fileDir)#取图片的原始路径
# filenumber=len(pathDir)
# rate=0.5#自定义抽取图片的比例,比方说100张抽10张,那就是0.1
# picknumber=int(filenumber*rate) #按照rate比例从文件夹中取一定数量图片
# sample = random.sample(pathDir, picknumber)#随机选取picknumber数量的样本图片
# print (len(sample))
# for name in sample:
# shutil.move(fileDir+name, tarDir+name)
# return
#
#if __name__ == '__main__':
# fileDir = "D:/BaiduNetdiskDownload/train/"
# tarDir = 'D:/BaiduNetdiskDownload/train/data_batch_4/'
# moveFile(fileDir)
#
import os
import numpy as np
import pickle
import matplotlib.pyplot as plt
from collections import Counter
import pandas as pd
from sklearn.model_selection import KFold#引入K折交差验证
from sklearn.metrics import recall_score
class KNN(object):
def __init__(self):
pass
def train(self,X,Y):
self.X_train=X
self.Y_train=Y
#欧式距离0
def cal_dis0(self,X):
test_num=X.shape[0]
train_num=self.X_train.shape[0]
dists=np.zeros((test_num,train_num))
M=np.dot(X,self.X_train.T)
tr=np.square(self.X_train).sum(axis=1)
te=np.square(X).sum(axis=1)
dists=np.sqrt(np.matrix(te).T+tr-2*M)
return dists
#欧式距离2
def cal_dis2(self,X):
test_num=X.shape[0]
train_num=self.X_train.shape[0]
dists=np.zeros((test_num,train_num))
for i in range(test_num):