当前位置:网站首页>Stanford cs231n course assignment - nearest neighbor classifier

Stanford cs231n course assignment - nearest neighbor classifier

2022-07-23 05:10:00 Fu_ Tianshu

Course website :http://cs231n.stanford.edu/
Course materials :http://cs231n.stanford.edu/syllabus.html
Course PDF:http://cs231n.stanford.edu/slides/2020/lecture_2.pdf
Operation data :https://cs231n.github.io/classification/#k—nearest-neighbor-classifier
CIFAR-10 Data official website :http://www.cs.toronto.edu/~kriz/cifar.html

Here is the code , Note the loaded file path

import numpy as np
import os
import pickle
import time


class NearestNeighbor(object):
    def __init__(self):
        pass
    #  There is no training on this network , Just load all training data into memory 
    def train(self, X, y):
        # X by 50000x3072 Array of ,y by 50000x1 Array of 
        self.Xtr = X
        self.ytr = y

    def predict(self, X):
        #  Get the number of test data , In this case 10000
        num_test = X.shape[0]
        #  Generate a 10000x10000 Of all the 0 matrix , Used as a storage test to generate labels, The type of element is the same as that in the training data labels identical , In this example, it should be int32
        Ypred = np.zeros(num_test, dtype=self.ytr.dtype)
        #  Cycle through each picture to be tested , common 10000 Time 
        for i in range(num_test):
            print("testing %d" % i)
            #  Use L1 distance , Calculate all training pictures to i Distance between test pictures 
            # np.abs To calculate the absolute value 
            # np.sum(..., axis=1) In order to compare the results according to paragraph 2 Sum the expansion direction of the coordinate axes 
            # distances = np.sum(np.abs(self.Xtr - X[i,:]), axis=1)
            #  Use L2 distance , Calculate all training pictures to i Distance between test pictures 
            # np.square Square each element in the array 
            # np.sum(..., axis=1) In order to compare the results according to paragraph 2 Sum the expansion direction of the coordinate axes 
            # np.sqrt For square root 
            distances = np.sqrt(np.sum(np.square(self.Xtr - X[i, :]), axis=1))
            # np.argmin Used to find out distances The smallest element in ( That is, the training image closest to the test image ) Of index
            min_index = np.argmin(distances)
            Ypred[i] = self.ytr[min_index]
        return Ypred


def load_CIFAR10(path):
    xs = []
    ys = []
    #  loop ,b In turn 1,2,3,4,5
    for b in range(1,6):
        # os.path.join Used to splice file paths 
        f = os.path.join(path, 'data_batch_%d' % (b,))
        #  Load each data_batch_x file 
        X, Y = load_CIFAR_batch(f)
        #  load 5 Time ,xs by 50000 Data of pictures ,ys by 50000 individual 0-9 The number of 
        xs.append(X)
        ys.append(Y)
    # np.concatenate For splicing arrays ,???
    Xtr = np.concatenate(xs)
    Ytr = np.concatenate(ys)
    #  Delete variables X and Y,???
    del X, Y
    #  load test_batch file 
    Xte, Yte = load_CIFAR_batch(os.path.join(path, 'test_batch'))
    return Xtr, Ytr, Xte, Yte


def load_CIFAR_batch(filename):
    # with  It can close automatically after using up files (close) file ,'rb' Is a binary read-only file 
    with open(filename, 'rb') as f:
        # pickle.load Deserialize the target into an object 
        datadict = pickle.load(f, encoding='latin1')
        # batch After the file is deserialized, a dictionary is obtained , contain 'data' and 'labels' Two key
        ''' data It's a 10000x3072 Of numpy Array , Each row of the array stores one 32x32 Color image of . front 1024 Numbers are red (red), middle 1024 It's green (green), Last 1024 For blue (blue). labels It's a 10000 individual 0-9 List of numbers for . '''
        X = datadict['data']
        Y = datadict['labels']
        # reshape Used to change the format of data ,transpose The different axes used to exchange tensors ,astype To change np.array The data type of all data elements in the .
        X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float")
        #  hold Y The format of becomes np.array
        Y = np.array(Y)
        return X, Y


def runNN():
    print("load data")
    # Xtr For the picture data in the training data ,Ytr Label data in training data 
    # Xte For the picture data in the test data ,Yte Label data in test data 
    Xtr, Ytr, Xte, Yte = load_CIFAR10('data/cifar-10-batches-py')
    #  hold Xtr and Xte The format of the two variables becomes 50000x3072 Array of 
    Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3)
    Xte_rows = Xte.reshape(Xte.shape[0], 32 * 32 * 3)
    #  Construct a NearestNeighbor() Class object nn
    nn = NearestNeighbor()
    print("start training")
    #  Use training data to nn This network conducts training 
    nn.train(Xtr_rows, Ytr)
    print("start testing")
    #  Use the trained network nn To test 
    Yte_predict = nn.predict(Xte_rows)
    #  Statistical test accuracy 
    print('accuracy: %f' % (np.mean(Yte_predict == Yte)))


if __name__ == '__main__':
    print (" Starting time : ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    runNN()
    print (" End time : ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

Use L1 Print the distance

accuracy: 0.385900

Use L2 Print the distance

accuracy: 0.353900
原网站

版权声明
本文为[Fu_ Tianshu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221753364155.html