Skip to content

Commit

Permalink
Add unit test for metrics module.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Oct 3, 2019
1 parent 0d17d5e commit 9736259
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## files generated by popular Visual Studio add-ons.

# User defined files
lime_test.h
version.h
VERSION

Expand Down
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ dependencies:
- opencv>=3.0.0
- matplotlib
- scikit-image
- ipython
- jupyter
- sphinx
- sphinx_rtd_theme
Expand Down
17 changes: 5 additions & 12 deletions sources/core/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef _CORE_RANDOM_H_
#define _CORE_RANDOM_H_

#include <random>

#include "common.h"

namespace lime {
Expand All @@ -15,8 +17,9 @@ namespace lime {
**/
class LIME_API Random {
public:
explicit Random(unsigned int seed = 0);
explicit Random(uint32_t seed = UINT_MAX);

//! Generate a random 32-bit signed integer.
int nextInt();

//! Generate a random integer from [0, n-1].
Expand All @@ -30,17 +33,7 @@ class LIME_API Random {

private:
// Private methods
void init_genrand(unsigned int s);
unsigned int genrand_int32(void);
int genrand_int31(void);
double genrand_real2(void);

// Private parameters
static const int N = 624;
static const int M = 397;

unsigned int mt[N];
int mti;
std::mt19937 mt;

}; // class Random

Expand Down
131 changes: 6 additions & 125 deletions sources/core/random_detail.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,3 @@
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Copyright (C) 2005, Mutsuo Saito,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
**************************************************************************
*/

#ifdef _MSC_VER
#pragma once
#endif
Expand All @@ -58,101 +12,28 @@ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)

namespace lime {

namespace {

/* Period parameters */
static const unsigned int MATRIX_A = 0x9908b0dfU; /* constant vector a */
static const unsigned int UPPER_MASK = 0x80000000U; /* most significant w-r bits */
static const unsigned int LOWER_MASK = 0x7fffffffU; /* least significant r bits */

} // unnamed namespace

LIME_METHOD_API Random::Random(unsigned int seed)
: mti(N + 1)
LIME_METHOD_API Random::Random(uint32_t seed)
: mt(seed == UINT_MAX ? std::random_device()() : seed)
{
init_genrand(seed);
}

LIME_METHOD_API int Random::nextInt() {
return genrand_int31();
return std::uniform_int_distribution<int>()(mt);
}

LIME_METHOD_API int Random::nextInt(const int n) {
Assertion(n > 0, "Upper bound of random integers must be positive.");
return genrand_int31() % n;
return std::uniform_int_distribution<int>(0, n - 1)(mt);
}

LIME_METHOD_API double Random::nextReal() {
return genrand_real2();
return std::uniform_real_distribution<double>()(mt);
}

LIME_METHOD_API double Random::normal() {
const double r1 = nextReal();
const double r2 = nextReal();
return sqrt(-2.0 * log(r1)) * sin(2.0 * PI * r2);
}

/* initializes mt[N] with a seed */
LIME_METHOD_API void Random::init_genrand(unsigned int s) {
mt[0] = s & 0xffffffffU;
for (mti = 1; mti < N; mti++) {
mt[mti] =
(1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect */
/* only MSBs of the array mt[]. */
/* 2002/01/09 modified by Makoto Matsumoto */
mt[mti] &= 0xffffffffU;
/* for >32 bit machines */
}
}

/* generates a random number on [0,0xffffffff]-interval */
LIME_METHOD_API unsigned int Random::genrand_int32(void) {
unsigned int y;
static unsigned int mag01[2] = { 0x0U, MATRIX_A };
/* mag01[x] = x * MATRIX_A for x=0,1 */

if (mti >= N) { /* generate N words at one time */
int kk;

if (mti == N + 1) /* if init_genrand() has not been called, */
init_genrand(5489UL); /* a default initial seed is used */

for (kk = 0; kk < N - M; kk++) {
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
for (; kk < N - 1; kk++) {
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
}
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];

mti = 0;
}

y = mt[mti++];

/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680ULL;
y ^= (y << 15) & 0xefc60000ULL;
y ^= (y >> 18);

return y;
}

/* generates a random number on [0,0x7fffffff]-interval */
LIME_METHOD_API int Random::genrand_int31(void) {
return static_cast<int>(genrand_int32() >> 1);
}

/* generates a random number on [0,1)-real-interval */
LIME_METHOD_API double Random::genrand_real2(void) {
return genrand_int32()*(1.0 / 4294967296.0);
/* divided by 2^32 */
return sqrt(-2.0 * std::log(r1)) * std::sin(2.0 * PI * r2);
}

} // namespace lime
Expand Down
35 changes: 29 additions & 6 deletions sources/metrics/basics_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ double MSE(cv::InputArray I_, cv::InputArray J_) {
cv::Mat I = I_.getMat();
cv::Mat J = J_.getMat();

Assertion(I.cols == J.cols && I.rows && J.rows, "Sizes of two images must be the same!");
Assertion(I.cols == J.cols && I.rows == J.rows, "Sizes of two images must be the same!");
Assertion(I.depth() == J.depth(), "Bit depths of two images must be the same!");
Assertion(I.channels() == 1 && J.channels() == 1, "Two images must be grayscale!");
Assertion((I.channels() == 1 && J.channels() == 1) ||
(I.channels() == 3 && J.channels() == 3),
"Two images must have 1 or 3 channels!");
Assertion((I.depth() == CV_8U && J.depth() == CV_8U) ||
(I.depth() == CV_32F && J.depth() == CV_32F),
"Two images must be with 8-bit unsigned integer or 32-bit floating point number");
Expand All @@ -28,6 +30,14 @@ double MSE(cv::InputArray I_, cv::InputArray J_) {
J.convertTo(J, CV_8U, 255.0);
}

if (I.channels() == 3) {
cv::cvtColor(I, I, cv::COLOR_BGR2GRAY);
}

if (J.channels() == 3) {
cv::cvtColor(J, J, cv::COLOR_BGR2GRAY);
}

const int width = I.cols;
const int height = I.rows;
double score = 0.0;
Expand All @@ -43,6 +53,9 @@ double MSE(cv::InputArray I_, cv::InputArray J_) {
double PSNR(cv::InputArray I, cv::InputArray J) {
const int maxI = 255;
const double mse = MSE(I, J);
if (mse == 0.0) {
return INFTY;
}
return 10.0 * std::log10(maxI * maxI / mse);
}

Expand All @@ -55,9 +68,11 @@ double SSIM(cv::InputArray I_, cv::InputArray J_) {
cv::Mat I = I_.getMat();
cv::Mat J = J_.getMat();

Assertion(I.cols == J.cols && I.rows && J.rows, "Sizes of two images must be the same!");
Assertion(I.cols == J.cols && I.rows == J.rows, "Sizes of two images must be the same!");
Assertion(I.depth() == J.depth(), "Bit depths of two images must be the same!");
Assertion(I.channels() == 1 && J.channels() == 1, "Two images must be grayscale!");
Assertion((I.channels() == 1 && J.channels() == 1) ||
(I.channels() == 3 && J.channels() == 3),
"Two images must have 1 or 3 channels!");
Assertion((I.depth() == CV_8U && J.depth() == CV_8U) ||
(I.depth() == CV_32F && J.depth() == CV_32F),
"Two images must be with 8-bit unsigned integer or 32-bit floating point number");
Expand All @@ -70,6 +85,14 @@ double SSIM(cv::InputArray I_, cv::InputArray J_) {
J.convertTo(J, CV_8U, 255.0);
}

if (I.channels() == 3) {
cv::cvtColor(I, I, cv::COLOR_BGR2GRAY);
}

if (J.channels() == 3) {
cv::cvtColor(J, J, cv::COLOR_BGR2GRAY);
}

const int width = I.cols;
const int height = I.rows;
const int totalSize = width * height;
Expand All @@ -94,8 +117,8 @@ double SSIM(cv::InputArray I_, cv::InputArray J_) {
cov_xy += diff_x * diff_y;
}
}
sig_x /= totalSize;
sig_y /= totalSize;
sig_x = std::sqrt(sig_x / totalSize);
sig_y = std::sqrt(sig_y / totalSize);
cov_xy /= totalSize;

const double k1 = 0.01;
Expand Down
9 changes: 7 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
set(BUILD_TARGET "run_all_tests")

set(SOURCE_FILES gtest.cpp)
set(LIME_TEST_ROOT ${CMAKE_CURRENT_LIST_DIR})
configure_file(${CMAKE_CURRENT_LIST_DIR}/lime_test.h.in
${CMAKE_CURRENT_LIST_DIR}/lime_test.h @ONLY)

set(SOURCE_FILES gtest.cpp lime_test.h)
add_folder(SOURCE_FILES "core")
add_folder(SOURCE_FILES "metrics")

include_directories(${LIME_INCLUDE_DIR} ${GTEST_INCLUDE_DIRS})
include_directories(${LIME_TEST_ROOT} ${LIME_INCLUDE_DIR} ${GTEST_INCLUDE_DIRS})
add_executable(${BUILD_TARGET} ${SOURCE_FILES})
target_link_libraries(${BUILD_TARGET} ${OpenCV_LIBS} ${GTEST_LIBRARIES})

Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using lime::Random;

static const int nTest = 100;
static const int nLoop = 50000;
Random rng = Random((unsigned int)time(0));
Random rng = Random();

TEST(Random, RandInt) {
int nBins = 10;
Expand Down
12 changes: 12 additions & 0 deletions tests/lime_test.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifdef _MSC_VER
#pragma once
#endif

#ifndef _TESTS_LIME_TEST_H_
#define _TESTS_LIME_TEST_H_

#include <string>

static const std::string DATA_DIR = "@LIME_TEST_ROOT@/data/";

#endif // _TESTS_LIME_TEST_H_
Loading

0 comments on commit 9736259

Please sign in to comment.