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 29, 2019
1 parent b9e02ab commit 871d250
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 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
14 changes: 13 additions & 1 deletion tests/python/unittest/test_contrib_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def f(x, a, b, c):
b = np.random.random_sample()
c = np.random.random_sample()
m = np.random.random_sample() - 0.5

data = mx.symbol.Variable('data')
quad_sym = mx.sym.contrib.quadratic(data=data, a=a, b=b, c=c)
gr_q_sym = mx.sym.contrib.gradientmultiplier(quad_sym, scalar=m)
Expand All @@ -297,6 +297,18 @@ def f(x, a, b, c):
[backward_expected],
rtol=1e-2 if dtype is np.float16 else 1e-5,
atol=1e-2 if dtype is np.float16 else 1e-5)
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 871d250

Please sign in to comment.