Skip to content

Commit

Permalink
Complete labels portion of dataloader
Browse files Browse the repository at this point in the history
Style Fix

Style Fix
  • Loading branch information
kartikdutt18 committed May 29, 2020
1 parent 3353e2e commit 94fba51
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
32 changes: 30 additions & 2 deletions dataloader/dataloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,44 @@ class DataLoader
* 4. Each object tag should contain name tag i.e. class of the object.
* 5. Each object tag should contain bndbox tag containing xmin, ymin, xmax, ymax.
*
* NOTE : Labels are assigned using lexicographically. Set verbose to 1 to print labels
* NOTE : Labels are assigned using classes vector. Set verbose to 1 to print labels
* and their corresponding class.
*
* @param pathToAnnotations Path to the folder containg xml type annotation files.
* @param pathToImages Path to folder containing images corresponding to annotations.
* @param classes Vector of strings containing list of classes. Labels are assigned
* according to this vector.
* @param absolutePath Boolean to determine if absolute path is used. Defaults to false.
* @param baseXMLTag XML tag name which wraps around the annotation file.
* @param imageNameXMLTag XML tag name which holds the value of image filename.
* @param objectXMLTag XML tag name which holds details of bounding box i.e. class and
* coordinates of bounding box.
* @param bndboxXMLTag XML tag name which holds coordinates of bounding box.
* @param classNameXMLTag XML tag name inside objectXMLTag which holds the name of the
* class of bounding box.
* @param x1XMLTag XML tag name inside bndboxXMLTag which hold value of lower most
* x coordinate of bounding box.
* @param y1XMLTag XML tag name inside bndboxXMLTag which hold value of lower most
* y coordinate of bounding box.
* @param x2XMLTag XML tag name inside bndboxXMLTag which hold value of upper most
* x coordinate of bounding box.
* @param y2XMLTag XML tag name inside bndboxXMLTag which hold value of upper most
* y coordinate of bounding box.
*/
void LoadObjectDetectionDataset(const std::string& pathToAnnotations,
const std::string& pathToImages,
const bool absolutePath = false);
const std::vector<std::string>& classes,
const bool absolutePath = false,
const std::string& baseXMLTag = "annotation",
const std::string& imageNameXMLTag =
"filename",
const std::string& objectXMLTag = "object",
const std::string& bndboxXMLTag = "bndbox",
const std::string& classNameXMLTag = "name",
const std::string& x1XMLTag = "xmin",
const std::string& y1XMLTag = "ymin",
const std::string& x2XMLTag = "xmax",
const std::string& y2XMLTag = "ymax");

//! Get the training dataset features.
DatasetX TrainFeatures() const { return trainFeatures; }
Expand Down
60 changes: 55 additions & 5 deletions dataloader/dataloader_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,77 @@ template<
DatasetX, DatasetY, ScalerType
>::LoadObjectDetectionDataset(const std::string& pathToAnnotations,
const std::string& pathToImages,
const bool absolutePath)
const std::vector<std::string>& classes,
const bool absolutePath,
const std::string& baseXMLTag,
const std::string& imageNameXMLTag,
const std::string& objectXMLTag,
const std::string& bndboxXMLTag,
const std::string& classNameXMLTag,
const std::string& x1XMLTag,
const std::string& y1XMLTag,
const std::string& x2XMLTag,
const std::string& y2XMLTag)
{
std::vector<boost::filesystem::path> annotationsDirectory, imagesDirectory;

// Fill the directory.
Utils::ListDir(pathToAnnotations, annotationsDirectory, absolutePath);
Utils::ListDir(pathToImages, imagesDirectory, absolutePath);

// Create a map for labels and corresponding class name.
// This provides faster access to class labels.
std::unordered_map<std::string, size_t> classMap;
for (size_t i = 0; i < classes.size(); i++)
{
classMap.insert(std::make_pair(classes[i], i));
}

// Read the xml file.
for (boost::filesystem::path annotationFile : annotationsDirectory)
{
// Read the xml file.
boost::property_tree::ptree annotation;
std::cout << annotationFile.string() << std::endl;
boost::property_tree::read_xml(annotationFile.string(), annotation);

// Map to insert values in a column vector.
std::unordered_map<std::string, size_t> indexMap;
indexMap.insert(std::make_pair(classNameXMLTag, 0));
indexMap.insert(std::make_pair(x1XMLTag, 1));
indexMap.insert(std::make_pair(y1XMLTag, 2));
indexMap.insert(std::make_pair(x2XMLTag, 3));
indexMap.insert(std::make_pair(y2XMLTag, 4));

// Read properties inside annotation file.
BOOST_FOREACH (boost::property_tree::ptree::value_type const& object,
annotation.get_child("annotation.object"))
BOOST_FOREACH(boost::property_tree::ptree::value_type const& object,
annotation.get_child(baseXMLTag))
{
std::cout << object.first << std::endl;
// Column vector to temporarily store details of bounding box.
if (object.first == objectXMLTag)
{
arma::uvec predictions(5);

// Iterate over property of the object to get class label and
// bounding box coordinates.
if (classMap.count(object.second.get_child(classNameXMLTag).data()))
{
predictions(indexMap[classNameXMLTag]) = classMap[
object.second.get_child(classNameXMLTag).data()];
boost::property_tree::ptree const &boundingBox =
object.second.get_child(bndboxXMLTag);

BOOST_FOREACH(boost::property_tree::ptree::value_type
const& coordinate, boundingBox)
{
if (indexMap.count(coordinate.first))
{
predictions(indexMap[coordinate.first]) =
std::stoi(coordinate.second.data());
}
}
// predictions.print();
}
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/dataloader_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ BOOST_AUTO_TEST_CASE(MNISTDataLoaderTest)
BOOST_AUTO_TEST_CASE(ObjectDetectionDataLoader)
{
DataLoader<> dataloader;
dataloader.LoadObjectDetectionDataset("./../data/annotations/", "./../data");
dataloader.LoadObjectDetectionDataset("./../data/annotations/", "./../data",
{"person", "foot", "aeroplane", "head", "hand"});
}

BOOST_AUTO_TEST_SUITE_END();

0 comments on commit 94fba51

Please sign in to comment.