Refactoring for the ages
This commit is contained in:
+148
-138
@@ -5,18 +5,25 @@ DescriptorManager::DescriptorManager() {
|
||||
img_path = "data"; //default value
|
||||
}
|
||||
|
||||
//Sets image path and defines the image descriptors.
|
||||
DescriptorManager::DescriptorManager(const fs::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!
|
||||
/*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();
|
||||
@@ -27,133 +34,128 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
|
||||
mutators[5] = new Hue();
|
||||
mutators[6] = new Brighten();
|
||||
mutators[7] = new Watermark();
|
||||
}
|
||||
|
||||
//No need to call destructor.. yet?
|
||||
DescriptorManager::~DescriptorManager() {
|
||||
//Do some clean-up?
|
||||
//Realistically, when this gets destroyed the application is done anyway.
|
||||
//Let the OS take care of garbage collection
|
||||
}
|
||||
|
||||
//Loads all base images, calculates their descriptors,
|
||||
//calculates all image mutations and stores their descriptors
|
||||
//on disk.
|
||||
void DescriptorManager::Init() {
|
||||
//First load all images and store descriptors in cache and disk
|
||||
if (!fs::exists(img_path)) {
|
||||
cout << "Error. Data folder not found." << endl;
|
||||
return;
|
||||
//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);
|
||||
}
|
||||
}
|
||||
fs::ifstream ifs;
|
||||
fs::ofstream ofs;
|
||||
//LoadFeatures();
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
||||
if (!fs::is_directory(mutationBase)) fs::create_directory(mutationBase);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
//Returns a pointer to the image mutated by the given method.
|
||||
//Either gets it from disk, or calculates it directly.
|
||||
Image* DescriptorManager::GetMutated(Image* image, string image_name, int method) {
|
||||
fs::path mutPath(img_path / mutators[method]->ToPath() / image_name);
|
||||
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;
|
||||
}
|
||||
|
||||
//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.
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
current_image = new Image(image_path);
|
||||
fs::path featurePath("meta" / descriptor_types[i]->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.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.close();
|
||||
}
|
||||
else { //not on disk, calculate it
|
||||
b1 = descriptor_types[i]->feature(image);
|
||||
fs::ofstream ofs(featurePath, fs::fstream::binary);
|
||||
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
|
||||
ofs.close();
|
||||
}
|
||||
mutatedFeatures[i][mutation][image_name] = b1;
|
||||
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
|
||||
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;
|
||||
continue;
|
||||
}
|
||||
//folder to store descriptors; create if not exists
|
||||
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
|
||||
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;
|
||||
if (fs::exists(featurePath)) { //feature on disk, load it in
|
||||
ifs.open(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(image);
|
||||
ofs.open(featurePath, fs::fstream::binary);
|
||||
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
|
||||
ofs.close();
|
||||
}
|
||||
features[i][image_name] = b1;
|
||||
//SaveFeatures();
|
||||
//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++) {
|
||||
//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;
|
||||
LoadMutationFeatures(image_path, image_name, i);
|
||||
}
|
||||
delete image;
|
||||
if (current_image) delete current_image;
|
||||
current_image = NULL;
|
||||
//delete image;
|
||||
SaveToDisk();
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
@@ -161,7 +163,7 @@ void DescriptorManager::Init() {
|
||||
|
||||
//Saves feature map to disk.
|
||||
//Makes it so that it can reload easily
|
||||
void DescriptorManager::SaveFeatures() {
|
||||
void DescriptorManager::SaveToDisk() {
|
||||
{ //Boost serialization
|
||||
ofstream f("features.map", ios::binary);
|
||||
//ofstream feature_cache_backup("features.map.bak", ios::binary);
|
||||
@@ -183,7 +185,7 @@ void DescriptorManager::SaveFeatures() {
|
||||
}
|
||||
|
||||
//Loads saved feature maps from disk.
|
||||
void DescriptorManager::LoadFeatures() {
|
||||
void DescriptorManager::LoadFromDisk() {
|
||||
{ //Boost
|
||||
ifstream f("features.map", ios::binary);
|
||||
if (f.is_open()) {
|
||||
@@ -213,8 +215,8 @@ void DescriptorManager::rankImages(string image_name, int method) {
|
||||
}
|
||||
distances.clear(); //start fresh
|
||||
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);
|
||||
uint64_t d = descriptor_types[method]->distance(target, it->second);
|
||||
pair<uint64_t, string> p(d, it->first);
|
||||
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.
|
||||
//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) {
|
||||
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
|
||||
target = features[method][image_name];
|
||||
}
|
||||
|
||||
//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++) {
|
||||
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());
|
||||
uint64_t d = descriptor_types[method]->distance(target, b1);
|
||||
pair<uint64_t, string> p(d, mutators[i]->ToString());
|
||||
local_distance.insert(p);
|
||||
//upper_bound
|
||||
}
|
||||
@@ -242,7 +244,7 @@ multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(stri
|
||||
//cout << "..." << endl;
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
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;
|
||||
if (it->second == mutators[i]->ToString()) {
|
||||
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) {
|
||||
//cout << "=== " << descriptor_types[i]->ToString() << ": " << it->first << " ===" << endl;
|
||||
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:
|
||||
//Put all results in some useful files in the results directory:
|
||||
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++) {
|
||||
int j = 0;
|
||||
fs::path resultPath(resultFolder / mutators[i]->ToPath());
|
||||
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()) {
|
||||
double percentile = (j + 1) / (double)(numImages+numMutations);
|
||||
|
||||
@@ -289,8 +289,7 @@ void DescriptorManager::runExperiments() {
|
||||
}
|
||||
|
||||
//outputs the gathered data to iostream
|
||||
//can be called directly or is called by its overload
|
||||
//assumes a < numDescriptors
|
||||
//Tries to do some nice formatting and grouping of bits.
|
||||
void DescriptorManager::outputMap(int a) {
|
||||
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
|
||||
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) {
|
||||
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.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user