Refactoring for the ages

This commit is contained in:
2019-06-23 21:30:08 +02:00
parent 997ea91cb4
commit 8a78f03814
5 changed files with 189 additions and 376 deletions
+27 -54
View File
@@ -20,7 +20,7 @@ public:
//Interpret the bitsets as integers and calculate their distance. //Interpret the bitsets as integers and calculate their distance.
virtual uint64_t distance(uint64_t a, uint64_t b) { virtual uint64_t distance(uint64_t a, uint64_t b) {
return abs((long long)a - (long long)b); return abs((int64_t)(a - b));
}; };
virtual string ToString() = 0; virtual string ToString() = 0;
virtual fs::path ToPath() { virtual fs::path ToPath() {
@@ -37,8 +37,6 @@ public:
virtual void preview(Image* img) { virtual void preview(Image* img) {
cout << "No preview defined. :-( " << endl; cout << "No preview defined. :-( " << endl;
} }
//Helper functions
}; };
//Average color of the image. //Average color of the image.
@@ -52,7 +50,7 @@ public:
ssize_t m = img->rows(); ssize_t m = img->rows();
Pixels view(*img); Pixels view(*img);
const Quantum *p = view.getConst(0, 0, n, m); //entire image const Quantum *p = view.getConst(0, 0, n, m); //entire image
unsigned long long red = 0, green = 0, blue = 0; uint64_t red = 0, green = 0, blue = 0;
float numpixels = (float)(n*m); float numpixels = (float)(n*m);
if (img->channels() < 3) return 0; if (img->channels() < 3) return 0;
for (ssize_t j = 0; j < m; j++) { for (ssize_t j = 0; j < m; j++) {
@@ -152,19 +150,14 @@ public:
return retval; return retval;
} }
//biterror uint64_t distance(uint64_t a, uint64_t b) {
unsigned long long distance(uint64_t a, uint64_t b) { return (uint64_t)__builtin_popcountll(a^b);
int biterror = 0;
for (int i = 0; i < 64; i++) {
if (a[i] != b[i]) biterror++;
}
return biterror;
} }
string ToString() { string ToString() {
return "median"; return "median";
} }
}; };
/*
//Sobel descriptor //Sobel descriptor
//Calculates average intensity of the image with //Calculates average intensity of the image with
//sobel convolution applied in both directions //sobel convolution applied in both directions
@@ -185,7 +178,7 @@ public:
Pixels view(*local_image); Pixels view(*local_image);
const Quantum *p = view.getConst(0, 0, m, n); //entire image const Quantum *p = view.getConst(0, 0, m, n); //entire image
unsigned long long intensity = 0; uint64_t intensity = 0;
int chan = (int)local_image->channels(); int chan = (int)local_image->channels();
for (ssize_t j = 0; j < m; j++) { for (ssize_t j = 0; j < m; j++) {
for (ssize_t i = 0; i < n; i++) { for (ssize_t i = 0; i < n; i++) {
@@ -196,9 +189,9 @@ public:
} }
//cout << endl; //cout << endl;
} }
int scaledIntensity = ((float)(intensity / (n*m)) / QuantumRange) * 255; uint64_t scaledIntensity = ((float)(intensity / (n*m)) / QuantumRange) * 255;
delete local_image; delete local_image;
return uint64_t(scaledIntensity); return scaledIntensity;
} }
string ToString() { string ToString() {
return "sobel"; return "sobel";
@@ -223,29 +216,25 @@ public:
Pixels view(*local_image); Pixels view(*local_image);
int chan = (int)local_image->channels(); int chan = (int)local_image->channels();
const Quantum *p = view.getConst(0, 0, n, m); //entire image const Quantum *p = view.getConst(0, 0, n, m); //entire image
uint64_t retval(0); uint64_t retval = 0;
int prev = p[0]; //so we always start with a 0 int prev = p[0]; //so we always start with a 0
for (ssize_t j = 0; j < m; j++) { for (ssize_t j = 0; j < m; j++) {
for (ssize_t i = 0; i < n; i++) { for (ssize_t i = 0; i < n; i++) {
//LTR scanning //LTR scanning
//NOTE: Scanning order might be quite relevant for this descriptor //NOTE: Scanning order might be quite relevant for this descriptor
int cur = p[0]; int cur = p[0];
if(prev < cur) retval |= uint64_t(1); //XOR if(prev < cur) retval++;
prev = cur; retval <<= 1;
p = p + chan; p = p + chan;
retval <<= 1; prev = cur;
} }
} }
delete local_image; delete local_image;
return retval; return retval;
} }
unsigned long long distance(uint64_t a, uint64_t b) { uint64_t distance(uint64_t a, uint64_t b) {
int biterror = 0; return (uint64_t)__builtin_popcountll(a^b);
for (int i = 0; i < 64; i++) {
if (a[i] != b[i]) biterror++;
}
return biterror;
} }
string ToString() { string ToString() {
return "pearson"; return "pearson";
@@ -267,11 +256,10 @@ public:
Pixels view(*local_image); Pixels view(*local_image);
int chan = (int)local_image->channels(); int chan = (int)local_image->channels();
const Quantum *p = view.getConst(0, 0, n, m); //entire image const Quantum *p = view.getConst(0, 0, n, m); //entire image
uint64_t retval(0); uint64_t retval = 0;
for (ssize_t j = 0; j < m; j++) { for (ssize_t j = 0; j < m; j++) {
for (ssize_t i = 0; i < n; i++) { for (ssize_t i = 0; i < n; i++) {
if(p[0] != 0) if(p[0] != 0) retval++;
retval |= uint64_t(1); //XOR
retval <<= 1; retval <<= 1;
p = p + chan; p = p + chan;
@@ -280,12 +268,8 @@ public:
delete local_image; delete local_image;
return retval; return retval;
} }
unsigned long long distance(uint64_t a, uint64_t b) { uint64_t distance(uint64_t a, uint64_t b) {
int biterror = 0; return (uint64_t)__builtin_popcountll(a^b);
for (int i = 0; i < 64; i++) {
if (a[i] != b[i]) biterror++;
}
return biterror;
} }
string ToString() { string ToString() {
return "threshold"; return "threshold";
@@ -343,23 +327,17 @@ public:
double* converted = CalculateDCT(local_image); double* converted = CalculateDCT(local_image);
double prev = 0; double prev = 0;
uint64_t retval(0); uint64_t retval = 0;
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
if (converted[i] > prev) { if (converted[i] > prev) retval++;
retval |= uint64_t(1); //XOR
}
retval <<= 1; retval <<= 1;
prev = converted[i]; prev = converted[i];
} }
delete local_image; delete local_image;
return retval; return retval;
} }
unsigned long long distance(uint64_t a, uint64_t b) { uint64_t distance(uint64_t a, uint64_t b) {
int biterror = 0; return (uint64_t)__builtin_popcountll(a^b);
for (int i = 0; i < 64; i++) {
if (a[i] != b[i]) biterror++;
}
return biterror;
} }
string ToString() { string ToString() {
return "DCTPearson"; return "DCTPearson";
@@ -393,23 +371,18 @@ public:
sort(bullshit, bullshit + 64); sort(bullshit, bullshit + 64);
float median = (float)(bullshit[31] + bullshit[32]) / 2; float median = (float)(bullshit[31] + bullshit[32]) / 2;
uint64_t retval(0); uint64_t retval = 0;
for (ssize_t i = 0; i < 64; i++) { for (ssize_t i = 0; i < 64; i++) {
retval <<= 1; retval <<= 1;
if (converted[i] > median) if (converted[i] > median) retval++;
retval |= uint64_t(1);
} }
delete local_image; delete local_image;
return retval; return retval;
} }
unsigned long long distance(uint64_t a, uint64_t b) { uint64_t distance(uint64_t a, uint64_t b) {
int biterror = 0; return (uint64_t)__builtin_popcountll(a^b);
for (int i = 0; i < 64; i++) {
if (a[i] != b[i]) biterror++;
}
return biterror;
} }
string ToString() { string ToString() {
return "DCTMedian"; return "DCTMedian";
} }
};*/ };
+137 -127
View File
@@ -5,18 +5,25 @@ DescriptorManager::DescriptorManager() {
img_path = "data"; //default value img_path = "data"; //default value
} }
//Sets image path and defines the image descriptors.
DescriptorManager::DescriptorManager(const fs::path & image_path) { DescriptorManager::DescriptorManager(const fs::path & image_path) {
img_path = image_path; img_path = image_path;
if (!fs::exists(img_path)) {
cout << "Error. Data folder not found." << endl;
return; //No data: giving up.
}
numImages = std::count_if(
fs::directory_iterator(img_path),
fs::directory_iterator(),
static_cast<bool(*)(const fs::path&)>(fs::is_regular_file));
current_image = NULL;
//Manual definitions.. SAD! //Manual definitions.. SAD!
/*descriptor_types[7] = new DCTMedian(); descriptor_types[7] = new DCTMedian();
descriptor_types[6] = new DCTPearson(); descriptor_types[6] = new DCTPearson();
descriptor_types[5] = new threshold(); descriptor_types[5] = new threshold();
descriptor_types[4] = new pearson(); descriptor_types[4] = new pearson();
descriptor_types[3] = new sobel(); descriptor_types[3] = new sobel();
descriptor_types[2] = new median(); descriptor_types[2] = new median();
descriptor_types[1] = new numColors();*/ descriptor_types[1] = new numColors();
descriptor_types[0] = new averageColor(); descriptor_types[0] = new averageColor();
mutators[0] = new Grayscale(); mutators[0] = new Grayscale();
@@ -27,141 +34,136 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
mutators[5] = new Hue(); mutators[5] = new Hue();
mutators[6] = new Brighten(); mutators[6] = new Brighten();
mutators[7] = new Watermark(); mutators[7] = new Watermark();
//Check if all folders are available, create if needed
if (!fs::is_directory("meta"))
fs::create_directory("meta");
if (!fs::is_directory("results"))
fs::create_directory("results");
for (int i = 0; i < numDescriptors; i++) {
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
fs::path resultFolder("results" / descriptor_types[i]->ToPath());
if (!fs::is_directory(descriptorBase)) fs::create_directory(descriptorBase);
if (!fs::is_directory(resultFolder)) fs::create_directory(resultFolder);
for (int j = 0; j < numMutations; j++) {
fs::path mutatorDescriptor(descriptorBase / mutators[j]->ToPath());
if (!fs::is_directory(mutatorDescriptor)) fs::create_directory(mutatorDescriptor);
}
}
for (int i = 0; i < numMutations; i++) {
fs::path mutationBase(img_path / mutators[i]->ToPath());
if (!fs::is_directory(mutationBase)) fs::create_directory(mutationBase);
}
} }
//No need to call destructor.. yet? //Returns a pointer to the image mutated by the given method.
DescriptorManager::~DescriptorManager() { //Either gets it from disk, or calculates it directly.
//Do some clean-up? Image* DescriptorManager::GetMutated(Image* image, string image_name, int method) {
//Realistically, when this gets destroyed the application is done anyway. fs::path mutPath(img_path / mutators[method]->ToPath() / image_name);
//Let the OS take care of garbage collection Image* mutated;
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
mutated = mutators[method]->getMutated(image);
mutated->write(mutPath.generic_string());
}
else {
mutated = new Image;
mutated->read(mutPath.generic_string());
}
return mutated;
} }
//Loads all base images, calculates their descriptors, //Takes the unmodified image and loads its features into memory.
//calculates all image mutations and stores their descriptors //Either by reading them from disk, or calculating them directly.
//on disk. //For each of the defined descriptors.
void DescriptorManager::Init() { void DescriptorManager::LoadFeatures(string image_path, string image_name) {
//First load all images and store descriptors in cache and disk
if (!fs::exists(img_path)) {
cout << "Error. Data folder not found." << endl;
return;
}
fs::ifstream ifs;
fs::ofstream ofs;
//LoadFeatures();
numImages = std::count_if(
fs::directory_iterator(img_path),
fs::directory_iterator(),
static_cast<bool(*)(const fs::path&)>(fs::is_regular_file));
//foreach image in directory (non-recursive)
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
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++) { for (int i = 0; i < numDescriptors; i++) {
//First check if we already have this information from the loaded cache //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) {
//cout << "We already have feature for " << image_name << endl;
continue; continue;
} }
//folder to store descriptors; create if not exists current_image = new Image(image_path);
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath()); fs::path featurePath("meta" / descriptor_types[i]->ToPath() / image_name);
if (!fs::is_directory(descriptorBase))
fs::create_directory(descriptorBase);
//cout << "Starting with descriptor " << i << " on location " << descriptorBase.generic_string() << endl;
fs::path featurePath(descriptorBase / itr->path().stem());
uint64_t b1; uint64_t b1;
if (fs::exists(featurePath)) { //feature on disk, load it in if (fs::exists(featurePath)) { //feature on disk, load it in
ifs.open(featurePath, fs::fstream::binary); fs::ifstream ifs(featurePath, fs::fstream::binary);
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
ifs.close();
}
else { //not on disk, calculate it
b1 = descriptor_types[i]->feature(current_image);
fs::ofstream ofs(featurePath, fs::fstream::binary);
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
ofs.close();
}
features[i][image_name] = b1;
}
}
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) {
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);
uint64_t b1;
if (fs::exists(featurePath)) { //feature on disk, load it in
fs::ifstream ifs(featurePath, fs::fstream::binary);
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1)); ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
ifs.close(); ifs.close();
} }
else { //not on disk, calculate it else { //not on disk, calculate it
b1 = descriptor_types[i]->feature(image); b1 = descriptor_types[i]->feature(image);
ofs.open(featurePath, fs::fstream::binary); fs::ofstream ofs(featurePath, fs::fstream::binary);
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1)); ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
ofs.close(); ofs.close();
} }
features[i][image_name] = b1; mutatedFeatures[i][mutation][image_name] = b1;
//SaveFeatures();
}
for (int i = 0; i < numMutations; i++) {
//Check if we have somewhere to store the mutation
fs::path mutationBase(img_path / mutators[i]->ToPath());
if (!fs::is_directory(mutationBase))
fs::create_directory(mutationBase);
bool missing = false;
for (int j = 0; j < numDescriptors; j++) {
if (mutatedFeatures[j][i].count(image_name) <= 0) {
missing = true;
}
}
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
Image* mutated;
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
//Measured: about 10ms
auto start = high_resolution_clock::now();
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;
}
else {
//Measured: about 4ms
auto start = high_resolution_clock::now();
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;
}
//now calculate all the features for the mutated images as well
//foreach descriptor
for (int j = 0; j < numDescriptors; j++) {
//folder to store descriptors; create if not exists
fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath());
if (!fs::is_directory(descriptorBase))
if (fs::create_directory(descriptorBase)) {}
fs::path featurePath(descriptorBase / itr->path().stem());
//cout << featurePath << endl;
string image_name = itr->path().filename().generic_string();
uint64_t b1;
if (!fs::exists(featurePath)) { //feature not yet calculated
//descriptor_types[i]->distance(b1, b1);
//descriptors[i][image_name] = b1;
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*>(&b1), sizeof(b1));
ofs.close();
}
else {
ifs.open(featurePath, fs::fstream::binary);
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
ifs.close();
}
mutatedFeatures[j][i][image_name] = b1;
}
delete mutated;
}
delete image; delete image;
} }
}
//Calculates all features, mutations and features of mutations.
//This is generally the first step required
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++;
cout << setw(60) << setfill('*') << "\r" << string(60, ' ') << flush;
cout << setfill('*') << setw(60) << right<< "\rProgress: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << "): " << image_name << flush;
}
//Store all base features in the features array
LoadFeatures(image_path, image_name);
//now calculate all the features for the mutated images as well
for (int i = 0; i < numMutations; i++) {
LoadMutationFeatures(image_path, image_name, i);
}
if (current_image) delete current_image;
current_image = NULL;
//delete image;
SaveToDisk();
}
initialized = true; initialized = true;
} }
//Saves feature map to disk. //Saves feature map to disk.
//Makes it so that it can reload easily //Makes it so that it can reload easily
void DescriptorManager::SaveFeatures() { void DescriptorManager::SaveToDisk() {
{ //Boost serialization { //Boost serialization
ofstream f("features.map", ios::binary); ofstream f("features.map", ios::binary);
//ofstream feature_cache_backup("features.map.bak", ios::binary); //ofstream feature_cache_backup("features.map.bak", ios::binary);
@@ -183,7 +185,7 @@ void DescriptorManager::SaveFeatures() {
} }
//Loads saved feature maps from disk. //Loads saved feature maps from disk.
void DescriptorManager::LoadFeatures() { void DescriptorManager::LoadFromDisk() {
{ //Boost { //Boost
ifstream f("features.map", ios::binary); ifstream f("features.map", ios::binary);
if (f.is_open()) { if (f.is_open()) {
@@ -213,8 +215,8 @@ void DescriptorManager::rankImages(string image_name, int method) {
} }
distances.clear(); //start fresh distances.clear(); //start fresh
for (map<string, uint64_t>::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); uint64_t d = descriptor_types[method]->distance(target, it->second);
pair<unsigned long long, string> p(d, it->first); pair<uint64_t, string> p(d, it->first);
distances.insert(p); //pairs are compared by their first element.. distances.insert(p); //pairs are compared by their first element..
} }
} }
@@ -222,18 +224,18 @@ void DescriptorManager::rankImages(string image_name, int method) {
//Adds mutated descriptors of a given image to a copy of the distances array. //Adds mutated descriptors of a given image to a copy of the distances array.
//It inserts these mutated descriptors in a sorted way //It inserts these mutated descriptors in a sorted way
//Assumes the distances array is filled with the normal distances //Assumes the distances array is filled with the normal distances
multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(string image_name, int method) { multiset<pair<uint64_t, string>> DescriptorManager::rankMutations(string image_name, int method) {
uint64_t target; uint64_t target;
if (features[method].count(image_name) && initialized) { //this should always be the case if (features[method].count(image_name) && initialized) { //this should always be the case
target = features[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;
multiset<pair<unsigned long long, string>> local_distance = distances; //get our local copy of the distances. multiset<pair<uint64_t, string>> local_distance = distances; //get our local copy of the distances.
for (int i = 0; i < numMutations; i++) { for (int i = 0; i < numMutations; i++) {
uint64_t b1 = mutatedFeatures[method][i][image_name]; uint64_t b1 = mutatedFeatures[method][i][image_name];
unsigned long long d = descriptor_types[method]->distance(target, b1); uint64_t d = descriptor_types[method]->distance(target, b1);
pair<unsigned long long, string> p(d, mutators[i]->ToString()); pair<uint64_t, string> p(d, mutators[i]->ToString());
local_distance.insert(p); local_distance.insert(p);
//upper_bound //upper_bound
} }
@@ -242,7 +244,7 @@ multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(stri
//cout << "..." << endl; //cout << "..." << endl;
for (int i = 0; i < numMutations; i++) { for (int i = 0; i < numMutations; i++) {
int j = 0; int j = 0;
for (std::multiset<pair<unsigned long long, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) { for (std::multiset<pair<uint64_t, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) {
//cout << "hey " << endl; //cout << "hey " << endl;
if (it->second == mutators[i]->ToString()) { if (it->second == mutators[i]->ToString()) {
cout << j << "th position: "; cout << j << "th position: ";
@@ -261,18 +263,16 @@ void DescriptorManager::runExperiments() {
for (map<string, uint64_t>::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; //cout << "=== " << descriptor_types[i]->ToString() << ": " << it->first << " ===" << endl;
rankImages(it->first, i); rankImages(it->first, i);
multiset<pair<unsigned long long, string>> local_distance = rankMutations(it->first, i); multiset<pair<uint64_t, string>> local_distance = rankMutations(it->first, i);
//Now we can draw some conclusions from the received data: //Now we can draw some conclusions from the received data:
//Put all results in some useful files in the results directory: //Put all results in some useful files in the results directory:
fs::path resultFolder("results" / descriptor_types[i]->ToPath()); fs::path resultFolder("results" / descriptor_types[i]->ToPath());
if (!fs::is_directory(resultFolder)) //TODO: Create all folders in Init()
fs::create_directory(resultFolder);
for (int i = 0; i < numMutations; i++) { for (int i = 0; i < numMutations; i++) {
int j = 0; int j = 0;
fs::path resultPath(resultFolder / mutators[i]->ToPath()); fs::path resultPath(resultFolder / mutators[i]->ToPath());
ofs.open(resultPath, ofstream::out | ofstream::app); ofs.open(resultPath, ofstream::out | ofstream::app);
for (std::multiset<pair<unsigned long long, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) { for (std::multiset<pair<uint64_t, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) {
if (it->second == mutators[i]->ToString()) { if (it->second == mutators[i]->ToString()) {
double percentile = (j + 1) / (double)(numImages+numMutations); double percentile = (j + 1) / (double)(numImages+numMutations);
@@ -289,8 +289,7 @@ void DescriptorManager::runExperiments() {
} }
//outputs the gathered data to iostream //outputs the gathered data to iostream
//can be called directly or is called by its overload //Tries to do some nice formatting and grouping of bits.
//assumes a < numDescriptors
void DescriptorManager::outputMap(int a) { void DescriptorManager::outputMap(int a) {
cout << "Outputting map " << descriptor_types[a]->ToString() << endl; cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
map<string, uint64_t> a_map = features[a]; map<string, uint64_t> a_map = features[a];
@@ -299,7 +298,17 @@ void DescriptorManager::outputMap(int a) {
} }
for (map<string, uint64_t>::iterator it = a_map.begin(); it != a_map.end(); ++it) { 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; string bits = bitset<64>(it->second).to_string();
int groupsize = 8;
cout << setfill(' ') << setw(14) << it->first << " => " << setw(20) << it->second << " (";
for (int i=0; i < bits.length(); ++i) {
cout << bits[i];
if(i % groupsize == groupsize-1 && bits.length()-i > groupsize) cout << " ";
}
cout << ")" << endl;
} }
} }
@@ -312,7 +321,8 @@ void DescriptorManager::outputMap() {
//Outputs all generated features ordered on distance from the query image. //Outputs all generated features ordered on distance from the query image.
void DescriptorManager::outputRank(string imagename, int method) { void DescriptorManager::outputRank(string imagename, int method) {
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end(); ++it) { cout << "Outputting rank.." << endl;
for (std::multiset<pair<uint64_t, string>>::iterator it = distances.begin(); it != distances.end(); ++it) {
cout << "(" << it->first << ", " << it->second << ")" << endl; cout << "(" << it->first << ", " << it->second << ")" << endl;
} }
} }
+9 -7
View File
@@ -42,28 +42,30 @@ class DescriptorManager {
public: public:
DescriptorManager(); DescriptorManager();
DescriptorManager(const fs::path & image_path); DescriptorManager(const fs::path & image_path);
~DescriptorManager(); void LoadFeatures(string image_path, string image_name);
void LoadMutationFeatures(string image_path, string image_name, int mutation);
Image* GetMutated(Image* image, string image_name, int method);
void Init(); void Init();
void SaveFeatures(); void SaveToDisk();
void LoadFeatures(); void LoadFromDisk();
void rankImages(string image_name, int method); void rankImages(string image_name, int method);
multiset<pair<unsigned long long, string>> rankMutations(string image_name, int method); multiset<pair<uint64_t, string>> rankMutations(string image_name, int method);
void runExperiments(); void runExperiments();
void generatePreviews(string imagename); void generatePreviews(string imagename);
void outputMap(int a); void outputMap(int a);
void outputMap(); void outputMap();
void outputRank(string imagename, int method); void outputRank(string imagename, int method);
private: private:
static const int numDescriptors = 1; static const int numDescriptors = 8;
static const int numMutations = 8; static const int numMutations = 8;
//TODO: automagically update this? //TODO: automagically update this?
bool initialized = false; bool initialized = false;
int numImages; int numImages;
Image* current_image = NULL; //for caching purposes
map<string, uint64_t> features[numDescriptors]; //array of maps that will hold the descriptors map<string, uint64_t> features[numDescriptors]; //array of maps that will hold the descriptors
map<string, uint64_t> mutatedFeatures[numDescriptors][numMutations]; map<string, uint64_t> mutatedFeatures[numDescriptors][numMutations];
multiset<pair<unsigned long long, string> > distances; //multiset that stores the distances multiset<pair<uint64_t, 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];
Mutation* mutators[numMutations]; Mutation* mutators[numMutations];
-173
View File
@@ -1,173 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{757D5AAF-32EE-484D-AD0D-B80A212A8870}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SmallImageDescriptors</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(SolutionDir)\include;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)\include;C:\Program Files\boost;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)\lib;C:\Program Files\boost\libs;C:\Program Files\boost\stage\lib64;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>CORE_RL_Magick++_.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>CORE_RL_Magick++_.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy "$(TargetPath)" "$(SolutionDir)"</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Descriptor.h" />
<ClInclude Include="Enumstrings.h" />
<ClInclude Include="DescriptorManager.h" />
<ClInclude Include="Mutation.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="DescriptorManager.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+5 -4
View File
@@ -23,8 +23,8 @@ using namespace Magick;
int main(int argc, char **argv) { int main(int argc, char **argv) {
string imagename = "spook.jpg"; string imagename = "im3102.jpg";
int method = 6; int method = 0;
int count = 1000; int count = 1000;
if (argc > 2) { if (argc > 2) {
method = atoi(argv[2]); method = atoi(argv[2]);
@@ -43,8 +43,9 @@ int main(int argc, char **argv) {
//smanager->generatePreviews(imagename); //smanager->generatePreviews(imagename);
//cout << "Intializing..." << endl; //cout << "Intializing..." << endl;
manager->Init(); manager->Init();
cout << endl; //cout << endl;
manager->outputMap(); //manager->outputMap();
//manager->outputRank(imagename, method);
//cout << "done!" << endl; //cout << "done!" << endl;
// manager->runExperiments(); // manager->runExperiments();