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
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,55 @@
package ai.djl.mxnet.engine;

import ai.djl.engine.Engine;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.internal.NDArrayEx;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MxNDArrayExTest {
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
MxNDArrayExTest mxNDArrayExTest = new MxNDArrayExTest();
mxNDArrayExTest.testMultiBoxPrior();
}

@Test
public void testMultiBoxPrior() {
Engine engine = Engine.getInstance();

try (NDManager manager = NDManager.newBaseManager()) {

Shape shape = new Shape(1, 64, 32, 32);
DataType dataType = DataType.FLOAT32;
NDArray mxNDArray = manager.create(shape, dataType);
NDArrayEx mxNDArrayEx = mxNDArray.getNDArrayInternal();

List<Float> sizes = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
List<Float> ratios = new ArrayList<Float>(Arrays.asList(1f, 2f, 0.5f));
List<Float> steps = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
List<Float> offsets = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
boolean clip = false;
try (NDList result = mxNDArrayEx.multiBoxPrior(sizes, ratios, steps, offsets, clip)) {
NDArray firstitem = result.get(0);
Shape shapeResult = firstitem.getShape();

assert(shapeResult.getLayout().length==2);
assert(shapeResult.get(0)==6534);
assert(shapeResult.get(1)==4);
for (NDArray item: result) {
System.out.println(item);
//ND: (1, 4096, 4) cpu() float32
}

}
System.out.println("Done!");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static ai.djl.pytorch.jni.JniUtils.sqrt;
juliangamble marked this conversation as resolved.
Show resolved Hide resolved

/** {@code PtNDArrayEx} is the PyTorch implementation of the {@link NDArrayEx}. */
public class PtNDArrayEx implements NDArrayEx {

Expand Down Expand Up @@ -694,7 +696,59 @@ 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");
//Based on https:/apache/mxnet/blob/a720b15b5fa011a9610dfaeabf0792443c6abec5/src/operator/contrib/multibox_prior.cc
juliangamble marked this conversation as resolved.
Show resolved Hide resolved

NDManager ndManager = array.getManager().getParentManager();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
NDManager ndManager = array.getManager().getParentManager();
NDManager ndManager = array.getManager();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thankyou. This has been updated.


Float step_x = steps.get(1);
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
Float step_y = steps.get(0);
int num_sizes = sizes.size();
int num_ratios = ratios.size();
int count = 0;
float[][] out = new float[num_sizes][num_ratios];
juliangamble marked this conversation as resolved.
Show resolved Hide resolved

//Based on https:/apache/mxnet/blob/a720b15b5fa011a9610dfaeabf0792443c6abec5/src/operator/contrib/multibox_prior-inl.h#L116

PtNDArray input = array;
//int in_height = in_data[mboxprior_enum::kData].size(2);
//int in_width = in_data[mboxprior_enum::kData].size(3);
int in_height = input.getInt(2);
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
int in_width = input.getInt(3);

for (int r = 0; r < in_height; ++r) {
float center_y = (r + offsets.get(0)) * step_y;
for (int c = 0; c < in_width; ++c) {
float center_x = (c + offsets.get(1)) * step_x;
// ratio = first ratio, various sizes
float ratio = num_ratios > 0 ? (float)Math.sqrt(ratios.get(0)) : 1.f;
for (int i = 0; i < num_sizes; ++i) {
float size = sizes.get(i);
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
out[count][3] = center_y + h; // ymax
++count;
}
// various ratios, size = min_size = size[0]
float size = sizes.get(0);
for (int j = 1; j < num_ratios; ++j) {
float ratioLocal = (float)Math.sqrt(ratios.get(j));
float w = size * in_height / in_width * ratioLocal / 2;
float h = size / ratioLocal / 2;
out[count][0] = center_x - w; // xmin
out[count][1] = center_y - h; // ymin
out[count][2] = center_x + w; // xmax
out[count][3] = center_y + h; // ymax
++count;
}
}
}
NDArray ndArray = ndManager.create(out);
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
NDList outResult = new NDList(ndArray);
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
return outResult;
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ai.djl.pytorch.engine;

import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class PtNDArrayExTest {
public static void main(String[] args) {
PtNDArrayExTest ptNDArrayExTest = new PtNDArrayExTest();
ptNDArrayExTest.testMultiBoxPrior();
}

@Test
public void testMultiBoxPrior() {
juliangamble marked this conversation as resolved.
Show resolved Hide resolved
try (PtNDManager manager = (PtNDManager) NDManager.newBaseManager()) {
Shape shape = new Shape(1, 64, 32, 32);
DataType dataType = DataType.FLOAT32;
PtNDArray ptNDArray = manager.create(shape, dataType);
PtNDArrayEx ptNDArrayEx = new PtNDArrayEx(ptNDArray);

List<Float> sizes = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
List<Float> ratios = new ArrayList<Float>(Arrays.asList(1f, 2f, 0.5f));
List<Float> steps = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
List<Float> offsets = new ArrayList<Float>(Arrays.asList(0.2f, 0.272f));
boolean clip = false;
try (NDList result = ptNDArrayEx.multiBoxPrior(sizes, ratios, steps, offsets, clip)) {
NDArray firstitem = result.get(0);
Shape shapeResult = firstitem.getShape();
assert(shapeResult.getLayout().length==2);
assert(shapeResult.get(0)==6534);
assert(shapeResult.get(1)==4);
for (NDArray item: result) {
System.out.println(item);
//ND: (6534, 4) cpu() float32
}
}

System.out.println("Done!");
}
}
}
Loading