Skip to content

Commit

Permalink
add rcnn loss tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhung0 committed Aug 25, 2017
1 parent 0d9c1db commit 2a1e299
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions tests/layers/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy
import keras_rcnn.layers.object_detection

def test_call_classification():
def test_rpn_classification():
anchors = 9
layer = keras_rcnn.layers.RPNClassificationLoss(anchors=anchors)
y_true = keras.backend.variable(100 * numpy.random.random((1, 91, 4)))
Expand All @@ -19,7 +19,7 @@ def test_call_classification():
assert numpy.isclose(keras.backend.eval(layer.losses[0]), expected_loss)


def test_call_regression():
def test_rpn_regression():
anchors = 9
metadata = keras.backend.variable(numpy.array([[224, 224, 1]]))
layer = keras_rcnn.layers.RPNRegressionLoss(anchors=anchors)
Expand All @@ -41,3 +41,54 @@ def test_call_regression():
assert len(layer.losses) == 1

assert numpy.isclose(keras.backend.eval(layer.losses[0]), expected_loss)


def test_rcnn_classification():
num_classes = 5
layer = keras_rcnn.layers.losses.RCNNClassificationLoss()
classes = numpy.random.choice(range(0, num_classes), (91))
target = numpy.zeros((1, 91, num_classes))
target[0, numpy.arange(91), classes] = 1
target = keras.backend.variable(target)
scores = keras.backend.variable(numpy.random.random((1, 91, num_classes)))

numpy.testing.assert_array_equal(layer.call([target, scores]), scores)

assert len(layer.losses) == 1


def test_rcnn_regression():
num_classes = 5
layer = keras_rcnn.layers.losses.RCNNRegressionLoss()

deltas = numpy.zeros((1, 91, 4 * num_classes))
target = numpy.zeros((1, 91, 4 * num_classes))

expected_loss = 0

numpy.testing.assert_array_equal(layer.call([target, deltas]), deltas)

assert len(layer.losses) == 1

assert numpy.isclose(keras.backend.eval(layer.losses[0]), expected_loss)

deltas = numpy.array([[1, 0, 1, 0, 3, 4, 5, 6, 0, 0, 0, 0],
[0, 0, 0, 0, 7, 3, 5, 6, 4, 1, 1, 5],
[5, 2, 0, 0, 2, 0, 7, 8, 1, 5, 2, 10],
[0, 0, 0, 0, 5, 3, 13, 4, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
deltas = numpy.expand_dims(deltas, 0)
deltas = keras.backend.variable(deltas)
target = numpy.array([[0, 0, 0, 0, 3, 4, 5, 6, 0, 0, 0, 0],
[0, 0, 0, 0, 7, 3, 5, 6, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 2, 10],
[0, 0, 0, 0, 5, 3, 13, 4, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 40, 12, 2, .5]])
target = numpy.expand_dims(target, 0)
target = keras.backend.variable(target)

numpy.testing.assert_array_equal(layer.call([target, deltas]), deltas)

expected_loss = 52.625

assert keras.backend.eval(layer.losses[1]) == expected_loss

0 comments on commit 2a1e299

Please sign in to comment.