Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2693 Implement PtNDArrayEx.multiBoxPrior with validation #2715

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,60 @@ public NDList multiBoxPrior(
List<Float> steps,
List<Float> offsets,
boolean clip) {
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
throw new UnsupportedOperationException("Not implemented");

NDManager ndManager = array.getManager();

Shape input = array.getShape();
int inHeight = Math.toIntExact(input.get(2));
int inWidth = Math.toIntExact(input.get(3));

if (steps.get(0) <= 0 || steps.get(1) <= 0) {
// estimate using layer shape
steps.set(0, 1.f / inHeight);
steps.set(1, 1.f / inWidth);
}

float stepX = steps.get(1);
float stepY = steps.get(0);
int numSizes = sizes.size();
int numRatios = ratios.size();
int count = 0;

float[][] out = new float[inHeight * inWidth * numSizes * 2][4];

for (int r = 0; r < inHeight; ++r) {
float centerY = (r + offsets.get(0)) * stepY;
for (int c = 0; c < inWidth; ++c) {
float centerX = (c + offsets.get(1)) * stepX;
// ratio = first ratio, various sizes
float ratio = numRatios > 0 ? (float) Math.sqrt(ratios.get(0)) : 1.f;
for (int i = 0; i < numSizes; ++i) {
float size = sizes.get(i);
float w = size * inHeight / inWidth * ratio / 2;
float h = size / ratio / 2;

out[count][0] = centerX - w; // xmin
out[count][1] = centerY - h; // ymin
out[count][2] = centerX + w; // xmax
out[count][3] = centerY + h; // ymax
++count;
}
// various ratios, size = min_size = size[0]
float size = sizes.get(0);
for (int j = 1; j < numRatios; ++j) {
float ratioLocal = (float) Math.sqrt(ratios.get(j));
float w = size * inHeight / inWidth * ratioLocal / 2;
float h = size / ratioLocal / 2;
out[count][0] = centerX - w; // xmin
out[count][1] = centerY - h; // ymin
out[count][2] = centerX + w; // xmax
out[count][3] = centerY + h; // ymax
++count;
}
}
}
NDArray ndArray = ndManager.create(out).expandDims(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't look into your algorithm, but seems the final output is different from MXNet. Can you debug why it's different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some discussion of this here:
#2693

I'd love to debug it on the mxnet side, but this is non-trivial. I'm working on a method to do so, but would appreciate some pointers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @frankfliu I found the bug and put in a fix.

For me the first element in the result is -0.09902344 in my implementation and mxnet.
From your perspective is this now returning the correct values?

return new NDList(ndArray);
}

/** {@inheritDoc} */
Expand Down
Loading