First two descriptors migrated

This commit is contained in:
Mark Hoekveen
2019-06-04 19:44:49 +02:00
parent 375626e6f6
commit 9bde653a2a
4 changed files with 76 additions and 81 deletions
+27 -34
View File
@@ -10,13 +10,13 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
img_path = image_path;
//Manual definitions.. SAD!
descriptor_types[7] = new DCTMedian();
/*descriptor_types[7] = new DCTMedian();
descriptor_types[6] = new DCTPearson();
descriptor_types[5] = new threshold();
descriptor_types[4] = new pearson();
descriptor_types[3] = new sobel();
descriptor_types[2] = new median();
descriptor_types[1] = new numColors();
descriptor_types[1] = new numColors();*/
descriptor_types[0] = new averageColor();
mutators[0] = new Grayscale();
@@ -47,7 +47,7 @@ void DescriptorManager::Init() {
}
fs::ifstream ifs;
fs::ofstream ofs;
LoadFeatures();
//LoadFeatures();
numImages = std::count_if(
fs::directory_iterator(img_path),
@@ -56,17 +56,18 @@ void DescriptorManager::Init() {
//foreach image in directory (non-recursive)
int filecount = 0;
for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) {
cout << "\r" << "Progress: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << ")" << flush;
if (!itr->path().has_extension()) continue; //skip directories and such
filecount++;
string image_name = itr->path().filename().generic_string();
cout << setw(60) << setfill('*') << "\r" << string(60, ' ') << flush;
cout << setfill('*') << setw(60) << right<< "\rProgress: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << "): " << image_name << flush;
Image* image = new Image;
image->read(itr->path().string());
//foreach descriptor
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) {
cout << "We already have feature for " << image_name << endl;
//cout << "We already have feature for " << image_name << endl;
continue;
}
//folder to store descriptors; create if not exists
@@ -76,24 +77,20 @@ void DescriptorManager::Init() {
//cout << "Starting with descriptor " << i << " on location " << descriptorBase.generic_string() << endl;
fs::path featurePath(descriptorBase / itr->path().stem());
bitset<64>* b1;
uint64_t b1;
if (fs::exists(featurePath)) { //feature on disk, load it in
ifs.open(featurePath, fs::fstream::binary);
unsigned long long n;
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
ifs.close();
b1 = new bitset<64>(n);
}
else { //not on disk, calculate it
b1 = new bitset<64>(descriptor_types[i]->feature(image));
unsigned long long n = b1->to_ullong();
b1 = descriptor_types[i]->feature(image);
ofs.open(featurePath, fs::fstream::binary);
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
ofs.close();
}
features[i][image_name] = *b1;
SaveFeatures();
delete b1;
features[i][image_name] = b1;
//SaveFeatures();
}
for (int i = 0; i < numMutations; i++) {
//Check if we have somewhere to store the mutation
@@ -114,7 +111,7 @@ void DescriptorManager::Init() {
mutated = mutators[i]->getMutated(image);
mutated->write(mutPath.generic_string());
auto duration = duration_cast<microseconds>(high_resolution_clock::now() - start);
cout << "Calculating mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
//cout << "Calculating mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
}
else {
@@ -123,7 +120,7 @@ void DescriptorManager::Init() {
mutated = new Image;
mutated->read(mutPath.generic_string());
auto duration = duration_cast<microseconds>(high_resolution_clock::now() - start);
cout << "Loading mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
//cout << "Loading mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
}
//now calculate all the features for the mutated images as well
@@ -136,27 +133,23 @@ void DescriptorManager::Init() {
fs::path featurePath(descriptorBase / itr->path().stem());
//cout << featurePath << endl;
string image_name = itr->path().filename().generic_string();
bitset<64>* b1 = NULL;
uint64_t b1;
if (!fs::exists(featurePath)) { //feature not yet calculated
//descriptor_types[i]->distance(b1, b1);
//descriptors[i][image_name] = b1;
b1 = new bitset<64>(descriptor_types[j]->feature(mutated));
unsigned long long n = b1->to_ullong();
b1 = descriptor_types[j]->feature(mutated);
//cout << "Writing " << *b1 << "(" << n << ")" << " to " << featurePath << endl;
ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
ofs.close();
}
else {
ifs.open(featurePath, fs::fstream::binary);
unsigned long long n;
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
ifs.close();
b1 = new bitset<64>(n);
}
mutatedFeatures[j][i][image_name] = *b1;
if(b1) delete b1;
mutatedFeatures[j][i][image_name] = b1;
}
delete mutated;
}
@@ -211,7 +204,7 @@ void DescriptorManager::LoadFeatures() {
//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) {
bitset<64> target;
uint64_t target;
if (features[method].count(image_name) && initialized) { //if we have image
target = features[method][image_name];
}
@@ -219,7 +212,7 @@ void DescriptorManager::rankImages(string image_name, int method) {
return;
}
distances.clear(); //start fresh
for (map<string, bitset<64>>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
for (map<string, uint64_t>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
unsigned long long d = descriptor_types[method]->distance(target, it->second);
pair<unsigned long long, string> p(d, it->first);
distances.insert(p); //pairs are compared by their first element..
@@ -230,7 +223,7 @@ void DescriptorManager::rankImages(string image_name, int method) {
//It inserts these mutated descriptors in a sorted way
//Assumes the distances array is filled with the normal distances
multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(string image_name, int method) {
bitset<64> target;
uint64_t target;
if (features[method].count(image_name) && initialized) { //this should always be the case
target = features[method][image_name];
}
@@ -238,7 +231,7 @@ multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(stri
//cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
multiset<pair<unsigned long long, string>> local_distance = distances; //get our local copy of the distances.
for (int i = 0; i < numMutations; i++) {
bitset<64> b1 = mutatedFeatures[method][i][image_name];
uint64_t b1 = mutatedFeatures[method][i][image_name];
unsigned long long d = descriptor_types[method]->distance(target, b1);
pair<unsigned long long, string> p(d, mutators[i]->ToString());
local_distance.insert(p);
@@ -265,7 +258,7 @@ multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(stri
void DescriptorManager::runExperiments() {
fs::ofstream ofs;
for (int i = 0; i < numDescriptors; i++) {
for (map<string, bitset<64>>::iterator it = features[i].begin(); it != features[i].end(); ++it) {
for (map<string, uint64_t>::iterator it = features[i].begin(); it != features[i].end(); ++it) {
//cout << "=== " << descriptor_types[i]->ToString() << ": " << it->first << " ===" << endl;
rankImages(it->first, i);
multiset<pair<unsigned long long, string>> local_distance = rankMutations(it->first, i);
@@ -300,13 +293,13 @@ void DescriptorManager::runExperiments() {
//assumes a < numDescriptors
void DescriptorManager::outputMap(int a) {
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
map<string, bitset<64>> a_map = features[a];
map<string, uint64_t> a_map = features[a];
if (a_map.begin() == a_map.end()) {
cout << "------------------------" << endl << "No data found" << endl << endl;
}
for (map<string, bitset<64>>::iterator it = a_map.begin(); it != a_map.end(); ++it) {
cout << it->first << " => " << it->second << " (" << it->second.to_ullong() << ")" << endl;
for (map<string, uint64_t>::iterator it = a_map.begin(); it != a_map.end(); ++it) {
cout << it->first << " => " << it->second << " (" << bitset<64>(it->second) << ")" << endl;
}
}