|
|
|
@ -19,7 +19,8 @@ import math
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def box_coder(target_box, prior_box, prior_box_var, output_box, code_type):
|
|
|
|
|
def box_coder(target_box, prior_box, prior_box_var, output_box, code_type,
|
|
|
|
|
box_normalized):
|
|
|
|
|
prior_box_x = (
|
|
|
|
|
(prior_box[:, 2] + prior_box[:, 0]) / 2).reshape(1, prior_box.shape[0])
|
|
|
|
|
prior_box_y = (
|
|
|
|
@ -30,6 +31,9 @@ def box_coder(target_box, prior_box, prior_box_var, output_box, code_type):
|
|
|
|
|
(prior_box[:, 3] - prior_box[:, 1])).reshape(1, prior_box.shape[0])
|
|
|
|
|
prior_box_var = prior_box_var.reshape(1, prior_box_var.shape[0],
|
|
|
|
|
prior_box_var.shape[1])
|
|
|
|
|
if not box_normalized:
|
|
|
|
|
prior_box_height = prior_box_height + 1
|
|
|
|
|
prior_box_width = prior_box_width + 1
|
|
|
|
|
|
|
|
|
|
if (code_type == "EncodeCenterSize"):
|
|
|
|
|
target_box_x = ((target_box[:, 2] + target_box[:, 0]) / 2).reshape(
|
|
|
|
@ -40,6 +44,9 @@ def box_coder(target_box, prior_box, prior_box_var, output_box, code_type):
|
|
|
|
|
target_box.shape[0], 1)
|
|
|
|
|
target_box_height = ((target_box[:, 3] - target_box[:, 1])).reshape(
|
|
|
|
|
target_box.shape[0], 1)
|
|
|
|
|
if not box_normalized:
|
|
|
|
|
target_box_height = target_box_height + 1
|
|
|
|
|
target_box_width = target_box_width + 1
|
|
|
|
|
|
|
|
|
|
output_box[:,:,0] = (target_box_x - prior_box_x) / prior_box_width / \
|
|
|
|
|
prior_box_var[:,:,0]
|
|
|
|
@ -64,9 +71,13 @@ def box_coder(target_box, prior_box, prior_box_var, output_box, code_type):
|
|
|
|
|
output_box[:, :, 1] = target_box_y - target_box_height / 2
|
|
|
|
|
output_box[:, :, 2] = target_box_x + target_box_width / 2
|
|
|
|
|
output_box[:, :, 3] = target_box_y + target_box_height / 2
|
|
|
|
|
if not box_normalized:
|
|
|
|
|
output_box[:, :, 2] = output_box[:, :, 2] - 1
|
|
|
|
|
output_box[:, :, 3] = output_box[:, :, 3] - 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def batch_box_coder(prior_box, prior_box_var, target_box, lod, code_type):
|
|
|
|
|
def batch_box_coder(prior_box, prior_box_var, target_box, lod, code_type,
|
|
|
|
|
box_normalized):
|
|
|
|
|
n = target_box.shape[0]
|
|
|
|
|
m = prior_box.shape[0]
|
|
|
|
|
output_box = np.zeros((n, m, 4), dtype=np.float32)
|
|
|
|
@ -74,11 +85,11 @@ def batch_box_coder(prior_box, prior_box_var, target_box, lod, code_type):
|
|
|
|
|
if (code_type == "EncodeCenterSize"):
|
|
|
|
|
box_coder(target_box[lod[i]:lod[i + 1], :], prior_box,
|
|
|
|
|
prior_box_var, output_box[lod[i]:lod[i + 1], :, :],
|
|
|
|
|
code_type)
|
|
|
|
|
code_type, box_normalized)
|
|
|
|
|
elif (code_type == "DecodeCenterSize"):
|
|
|
|
|
box_coder(target_box[lod[i]:lod[i + 1], :, :], prior_box,
|
|
|
|
|
prior_box_var, output_box[lod[i]:lod[i + 1], :, :],
|
|
|
|
|
code_type)
|
|
|
|
|
code_type, box_normalized)
|
|
|
|
|
return output_box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -93,15 +104,19 @@ class TestBoxCoderOp(OpTest):
|
|
|
|
|
prior_box_var = np.random.random((10, 4)).astype('float32')
|
|
|
|
|
target_box = np.random.random((5, 10, 4)).astype('float32')
|
|
|
|
|
code_type = "DecodeCenterSize"
|
|
|
|
|
box_normalized = False
|
|
|
|
|
output_box = batch_box_coder(prior_box, prior_box_var, target_box,
|
|
|
|
|
lod[0], code_type)
|
|
|
|
|
lod[0], code_type, box_normalized)
|
|
|
|
|
|
|
|
|
|
self.inputs = {
|
|
|
|
|
'PriorBox': prior_box,
|
|
|
|
|
'PriorBoxVar': prior_box_var,
|
|
|
|
|
'TargetBox': target_box,
|
|
|
|
|
}
|
|
|
|
|
self.attrs = {'code_type': 'decode_center_size'}
|
|
|
|
|
self.attrs = {
|
|
|
|
|
'code_type': 'decode_center_size',
|
|
|
|
|
'box_normalized': False
|
|
|
|
|
}
|
|
|
|
|
self.outputs = {'OutputBox': output_box}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -116,15 +131,16 @@ class TestBoxCoderOpWithLoD(OpTest):
|
|
|
|
|
prior_box_var = np.random.random((10, 4)).astype('float32')
|
|
|
|
|
target_box = np.random.random((20, 4)).astype('float32')
|
|
|
|
|
code_type = "EncodeCenterSize"
|
|
|
|
|
box_normalized = True
|
|
|
|
|
output_box = batch_box_coder(prior_box, prior_box_var, target_box,
|
|
|
|
|
lod[0], code_type)
|
|
|
|
|
lod[0], code_type, box_normalized)
|
|
|
|
|
|
|
|
|
|
self.inputs = {
|
|
|
|
|
'PriorBox': prior_box,
|
|
|
|
|
'PriorBoxVar': prior_box_var,
|
|
|
|
|
'TargetBox': (target_box, lod),
|
|
|
|
|
}
|
|
|
|
|
self.attrs = {'code_type': 'encode_center_size'}
|
|
|
|
|
self.attrs = {'code_type': 'encode_center_size', 'box_normalized': True}
|
|
|
|
|
self.outputs = {'OutputBox': output_box}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|