KNN分类CIFAR-10,并且做Cross Validation,CIDAR-10数据库数据如下:

knn.py : 主要的试验流程

  1. from cs231n.data_utils import load_CIFAR10
  2. from cs231n.classifiers import KNearestNeighbor
  3. import random
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. # set plt params
  7. plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
  8. plt.rcParams['image.interpolation'] = 'nearest'
  9. plt.rcParams['image.cmap'] = 'gray'
  10.  
  11. cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
  12. x_train,y_train,x_test,y_test = load_CIFAR10(cifar10_dir)
  13. print'x_train : ',x_train.shape
  14. print'y_train : ',y_train.shape
  15. print'x_test : ',x_test.shape,'y_test : ',y_test.shape
  16.  
  17. #visual training example
  18. classes = ['plane','car','bird','cat','deer','dog','forg','horse','ship','truck']
  19. num_classes = len(classes)
  20. samples_per_class = 7
  21. for y,cls in enumerate(classes):
  22. #flaznonzero return indices_array of the none-zero elements
  23. # ten classes, y_train and y_test all in [1...10]
  24. idxs = np.flatnonzero(y_train == y)
  25. idxs = np.random.choice(idxs , samples_per_class, replace = False)
  26. for i,idx in enumerate(idxs):
  27. plt_idx = i*num_classes + y + 1
  28. # subplot(m,n,p)
  29. # m : length of subplot
  30. # n : width of subplot
  31. # p : location of subplot
  32. plt.subplot(samples_per_class,num_classes,plt_idx)
  33. plt.imshow(x_train[idx].astype('uint8'))
  34. # hidden the axis info
  35. plt.axis('off')
  36. if i == 0:
  37. plt.title(cls)
  38. plt.show()
  39.  
  40. # subsample data for more dfficient code execution
  41. num_training = 5000
  42. #range(5)=[0,1,2,3,4]
  43. mask = range(num_training)
  44. x_train = x_train[mask]
  45. y_train = y_train[mask]
  46. num_test = 500
  47. mask = range(num_test)
  48. x_test = x_test[mask]
  49. y_test = y_test[mask]
  50. #the image data has three chanels
  51. #the next two step shape the image size 32*32*3 to 3072*1
  52. x_train = np.reshape(x_train,(x_train.shape[0],-1))
  53. x_test = np.reshape(x_test,(x_test.shape[0],-1))
  54. print 'after subsample and re shape:'
  55. print 'x_train : ',x_train.shape," x_test : ",x_test.shape
  56. #KNN classifier
  57. classifier = KNearestNeighbor()
  58. classifier.train(x_train,y_train)
  59. # compute the distance between test_data and train_data
  60. dists = classifier.compute_distances_no_loops(x_test)
  61. #each row is a single test example and its distances to training example
  62. print 'dist shape : ',dists.shape
  63. plt.imshow(dists , interpolation='none')
  64. plt.show()
  65. y_test_pred = classifier.predict_labels(dists,k = 5)
  66. num_correct = np.sum(y_test_pred == y_test)
  67. acc = float(num_correct)/num_test
  68. print'k=5 ,The Accurancy is : ', acc
  69.  
  70. #Cross-Validation
  71.  
  72. #5-fold cross validation split the training data to 5 pieces
  73. num_folds = 5
  74. #k is params of knn
  75. k_choice = [1,5,8,11,15,18,20,50,100]
  76. x_train_folds = []
  77. y_train_folds = []
  78. x_train_folds = np.array_split(x_train,num_folds)
  79. y_train_folds = np.array_split(y_train,num_folds)
  80.  
  81. k_to_acc={}
  82.  
  83. for k in k_choice:
  84. k_to_acc[k] =[]
  85. for k in k_choice:
  86. print 'cross validation : k = ', k
  87. for j in range(num_folds):
  88. #vstack :stack the array to matrix
  89. #vertical
  90. x_train_cv = np.vstack(x_train_folds[0:j]+x_train_folds[j+1:])
  91. x_test_cv = x_train_folds[j]
  92.  
  93. #>>> a = np.array((1,2,3))
  94. #>>> b = np.array((2,3,4))
  95. #>>> np.hstack((a,b))
  96. # horizontally
  97. y_train_cv = np.hstack(y_train_folds[0:j]+y_train_folds[j+1:])
  98. y_test_cv = y_train_folds[j]
  99.  
  100. classifier.train(x_train_cv,y_train_cv)
  101. dists_cv = classifier.compute_distances_no_loops(x_test_cv)
  102. y_test_pred = classifier.predict_labels(dists_cv,k)
  103. num_correct = np.sum(y_test_pred == y_test_cv)
  104. acc = float(num_correct)/ num_test
  105. k_to_acc[k].append(acc)
  106. print k_to_acc

