New init done.

Still needs some testing.
This commit is contained in:
Mark Hoekveen
2018-10-10 14:50:12 +02:00
parent 06aa50e3b4
commit d0cbf6b420
2 changed files with 27 additions and 17 deletions
+25 -16
View File
@@ -74,7 +74,7 @@ void DescriptorManager::Init() {
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n)); ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
ofs.close(); ofs.close();
} }
descriptors[i][image_name] = *b1; features[i][image_name] = *b1;
delete b1; delete b1;
} }
for (int i = 0; i < numMutations; i++) { for (int i = 0; i < numMutations; i++) {
@@ -90,25 +90,34 @@ void DescriptorManager::Init() {
//now calculate all the features for the mutated images as well //now calculate all the features for the mutated images as well
//foreach descriptor //foreach descriptor
for (int j = 0; j < numDescriptors; j++) { for (int j = 0; j < numDescriptors; j++) {
/*//folder to store descriptors; create if not exists //folder to store descriptors; create if not exists
fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath()); fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath());
//cout << "Doing mutated descriptors for " << descriptorBase << endl;
if (!fs::is_directory(descriptorBase)) if (!fs::is_directory(descriptorBase))
if (fs::create_directory(descriptorBase)) {} if (fs::create_directory(descriptorBase)) {}
fs::path featurePath(descriptorBase / itr->path().stem()); fs::path featurePath(descriptorBase / itr->path().stem());
//cout << featurePath << endl; //cout << featurePath << endl;
string image_name = itr->path().filename().generic_string(); string image_name = itr->path().filename().generic_string();
bitset<64>* b1;
if (!fs::exists(featurePath)) { //feature not yet calculated if (!fs::exists(featurePath)) { //feature not yet calculated
//cout << "Calculating " << image_name << endl;
//descriptor_types[i]->distance(b1, b1); //descriptor_types[i]->distance(b1, b1);
//descriptors[i][image_name] = b1; //descriptors[i][image_name] = b1;
bitset<64> b1 = descriptor_types[j]->feature(mutated); b1 = &descriptor_types[j]->feature(mutated);
unsigned long long n = b1.to_ullong(); unsigned long long n = b1->to_ullong();
cout << "Writing " << b1 << "(" << n << ")" << " to " << featurePath << endl; 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.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*>(&n), sizeof(n));
ofs.close(); ofs.close();
}*/ }
else {
ifs.open(featurePath, fs::fstream::binary);
unsigned long long n;
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
ifs.close();
b1 = new bitset<64>(n);
}
//Else...?
//Do we need to store mutated features in a cache? For large experiments it's probably faster.
mutatedFeatures[j][i][image_name] = *b1;
} }
delete mutated; delete mutated;
} }
@@ -146,7 +155,7 @@ bool DescriptorManager::loadDescriptors() {
ifs.close(); ifs.close();
bitset<64> b1(n); bitset<64> b1(n);
descriptors[i][image_name] = b1; features[i][image_name] = b1;
} }
else { //feature not yet calculated else { //feature not yet calculated
Image* image; //only load image from disk if not loaded before Image* image; //only load image from disk if not loaded before
@@ -161,7 +170,7 @@ bool DescriptorManager::loadDescriptors() {
//cout << "Calculating " << image_name << endl; //cout << "Calculating " << image_name << endl;
bitset<64> b1 = descriptor_types[i]->feature(image); bitset<64> b1 = descriptor_types[i]->feature(image);
//descriptor_types[i]->distance(b1, b1); //descriptor_types[i]->distance(b1, b1);
descriptors[i][image_name] = b1; features[i][image_name] = b1;
unsigned long long n = b1.to_ullong(); unsigned long long n = b1.to_ullong();
ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done
@@ -234,8 +243,8 @@ bool DescriptorManager::createMutations() {
//essentially insertion sort into a map //essentially insertion sort into a map
void DescriptorManager::similarImages(string image_name, int method, int numImages) { void DescriptorManager::similarImages(string image_name, int method, int numImages) {
bitset<64> target; bitset<64> target;
if (descriptors[method].count(image_name)) { //if image exists in map if (features[method].count(image_name)) { //if image exists in map
target = descriptors[method][image_name]; target = features[method][image_name];
} }
else { else {
Image* image = new Image; Image* image = new Image;
@@ -246,7 +255,7 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
//first we need all distances to this image.. and insert them in a sorted way //first we need all distances to this image.. and insert them in a sorted way
//multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows.. //multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows..
//distances.clear(); //start fresh //distances.clear(); //start fresh
for (map<string, bitset<64>>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) { for (map<string, bitset<64>>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
//cout << "ho " << endl; //cout << "ho " << endl;
unsigned long long d = descriptor_types[method]->distance(target, it->second); unsigned long long d = descriptor_types[method]->distance(target, it->second);
pair<unsigned long long, string> p(d, it->first); pair<unsigned long long, string> p(d, it->first);
@@ -269,11 +278,11 @@ void DescriptorManager::insertMutations(string image_name, int method) {
multiset<pair<unsigned long long, string> > local_distance; multiset<pair<unsigned long long, string> > local_distance;
fs::ifstream ifs; fs::ifstream ifs;
bitset<64> target; bitset<64> target;
if (descriptors[method].count(image_name)) { //if image exists in map if (features[method].count(image_name)) { //if image exists in map
target = descriptors[method][image_name]; target = features[method][image_name];
} }
cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl; cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
for (map<string, bitset<64>>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) { for (map<string, bitset<64>>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
//cout << "ho " << endl; //cout << "ho " << endl;
unsigned long long d = descriptor_types[method]->distance(target, it->second); unsigned long long d = descriptor_types[method]->distance(target, it->second);
pair<unsigned long long, string> p(d, it->first); pair<unsigned long long, string> p(d, it->first);
@@ -329,7 +338,7 @@ void DescriptorManager::checkImages(int method) {
void DescriptorManager::outputMap(int a) { void DescriptorManager::outputMap(int a) {
if (a != 2) return; if (a != 2) return;
cout << "Outputting map " << descriptor_types[a]->ToString() << endl; cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
map<string, bitset<64>> a_map = descriptors[a]; map<string, bitset<64>> a_map = features[a];
if (a_map.begin() == a_map.end()) { if (a_map.begin() == a_map.end()) {
cout << "------------------------" << endl << "No data found" << endl << endl; cout << "------------------------" << endl << "No data found" << endl << endl;
} }
+2 -1
View File
@@ -39,7 +39,8 @@ private:
static const int numMutations = 7; static const int numMutations = 7;
//TODO: automagically update this? //TODO: automagically update this?
map<string, bitset<64>> descriptors[numDescriptors]; //array of maps that will hold the descriptors map<string, bitset<64>> features[numDescriptors]; //array of maps that will hold the descriptors
map<string, bitset<64>> mutatedFeatures[numDescriptors][numMutations];
multiset<pair<unsigned long long, string> > distances; //multiset that stores the distances multiset<pair<unsigned long long, string> > distances; //multiset that stores the distances
fs::path img_path; //where the images are fs::path img_path; //where the images are
Descriptor* descriptor_types[numDescriptors]; Descriptor* descriptor_types[numDescriptors];