KNN image recognition

KNN image recognition

#!/usr/bin/env python3.7
import torch
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import operator
import numpy as np
import matplotlib.pyplot as plt

batch_size = 100

def getXmean(X_train):
    X_train = np.reshape(X_train, (X_train.shape[0], -1))  # 将图片从二维展开为一维
    mean_image = np.mean(X_train, axis=0)  # 求出训练集所有图片每个像素位置上的平均值
    return mean_image


def centralized(X_test,mean_image):
    X_test = np.reshape(X_test, (X_test.shape[0], -1))  # 将图片从二维展开为一维
    X_test = X_test.astype(np.float)
    X_test -= mean_image  # 减去均值图像,实现零均值化
    return X_test



# MNIST dataset
train_dataset = dsets.MNIST(root = './pymnist', #
                           train = True, #
                           transform = None, #
                           #download = True) #
                           download = False) # 
test_dataset = dsets.MNIST(root = './pymnist', #
                           train = False, #
                           transform = None, #
                           #download = True) # 
                           download = False) # 
#loader train data and test date
train_loader = torch.utils.data.DataLoader(dataset = train_dataset, 
                                           batch_size = batch_size, 
                                           shuffle = True)  #
test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
                                          batch_size = batch_size,
                                          shuffle = True)

def kNN_classify(k,dis,X_train,x_train,Y_test):
    assert dis == 'E' or dis == 'M', 'dis must E or M£¬E´ú±íÅ·À­¾àÀ룬M´ú±íÂü¹þ¶Ù¾àÀë'
    num_test = Y_test.shape[0]  #
    labellist = []
    '''
    ʹÓÃÅ·À­¹«Ê½×÷Ϊ¾àÀë¶ÈÁ¿
    '''
    if (dis == 'E'):
        for i in range(num_test):
            # 
            distances = np.sqrt(np.sum(((X_train - np.tile(Y_test[i], (X_train.shape[0], 1))) ** 2), axis=1))
            nearest_k = np.argsort(distances)#
            topK = nearest_k[:k]#
            classCount = {}
            for i in topK: #
                classCount[x_train[i]] = classCount.get(x_train[i],0) + 1
            sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
            labellist.append(sortedClassCount[0][0])
        return np.array(labellist)
    elif(dis == 'M'):
        for i in range(num_test):
            # 
            distances = (np.sum((np.abs(X_train - np.tile(Y_test[i], (X_train.shape[0], 1)))), axis=1))
            nearest_k = np.argsort(distances)#
            topK = nearest_k[:k]#
            classCount = {}
            for i in topK: #
                classCount[x_train[i]] = classCount.get(x_train[i],0) + 1
            sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)
            labellist.append(sortedClassCount[0][0])
        return np.array(labellist)

if __name__=='__main__':
  print("train_data:", train_dataset.train_data.size())
  print("train_labels:", train_dataset.train_labels.size())
  print("test_data:", test_dataset.train_data.size())
  print("test_labels:", train_dataset.test_labels.size())

  #show train data 0
  digit = train_loader.dataset.train_data[0]
  plt.imshow(digit,cmap=plt.cm.binary)
  plt.show()
  print(train_loader.dataset.train_labels[0])


  X_train = train_loader.dataset.train_data.numpy()
  mean_image = getXmean(X_train)
  X_train = centralized(X_train,mean_image)
  y_train = train_loader.dataset.train_labels.numpy()
  X_test = test_loader.dataset.test_data[:1000].numpy()
  X_test = centralized(X_test,mean_image)
  y_test = test_loader.dataset.test_labels[:1000].numpy()
  num_test = y_test.shape[0]
  y_test_pred = kNN_classify(5, 'M', X_train, y_train, X_test)
  num_correct = np.sum(y_test_pred == y_test)
  accuracy = float(num_correct) / num_test
  print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))

  mean_image = getXmean(X_train)
  cdata = centralized(test_loader.dataset.test_data.numpy(),mean_image)
  cdata = cdata.reshape(cdata.shape[0],28,28)
  plt.imshow(cdata[0],cmap=plt.cm.binary)
  plt.show()
  print(test_loader.dataset.test_labels[0]) #
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值