Code cleanups

This commit is contained in:
2019-06-29 13:52:38 +02:00
parent 8a78f03814
commit 845a3309b0
3 changed files with 12 additions and 56 deletions
+6 -41
View File
@@ -1,10 +1,6 @@
#include "DescriptorManager.h"
//Default constructor, don't use this
DescriptorManager::DescriptorManager() {
img_path = "data"; //default value
}
DescriptorManager::DescriptorManager() {} //don't use this
DescriptorManager::DescriptorManager(const fs::path & image_path) {
img_path = image_path;
if (!fs::exists(img_path)) {
@@ -75,9 +71,7 @@ Image* DescriptorManager::GetMutated(Image* image, string image_name, int method
return mutated;
}
//Takes the unmodified image and loads its features into memory.
//Either by reading them from disk, or calculating them directly.
//For each of the defined descriptors.
//Fills the features array with all descriptors for the given image.
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
@@ -104,6 +98,7 @@ void DescriptorManager::LoadFeatures(string image_path, string image_name) {
}
}
//Fills the mutatedFeatures array with all descriptors for the given mutation and image.
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
@@ -132,14 +127,13 @@ void DescriptorManager::LoadMutationFeatures(string image_path, string image_nam
}
//Calculates all features, mutations and features of mutations.
//This is generally the first step required
//Do this before anything else.
void DescriptorManager::Init() {
LoadFromDisk();
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();
//Image* image = new Image;
string image_path = itr->path().string();
{ //Progress bar
filecount++;
@@ -154,20 +148,15 @@ void DescriptorManager::Init() {
}
if (current_image) delete current_image;
current_image = NULL;
//delete image;
SaveToDisk();
}
initialized = true;
}
//Saves feature map to disk.
//Makes it so that it can reload easily
void DescriptorManager::SaveToDisk() {
{ //Boost serialization
ofstream f("features.map", ios::binary);
//ofstream feature_cache_backup("features.map.bak", ios::binary);
//feature_cache_backup << feature_cache.rdbuf();
ar::binary_oarchive oa(f);
oa << features;
@@ -175,13 +164,6 @@ void DescriptorManager::SaveToDisk() {
ar::binary_oarchive oam(fm);
oam << mutatedFeatures;
}
/*{ //YAS serialization
yas::file_ostream os("features.yas", yas::file_mode::file_trunc);
//const std::size_t flg = yas::binary|yas::file;
yas::binary_oarchive<yas::file_ostream, yas::file|yas::binary> oa(os);
oa & features;
//yas::save<yas::file|yas::binary>(image_name, features);
}*/
}
//Loads saved feature maps from disk.
@@ -198,16 +180,13 @@ void DescriptorManager::LoadFromDisk() {
bim >> mutatedFeatures;
}
}
{ //YAS
}
}
//Sorts all base (unmutated) images in order of distance from the given image
//After this function the distances array will be filled
void DescriptorManager::rankImages(string image_name, int method) {
uint64_t target;
if (features[method].count(image_name) && initialized) { //if we have image
if (features[method].count(image_name)) { //Check if this image was properly initialized
target = features[method][image_name];
}
else {
@@ -226,7 +205,7 @@ void DescriptorManager::rankImages(string image_name, int method) {
//Assumes the distances array is filled with the normal distances
multiset<pair<uint64_t, string>> DescriptorManager::rankMutations(string image_name, int method) {
uint64_t target;
if (features[method].count(image_name) && initialized) { //this should always be the case
if (features[method].count(image_name)) { //Check if this image was properly initialized
target = features[method][image_name];
}
@@ -240,22 +219,8 @@ multiset<pair<uint64_t, string>> DescriptorManager::rankMutations(string image_n
//upper_bound
}
return local_distance;
/*
//cout << "..." << endl;
for (int i = 0; i < numMutations; i++) {
int j = 0;
for (std::multiset<pair<uint64_t, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) {
//cout << "hey " << endl;
if (it->second == mutators[i]->ToString()) {
cout << j << "th position: ";
cout << "(" << it->first << ", " << it->second << ")" << endl;
}
j++;
}
}*/
}
//Needs to be called after Init().
//Takes all the calculated features and rankes
void DescriptorManager::runExperiments() {
fs::ofstream ofs;