From 3a3a250a8f1890a56bf48413f26c8fbcb91aeba4 Mon Sep 17 00:00:00 2001 From: Mike Clark Date: Sun, 24 Sep 2017 09:50:32 +0800 Subject: [PATCH] Create jaccard_test.py --- tests/keras_contrib/losses/jaccard_test.py | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/keras_contrib/losses/jaccard_test.py diff --git a/tests/keras_contrib/losses/jaccard_test.py b/tests/keras_contrib/losses/jaccard_test.py new file mode 100644 index 0000000..440ebb0 --- /dev/null +++ b/tests/keras_contrib/losses/jaccard_test.py @@ -0,0 +1,31 @@ +from keras_contrib.losses import jaccard_distance +import numpy as np + +def test_jaccard_distance(): + # all_right, almost_right, half_right, all_wrong + y_true = np.array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], + [0, 0, 1., 0.]]) + y_pred = np.array([[0, 0, 1, 0], [0, 0, 0.9, 0], [0, 0, 0.1, 0], + [1, 1, 0.1, 1.]]) + + r = jaccard_distance( + K.variable(y_true), + K.variable(y_pred), ) + all_right, almost_right, half_right, all_wrong = K.eval(r) + assert r.shape == (4, ) + assert all_right == 0, 'should converge on zero' + assert all_right < almost_right + assert almost_right < half_right + assert half_right < all_wrong + +def test_jaccard_distance_shapes_3d(): + y_a = K.variable(np.random.random((5, 6, 7))) + y_b = K.variable(np.random.random((5, 6, 7))) + objective_output = jaccard_distance(y_a, y_b) + assert K.eval(objective_output).shape == (5, 6) + +def test_jaccard_distance_shapes_2d(): + y_a = K.variable(np.random.random((6, 7))) + y_b = K.variable(np.random.random((6, 7))) + objective_output = jaccard_distance(y_a, y_b) + assert K.eval(objective_output).shape == (6, )