k_nearest_neighbor.py : knn算法的实现:

  1. import numpy as np
  2. from collections import Counter
  3. class KNearestNeighbor(object):
  4. """ a kNN classifier with L2 distance """
  5.  
  6. def __init__(self):
  7. pass
  8.  
  9. def train(self, X, y):
  10. """
  11. Train the classifier. For k-nearest neighbors this is just
  12. memorizing the training data.
  13.  
  14. Inputs:
  15. - X: A numpy array of shape (num_train, D) containing the training data
  16. consisting of num_train samples each of dimension D.
  17. - each row is a training example
  18. - y: A numpy array of shape (N,) containing the training labels, where
  19. y[i] is the label for X[i].
  20. """
  21. self.X_train = X
  22. self.y_train = y
  23.  
  24. def predict(self, X, k=1, num_loops=0):
  25. """
  26. Predict labels for test data using this classifier.
  27.  
  28. Inputs:
  29. - X: A numpy array of shape (num_test, D) containing test data consisting
  30. of num_test samples each of dimension D.
  31. - k: The number of nearest neighbors that vote for the predicted labels.
  32. - num_loops: Determines which implementation to use to compute distances
  33. between training points and testing points.
  34.  
  35. Returns:
  36. - y: A numpy array of shape (num_test,) containing predicted labels for the
  37. test data, where y[i] is the predicted label for the test point X[i].
  38. """
  39. if num_loops == 0:
  40. dists = self.compute_distances_no_loops(X)
  41. elif num_loops == 1:
  42. dists = self.compute_distances_one_loop(X)
  43. elif num_loops == 2:
  44. dists = self.compute_distances_two_loops(X)
  45. else:
  46. raise ValueError('Invalid value %d for num_loops' % num_loops)
  47.  
  48. return self.predict_labels(dists, k=k)
  49. def compute_distances_two_loops(self, X):
  50. """
  51. Compute the distance between each test point in X and each training point
  52. in self.X_train using a nested loop over both the training data and the
  53. test data.
  54.  
  55. Inputs:
  56. - X: A numpy array of shape (num_test, D) containing test data.
  57.  
  58. Returns:
  59. - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
  60. is the Euclidean distance between the ith test point and the jth training
  61. point.
  62. """
  63. num_test = X.shape[0]
  64. num_train = self.X_train.shape[0]
  65. dists = np.zeros((num_test, num_train))
  66. for i in xrange(num_test):
  67. for j in xrange(num_train):
  68. #####################################################################
  69. # TODO: #
  70. # Compute the l2 distance between the ith test point and the jth #
  71. # training point, and store the result in dists[i, j]. You should #
  72. # not use a loop over dimension. #
  73. #####################################################################
  74. #Euclidean distance
  75. #dists[i,j] = np.sqrt(np.sum(X[i,:]-self.X_train[j,:])**2)
  76. # use linalg make it more easy
  77. dists[i,j] = np.linalg.norm(self.X_train[j,:]-X[i,:])
  78. #####################################################################
  79. # END OF YOUR CODE #
  80. #####################################################################
  81. return dists
  82.  
  83. def compute_distances_one_loop(self, X):
  84. """
  85. Compute the distance between each test point in X and each training point
  86. in self.X_train using a single loop over the test data.
  87.  
  88. Input / Output: Same as compute_distances_two_loops
  89. """
  90. num_test = X.shape[0]
  91. num_train = self.X_train.shape[0]
  92. dists = np.zeros((num_test, num_train))
  93. for i in xrange(num_test):
  94. #######################################################################
  95. # TODO: #
  96. # Compute the l2 distance between the ith test point and all training #
  97. # points, and store the result in dists[i, :]. #
  98. #######################################################################
  99. #evevy row minus X[i,:] then norm it
  100. # axis = 1 imply operations by row
  101. dist[i,:] = np.linalg.norm(self.X_train - X[i,:],axis = 1)
  102. #######################################################################
  103. # END OF YOUR CODE #
  104. #######################################################################
  105. return dists
  106.  
  107. def compute_distances_no_loops(self, X):
  108. """
  109. Compute the distance between each test point in X and each training point
  110. in self.X_train using no explicit loops.
  111.  
  112. Input / Output: Same as compute_distances_two_loops
  113. """
  114. num_test = X.shape[0]
  115. num_train = self.X_train.shape[0]
  116. dists = np.zeros((num_test, num_train))
  117. #########################################################################
  118. # TODO: #
  119. # Compute the l2 distance between all test points and all training #
  120. # points without using any explicit loops, and store the result in #
  121. # dists. #
  122. # #
  123. # You should implement this function using only basic array operations; #
  124. # in particular you should not use functions from scipy. #
  125. # #
  126. # HINT: Try to formulate the l2 distance using matrix multiplication #
  127. # and two broadcast sums. #
  128. #########################################################################
  129. M = np.dot(X , self.X_train.T)
  130. te = np.square(X).sum(axis = 1)
  131. tr = np.square(self.X_train).sum(axis = 1)
  132. dists = np.sqrt(-2*M +tr+np.matrix(te).T)
  133. #########################################################################
  134. # END OF YOUR CODE #
  135. #########################################################################
  136. return dists
  137.  
  138. def predict_labels(self, dists, k=1):
  139. """
  140. Given a matrix of distances between test points and training points,
  141. predict a label for each test point.
  142.  
  143. Inputs:
  144. - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
  145. gives the distance betwen the ith test point and the jth training point.
  146.  
  147. Returns:
  148. - y: A numpy array of shape (num_test,) containing predicted labels for the
  149. test data, where y[i] is the predicted label for the test point X[i].
  150. """
  151. num_test = dists.shape[0]
  152. y_pred = np.zeros(num_test)
  153. for i in xrange(num_test):
  154. # A list of length k storing the labels of the k nearest neighbors to
  155. # the ith test point.
  156. closest_y = []
  157. #########################################################################
  158. # TODO: #
  159. # Use the distance matrix to find the k nearest neighbors of the ith #
  160. # testing point, and use self.y_train to find the labels of these #
  161. # neighbors. Store these labels in closest_y. #
  162. # Hint: Look up the function numpy.argsort. #
  163. #########################################################################
  164. labels = self.y_train[np.argsort(dists[i,:])].flatten()
  165. closest_y = labels[0:k]
  166. #########################################################################
  167. # TODO: #
  168. # Now that you have found the labels of the k nearest neighbors, you #
  169. # need to find the most common label in the list closest_y of labels. #
  170. # Store this label in y_pred[i]. Break ties by choosing the smaller #
  171. # label. #
  172. #########################################################################
  173. c = Counter(closest_y)
  174. y_pred[i] = c.most_common(1)[0][0]
  175. #########################################################################
  176. # END OF YOUR CODE #
  177. #########################################################################
  178.  
  179. return y_pred

