Skip to content

Commit

Permalink
Merge pull request broadinstitute#90 from jhung0/tests
Browse files Browse the repository at this point in the history
Fix Tests
  • Loading branch information
0x00b1 authored Aug 21, 2017
2 parents c18fada + b5f5765 commit 1e48b1d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion keras_rcnn/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def overlap(a, b):


def smooth_l1_loss(y_true, y_pred):
delta = 0.5
delta = 1

x = keras.backend.abs(y_true - y_pred)

Expand Down
15 changes: 7 additions & 8 deletions tests/layers/test_losses.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import keras.backend
import keras_rcnn.layers
import numpy

import keras_rcnn.layers.object_detection

def test_call_classification():
anchors = 9
layer = keras_rcnn.layers.RPNClassificationLoss(anchors=anchors)
y_true = keras.backend.variable(100 * numpy.random.random((1, 91, 4)))
scores = keras.backend.variable(0.5 * numpy.ones((1, 14, 14, anchors * 2)))
image = keras.layers.Input((224, 224, 3))
metadata = keras.backend.variable(numpy.array([[224, 224, 1]]))

labels, bbox_reg_targets = keras_rcnn.layers.AnchorTarget()([scores, y_true, image])
labels, bbox_reg_targets = keras_rcnn.layers.AnchorTarget()([scores, y_true, metadata])
numpy.testing.assert_array_equal(layer.call([scores, labels]), scores)

assert len(layer.losses) == 1
Expand All @@ -21,21 +21,20 @@ def test_call_classification():

def test_call_regression():
anchors = 9
image = keras.layers.Input((224, 224, 3))
metadata = keras.backend.int_shape(image)[1:]
metadata = keras.backend.variable(numpy.array([[224, 224, 1]]))
layer = keras_rcnn.layers.RPNRegressionLoss(anchors=anchors)
rr, cc = 14, 14
stride = 16
all_anchors = keras_rcnn.backend.shift((rr, cc), stride)
# only keep anchors inside the image
inds_inside, y_true = keras_rcnn.backend.inside_image(all_anchors, metadata)
#y_true = keras.backend.variable(numpy.zeros((91, 4)))
inds_inside, y_true = keras_rcnn.layers.object_detection._anchor_target.inside_image(all_anchors, metadata[0])

scores = keras.backend.variable(numpy.zeros((1, 14, 14, anchors * 2)))
deltas = keras.backend.variable(numpy.zeros((1, 14, 14, anchors * 4)))

expected_loss = 0

labels, bbox_reg_targets = keras_rcnn.layers.AnchorTarget()([scores, keras.backend.expand_dims(y_true, 0), image])
labels, bbox_reg_targets = keras_rcnn.layers.AnchorTarget()([scores, keras.backend.expand_dims(y_true, 0), metadata])
numpy.testing.assert_array_equal(
layer.call([deltas, bbox_reg_targets, labels]), deltas)

Expand Down
2 changes: 1 addition & 1 deletion tests/layers/test_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def test_roi():
a = keras.backend.placeholder(shape=(None, 224, 224, 3))
b = keras.backend.placeholder(shape=(1, None, 4))
y = keras_rcnn.layers.RegionOfInterest([7, 7])([a, b])
assert keras.backend.int_shape(y) == (1, None, 7, 7, 3)
assert keras.backend.int_shape(y) == (None, None, 7, 7, 3)
8 changes: 3 additions & 5 deletions tests/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@


def test_smooth_l1_loss():
y_true = numpy.random.random((1, 10, 4))
y_pred = numpy.random.random((1, 10, 4))
y_true = numpy.zeros((1, 4, 4))
y_pred = numpy.array([[[0, 0, 0, 0], [1, 0, 0, 1], [0, 0.1, 0.5, 0.5], [0, 0, 0, 2]]])

loss = keras_rcnn.backend.smooth_l1_loss(y_true, y_pred)

loss = keras.backend.eval(loss)

import IPython
IPython.embed()
assert loss == 2.755

0 comments on commit 1e48b1d

Please sign in to comment.