WIP on new init
Rework seems done, but man are there bugs
This commit is contained in:
+36
-181
@@ -83,9 +83,15 @@ void DescriptorManager::Init() {
|
||||
if (!fs::is_directory(mutationBase))
|
||||
fs::create_directory(mutationBase);
|
||||
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
||||
|
||||
Image* mutated = mutators[i]->getMutated(image);
|
||||
mutated->write(mutPath.generic_string());
|
||||
Image* mutated;
|
||||
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
|
||||
mutated = mutators[i]->getMutated(image);
|
||||
mutated->write(mutPath.generic_string());
|
||||
}
|
||||
else {
|
||||
mutated = new Image;
|
||||
mutated->read(mutPath.generic_string());
|
||||
}
|
||||
|
||||
//now calculate all the features for the mutated images as well
|
||||
//foreach descriptor
|
||||
@@ -103,7 +109,7 @@ void DescriptorManager::Init() {
|
||||
//descriptors[i][image_name] = b1;
|
||||
b1 = &descriptor_types[j]->feature(mutated);
|
||||
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.write(reinterpret_cast<const char*>(&n), sizeof(n));
|
||||
ofs.close();
|
||||
@@ -115,201 +121,52 @@ void DescriptorManager::Init() {
|
||||
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 image;
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Loads all images and determines the feature vectors
|
||||
bool DescriptorManager::loadDescriptors() {
|
||||
if (!fs::exists(img_path)) return false;
|
||||
fs::directory_iterator end_itr; //last file in dir
|
||||
map<string, Image*> images; //image cache
|
||||
fs::ifstream ifs;
|
||||
fs::ofstream ofs;
|
||||
for (int i = 0; i < numDescriptors; i++) {
|
||||
//cout << "waddup " << i << endl;
|
||||
//folder to store descriptors; create if not exists
|
||||
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
|
||||
if (!fs::is_directory(descriptorBase))
|
||||
if (fs::create_directory(descriptorBase)) {}
|
||||
//cout << descriptorBase << " created." << endl;
|
||||
|
||||
//foreach image
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
if (!itr->path().has_extension()) continue;
|
||||
fs::path featurePath(descriptorBase / itr->path().stem());
|
||||
//string image_name = featurePath.filename().generic_string();
|
||||
string image_name = itr->path().filename().generic_string();
|
||||
if (fs::exists(featurePath)) { //feature already calculated
|
||||
ifs.open(featurePath, fs::fstream::binary); //in binary mode
|
||||
unsigned long long n;
|
||||
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
|
||||
ifs.close();
|
||||
|
||||
bitset<64> b1(n);
|
||||
features[i][image_name] = b1;
|
||||
}
|
||||
else { //feature not yet calculated
|
||||
Image* image; //only load image from disk if not loaded before
|
||||
if (images.count(image_name)) { //if image exists in map
|
||||
image = images[image_name];
|
||||
}
|
||||
else {
|
||||
image = new Image;
|
||||
image->read(itr->path().string());
|
||||
images[image_name] = image;
|
||||
}
|
||||
//cout << "Calculating " << image_name << endl;
|
||||
bitset<64> b1 = descriptor_types[i]->feature(image);
|
||||
//descriptor_types[i]->distance(b1, b1);
|
||||
features[i][image_name] = b1;
|
||||
|
||||
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.write(reinterpret_cast<const char*>(&n), sizeof(n));
|
||||
ofs.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//Loads all images and makes and stores the mutations
|
||||
bool DescriptorManager::createMutations() {
|
||||
if (!fs::exists(img_path)) return false;
|
||||
fs::directory_iterator end_itr; //last file in dir
|
||||
map<string, Image*> images; //image cache
|
||||
fs::ifstream ifs;
|
||||
fs::ofstream ofs;
|
||||
|
||||
//foreach mutation
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
||||
if (!fs::is_directory(mutationBase))
|
||||
if (fs::create_directory(mutationBase)) {}
|
||||
|
||||
//foreach image
|
||||
int z = 0;
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
||||
//if (z++ > 50) break; //for quick execution
|
||||
//if (fs::exists(mutPath)) continue; //already exists
|
||||
if (!itr->path().has_extension()) continue; //skip directories
|
||||
|
||||
Image* image = new Image(itr->path().string());
|
||||
Image* mutated = mutators[i]->getMutated(image);
|
||||
mutated->write(mutPath.generic_string());
|
||||
|
||||
//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());
|
||||
//cout << "Doing mutated descriptors for " << descriptorBase << endl;
|
||||
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();
|
||||
if (!fs::exists(featurePath)) { //feature not yet calculated
|
||||
//cout << "Calculating " << image_name << endl;
|
||||
//descriptor_types[i]->distance(b1, b1);
|
||||
//descriptors[i][image_name] = b1;
|
||||
bitset<64> b1 = descriptor_types[j]->feature(mutated);
|
||||
unsigned long long n = b1.to_ullong();
|
||||
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.close();
|
||||
}
|
||||
}
|
||||
delete mutated;
|
||||
delete image;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//essentially insertion sort into a map
|
||||
void DescriptorManager::similarImages(string image_name, int method, int numImages) {
|
||||
//Sorts all images we have on distance from the query image
|
||||
void DescriptorManager::rankImages(string image_name, int method) {
|
||||
bitset<64> target;
|
||||
if (features[method].count(image_name)) { //if image exists in map
|
||||
if (features[method].count(image_name) && initialized) { //if we have image
|
||||
target = features[method][image_name];
|
||||
}
|
||||
else {
|
||||
Image* image = new Image;
|
||||
image->read(image_name); //assuming http url
|
||||
target = descriptor_types[method]->feature(image);
|
||||
} //TODO: Add support for uploaded images
|
||||
//cout << "HEY?!" << endl;
|
||||
//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..
|
||||
//distances.clear(); //start fresh
|
||||
else {
|
||||
return;
|
||||
}
|
||||
distances.clear(); //start fresh
|
||||
for (map<string, bitset<64>>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
|
||||
//cout << "ho " << endl;
|
||||
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..
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end() && i < numImages; ++it) {
|
||||
//cout << "hey " << endl;
|
||||
if (true || it->second != image_name) {
|
||||
//cout << "(" << it->first << ", " << it->second << ")" << endl;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//inserts the descriptors of the mutated versions of the image.
|
||||
//Assumes the distances array is filled with the normal distances
|
||||
void DescriptorManager::insertMutations(string image_name, int method) {
|
||||
multiset<pair<unsigned long long, string> > local_distance;
|
||||
fs::ifstream ifs;
|
||||
void DescriptorManager::rankMutations(string image_name, int method) {
|
||||
bitset<64> target;
|
||||
if (features[method].count(image_name)) { //if image exists in map
|
||||
if (features[method].count(image_name) && initialized) { //if we have image
|
||||
target = features[method][image_name];
|
||||
}
|
||||
cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
|
||||
for (map<string, bitset<64>>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
|
||||
//cout << "ho " << endl;
|
||||
unsigned long long d = descriptor_types[method]->distance(target, it->second);
|
||||
pair<unsigned long long, string> p(d, it->first);
|
||||
local_distance.insert(p); //pairs are compared by their first element..
|
||||
else {
|
||||
return;
|
||||
}
|
||||
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++) {
|
||||
fs::path descriptorBase("meta" / descriptor_types[method]->ToPath() / mutators[i]->ToPath());
|
||||
fs::path imagePath(image_name);
|
||||
fs::path featurePath(descriptorBase / imagePath.stem());
|
||||
cout << "Finding mutation for this image at: " << featurePath << endl;
|
||||
if (fs::exists(featurePath)) { //feature already calculated
|
||||
ifs.open(featurePath, fs::fstream::binary); //in binary mode
|
||||
unsigned long long n;
|
||||
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
|
||||
ifs.close();
|
||||
cout << "Number: " << n << endl;
|
||||
bitset<64> b1(n);
|
||||
unsigned long long d = descriptor_types[method]->distance(target, b1);
|
||||
pair<unsigned long long, string> p(d, mutators[i]->ToString());
|
||||
local_distance.insert(p);
|
||||
}
|
||||
else {
|
||||
cout << "data not set properly" << endl;
|
||||
}
|
||||
bitset<64> 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);
|
||||
}
|
||||
|
||||
|
||||
cout << "..." << endl;
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
int j = 0;
|
||||
@@ -324,19 +181,10 @@ void DescriptorManager::insertMutations(string image_name, int method) {
|
||||
}
|
||||
}
|
||||
|
||||
void DescriptorManager::checkImages(int method) {
|
||||
fs::directory_iterator end_itr;
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
if (!itr->path().has_extension()) continue; //skip directories
|
||||
insertMutations(itr->path().string(), method);
|
||||
}
|
||||
}
|
||||
|
||||
//outputs the gathered data to iostream
|
||||
//can be called directly or is called by its overload
|
||||
//assumes a < numDescriptors
|
||||
void DescriptorManager::outputMap(int a) {
|
||||
if (a != 2) return;
|
||||
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
|
||||
map<string, bitset<64>> a_map = features[a];
|
||||
if (a_map.begin() == a_map.end()) {
|
||||
@@ -353,4 +201,11 @@ void DescriptorManager::outputMap() {
|
||||
for (int i = 0; i < numDescriptors; i++) {
|
||||
outputMap(i);
|
||||
}
|
||||
}
|
||||
|
||||
//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 << "(" << it->first << ", " << it->second << ")" << endl;
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -27,18 +27,18 @@ public:
|
||||
DescriptorManager(const fs::path & image_path);
|
||||
~DescriptorManager();
|
||||
void Init();
|
||||
bool loadDescriptors();
|
||||
bool createMutations();
|
||||
void similarImages(string image_name, int method, int numImages);
|
||||
void insertMutations(string image_name, int method);
|
||||
void checkImages(int method);
|
||||
void rankImages(string image_name, int method);
|
||||
void rankMutations(string image_name, int method);
|
||||
void outputMap(int a);
|
||||
void outputMap();
|
||||
void DescriptorManager::outputRank(string imagename, int method);
|
||||
private:
|
||||
static const int numDescriptors = 8;
|
||||
static const int numMutations = 7;
|
||||
//TODO: automagically update this?
|
||||
|
||||
bool initialized = false;
|
||||
|
||||
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
|
||||
|
||||
@@ -23,7 +23,7 @@ using namespace Magick;
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
string imagename = "im124.jpg";
|
||||
string imagename = "im80.jpg";
|
||||
int method = 6;
|
||||
int count = 1000;
|
||||
if (argc > 2) {
|
||||
@@ -41,12 +41,16 @@ int main(int argc, char **argv) {
|
||||
InitializeMagick(*argv);
|
||||
DescriptorManager* manager = new DescriptorManager("data");
|
||||
manager->Init();
|
||||
manager->rankImages(imagename, method);
|
||||
manager->rankMutations(imagename, method);
|
||||
//manager->outputMap(method);
|
||||
manager->outputRank(imagename, method);
|
||||
//cout << "Loading descriptiors..";
|
||||
//manager->loadDescriptors();
|
||||
manager->createMutations();
|
||||
//manager->createMutations();
|
||||
//cout << "done?" << endl;
|
||||
//manager->similarImages(imagename, method, count);
|
||||
manager->insertMutations(imagename, method);
|
||||
//manager->insertMutations(imagename, method);
|
||||
//cout << "done!" << endl << "Calculating distances.." << endl;
|
||||
//cout << "done!" << endl << "Outputting map.." << endl;
|
||||
//manager->outputMap();
|
||||
|
||||
Reference in New Issue
Block a user