Пример #1
0
        mnist_dataset(const std::string& path, const bool out_of_n_coding = true){
            std::cout << "Reading MNIST dataset..."<<std::flush;
            std::ifstream ftraind((path + "/train-images.idx3-ubyte").c_str(),std::ios::in | std::ios::binary); // image data
            std::ifstream ftrainl((path + "/train-labels.idx1-ubyte").c_str(),std::ios::in | std::ios::binary); // label data
            std::ifstream ftestd ((path + "/t10k-images.idx3-ubyte").c_str(),std::ios::in | std::ios::binary); // image data
            std::ifstream ftestl ((path + "/t10k-labels.idx1-ubyte").c_str(),std::ios::in | std::ios::binary); // label data
            assert(ftraind.is_open());
            assert(ftrainl.is_open());
            assert(ftestd.is_open());
            assert(ftestl.is_open());

            char buf[16];
            ftraind.read(buf,16);
            ftrainl.read(buf, 8);
            ftestd.read(buf,16);
            ftestl.read(buf, 8);
            cuv::tensor<unsigned char,cuv::host_memory_space> traind(cuv::extents[60000][784]);
            cuv::tensor<unsigned char,cuv::host_memory_space> trainl(cuv::extents[60000]);
            cuv::tensor<unsigned char,cuv::host_memory_space> testd(cuv::extents[10000][784]);
            cuv::tensor<unsigned char,cuv::host_memory_space> testl(cuv::extents[10000]);
            ftraind.read((char*)traind.ptr(), traind.size()); assert(ftraind.good());
            ftrainl.read((char*)trainl.ptr(), trainl.size()); assert(ftrainl.good());
            ftestd.read((char*)testd.ptr(), testd.size());    assert(ftestd.good());
            ftestl.read((char*)testl.ptr(), testl.size());    assert(ftestl.good());

            train_data.resize(traind.shape());
            test_data.resize(testd.shape());
            convert(train_data , traind); // convert data type
            convert(test_data  , testd); // convert data type

            if (out_of_n_coding){
                train_labels.resize(cuv::extents[60000][10]);
                test_labels.resize(cuv::extents[10000][10]);
                train_labels = 0.f;
                test_labels = 0.f;
                for (unsigned int i = 0; i < trainl.size(); ++i){
                    train_labels(i, trainl[i]) = 1.f;
                }
                for (unsigned int i = 0; i < testl.size(); ++i){
                    test_labels(i, testl[i]) = 1.f;
                }
            } else {
                train_labels.resize(cuv::extents[60000]);
                test_labels.resize(cuv::extents[10000]);
                for (unsigned int i = 0; i < trainl.size(); ++i){
                    train_labels(i) = trainl[i];
                }
                for (unsigned int i = 0; i < testl.size(); ++i){
                    test_labels(i) = testl[i];
                }
            }

            //train_data = train_data[cuv::indices[cuv::index_range(0,5000)][cuv::index_range()]];
            //train_labels = train_labels[cuv::indices[cuv::index_range(0,5000)][cuv::index_range()]];

            binary = true;
            channels = 1;
            image_size = 28;
            std::cout << "done."<<std::endl;
        }
int main(int argc, char **argv[])
{
	string name;
	vector<Mat>Images(100), TestImages(50);
	vector<Mat> Descriptor(100), TestDescriptor(50), TestPcafeature(50);
	vector<vector<KeyPoint>>Keypoints(100), TestKeypoint(50);
	Mat histogram = Mat::zeros(100, Cluster, CV_32F);
	Mat Testhistogram = Mat::zeros(50, Cluster, CV_32F);
	Mat Keyword = Mat::zeros(Cluster, 20, CV_32F);
	Mat full_Descriptor, Pcafeature, Pcaduplicate, clusteridx, trainlabels(100, 1, CV_32F);
	vector<vector<DMatch>> matches(50);
	Mat predicted(Testhistogram.rows, 1, CV_32F);

	// Read Training Images.
	read_train(Images, name);

	//Calculate SIFT features for the Training Images.
	calculate_SIFT(Images,Keypoints,Descriptor);
	merge_descriptor(full_Descriptor,Descriptor);

	//Compute PCA for all the features across all Images.
	PCA pca;
	perform_PCA(full_Descriptor, Pcafeature, pca);
	
	//Perform K-Means on all the PCA reduced features.
	Pcafeature.convertTo(Pcaduplicate, CV_32F);
	calculate_Kmeans(Pcaduplicate, clusteridx);

	//Calculate the Keywords in the Feature Space.
	make_dictionary(clusteridx, Pcaduplicate, Keyword);

	//Get the Histogram for each Training Image.
	hist(Descriptor, clusteridx, histogram);

	//Read Test Image
	read_test(TestImages, name);

	//Calculate the SIFT feature for all the test Images.
	calculate_SIFT(TestImages, TestKeypoint, TestDescriptor);

	//Project the SIFT feature of each feature on the lower dimensional PCA plane calculated above. 
	pca_testProject(TestDescriptor, TestPcafeature, pca);

	//Find the Label by searching for keywords closest to current feature.
	get_matches(TestPcafeature,Keyword,matches);

	//Calculate Histogram for each test Image.
	hist_test(TestDescriptor, matches, Testhistogram);
	
	//Perform classification through Knn Classifier. 
	train_labels(trainlabels);
	KNearest knn;
	train_classifier(histogram, trainlabels, knn);
	test_classify(Testhistogram,predicted,knn);

	//Calculate Accuracy for each class.
	calculate_accuracy(predicted);
	
	getchar();
	return 0;
}