if __name__ == '__main__': x_train, y_train = load_cifar10_data(dataset='train') x_test, y_test = load_cifar10_data(dataset='test') print("Dense SIFT feature extraction") x_train_feature = [extract_DenseSift_descriptors(img) for img in x_train] x_test_feature = [extract_DenseSift_descriptors(img) for img in x_test] x_train_kp, x_train_des = zip(*x_train_feature) x_test_kp, x_test_des = zip(*x_test_feature) print("Train/Test split: {:d}/{:d}".format(len(y_train), len(y_test))) print("Codebook Size: {:d}".format(VOC_SIZE)) print("Pyramid level: {:d}".format(PYRAMID_LEVEL)) print("Building the codebook, it will take some time") codebook = build_codebook(x_train_des, VOC_SIZE) # import cPickle # # with open('./spm_lv1_codebook.pkl', 'w') as f: # cPickle.dump(codebook, f) print("Spatial Pyramid Matching encoding") x_train = [ spatial_pyramid_matching(x_train[i], x_train_des[i], codebook, level=PYRAMID_LEVEL) for i in range(len(x_train)) ] x_test = [
print "抽取 SIFT 特征..." x_train = [extract_sift_descriptors(img) for img in x_train] x_test = [extract_sift_descriptors(img) for img in x_test] # 移除没有抽取出sift特征的图片 x_train = [each for each in zip(x_train, y_train) if each[0] is not None] x_train, y_train = zip(*x_train) x_test = [each for each in zip(x_test, y_test) if each[0] is not None] x_test, y_test = zip(*x_test) print "Train/Test split: {:d}/{:d}".format(len(y_train), len(y_test)) print "Vocab Size: {:d}".format(VOC_SIZE) # 使用Kmeans聚类算法对抽取出来的特征进行聚类,VOC_SIZE表示k-means中的k # 这一步可能需要很长时间... print "构建词典..." codebook = build_codebook(x_train, voc_size=VOC_SIZE) with open('./bow_codebook.pkl', 'w') as f: cPickle.dump(codebook, f) # 将图片转成它的向量化表示 print "图片向量化表示..." x_train = [input_vector_encoder(x, codebook) for x in x_train] x_train = np.asarray(x_train) x_test = [input_vector_encoder(each, codebook) for each in x_test] x_test = np.asarray(x_test) # 输入到分类器进行分类 print("训练与测试分类器...") svm_classifier(x_train, y_train, x_test, y_test)
x_train, y_train = load_cifar10_data(dataset='train') x_test, y_test = load_cifar10_data(dataset='test') print "SIFT feature extraction" x_train = [extract_sift_descriptors(img) for img in x_train] x_test = [extract_sift_descriptors(img) for img in x_test] # Remove None in SIFT extraction x_train = [each for each in zip(x_train, y_train) if each[0] != None] x_train, y_train = zip(*x_train) x_test = [each for each in zip(x_test, y_test) if each[0] != None] x_test, y_test = zip(*x_test) print "Train/Test split: {:d}/{:d}".format(len(y_train), len(y_test)) print "Codebook Size: {:d}".format(VOC_SIZE) print "Building the codebook, it will take some time" codebook = build_codebook(x_train, voc_size=VOC_SIZE) import cPickle with open('./bow_codebook.pkl','w') as f: cPickle.dump(codebook, f) print "Bag of words encoding" x_train = [input_vector_encoder(x, codebook) for x in x_train] x_train = np.asarray(x_train) x_test = [input_vector_encoder(each, codebook) for each in x_test] x_test = np.asarray(x_test) svm_classifier(x_train, y_train, x_test, y_test)
print "Training data load from {}".format(args.train) print "Test data load from {}".format(args.test) X, y = load_my_data(args.train) # to save time... X = X[:200] y = y[:200] print "Codebook Size: {:d}".format(spm.VOC_SIZE) print "Pyramid level: {:d}".format(spm.PYRAMID_LEVEL) print "Dense SIFT feature extraction" x_feature = [extract_DenseSift_descriptors(img) for img in X] x_kp, x_des = zip(*x_feature) print "Building the codebook, it will take some time" codebook = build_codebook(x_des, spm.VOC_SIZE) #import cPickle #with open('./data/codebook_spm.pkl','w') as f: # cPickle.dump(codebook, f) print "Spatial Pyramid Matching encoding" X = [spm.spatial_pyramid_matching(X[i], x_des[i], codebook, level=spm.PYRAMID_LEVEL) for i in xrange(len(x_des))] X = np.asarray(X) svm_classifier(X, y)
if __name__ == '__main__': x_train, y_train = load_cifar10_data(dataset='train') x_test, y_test = load_cifar10_data(dataset='test') print "Dense SIFT feature extraction" x_train_feature = [extract_DenseSift_descriptors(img) for img in x_train] x_test_feature = [extract_DenseSift_descriptors(img) for img in x_test] x_train_kp, x_train_des = zip(*x_train_feature) x_test_kp, x_test_des = zip(*x_test_feature) print "Train/Test split: {:d}/{:d}".format(len(y_train), len(y_test)) print "Codebook Size: {:d}".format(VOC_SIZE) print "Pyramid level: {:d}".format(PYRAMID_LEVEL) print "Building the codebook, it will take some time" codebook = build_codebook(x_train_des, VOC_SIZE) import cPickle with open('./spm_lv1_codebook.pkl','w') as f: cPickle.dump(codebook, f) print "Spatial Pyramid Matching encoding" x_train = [spatial_pyramid_matching(x_train[i], x_train_des[i], codebook, level=PYRAMID_LEVEL) for i in xrange(len(x_train))] x_test = [spatial_pyramid_matching(x_test[i], x_test_des[i], codebook, level=PYRAMID_LEVEL) for i in xrange(len(x_test))]