Esempio n. 1
0
def compute_dists(testset, trainset):
    dist = np.zeros([len(testset), len(trainset)])
    for i in range(len(testset)):
        b_i = tools.normalizeL1(bow[testset[i], :])
        for j in range(len(trainset)):
            b_j = tools.normalizeL1(bow[trainset[j], :])
            dist[i, j] = sum(np.minimum(b_i, b_j))
    return dist
Esempio n. 2
0
def match_sift(sift1,sift2, dist_thresh = 2.0):
    matches = -1 * np.ones((sift1.shape[0]), 'int')
    
    # FIRST NORMALIZE SIFT VECTORS
    sift1 = tools.normalizeL1(sift1, 0)
    sift2 = tools.normalizeL2(sift1, 0)
    
    # FOR ALL FEATURES IN SIFT1 FIND THE 2 CLOSEST FEATURES IN SIFT2
    N1 = sift1.shape[0]
    N2 = sift2.shape[0]

    matches = -1 * np.ones((sift1.shape[0]), 'int')
    dist = np.dot(sift1, sift2.T)
    for i in range(0, N1):
        temp = dist[i,:]
        indx = np.argsort(temp)
        indx = indx[::-1]
        if temp[indx[0]] / temp[indx[1]] > dist_thresh:
            matches[i] = indx[0]

    # IF SIFT1_i IS MUCH CLOSER TO SIFT2_j1 THAN SIFT2_j2, THEN CONSIDER THIS A MUCH
    # MUCH CLOSER MEANS MORE THAN A PREDEFINED DISTANCE THRESHOLD
        
    return matches
Esempio n. 3
0
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]]
        dist[j,k] = sum(np.minimum(b_j,b_k))

# Specify a maximum rank here. Default is 9.
K = 9

# Change image value here; (366 = goal), (150 = bicycle), (84 = beach), (450 = mountain).
query_id = int(150)
Esempio n. 4
0
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. 
#*****
# YOU CAN USE CODE FROM PREVIOUS WEEK

# Compute the distance matrix. dist =  len(testset) x len(trainset)
dist = np.zeros([len(testset),len(trainset)])
for i in range(len(testset)):
    b_i = tools.normalizeL1(bow[testset[i],:])
    for j in range(len(trainset)):
        b_j = tools.normalizeL1(bow[trainset[j],:])
        dist[i,j] = sum(np.minimum(b_i,b_j))

# Rank all images
ranking = np.argsort(dist,1)
ranking = ranking[:,::-1]

# The lines above have computed and sorted the distances for all test images
# Below, for a single test image we will visualize the results

# Get query id in {0 ... 599}, and transform it to the {0... 99} range
query_id = 366# (366, goal), (150, bicycle), (84, beach ), (450, mountain)
qinx = find(testset==query_id)
Esempio n. 5
0
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. 
# YOU CAN USE CODE FROM PREVIOUS WEEK

K = 9 
query_id = 366# (366, goal), (150, bicycle), (84, beach ), (450, mountain)
bow_qid = tools.normalizeL1(bow[query_id, :])
dist = []

for i in range(len(trainset)):
    bow_tmp = tools.normalizeL1(bow[trainset[i], :])
    dist.append(sum(np.minimum(bow_qid, bow_tmp)))
    
#ranking = np.argsort(dist[query_id, :])
ranking = np.argsort(dist)
ranking = ranking[::-1]
nearest_labels = labels[trainset[ranking[0 : K]]]

# VISUALIZE RESULTS
figure
subplot(2, 6, 1)
imshow(Image.open(files[query_id]))
Esempio n. 6
0
bows = []
for f in files:
    bows.append(week3.load_bow('../../data/bow/codebook_100/' + f))

# PART 5. STEP 2. COMPUTE DISTANCE MATRIX
dist = []
dist_type = 'intersect'
dist = np.zeros(len(files)**2).reshape((len(files),len(files)))
for i in range(0,len(files)):
    for j in range(0,len(files)):
        if dist_type == 'euclidean' or dist_type == 'l2':
            dist[i][j] = sum(((bows[i][k] - bows[j][k])**2) for k in range(len(bows[i])))
            tools.normalizeL2(dist[i][j])
        elif dist_type == 'intersect' or dist_type == 'l1':
            dist[i][j] = sum((np.minimum(bows[i][k], bows[j][k])) for k in range(len(bows[i])))
            tools.normalizeL1(dist[i][j])
        elif dist_type == 'chi2':
            dist[i][j] = sum((((bows[i][k] - bows[j][k]) **2) / (bows[i][k]+bows[j][k])) for k in range(len(bows[i])))
        elif dist_type == 'hellinger':
            dist[i][j] = sum((np.sqrt((bows[i][k]*bows[j][k]))) for k in range(len(bows[i])))
            tools.normalizeL2(dist[i][j])

print dist[i][j]

# PART 5. STEP 3. PERFORM RANKING SIMILAR TO WEEK 1 & 2 WITH QUERIES 'all_souls_000065.jpg', 'all_souls_0000XX.jpg', 'all_souls_0000XX.jpg'
query_id = int(89) #89, 21, 48
ranking = np.argsort(dist[query_id])

if dist_type == 'euclidean' or dist_type == 'intersect':
    ranking = np.argsort(dist[query_id])
elif dist_type == 'chi2' or dist_type == 'hellinger':