Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the discretize function. Design changes as discussed. #703

Closed
wants to merge 14 commits into from
1 change: 1 addition & 0 deletions src/mlpack/core/arma_extend/arma_extend.hpp
Expand Up @@ -68,6 +68,7 @@ namespace arma {

// index to subscript and vice versa
#include "fn_ind2sub.hpp"

// inplace_reshape()
#include "fn_inplace_reshape.hpp"

Expand Down
3 changes: 2 additions & 1 deletion src/mlpack/methods/CMakeLists.txt
Expand Up @@ -23,7 +23,8 @@ set(DIRS
decision_stump
det
emst
fastmks
edge_boxes
# fastmks
gmm
hmm
hoeffding_trees
Expand Down
21 changes: 21 additions & 0 deletions src/mlpack/methods/edge_boxes/CMakeLists.txt
@@ -0,0 +1,21 @@

cmake_minimum_required(VERSION 2.8)

# Define the files we need to compile.
# Anything not in this list will not be compiled into mlpack.
set(SOURCES
feature_extraction.hpp
feature_extraction_impl.hpp
)

# Add directory name to sources.
set(DIR_SRCS)
foreach(file ${SOURCES})
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
endforeach()
# Append sources (with directory name) to list of all mlpack sources (used at
# the parent scope).
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)

add_cli_executable(edge_boxes)

83 changes: 83 additions & 0 deletions src/mlpack/methods/edge_boxes/edge_boxes_main.cpp
@@ -0,0 +1,83 @@
/**
* @file decision_stump.hpp
* @author
*
* Definition of decision stumps.
*/
#include <mlpack/core.hpp>
#include "feature_extraction.hpp"

using namespace mlpack;
using namespace mlpack::structured_tree;
using namespace std;

int main(int argc, char** argv)
{
CLI::ParseCommandLine(argc, argv);
/*
:param options:
num_images: number of images in the dataset.
rgbd: 0 for RGB, 1 for RGB + depth
shrink: amount to shrink channels
n_orient: number of orientations per gradient scale
grd_smooth_rad: radius for image gradient smoothing
grd_norm_rad: radius for gradient normalization
reg_smooth_rad: radius for reg channel smoothing
ss_smooth_rad: radius for sim channel smoothing
p_size: size of image patches
g_size: size of ground truth patches
n_cell: number of self similarity cells

n_pos: number of positive patches per tree
n_neg: number of negative patches per tree
fraction: fraction of features to use to train each tree
n_tree: number of trees in forest to train
n_class: number of classes (clusters) for binary splits
min_count: minimum number of data points to allow split
min_child: minimum number of data points allowed at child nodes
max_depth: maximum depth of tree
split: options include 'gini', 'entropy' and 'twoing'
discretize: optional function mapping structured to class labels

stride: stride at which to compute edges
sharpen: sharpening amount (can only decrease after training)
n_tree_eval: number of trees to evaluate per location
nms: if true apply non-maximum suppression to edges
*/

FeatureParameters params = FeatureParameters();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update 👍
In c++, when you declare an object, it will call the default constructor, so you can just write(Maybe you already know this)

FeatureParameters params;

In theory, initialize the object by using constructor is better than using copy assignment, but compiler should be able to optimize it, so it is just a preference of taste now(I guess)


params.NumImages(2);
params.RowSize(321);
params.ColSize(481);
params.RGBD(0);
params.Shrink(2);
params.NumOrient(4);
params.GrdSmoothRad(0);
params.GrdNormRad(4);
params.RegSmoothRad(2);
params.SSSmoothRad(8);
params.Fraction(0.25);
params.PSize(32);
params.GSize(16);
params.NumCell(5);
params.NumPos(10000);
params.NumNeg(10000);
params.NumCell(5);
params.NumTree(8);
StructuredForests <arma::mat, arma::cube> SF(params);
// arma::uvec x(2);
//SF.GetFeatureDimension(x);

arma::mat segmentations, boundaries, images;
data::Load("/home/nilay/example/small_images.csv", images);
data::Load("/home/nilay/example/small_boundary_1.csv", boundaries);
data::Load("/home/nilay/example/small_segmentation_1.csv", segmentations);

SF.PrepareData(images, boundaries, segmentations);
cout << "PrepareData done." << endl;
return 0;
}



102 changes: 102 additions & 0 deletions src/mlpack/methods/edge_boxes/feature_extraction.hpp
@@ -0,0 +1,102 @@
/**
* @file feature_extraction.hpp
* @author Nilay Jain
*
* Feature Extraction for the edge_boxes algorithm.
*/
#ifndef MLPACK_METHODS_EDGE_BOXES_STRUCTURED_TREE_HPP
#define MLPACK_METHODS_EDGE_BOXES_STRUCTURED_TREE_HPP
//#define INF 999999.9999
//#define EPS 1E-20
#include <mlpack/core.hpp>
#include "feature_parameters.hpp"
namespace mlpack {
namespace structured_tree {

template <typename MatType = arma::mat, typename CubeType = arma::cube>
class StructuredForests
{
private:
FeatureParameters params;
static constexpr double eps = 1e-20;

public:


StructuredForests(FeatureParameters F);
/* MatType LoadData(MatType const &images, MatType const &boundaries,\
MatType const &segmentations);*/

void PrepareData(const MatType& Images, const MatType& Boundaries,\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the ""?

const MatType& Segmentations);

void GetFeatureDimension(arma::vec& FtrDim);

void DistanceTransform1D(const arma::vec& f, const size_t n,\
const double inf, arma::vec& d);

void DistanceTransform2D(MatType &Im, const double inf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Variable, reference, and pointer names begin with a lower case letter. Take a look at: https://github.com/mlpack/mlpack/wiki/DesignGuidelines#naming-conventions for more informations.


void DistanceTransformImage(const MatType& Im, double on, MatType& Out);

void GetFeatures(const MatType &Image, arma::umat &loc,\
CubeType& RegFtr, CubeType& SSFtr,\
const arma::vec& table);

void CopyMakeBorder(const CubeType& InImage, size_t top,
size_t left, size_t bottom, size_t right,
CubeType& OutImage);

void GetShrunkChannels(const CubeType& InImage, CubeType &reg_ch,\
CubeType &ss_ch, const arma::vec& table);

void RGB2LUV(const CubeType& InImage, CubeType& OutImage,\
const arma::vec& table);

void BilinearInterpolation(const MatType& src,
size_t height, size_t width,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you forgot const?const size_t height, const size_t width?
Besides, this function is slow, I will implement a faster version after this project complete

MatType& dst);

void SepFilter2D(CubeType &InOutImage, const arma::vec& kernel, const size_t radius);

void ConvTriangle(CubeType &InImage, const size_t radius);

void Gradient(const CubeType& InImage,
MatType& Magnitude,
MatType& Orientation);

void MaxAndLoc(CubeType &mag, arma::umat &Location, MatType& MaxVal) const;

void Histogram(const MatType& Magnitude,
const MatType& Orientation,
size_t downscale, size_t interp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you forgot const?const size_t downscale, const size_t interp?

CubeType& HistArr);

void ViewAsWindows(const CubeType& channels, const arma::umat& loc,
CubeType& features);

void GetRegFtr(const CubeType& channels, const arma::umat& loc,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the doxygen style to cite the paper. Here is an example: https://github.com/mlpack/mlpack/blob/master/src/mlpack/methods/ann/layer/dropout_layer.hpp

CubeType& RegFtr);

void GetSSFtr(const CubeType& channels, const arma::umat& loc,
CubeType SSFtr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering, why I don't see any similarity features, turns out we missed a reference here.


void Rearrange(const CubeType& channels, CubeType& ch);

void PDist(const CubeType& features, const arma::uvec& grid_pos,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use an underscore for parameter names.

CubeType& Output);

size_t IndexMin(arma::vec& k);

size_t Discretize(const MatType& labels, const size_t nClass,\
const size_t nSample, arma::vec& DiscreteLabels);
};


} //namespace structured_tree
} // namespace mlpack
#include "feature_extraction_impl.hpp"
#endif