Implement multibox loss wrapper for SSD in Python API. (#8385)

* Implement multibox loss wrapper in Python API.

* Add some wrappers for SSD detection.

* Fix conflicts.

* Add unit testing for SSD loss wrapper.

* Update doc in Python API.

* Refine unit testing.

* Add more unit testing and update some interface arguments.
emailweixu-patch-1
qingqing01 7 years ago committed by GitHub
parent 9942565fe7
commit 057efd1709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -237,6 +237,8 @@ class MineHardExamplesOp : public framework::OperatorWithKernel {
} }
ctx->SetOutputDim("UpdatedMatchIndices", idx_dims); ctx->SetOutputDim("UpdatedMatchIndices", idx_dims);
// The first dimension of NegIndices will be set correcttly in Compute.
ctx->SetOutputDim("NegIndices", {-1, 1});
} }
protected: protected:

@ -16,8 +16,6 @@ import ops
from ops import * from ops import *
import nn import nn
from nn import * from nn import *
import detection
from detection import *
import io import io
from io import * from io import *
import tensor import tensor
@ -33,7 +31,6 @@ from detection import *
__all__ = [] __all__ = []
__all__ += math_op_patch.__all__ __all__ += math_op_patch.__all__
__all__ += detection.__all__
__all__ += nn.__all__ __all__ += nn.__all__
__all__ += io.__all__ __all__ += io.__all__
__all__ += tensor.__all__ __all__ += tensor.__all__

File diff suppressed because it is too large Load Diff

@ -13,16 +13,12 @@
# limitations under the License. # limitations under the License.
from __future__ import print_function from __future__ import print_function
import paddle.v2.fluid as fluid
import paddle.v2.fluid.core as core
import paddle.v2.fluid.layers as layers import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.layers.detection as detection
from paddle.v2.fluid.framework import Program, program_guard from paddle.v2.fluid.framework import Program, program_guard
import unittest import unittest
import numpy as np
class TestBook(unittest.TestCase): class TestDetection(unittest.TestCase):
def test_detection_output(self): def test_detection_output(self):
program = Program() program = Program()
with program_guard(program): with program_guard(program):
@ -49,6 +45,66 @@ class TestBook(unittest.TestCase):
out = layers.detection_output( out = layers.detection_output(
scores=scores, loc=loc, prior_box=pb, prior_box_var=pbv) scores=scores, loc=loc, prior_box=pb, prior_box_var=pbv)
self.assertIsNotNone(out) self.assertIsNotNone(out)
self.assertEqual(out.shape[-1], 6)
print(str(program))
def test_detection_api(self):
program = Program()
with program_guard(program):
x = layers.data(name='x', shape=[4], dtype='float32')
y = layers.data(name='y', shape=[4], dtype='float32')
z = layers.data(name='z', shape=[4], dtype='float32', lod_level=1)
iou = layers.iou_similarity(x=x, y=y)
bcoder = layers.box_coder(
prior_box=x,
prior_box_var=y,
target_box=z,
code_type='encode_center_size')
self.assertIsNotNone(iou)
self.assertIsNotNone(bcoder)
matched_indices, matched_dist = layers.bipartite_match(iou)
self.assertIsNotNone(matched_indices)
self.assertIsNotNone(matched_dist)
gt = layers.data(
name='gt', shape=[1, 1], dtype='int32', lod_level=1)
trg, trg_weight = layers.target_assign(
gt, matched_indices, mismatch_value=0)
self.assertIsNotNone(trg)
self.assertIsNotNone(trg_weight)
gt2 = layers.data(
name='gt2', shape=[10, 4], dtype='float32', lod_level=1)
trg, trg_weight = layers.target_assign(
gt2, matched_indices, mismatch_value=0)
self.assertIsNotNone(trg)
self.assertIsNotNone(trg_weight)
print(str(program))
def test_ssd_loss(self):
program = Program()
with program_guard(program):
pb = layers.data(
name='prior_box',
shape=[10, 4],
append_batch_size=False,
dtype='float32')
pbv = layers.data(
name='prior_box_var',
shape=[10, 4],
append_batch_size=False,
dtype='float32')
loc = layers.data(name='target_box', shape=[10, 4], dtype='float32')
scores = layers.data(name='scores', shape=[10, 21], dtype='float32')
gt_box = layers.data(
name='gt_box', shape=[4], lod_level=1, dtype='float32')
gt_label = layers.data(
name='gt_label', shape=[1], lod_level=1, dtype='int32')
loss = layers.ssd_loss(loc, scores, gt_box, gt_label, pb, pbv)
self.assertIsNotNone(loss)
self.assertEqual(loss.shape[-1], 1)
print(str(program)) print(str(program))
@ -62,40 +118,39 @@ class TestPriorBox(unittest.TestCase):
assert box.shape[1] == 4 assert box.shape[1] == 4
def prior_box_output(self, data_shape): def prior_box_output(self, data_shape):
images = fluid.layers.data( images = layers.data(name='pixel', shape=data_shape, dtype='float32')
name='pixel', shape=data_shape, dtype='float32') conv1 = layers.conv2d(
conv1 = fluid.layers.conv2d(
input=images, input=images,
num_filters=3, num_filters=3,
filter_size=3, filter_size=3,
stride=2, stride=2,
use_cudnn=False) use_cudnn=False)
conv2 = fluid.layers.conv2d( conv2 = layers.conv2d(
input=conv1, input=conv1,
num_filters=3, num_filters=3,
filter_size=3, filter_size=3,
stride=2, stride=2,
use_cudnn=False) use_cudnn=False)
conv3 = fluid.layers.conv2d( conv3 = layers.conv2d(
input=conv2, input=conv2,
num_filters=3, num_filters=3,
filter_size=3, filter_size=3,
stride=2, stride=2,
use_cudnn=False) use_cudnn=False)
conv4 = fluid.layers.conv2d( conv4 = layers.conv2d(
input=conv3, input=conv3,
num_filters=3, num_filters=3,
filter_size=3, filter_size=3,
stride=2, stride=2,
use_cudnn=False) use_cudnn=False)
conv5 = fluid.layers.conv2d( conv5 = layers.conv2d(
input=conv4, input=conv4,
num_filters=3, num_filters=3,
filter_size=3, filter_size=3,
stride=2, stride=2,
use_cudnn=False) use_cudnn=False)
box, var = detection.prior_box( box, var = layers.prior_box(
inputs=[conv1, conv2, conv3, conv4, conv5, conv5], inputs=[conv1, conv2, conv3, conv4, conv5, conv5],
image=images, image=images,
min_ratio=20, min_ratio=20,

Loading…
Cancel
Save