Ejemplo n.º 1
0
def load_bow(booksize, files, mute=False):
    bow = np.zeros([len(files), booksize])
    cnt = -1
    for impath in files:
        cnt = cnt + 1
        if not mute:
            print '\r' + str(cnt) + '/' + str(len(files)) + '): ' + impath,
        filpat, filnam, filext = tools.fileparts(impath)
        filpat2, filnam2, filext2 = tools.fileparts(filpat)
        bow[cnt, :] = week56.load_bow('../../data/bow_objects/codebook_' + str(booksize) + '/' + filnam2 + '/' + filnam + '.pkl')
    if not mute:
        print ''
    return bow
Ejemplo n.º 2
0
# Get object data
files, labels, label_names, unique_labels, trainset, testset = week56.get_objects_filedata()
####

###
# Load Bag-Of-Words
###
C = 100
bow = np.zeros([len(files), C])
cnt = -1
for impath in files:
    cnt = cnt + 1
    print str(cnt) + '/' + str(len(files)) + '): ' + impath
    filpat, filnam, filext = tools.fileparts(impath)
    filpat2, filnam2, filext2 = tools.fileparts(filpat)
    bow[cnt, :] = week56.load_bow('../../data/bow_objects/codebook_' + str(C) + '/' + filnam2 + '/' + filnam + '.pkl')

###############################################################################
# Q1: IMPLEMENT HERE kNN CLASSIFIER. 
###############################################################################

# Normalize Bag-Of-Words
for i in range(len(files)):
    bow[i] = tools.normalizeL1(bow[i])

# k-NN Classifier
dist = np.zeros([len(testset),len(trainset)])
for j in range(len(testset)):
    for k in range(len(trainset)):
        b_j = bow[testset[j]]
        b_k = bow[trainset[k]]