Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix confusion results if ratios are set differently
Browse files Browse the repository at this point in the history
  • Loading branch information
zhreshold committed Jan 2, 2019
1 parent d7f9a07 commit 408f7a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/operator/contrib/multibox_prior.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ inline void MultiBoxPriorForward(const Tensor<cpu, 2, DType> &out,
float center_y = (r + offsets[0]) * step_y;
for (int c = 0; c < in_width; ++c) {
float center_x = (c + offsets[1]) * step_x;
// ratio = 1, various sizes
// ratio = first ratio, various sizes
float ratio = num_ratios > 0? sqrtf(ratios[0]) : 1.f;
for (int i = 0; i < num_sizes; ++i) {
float size = sizes[i];
float w = size * in_height / in_width / 2;
float h = size / 2;
float w = size * in_height / in_width * ratio / 2;
float h = size / ratio / 2;
out[count][0] = center_x - w; // xmin
out[count][1] = center_y - h; // ymin
out[count][2] = center_x + w; // xmax
Expand Down
5 changes: 3 additions & 2 deletions src/operator/contrib/multibox_prior.cu
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ inline void MultiBoxPriorForward(const Tensor<gpu, 2, DType> &out,

const int stride = 4 * (num_sizes + num_ratios - 1);
int offset = 0;
// ratio = 1, various sizes
// ratio = first ratio, various sizes
float ratio = num_ratios > 0? sqrtf(ratios[0]) : 1.f;
for (int i = 0; i < num_sizes; ++i) {
cuda::AssignPriors<DType><<<dimGrid, dimBlock, 0, stream>>>(out_ptr,
sizes[i], 1.f, in_width, in_height, step_x, step_y, offset_y, offset_x, stride, offset);
sizes[i], ratio, in_width, in_height, step_x, step_y, offset_y, offset_x, stride, offset);
++offset;
}
MULTIBOXPRIOR_CUDA_CHECK(cudaPeekAtLastError());
Expand Down
12 changes: 12 additions & 0 deletions tests/python/unittest/test_contrib_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ def test_multibox_target_op():
assert_array_equal(loc_mask.asnumpy(), expected_loc_mask)
assert_array_equal(cls_target.asnumpy(), expected_cls_target)

def test_multibox_prior_op():
h = 561
w = 728
X = mx.nd.random.uniform(shape=(1, 3, h, w))
Y = mx.contrib.nd.MultiBoxPrior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5])
assert_array_equal(Y.shape, np.array((1, 2042040, 4)))
boxes = Y.reshape((h, w, 5, 4))
assert_allclose(boxes.asnumpy()[250, 250, 0, :], np.array([0.055117, 0.071524, 0.63307 , 0.821524]), atol=1e-5, rtol=1e-5)
# relax first ratio if user insists
Y = mx.contrib.nd.MultiBoxPrior(X, sizes=[0.75, 0.5, 0.25], ratios=[20, 2, 0.5])
boxes = Y.reshape((h, w, 5, 4))
assert_allclose(boxes.asnumpy()[250, 250, 0, :], np.array([-0.948249, 0.362671, 1.636436, 0.530377]), atol=1e-5, rtol=1e-5)

if __name__ == '__main__':
import nose
Expand Down

0 comments on commit 408f7a1

Please sign in to comment.