以下のリンクにあるCIFAR-10(ラベル付されたサイズが32x32のカラー画像8000万枚のデータセット)を読み取り、knnによりクラス分けしその精度を%で出力させたいのですが以下のエラー出てしまいました。問題は86行目のkにあるようですが解決方法がみつからず、何かアドバイス頂けると幸いです。
以下コード;
import pickle
import numpy as np
import os
def unpickle(file):
fo = open(file, 'rb')
dict = pickle.load(fo)
fo.close()
return dict
def conv_data2image(data):
return np.rollaxis(data.reshape((3,32,32)),0,3)
def get_cifar10(folder):
tr_data = np.empty((0,32*32*3))
tr_labels = np.empty(1)
'''
32x32x3
'''
for i in range(1,6):
fname = os.path.join(folder, "%s%d" % ("data_batch_", i))
data_dict = unpickle(fname)
if i == 1:
tr_data = data_dict['data']
tr_labels = data_dict['labels']
else:
tr_data = np.vstack((tr_data, data_dict['data']))
tr_labels = np.hstack((tr_labels, data_dict['labels']))
data_dict = unpickle(os.path.join(folder, 'test_batch'))
te_data = data_dict['data']
te_labels = np.array(data_dict['labels'])
bm = unpickle(os.path.join(folder, 'batches.meta'))
label_names = bm['label_names']
return tr_data, tr_labels, te_data, te_labels, label_names
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 xrange(num_test):
# find the nearest training image to the i'th test image
# using the L1 distance (sum of absolute value differences)
distances = np.sqrt(np.sum(np.square(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
if __name__ == '__main__':
datapath = "./data/cifar-10-batches-py"
Xtr, Ytr, Xte, Yte, label_names10 = get_cifar10(datapath)
Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3)
Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3)
# assume we have Xtr_rows, Ytr, Xte_rows, Yte as before
# recall Xtr_rows is 50,000 x 3072 matrix
Xval_rows = Xtr_rows[:1000, :] # take first 1000 for validation
Yval = Ytr[:1000]
Xtr_rows = Xtr_rows[1000:, :] # keep last 49,000 for train
Ytr = Ytr[1000:]
# find hyperparameters that work best on the validation set
validation_accuracies = []
for k in [1, 3, 5, 10, 20, 50, 100]:
# use a particular value of k and evaluation on validation data
nn = NearestNeighbor()
nn.train(Xtr_rows, Ytr)
# here we assume a modified NearestNeighbor class that can take a k as input
Yval_predict = nn.predict(Xval_rows, k=k)
acc = np.mean(Yval_predict == Yval)
print 'accuracy: %f' % (acc,)
# keep track of what works on the validation set
validation_accuracies.append((k, acc))
以下エラー
Traceback (most recent call last): File
"C:/…/PycharmProjects/Convolutional Neural Networks for Visual
Recognition/knn.py", line 86, in
Yval_predict = nn.predict(Xval_rows, k=k) TypeError: predict() got an unexpected keyword argument 'k'