KNN image recognition
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
train_dataset = dsets.MNIST(root = './pymnist',
train = True,
transform = None,
download = False)
test_dataset = dsets.MNIST(root = './pymnist',
train = False,
transform = None,
download = False)
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())
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])