data_utils.py : CIFAR-10数据的读取

  1. import cPickle as pickle
  2. import numpy as np
  3. import os
  4. from scipy.misc import imread
  5.  
  6. def load_CIFAR_batch(filename):
  7. """ load single batch of cifar """
  8. with open(filename, 'rb') as f:
  9. datadict = pickle.load(f)
  10. X = datadict['data']
  11. Y = datadict['labels']
  12. X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
  13. Y = np.array(Y)
  14. return X, Y
  15.  
  16. def load_CIFAR10(ROOT):
  17. """ load all of cifar """
  18. xs = []
  19. ys = []
  20. for b in range(1,6):
  21. f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
  22. X, Y = load_CIFAR_batch(f)
  23. xs.append(X)
  24. ys.append(Y)
  25. Xtr = np.concatenate(xs)
  26. Ytr = np.concatenate(ys)
  27. del X, Y
  28. Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
  29. return Xtr, Ytr, Xte, Yte
  30.  
  31. def load_tiny_imagenet(path, dtype=np.float32):
  32. """
  33. Load TinyImageNet. Each of TinyImageNet-100-A, TinyImageNet-100-B, and
  34. TinyImageNet-200 have the same directory structure, so this can be used
  35. to load any of them.
  36.  
  37. Inputs:
  38. - path: String giving path to the directory to load.
  39. - dtype: numpy datatype used to load the data.
  40.  
  41. Returns: A tuple of
  42. - class_names: A list where class_names[i] is a list of strings giving the
  43. WordNet names for class i in the loaded dataset.
  44. - X_train: (N_tr, 3, 64, 64) array of training images
  45. - y_train: (N_tr,) array of training labels
  46. - X_val: (N_val, 3, 64, 64) array of validation images
  47. - y_val: (N_val,) array of validation labels
  48. - X_test: (N_test, 3, 64, 64) array of testing images.
  49. - y_test: (N_test,) array of test labels; if test labels are not available
  50. (such as in student code) then y_test will be None.
  51. """
  52. # First load wnids
  53. with open(os.path.join(path, 'wnids.txt'), 'r') as f:
  54. wnids = [x.strip() for x in f]
  55.  
  56. # Map wnids to integer labels
  57. wnid_to_label = {wnid: i for i, wnid in enumerate(wnids)}
  58.  
  59. # Use words.txt to get names for each class
  60. with open(os.path.join(path, 'words.txt'), 'r') as f:
  61. wnid_to_words = dict(line.split('\t') for line in f)
  62. for wnid, words in wnid_to_words.iteritems():
  63. wnid_to_words[wnid] = [w.strip() for w in words.split(',')]
  64. class_names = [wnid_to_words[wnid] for wnid in wnids]
  65.  
  66. # Next load training data.
  67. X_train = []
  68. y_train = []
  69. for i, wnid in enumerate(wnids):
  70. if (i + 1) % 20 == 0:
  71. print 'loading training data for synset %d / %d' % (i + 1, len(wnids))
  72. # To figure out the filenames we need to open the boxes file
  73. boxes_file = os.path.join(path, 'train', wnid, '%s_boxes.txt' % wnid)
  74. with open(boxes_file, 'r') as f:
  75. filenames = [x.split('\t')[0] for x in f]
  76. num_images = len(filenames)
  77.  
  78. X_train_block = np.zeros((num_images, 3, 64, 64), dtype=dtype)
  79. y_train_block = wnid_to_label[wnid] * np.ones(num_images, dtype=np.int64)
  80. for j, img_file in enumerate(filenames):
  81. img_file = os.path.join(path, 'train', wnid, 'images', img_file)
  82. img = imread(img_file)
  83. if img.ndim == 2:
  84. ## grayscale file
  85. img.shape = (64, 64, 1)
  86. X_train_block[j] = img.transpose(2, 0, 1)
  87. X_train.append(X_train_block)
  88. y_train.append(y_train_block)
  89.  
  90. # We need to concatenate all training data
  91. X_train = np.concatenate(X_train, axis=0)
  92. y_train = np.concatenate(y_train, axis=0)
  93.  
  94. # Next load validation data
  95. with open(os.path.join(path, 'val', 'val_annotations.txt'), 'r') as f:
  96. img_files = []
  97. val_wnids = []
  98. for line in f:
  99. img_file, wnid = line.split('\t')[:2]
  100. img_files.append(img_file)
  101. val_wnids.append(wnid)
  102. num_val = len(img_files)
  103. y_val = np.array([wnid_to_label[wnid] for wnid in val_wnids])
  104. X_val = np.zeros((num_val, 3, 64, 64), dtype=dtype)
  105. for i, img_file in enumerate(img_files):
  106. img_file = os.path.join(path, 'val', 'images', img_file)
  107. img = imread(img_file)
  108. if img.ndim == 2:
  109. img.shape = (64, 64, 1)
  110. X_val[i] = img.transpose(2, 0, 1)
  111.  
  112. # Next load test images
  113. # Students won't have test labels, so we need to iterate over files in the
  114. # images directory.
  115. img_files = os.listdir(os.path.join(path, 'test', 'images'))
  116. X_test = np.zeros((len(img_files), 3, 64, 64), dtype=dtype)
  117. for i, img_file in enumerate(img_files):
  118. img_file = os.path.join(path, 'test', 'images', img_file)
  119. img = imread(img_file)
  120. if img.ndim == 2:
  121. img.shape = (64, 64, 1)
  122. X_test[i] = img.transpose(2, 0, 1)
  123.  
  124. y_test = None
  125. y_test_file = os.path.join(path, 'test', 'test_annotations.txt')
  126. if os.path.isfile(y_test_file):
  127. with open(y_test_file, 'r') as f:
  128. img_file_to_wnid = {}
  129. for line in f:
  130. line = line.split('\t')
  131. img_file_to_wnid[line[0]] = line[1]
  132. y_test = [wnid_to_label[img_file_to_wnid[img_file]] for img_file in img_files]
  133. y_test = np.array(y_test)
  134.  
  135. return class_names, X_train, y_train, X_val, y_val, X_test, y_test
  136.  
  137. def load_models(models_dir):
  138. """
  139. Load saved models from disk. This will attempt to unpickle all files in a
  140. directory; any files that give errors on unpickling (such as README.txt) will
  141. be skipped.
  142.  
  143. Inputs:
  144. - models_dir: String giving the path to a directory containing model files.
  145. Each model file is a pickled dictionary with a 'model' field.
  146.  
  147. Returns:
  148. A dictionary mapping model file names to models.
  149. """
  150. models = {}
  151. for model_file in os.listdir(models_dir):
  152. with open(os.path.join(models_dir, model_file), 'rb') as f:
  153. try:
  154. models[model_file] = pickle.load(f)['model']
  155. except pickle.UnpicklingError:
  156. continue
  157. return models

