From 2fc2ae5abadfb0ba772823031adcdec4a8e1b664 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Mon, 8 Jul 2019 23:09:22 +0200 Subject: [PATCH] WIP: vector migration --- DescriptorManager.cpp | 71 ++++++++++++++++++++++++++----------------- DescriptorManager.h | 9 +++--- main.cpp | 4 +-- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/DescriptorManager.cpp b/DescriptorManager.cpp index 31dbe67..b55ffa4 100644 --- a/DescriptorManager.cpp +++ b/DescriptorManager.cpp @@ -76,9 +76,9 @@ Image* DescriptorManager::GetMutated(Image* image, string image_name, int method void DescriptorManager::LoadFeatures(string image_path, string image_name) { for (int i = 0; i < numDescriptors; i++) { //First check if we already have this information from the loaded cache - if (features[i].count(image_name) > 0) { + /*if (features[i].count(image_name) > 0) { continue; - } + }*/ if(!current_image) current_image = new Image(image_path); fs::path featurePath("meta" / descriptor_types[i]->ToPath() / image_name); @@ -94,7 +94,7 @@ void DescriptorManager::LoadFeatures(string image_path, string image_name) { ofs.write(reinterpret_cast(&b1), sizeof(b1)); ofs.close(); } - + features_vector[i].push_back(pair(image_name, b1)); features[i][image_name] = b1; } } @@ -103,9 +103,9 @@ void DescriptorManager::LoadFeatures(string image_path, string image_name) { void DescriptorManager::LoadMutationFeatures(string image_path, string image_name, int mutation) { for (int i = 0; i < numDescriptors; i++) { //First check if we already have this information from the loaded cache - if (mutatedFeatures[i][mutation].count(image_name) > 0) { + /*if (mutatedFeatures[i][mutation].count(image_name) > 0) { continue; - } + }*/ if (!current_image) current_image->read(image_path); Image* image = GetMutated(current_image, image_name, i); fs::path featurePath("meta" / descriptor_types[i]->ToPath() / mutators[mutation]->ToPath() / image_name); @@ -122,6 +122,7 @@ void DescriptorManager::LoadMutationFeatures(string image_path, string image_nam ofs.write(reinterpret_cast(&b1), sizeof(b1)); ofs.close(); } + mutatedFeatures_vector[i][mutation].push_back(pair(image_name, b1)); mutatedFeatures[i][mutation][image_name] = b1; delete image; } @@ -130,12 +131,22 @@ void DescriptorManager::LoadMutationFeatures(string image_path, string image_nam //Calculates all features, mutations and features of mutations. //Do this before anything else. void DescriptorManager::Init() { + high_resolution_clock::time_point t1 = high_resolution_clock::now(); LoadFromDisk(); + CompareFeatures(); + CopyFeatures(); + high_resolution_clock::time_point t2 = high_resolution_clock::now(); + duration time_span = duration_cast>(t2 - t1); + cout << "It took me " << time_span.count() << " seconds compare " << numImages << " images from disk cache." << endl; + return; + + int filecount = 0; - for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) { - if (!itr->path().has_extension()) continue; //skip directories and such - string image_name = itr->path().filename().generic_string(); - string image_path = itr->path().string(); + //for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) { + for (vector::iterator itr = images.begin() ; itr != images.end(); ++itr) { + //if (!itr->path().has_extension()) continue; //skip directories and such + string image_name = (*itr).filename().generic_string(); + string image_path = (*itr).string(); { //Progress bar filecount++; cout << setw(60) << setfill('*') << "\r" << string(60, ' ') << flush; @@ -149,7 +160,22 @@ void DescriptorManager::Init() { } if (current_image) delete current_image; current_image = NULL; - SaveToDisk(); + //SaveToDisk(); + } +} + +//Copies the features from the old map to the new vector +//Should only need to be called once. +void DescriptorManager::CopyFeatures() { + for (int method = 0; method < numDescriptors; method++) { + for (map::iterator it = features[method].begin(); it != features[method].end(); ++it) { + features_vector[method].push_back(*it); + } + for (int mutation = 0; mutation < numMutations; mutation++) { + for (map::iterator mit = mutatedFeatures[method][mutation].begin(); mit != mutatedFeatures[method][mutation].end(); ++mit) { + mutatedFeatures_vector[method][mutation].push_back(*mit); + } + } } } @@ -157,30 +183,19 @@ void DescriptorManager::Init() { //skips checking all images we already have. //If only one part is missing, we mark the image for recalculation void DescriptorManager::CompareFeatures() { - vector> features_vector; - vector> mutatedFeatures_vector[numMutations]; + //vector> features_vector; + //vector> mutatedFeatures_vector[numMutations]; for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) { if (!itr->path().has_extension()) continue; //skip directories and such string image_name = itr->path().filename().generic_string(); - { //Progress bar - filecount++; - cout << setw(60) << setfill('*') << "\r" << string(60, ' ') << flush; - cout << setfill('*') << setw(60) << right<< "\rChecking which images we already have: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << "): " << image_name << flush; - } - - for (int method = 0; method < numDescriptors; i++) { - bool load = false; - if (features[method].count(image_name) == 0) { - load = true; + for (int method = 0; method < numDescriptors; method++) { + if (features[method].count(image_name) == 0){ + images.push_back(itr->path()); } - for (int mutation = 0; !load && mutation < numMutations; mutation++) { - if (mutatedFeatures[method][mutation].count(image_name) == 0) { - load = true; - } - } - if (load) images.push_back(itr->path()); + //No need to check for mutated features, because they are always calculated and saved together } } + cout << "IMAGES: " << images.size() << endl; } //Saves feature map to disk. diff --git a/DescriptorManager.h b/DescriptorManager.h index bc8bd17..2ffaf27 100644 --- a/DescriptorManager.h +++ b/DescriptorManager.h @@ -44,7 +44,8 @@ public: void LoadMutationFeatures(string image_path, string image_name, int mutation); Image* GetMutated(Image* image, string image_name, int method); void Init(); - void CompareFeatures() + void CopyFeatures(); + void CompareFeatures(); void SaveToDisk(); void LoadFromDisk(); void rankImages(string image_name, int method); @@ -68,9 +69,9 @@ private: //TODO: Are maps the best container for us? Logarithmic random access is bad, but otherwise it might be fine? map features[numDescriptors]; //array of maps that will hold the descriptors map mutatedFeatures[numDescriptors][numMutations]; - vector> features_vector; - vector> mutatedFeatures_vector[numMutations]; - vector images; + vector> features_vector[numDescriptors]; + vector> mutatedFeatures_vector[numDescriptors][numMutations]; + vector images; multiset > distances; //multiset that stores the distances }; diff --git a/main.cpp b/main.cpp index f63e041..6a46403 100644 --- a/main.cpp +++ b/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char **argv) { manager->rankImages(imagename, method); manager->outputRank(imagename, method); manager->outputMap(method);*/ - cout << endl; + /*cout << endl; const int thread_count = 8; thread* thrds[thread_count]; for (int i =0; i < thread_count; i++) { @@ -39,7 +39,7 @@ int main(int argc, char **argv) { } for (int i =0; i < thread_count; i++) { thrds[i]->join(); - } + }*/ //manager->test(); return 0; }