Refactor caching
This commit is contained in:
+51
-22
@@ -47,13 +47,7 @@ void DescriptorManager::Init() {
|
|||||||
}
|
}
|
||||||
fs::ifstream ifs;
|
fs::ifstream ifs;
|
||||||
fs::ofstream ofs;
|
fs::ofstream ofs;
|
||||||
{
|
LoadFeatures();
|
||||||
ifstream f("features.map", ios::binary);
|
|
||||||
if (f.is_open()) {
|
|
||||||
ar::binary_iarchive bi(f);
|
|
||||||
bi >> features;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
numImages = std::count_if(
|
numImages = std::count_if(
|
||||||
fs::directory_iterator(img_path),
|
fs::directory_iterator(img_path),
|
||||||
@@ -98,21 +92,7 @@ void DescriptorManager::Init() {
|
|||||||
ofs.close();
|
ofs.close();
|
||||||
}
|
}
|
||||||
features[i][image_name] = *b1;
|
features[i][image_name] = *b1;
|
||||||
{
|
SaveFeatures();
|
||||||
ofstream f("features.map", ios::binary);
|
|
||||||
//ofstream feature_cache_backup("features.map.bak", ios::binary);
|
|
||||||
//feature_cache_backup << feature_cache.rdbuf();
|
|
||||||
ar::binary_oarchive oa(f);
|
|
||||||
oa << features;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
yas::file_ostream os("features.yas", yas::file_mode::file_trunc);
|
|
||||||
//const std::size_t flg = yas::binary|yas::file;
|
|
||||||
yas::binary_oarchive<yas::file_ostream, yas::file|yas::binary> oa(os);
|
|
||||||
oa & features;
|
|
||||||
//yas::save<yas::file|yas::binary>(image_name, features);
|
|
||||||
}
|
|
||||||
delete b1;
|
delete b1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < numMutations; i++) {
|
for (int i = 0; i < numMutations; i++) {
|
||||||
@@ -120,6 +100,12 @@ void DescriptorManager::Init() {
|
|||||||
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
||||||
if (!fs::is_directory(mutationBase))
|
if (!fs::is_directory(mutationBase))
|
||||||
fs::create_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());
|
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
||||||
Image* mutated;
|
Image* mutated;
|
||||||
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
|
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
|
||||||
@@ -179,6 +165,49 @@ void DescriptorManager::Init() {
|
|||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Saves feature map to disk.
|
||||||
|
//Makes it so that it can reload easily
|
||||||
|
void DescriptorManager::SaveFeatures() {
|
||||||
|
{ //Boost serialization
|
||||||
|
ofstream f("features.map", ios::binary);
|
||||||
|
//ofstream feature_cache_backup("features.map.bak", ios::binary);
|
||||||
|
//feature_cache_backup << feature_cache.rdbuf();
|
||||||
|
ar::binary_oarchive oa(f);
|
||||||
|
oa << features;
|
||||||
|
|
||||||
|
ofstream fm("mutated_features.map", ios::binary);
|
||||||
|
ar::binary_oarchive oam(fm);
|
||||||
|
oam << mutatedFeatures;
|
||||||
|
}
|
||||||
|
{ //YAS serialization
|
||||||
|
yas::file_ostream os("features.yas", yas::file_mode::file_trunc);
|
||||||
|
//const std::size_t flg = yas::binary|yas::file;
|
||||||
|
yas::binary_oarchive<yas::file_ostream, yas::file|yas::binary> oa(os);
|
||||||
|
oa & features;
|
||||||
|
//yas::save<yas::file|yas::binary>(image_name, features);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Loads saved feature maps from disk.
|
||||||
|
void DescriptorManager::LoadFeatures() {
|
||||||
|
{ //Boost
|
||||||
|
ifstream f("features.map", ios::binary);
|
||||||
|
if (f.is_open()) {
|
||||||
|
ar::binary_iarchive bi(f);
|
||||||
|
bi >> features;
|
||||||
|
}
|
||||||
|
ifstream fm("mutated_features.map", ios::binary);
|
||||||
|
if (fm.is_open()) {
|
||||||
|
ar::binary_iarchive bim(fm);
|
||||||
|
bim >> mutatedFeatures;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ //YAS
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Sorts all base (unmutated) images in order of distance from the given image
|
//Sorts all base (unmutated) images in order of distance from the given image
|
||||||
//After this function the distances array will be filled
|
//After this function the distances array will be filled
|
||||||
void DescriptorManager::rankImages(string image_name, int method) {
|
void DescriptorManager::rankImages(string image_name, int method) {
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ public:
|
|||||||
DescriptorManager(const fs::path & image_path);
|
DescriptorManager(const fs::path & image_path);
|
||||||
~DescriptorManager();
|
~DescriptorManager();
|
||||||
void Init();
|
void Init();
|
||||||
|
void SaveFeatures();
|
||||||
|
void LoadFeatures();
|
||||||
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<unsigned long long, string>> rankMutations(string image_name, int method);
|
||||||
void runExperiments();
|
void runExperiments();
|
||||||
|
|||||||
Reference in New Issue
Block a user