通过 cv,最优的 k 值为7,accurancy=0.282,太低了,明天用cnn重复这个实验...

用KNN算法分类CIFAR-10图片数据的更多相关文章

  1. Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练

    在计算机视觉研究当中,HOG算法和LBP算法算是基础算法,但是却十分重要.后期很多图像特征提取的算法都是基于HOG和LBP,所以了解和掌握HOG,是学习计算机视觉的前提和基础. HOG算法的原理很多资 ...

  2. KNN算法——分类部分

    1.核心思想 如果一个样本在特征空间中的k个最相邻的样本中的大多数属于某一个类别,则该样本也属于这个类别,并具有这个类别上样本的特性.也就是说找出一个样本的k个最近邻居,将这些邻居的属性的平均值赋给该 ...

  3. KNN算法[分类算法]

    kNN(k-近邻)分类算法的实现 (1) 简介: (2)算法描述: (3) <?php /* *KNN K-近邻方法(分类算法的实现) */ /* *把.txt中的内容读到数组中保存,$file ...

  4. 机器学习实战(笔记)------------KNN算法

    1.KNN算法 KNN算法即K-临近算法,采用测量不同特征值之间的距离的方法进行分类. 以二维情况举例:         假设一条样本含有两个特征.将这两种特征进行数值化,我们就可以假设这两种特种分别 ...

  5. 吴裕雄--天生自然python机器学习实战:K-NN算法约会网站好友喜好预测以及手写数字预测分类实验

    实验设备与软件环境 硬件环境:内存ddr3 4G及以上的x86架构主机一部 系统环境:windows 软件环境:Anaconda2(64位),python3.5,jupyter 内核版本:window ...

  6. 机器学习【三】k-近邻(kNN)算法

    一.kNN算法概述 kNN算法是用来分类的,其依据测量不同特征值之间的距离,其核心思想在于用距离目标最近的k个样本数据的分类来代表目标的分类(这k个样本数据和目标数据最为相似).其精度高,对异常值不敏 ...

  7. 运用kNN算法识别潜在续费商家

    背景与目标 Youzan 是一家SAAS公司,服务于数百万商家,帮助互联网时代的生意人私有化顾客资产.拓展互联网客群.提高经营效率.现在,该公司希望能够从商家的交易数据中,挖掘出有强烈续费倾向的商家, ...

  8. KNN算法的R语言实现

    近邻分类 简言之,就是将未标记的案例归类为与它们最近相似的.带有标记的案例所在的类. 应用领域: 1.计算机视觉:包含字符和面部识别等 2.推荐系统:推荐受众喜欢电影.美食和娱乐等 3.基因工程:识别 ...

  9. 【Machine Learning】KNN算法虹膜图片识别

    K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

随机推荐

  1. LoaderManager使用详解(四)---实例:AppListLoader

    实例:AppListLoader   这篇文章将是我的第四篇,也就是最后一篇该系列的文章.请在评论里面告诉我他们是否有用.前面几篇文章的链接如下:   一:Loaders之前世界 二:了解Loader ...

  2. C# 设置程序开机自动运行(+注册表项)

    有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止.下面代码包含增加启动项到注册表和删除启动项. ...

  3. JS中的内部类

     js内部类 javascript中本身有提供一些可以直接使用的类,这种类就是内部类.主要有:Object.Array.Math.Boolean.String.Number.Date.RegExp. ...

  4. spring_150803_service

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  5. 【Apache运维基础(5)】Apache的Rewrite攻略(2)

    简述 .htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作 ...

  6. mq_getattr

    NAME mq_getattr - 获取消息队列的属性(REALTIME) SYNOPSIS #include <mqueue.h> int mq_getattr(mqd_t mqdes, ...

  7. GDB下查看内存命令(x命令)

    http://blog.csdn.net/allenlinrui/article/details/5964046 可以使用examine命令(简写是x)来查看内存地址中的值.x命令的语法如下所示: x ...

  8. sql server2008禁用远程连接

    1.打开SQL Server 配置管理器,双击左边 SQL Server 网络配置,点击TCP/IP协议,在协议一栏中,找到 全部侦听,修改为否,然后点击IP地址,将IP地址为127.0.0.1(IP ...

  9. Qt通过UDP传图片,实现自定义分包和组包

    一.包头结构体 //包头 struct PackageHeader { //包头大小(sizeof(PackageHeader)) unsigned int uTransPackageHdrSize; ...

  10. Rotate Matrix by One

    记得有道Amazon的OA题目,好像是给定一个矩阵,让把矩阵的每个元素向右shift一个位置.这道题之前没有好好自己想过.今天正好刷到了rotate matrix,所以正好一块想了. 思路是类似Lee ...