python 2.7 - Confusion on cs231n image classification implementation -


i taking cs231n online class. trying implement simple image classification code using cifar-10 dataset, in http://cs231n.github.io/classification/

while running code, on predict function, time takes long, , didn't completed. copy-and-pasted codes. desktop has state-of-art machine.

the code shown below.

load function

import os; import cpickle pickle; import numpy np; import matplotlib.pyplot plt;  def load_cifar_batch(filename):      open(filename, 'r') f:         datadict=pickle.load(f);          x=datadict['data'];         y=datadict['labels'];          x=x.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float");         y=np.array(y);          return x, y;  def load_cifar10(root):      xs=[];     ys=[];      b in range(1,6):         f=os.path.join(root, "data_batch_%d" % (b, ));         x, y=load_cifar_batch(f);         xs.append(x);         ys.append(y);      xtr=np.concatenate(xs);     ytr=np.concatenate(ys);      del x, y;      xte, yte=load_cifar_batch(os.path.join(root, "test_batch"));      return xtr, ytr, xte, yte;  def visualize_cifar(x_train, y_train, samples_per_class):     classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'];     num_classes=len(classes);      y, cls in enumerate(classes):         idxs = np.flatnonzero(y_train == y)         idxs = np.random.choice(idxs, samples_per_class, replace=false)         i, idx in enumerate(idxs):             plt_idx = * num_classes + y + 1             plt.subplot(samples_per_class, num_classes, plt_idx)             plt.imshow(x_train[idx].astype('uint8'))             plt.axis('off')             if == 0:                 plt.title(cls)      plt.show(); 

nearest neighbor function

class nearestneighbor:     def __init__(self):         pass      def train(self, x, y):         """x n x d each row example. y 1-dimension of size n"""         # nearest neighbor classifier remembers training data         self.xtr = x         self.ytr = y      def predict(self, x):         """x n x d each row example wish predict label for"""         num_test = x.shape[0]         # lets make sure output type matches input type         ypred = np.zeros(num_test, dtype = self.ytr.dtype)          # loop on test rows         in xrange(num_test):             # find nearest training image i'th test image             # using l1 distance (sum of absolute value differences)             distances = np.sum(np.abs(self.xtr - x[i,:]), axis = 1)             min_index = np.argmin(distances) # index smallest distance             ypred[i] = self.ytr[min_index] # predict label of nearest example          return ypred  xtr, ytr, xte, yte = load_cifar10('/home/tegg/downloads/cifar-10-batches-py/') # flatten out images one-dimensional xtr_rows = xtr.reshape(xtr.shape[0], 32*32*3) # xtr_rows become 50000 x 3072 xte_rows = xte.reshape(xte.shape[0], 32*32*3) # xte_rows become 10000 x 3072  nn = nearestneighbor() # create nearest neighbor classifier class nn.train(xtr_rows, ytr) # train classifier on training images , labels yte_predict = nn.predict(xte_rows) # predict labels on test images # , print classification accuracy, average number # of examples correctly predicted (i.e. label matches) 

i think last line having trouble.

yte_predict = nn.predict(xte_rows) 

while running code using ipython notebook, keep running code, , not showing result (finish compile). there wrong in code?


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -