Skip to content

Commit

Permalink
Build Fixed, Nice way to avoid hidden files
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikdutt18 committed Jun 4, 2020
1 parent c8159f8 commit 93acea2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
6 changes: 4 additions & 2 deletions dataloader/dataloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ class DataLoader
*
* @param pathToAnnotations Path to the folder containing XML type annotation files.
* @param pathToImages Path to folder containing images corresponding to annotations.
* @param validRatio Ratio of dataset that will be used for validation.
* @param classes Vector of strings containing list of classes. Labels are assigned
* according to this vector.
* @param validRatio Ratio of dataset that will be used for validation.
* @param shuffle Boolean to determine whether the dataset is shuffled.
* @param augmentation Vector strings of augmentations supported by mlpack.
* @param augmentationProbability Probability of applying augmentation to a particular cell.
* @param absolutePath Boolean to determine if absolute path is used. Defaults to false.
Expand All @@ -161,8 +162,9 @@ class DataLoader
*/
void LoadObjectDetectionDataset(const std::string& pathToAnnotations,
const std::string& pathToImages,
const double validRatio,
const std::vector<std::string>& classes,
const double validRatio = 0.2,
const bool shuffle = true,
const std::vector<std::string>& augmentation =
std::vector<std::string>(),
const double augmentationProbability = 0.2,
Expand Down
29 changes: 18 additions & 11 deletions dataloader/dataloader_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ template<
}

LoadObjectDetectionDataset(datasetMap[dataset].trainingAnnotationPath,
datasetMap[dataset].trainingImagesPath, validRatio,
datasetMap[dataset].classes, augmentations, augmentationProbability);
datasetMap[dataset].trainingImagesPath, datasetMap[dataset].classes,
validRatio, shuffle,augmentations, augmentationProbability);

// Load testing data if any. Most object detection dataset
// have private evaluation servers.
Expand Down Expand Up @@ -180,8 +180,9 @@ template<
DatasetX, DatasetY, ScalerType
>::LoadObjectDetectionDataset(const std::string& pathToAnnotations,
const std::string& pathToImages,
const double validRatio,
const std::vector<std::string>& classes,
const double validRatio,
const bool shuffle,
const std::vector<std::string>& augmentations,
const double augmentationProbability,
const bool absolutePath,
Expand Down Expand Up @@ -212,7 +213,6 @@ template<
for (size_t i = 0; i < classes.size(); i++)
classMap.insert(std::make_pair(classes[i], i));


// Map to insert values in a column vector.
std::unordered_map<std::string, size_t> indexMap;
indexMap.insert(std::make_pair(classNameXMLTag, 0));
Expand All @@ -223,13 +223,14 @@ template<

// Keep track of files loaded.
size_t totalFiles = annotationsDirectory.size(), loadedFiles = 0;
size_t imageWidth = 0, imageHeight = 0, imageDepth = 0;

// Read the XML file.
for (boost::filesystem::path annotationFile : annotationsDirectory)
{
if (annotationFile.string().length() <= 3 ||
annotationFile.string().substr(
annotationFile.string().length() - 3) != "xml")
annotationFile.string().length() - 3) != "xml")
{
continue;
}
Expand Down Expand Up @@ -260,9 +261,9 @@ template<
// Get the size of image to create image info required
// by mlpack::data::Load function.
boost::property_tree::ptree sizeInfo = annotation.get_child(sizeXMLTag);
size_t imageWidth = std::stoi(sizeInfo.get_child("width").data());
size_t imageHeight = std::stoi(sizeInfo.get_child("height").data());
size_t imageDepth = std::stoi(sizeInfo.get_child("depth").data());
imageWidth = std::stoi(sizeInfo.get_child("width").data());
imageHeight = std::stoi(sizeInfo.get_child("height").data());
imageDepth = std::stoi(sizeInfo.get_child("depth").data());
mlpack::data::ImageInfo imageInfo(imageWidth, imageHeight, imageDepth);

// Load the image.
Expand All @@ -276,6 +277,8 @@ template<
{
augmentation.ResizeTransform(image, imageWidth, imageHeight, imageDepth,
augmentation.augmentations[0]);
augmentation.GetResizeParam(imageWidth, imageHeight,
augmentation.augmentations[0]);
}

// Iterate over all object in annotation.
Expand Down Expand Up @@ -312,9 +315,13 @@ template<
}
}

// Add data split and augmentation here.
trainFeatures = dataset;
trainLabels = labels;
// Add data split here.
trainFeatures = std::move(dataset);
trainLabels = std::move(labels);

// Augment the training data.
augmentation.Transform(trainFeatures, imageWidth, imageHeight,
imageDepth);
}

template<
Expand Down
3 changes: 2 additions & 1 deletion tests/dataloader_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ BOOST_AUTO_TEST_CASE(ObjectDetectionDataLoader)
std::string annotaionPath = "Annotations/";
std::string imagesPath = "Images/";
double validRatio = 0.2;
bool shuffle = true;

// Classes in the dataset.
std::vector<std::string> classes = {"background", "aeroplane", "bicycle",
Expand All @@ -109,7 +110,7 @@ BOOST_AUTO_TEST_CASE(ObjectDetectionDataLoader)
// Resize the image to 64 x 64.
std::vector<std::string> augmentation = {"resize (64, 64)"};
dataloader.LoadObjectDetectionDataset(basePath + annotaionPath,
basePath + imagesPath, validRatio, classes, augmentation);
basePath + imagesPath, classes, validRatio, shuffle, augmentation);

// There are total 15 objects in images.
BOOST_REQUIRE_EQUAL(dataloader.TrainLabels().n_cols, 15);
Expand Down
7 changes: 7 additions & 0 deletions utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ class Utils
boost::filesystem::directory_iterator(),
std::back_inserter(pathVector));

// Remove hidden files.
pathVector.erase(std::remove_if(pathVector.begin(), pathVector.end(),
[](boost::filesystem::path curPath)
{
return curPath.filename().string()[0] == '.';
}), pathVector.end());

// Sort the path vector.
std::sort(pathVector.begin(), pathVector.end());
}
Expand Down

0 comments on commit 93acea2

Please sign in to comment.