Commit 44f47427 authored by Dmitriy Anisimov's avatar Dmitriy Anisimov

updated or_imagenet dataset loader

parent 3be484f1
...@@ -56,9 +56,8 @@ namespace datasets ...@@ -56,9 +56,8 @@ namespace datasets
struct OR_imagenetObj : public Object struct OR_imagenetObj : public Object
{ {
std::string wnid; int id;
int id2; std::string image;
std::string imageUrl;
}; };
class CV_EXPORTS OR_imagenet : public Dataset class CV_EXPORTS OR_imagenet : public Dataset
......
...@@ -58,7 +58,7 @@ int main(int argc, char *argv[]) ...@@ -58,7 +58,7 @@ int main(int argc, char *argv[])
{ {
const char *keys = const char *keys =
"{ help h usage ? | | show this message }" "{ help h usage ? | | show this message }"
"{ path p |true| path to file with urls: fall11_urls.txt }"; "{ path p |true| path to folder with dataset }";
CommandLineParser parser(argc, argv, keys); CommandLineParser parser(argc, argv, keys);
string path(parser.get<string>("path")); string path(parser.get<string>("path"));
if (parser.has("help") || path=="true") if (parser.has("help") || path=="true")
...@@ -71,13 +71,28 @@ int main(int argc, char *argv[]) ...@@ -71,13 +71,28 @@ int main(int argc, char *argv[])
dataset->load(path); dataset->load(path);
// *************** // ***************
// dataset contains for each object its id & image url. // dataset contains for each object its id & image path
// For example, let output dataset size and first object. // For example, let output train\test\validation size and first image.
printf("dataset size: %u\n", (unsigned int)dataset->getTrain().size()); vector< Ptr<Object> > &curr = dataset->getTrain();
OR_imagenetObj *example = static_cast<OR_imagenetObj *>(dataset->getTrain()[0].get()); printf("train:\nsize: %u\n", (unsigned int)curr.size());
printf("first object url: %s\n", example->imageUrl.c_str()); OR_imagenetObj *example = static_cast<OR_imagenetObj *>(curr[0].get());
printf("first object wnid: %s\n", example->wnid.c_str()); printf("first image:\n");
printf("first object id2: %u\n", example->id2); printf("image: %s\n", example->image.c_str());
printf("id: %u\n", example->id);
vector< Ptr<Object> > &currT = dataset->getTest();
printf("test:\nsize: %u\n", (unsigned int)currT.size());
example = static_cast<OR_imagenetObj *>(currT[0].get());
printf("first image:\n");
printf("image: %s\n", example->image.c_str());
printf("id: %u\n", example->id);
vector< Ptr<Object> > &currV = dataset->getValidation();
printf("validation:\nsize: %u\n", (unsigned int)currV.size());
example = static_cast<OR_imagenetObj *>(currV[0].get());
printf("first image:\n");
printf("image: %s\n", example->image.c_str());
printf("id: %u\n", example->id);
return 0; return 0;
} }
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
#include "opencv2/datasets/util.hpp" #include "opencv2/datasets/util.hpp"
#include "precomp.hpp" #include "precomp.hpp"
#include <map>
namespace cv namespace cv
{ {
namespace datasets namespace datasets
...@@ -61,6 +63,8 @@ public: ...@@ -61,6 +63,8 @@ public:
private: private:
void loadDataset(const string &path); void loadDataset(const string &path);
void numberToString(int number, string &out);
}; };
/*OR_imagenetImp::OR_imagenetImp(const string &path) /*OR_imagenetImp::OR_imagenetImp(const string &path)
...@@ -73,30 +77,87 @@ void OR_imagenetImp::load(const string &path) ...@@ -73,30 +77,87 @@ void OR_imagenetImp::load(const string &path)
loadDataset(path); loadDataset(path);
} }
void OR_imagenetImp::numberToString(int number, string &out)
{
char numberStr[9];
sprintf(numberStr, "%u", number);
for (unsigned int i=0; i<8-strlen(numberStr); ++i)
{
out += "0";
}
out += numberStr;
}
void OR_imagenetImp::loadDataset(const string &path) void OR_imagenetImp::loadDataset(const string &path)
{ {
train.push_back(vector< Ptr<Object> >()); train.push_back(vector< Ptr<Object> >());
test.push_back(vector< Ptr<Object> >()); test.push_back(vector< Ptr<Object> >());
validation.push_back(vector< Ptr<Object> >()); validation.push_back(vector< Ptr<Object> >());
ifstream infile((path + "fall11_urls.txt").c_str()); map<string, int> labels;
ifstream infile((path + "labels.txt").c_str());
string line; string line;
while (getline(infile, line)) while (getline(infile, line))
{ {
vector<string> elems; vector<string> elems;
split(line, elems, '\t'); split(line, elems, ',');
string syn = elems[0];
int number = atoi(elems[1].c_str());
labels.insert(make_pair(syn, number));
}
string pathTrain(path + "train/");
vector<string> fileNames;
getDirList(pathTrain, fileNames);
for (vector<string>::iterator it=fileNames.begin(); it!=fileNames.end(); ++it)
{
string pathSyn((*it) + "/");
vector<string> fileNamesSyn;
getDirList((pathTrain + pathSyn), fileNamesSyn);
for (vector<string>::iterator itSyn=fileNamesSyn.begin(); itSyn!=fileNamesSyn.end(); ++itSyn)
{
Ptr<OR_imagenetObj> curr(new OR_imagenetObj); Ptr<OR_imagenetObj> curr(new OR_imagenetObj);
curr->imageUrl = elems[1]; curr->image = "train/" + pathSyn + *itSyn;
curr->id = labels[*it];
string id(elems[0]); train.back().push_back(curr);
elems.clear(); }
split(id, elems, '_'); }
curr->wnid = elems[0]; ifstream infileVal((path + "ILSVRC2010_validation_ground_truth.txt").c_str());
curr->id2 = atoi(elems[1].c_str()); while (getline(infileVal, line))
{
Ptr<OR_imagenetObj> curr(new OR_imagenetObj);
curr->id = atoi(line.c_str());
numberToString(validation.back().size()+1, curr->image);
curr->image = "val/ILSVRC2010_val_" + curr->image + ".JPEG";
train.back().push_back(curr); validation.back().push_back(curr);
}
vector<int> testGT;
ifstream infileTest((path + "ILSVRC2010_test_ground_truth.txt").c_str());
while (getline(infileTest, line))
{
testGT.push_back(atoi(line.c_str()));
}
if (testGT.size()==0) // have no test labels, set them to 1000 - unknown
{
for (int i=0; i<150000; ++i)
{
testGT.push_back(1000); // unknown
}
}
for (vector<int>::iterator it=testGT.begin(); it!=testGT.end(); ++it)
{
Ptr<OR_imagenetObj> curr(new OR_imagenetObj);
curr->id = *it;
numberToString(test.back().size()+1, curr->image);
curr->image = "test/ILSVRC2010_test_" + curr->image + ".JPEG";
test.back().push_back(curr);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment