[mlpack-git] master: Add test case for the convolution batch mode. (1e5ff62)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Sun Apr 26 06:55:55 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/fbd6b1f878ec3b2fa365254a22daf3add743ee51...2eb28bcc2ada2fe09a7ad7073c0bbcbb96aac0c5

>---------------------------------------------------------------

commit 1e5ff628685c9276413c8f17d4cebd98ebeabbc6
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Sat Apr 25 16:35:37 2015 +0200

    Add test case for the convolution batch mode.


>---------------------------------------------------------------

1e5ff628685c9276413c8f17d4cebd98ebeabbc6
 src/mlpack/tests/convolution_test.cpp | 120 ++++++++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)

diff --git a/src/mlpack/tests/convolution_test.cpp b/src/mlpack/tests/convolution_test.cpp
index 7b96424..f7d012d 100644
--- a/src/mlpack/tests/convolution_test.cpp
+++ b/src/mlpack/tests/convolution_test.cpp
@@ -85,6 +85,38 @@ void Convolution3DMethodTest(const arma::cube input,
     BOOST_REQUIRE_CLOSE(*outputPtr, *convOutputPtr, 1e-3);
 }
 
+/*
+ * Implementation of the convolution function test using dense matrix as input
+ * and a 3rd order tensors as filter and output (batch modus).
+ *
+ * @param input Input used to perform the convolution.
+ * @param filter Filter used to perform the conolution.
+ * @param output The reference output data that contains the results of the
+ * convolution.
+ *
+ * @tparam ConvolutionFunction Convolution function used for the check.
+ */
+template<class ConvolutionFunction>
+void ConvolutionMethodBatchTest(const arma::mat input,
+                                const arma::cube filter,
+                                const arma::cube output)
+{
+  arma::cube convOutput;
+  ConvolutionFunction::Convolution(input, filter, convOutput);
+
+  // Check the outut dimension.
+  bool b = (convOutput.n_rows == output.n_rows) &&
+      (convOutput.n_cols == output.n_cols &&
+      convOutput.n_slices == output.n_slices);
+  BOOST_REQUIRE_EQUAL(b, 1);
+
+  const double* outputPtr = output.memptr();
+  const double* convOutputPtr = convOutput.memptr();
+
+  for (size_t i = 0; i < output.n_elem; i++, outputPtr++, convOutputPtr++)
+    BOOST_REQUIRE_CLOSE(*outputPtr, *convOutputPtr, 1e-3);
+}
+
 /**
  * Test the convolution (valid) methods.
  */
@@ -249,4 +281,92 @@ BOOST_AUTO_TEST_CASE(FullConvolution3DTest)
       filterCube, outputCube);
 }
 
+/**
+ * Test the convolution (valid) methods using dense matrix as input and a 3rd
+ * order tensors as filter and output (batch modus).
+ */
+BOOST_AUTO_TEST_CASE(ValidConvolutionBatchTest)
+{
+  // Generate dataset for convolution function tests.
+  arma::mat input, filter, output;
+  input << 1 << 2 << 3 << 4 << arma::endr
+        << 4 << 1 << 2 << 3 << arma::endr
+        << 3 << 4 << 1 << 2 << arma::endr
+        << 2 << 3 << 4 << 1;
+
+  filter << 1 << 0 << -1 << arma::endr
+         << 0 << 1 << 0 << arma::endr
+         << -1 << 0 << 1;
+
+  output << -3 << -2 << arma::endr
+         << 8 << -3;
+
+  arma::cube filterCube(filter.n_rows, filter.n_cols, 2);
+  filterCube.slice(0) = filter;
+  filterCube.slice(1) = filter;
+
+  arma::cube outputCube(output.n_rows, output.n_cols, 2);
+  outputCube.slice(0) = output;
+  outputCube.slice(1) = output;
+
+  // Perform the naive convolution approach.
+  ConvolutionMethodBatchTest<NaiveConvolution<ValidConvolution> >(input,
+      filterCube, outputCube);
+
+  // Perform the convolution trough fft.
+  ConvolutionMethodBatchTest<FFTConvolution<ValidConvolution> >(input,
+      filterCube, outputCube);
+
+  // Perform the convolution using using the singular value decomposition to
+  // speeded up the computation.
+  ConvolutionMethodBatchTest<SVDConvolution<ValidConvolution> >(input,
+      filterCube, outputCube);
+}
+
+/**
+ * Test the convolution (full) methods using dense matrix as input and a 3rd
+ * order tensors as filter and output (batch modus).
+ */
+BOOST_AUTO_TEST_CASE(FullConvolutionBatchTest)
+{
+  // Generate dataset for convolution function tests.
+  arma::mat input, filter, output;
+  input << 1 << 2 << 3 << 4 << arma::endr
+        << 4 << 1 << 2 << 3 << arma::endr
+        << 3 << 4 << 1 << 2 << arma::endr
+        << 2 << 3 << 4 << 1;
+
+  filter << 1 << 0 << -1 << arma::endr
+         << 1 << 1 << 1 << arma::endr
+         << -1 << 0 << 1;
+
+  output << 1 << 2 << 2 << 2 << -3 << -4 << arma::endr
+         << 5 << 4 << 4 << 11 << 5 << 1 << arma::endr
+         << 6 << 7 << 3 << 2 << 7 << 5 << arma::endr
+         << 1 << 9 << 12 << 3 << 1 << 4 << arma::endr
+         << -1 << 1 << 11 << 10 << 6 << 3 << arma::endr
+         << -2 << -3 << -2 << 2 << 4 << 1;
+
+  arma::cube filterCube(filter.n_rows, filter.n_cols, 2);
+  filterCube.slice(0) = filter;
+  filterCube.slice(1) = filter;
+
+  arma::cube outputCube(output.n_rows, output.n_cols, 2);
+  outputCube.slice(0) = output;
+  outputCube.slice(1) = output;
+
+  // Perform the naive convolution approach.
+  ConvolutionMethodBatchTest<NaiveConvolution<FullConvolution> >(input,
+      filterCube, outputCube);
+
+  // Perform the convolution trough fft.
+  ConvolutionMethodBatchTest<FFTConvolution<FullConvolution> >(input,
+      filterCube, outputCube);
+
+  // Perform the convolution using using the singular value decomposition to
+  // speeded up the computation.
+  ConvolutionMethodBatchTest<SVDConvolution<FullConvolution> >(input,
+      filterCube, outputCube);
+}
+
 BOOST_AUTO_TEST_SUITE_END();



More information about the mlpack-git mailing list