用Python制作的袋式Visual Words提供了可怕的准确性

时间:2018-07-04 08:15:29

标签: python image-processing machine-learning computer-vision

我正在尝试为自己实现一袋单词分类器,以对我拥有的数据集进行分类。为了确定我的实现是正确的,我只使用了Caltech数据集(http://www.vision.caltech.edu/Image_Datasets/Caltech101/)中的两个类来测试我的实现:大象和电吉他。由于它们在视觉上完全不同,我相信正确实施“视觉词袋”(BOVW)分类可以对这些图像进行准确的分类。

根据我的理解(如果我做错了,请纠正我),正确的BOVW分类分为三个步骤:

  1. 从训练图像中检测SIFT 128维描述符,并将其与k均值聚类。

  2. 在k均值分类器中对训练和测试图像SIFT描述符(在步骤1中进行了训练)进行测试,并制作分类结果的直方图。

  3. 将这些直方图用作SVM分类的特征向量

正如我之前解释的那样,我试图解决一个非常简单的问题,即对两个非常不同的类进行分类。我正在从文本文件中读取训练和测试文件,我使用训练图像SIFT描述符来训练k均值分类器,使用训练和测试图像获得分类的直方图,最后将它们用作分类的特征向量。

我的解决方案的源代码如下:

import numpy as np
from sklearn import svm
from sklearn.metrics import accuracy_score

#this function will get SIFT descriptors from training images and 
#train a k-means classifier    
def read_and_clusterize(file_images, num_cluster):

    sift_keypoints = []

    with open(file_images) as f:
        images_names = f.readlines()
        images_names = [a.strip() for a in images_names]

        for line in images_names:
        print(line)
        #read image
        image = cv2.imread(line,1)
        # Convert them to grayscale
        image =cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        # SIFT extraction
        sift = cv2.xfeatures2d.SIFT_create()
        kp, descriptors = sift.detectAndCompute(image,None)
        #append the descriptors to a list of descriptors
        sift_keypoints.append(descriptors)

    sift_keypoints=np.asarray(sift_keypoints)
    sift_keypoints=np.concatenate(sift_keypoints, axis=0)
    #with the descriptors detected, lets clusterize them
    print("Training kmeans")    
    kmeans = MiniBatchKMeans(n_clusters=num_cluster, random_state=0).fit(sift_keypoints)
    #return the learned model
    return kmeans

#with the k-means model found, this code generates the feature vectors 
#by building an histogram of classified keypoints in the kmeans classifier 
def calculate_centroids_histogram(file_images, model):

    feature_vectors=[]
    class_vectors=[]

    with open(file_images) as f:
        images_names = f.readlines()
        images_names = [a.strip() for a in images_names]

        for line in images_names:
        print(line)
        #read image
        image = cv2.imread(line,1)
        #Convert them to grayscale
        image =cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        #SIFT extraction
        sift = cv2.xfeatures2d.SIFT_create()
        kp, descriptors = sift.detectAndCompute(image,None)
        #classification of all descriptors in the model
        predict_kmeans=model.predict(descriptors)
        #calculates the histogram
        hist, bin_edges=np.histogram(predict_kmeans)
        #histogram is the feature vector
        feature_vectors.append(hist)
        #define the class of the image (elephant or electric guitar)
        class_sample=define_class(line)
        class_vectors.append(class_sample)

    feature_vectors=np.asarray(feature_vectors)
    class_vectors=np.asarray(class_vectors)
    #return vectors and classes we want to classify
    return class_vectors, feature_vectors


def define_class(img_patchname):

    #print(img_patchname)
    print(img_patchname.split('/')[4])

    if img_patchname.split('/')[4]=="electric_guitar":
        class_image=0

    if img_patchname.split('/')[4]=="elephant":
    class_image=1

    return class_image

def main(train_images_list, test_images_list, num_clusters):
    #step 1: read and detect SURF keypoints over the input image (train images) and clusterize them via k-means 
    print("Step 1: Calculating Kmeans classifier")
    model= bovw.read_and_clusterize(train_images_list, num_clusters)

    print("Step 2: Extracting histograms of training and testing images")
    print("Training")
    [train_class,train_featvec]=bovw.calculate_centroids_histogram(train_images_list,model)
    print("Testing")
    [test_class,test_featvec]=bovw.calculate_centroids_histogram(test_images_list,model)

    #vamos usar os vetores de treino para treinar o classificador
    print("Step 3: Training the SVM classifier")
    clf = svm.SVC()
    clf.fit(train_featvec, train_class)

    print("Step 4: Testing the SVM classifier")  
    predict=clf.predict(test_featvec)

    score=accuracy_score(np.asarray(test_class), predict)

    file_object  = open("results.txt", "a")
    file_object.write("%f\n" % score)
    file_object.close()

    print("Accuracy:" +str(score))

if __name__ == "__main__":
    main("train.txt", "test.txt", 1000)
    main("train.txt", "test.txt", 2000)
    main("train.txt", "test.txt", 3000)
    main("train.txt", "test.txt", 4000)
    main("train.txt", "test.txt", 5000)

如您所见,我试图改变kmeans分类器中的簇数。但是,无论我尝试什么,由于图像类的差别很大,准确度始终为53.62%。

那么,我对BOVW的理解或实施是否存在任何问题?我在这里弄错了什么?

2 个答案:

答案 0 :(得分:2)

解决方案比我想象的简单。

在这一行:

  hist, bin_edges=np.histogram(predict_kmeans)

垃圾箱数是numpy中标准垃圾箱数(我相信它是10)。通过这样做:

   hist, bin_edges=np.histogram(predict_kmeans, bins=num_clusters)

使用1000个簇和1000个维向量,准确度从我报告的53.62%提高到78.26%。

答案 1 :(得分:1)

您似乎正在为每个图像创建聚类和直方图。但是,为了使它起作用,您必须聚合所有图像的筛选功能,然后将这些特征聚类,并使用这些常见的聚类来创建直方图。还请检查https://github.com/shackenberg/Minimal-Bag-of-Visual-Words-Image-Classifier