From ab2f26e52fddbea7a6ea95744b01a0e5d5201152 Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Fri, 4 Aug 2023 23:52:16 +1000 Subject: [PATCH 1/6] Adding multiboxPrior test based on equivalent in deepjavalibrary/dlj --- .../operator/contrib/multibox_prior_test.cc | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/cpp/operator/contrib/multibox_prior_test.cc diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc new file mode 100644 index 00000000000..8f69640695c --- /dev/null +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -0,0 +1,88 @@ +// +// The goal of this class is to be the same as this +// https://github.com/deepjavalibrary/djl/blob/master/integration/src/main/java/ai/djl/integration/tests/modality/cv/MultiBoxPriorTest.java +// + +#include "../include/test_op_runner.h" +#include "operator/contrib/multibox_prior-inl.h" +#include "mxnet/operator.h" + +#include +#include +#include + +using namespace mxnet; +using DType = float; + +TEST(CORE_OP_RUNNER, Multibox_prior) { + + std::vector sizes = {0.2f, 0.272f}; + std::vector ratios = {1.0f, 2.0f, 0.5f}; + mxnet::Tuple steps = mxnet::Tuple({-1.0f, -1.0f}); + std::vector offsets = {0.5f, 0.5f}; + + mxnet::op::MultiBoxPriorParam p1 = mxnet::op::MultiBoxPriorParam(); + p1.sizes = sizes; + p1.ratios = ratios; + p1.steps = steps; + p1.offsets = offsets; + Operator* op2 = mxnet::op::CreateOp(p1, mshadow::kFloat32); + + std::vector in_data_fwd_, in_data_bwd_; + std::vector aux_data_, out_data_, in_grad_, out_grad_; + + std::vector inputs; + std::vector outputs; + + int in_width = 512; + int in_height = 512; + + Context ctx = Context(); + + long arrangeVal = 3.0f * 512.0f * 512.0f; + TShape arrangeShape = TShape({arrangeVal}); + + NDArray arangeNdArray = NDArray(arrangeShape, ctx); + + TShape reshapeShape = TShape({1, 3, in_height, in_width}); + NDArray ndArray = arangeNdArray.Reshape(reshapeShape); + + TShape outShape = TShape({1, 1048576, 4}); + NDArray outNdArray = NDArray(outShape, ctx); + + std::vector in_data; + std::vector req; + std::vector out_data; + std::vector aux_states; + + TBlob inData = ndArray.data(); + inData.type_flag_ = mshadow::kFloat32; + + in_data = {inData}; + + TBlob outData = outNdArray.data(); + outData.type_flag_ = mshadow::kFloat32; + out_data = {outData}; + + OpContext opContext = OpContext(); + + op2->Forward(opContext,in_data,req,out_data,aux_states); + + TBlob anchors = out_data.front(); + + int64_t resultShapeDim0 = anchors.size(0); + int64_t resultShapeDim1 = anchors.size(1); + int64_t resultShapeDim2 = anchors.size(2); + assert(resultShapeDim0==1); + assert(resultShapeDim1==1048576); + assert(resultShapeDim2==4); + + //TODO: Assert these results! + + float *anchorFloatArray = anchors.dptr(); + std::stringstream stream; + stream << std::fixed << std::setprecision(8) << anchorFloatArray[0]; + std::string expectedVal = "-0.09902344"; + assert(expectedVal.compare(stream.str()) == 0); + +} \ No newline at end of file From 2558d1fca4dfa7b6554ec69d9143a6461c4daf3d Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Sat, 5 Aug 2023 00:01:26 +1000 Subject: [PATCH 2/6] Adding multiboxPrior test based on equivalent in deepjavalibrary/dlj --- tests/cpp/operator/contrib/multibox_prior_test.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc index 8f69640695c..6940c65ca16 100644 --- a/tests/cpp/operator/contrib/multibox_prior_test.cc +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -77,8 +77,6 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { assert(resultShapeDim1==1048576); assert(resultShapeDim2==4); - //TODO: Assert these results! - float *anchorFloatArray = anchors.dptr(); std::stringstream stream; stream << std::fixed << std::setprecision(8) << anchorFloatArray[0]; From 4973889525485af515d3b4cc877a34f4dde9d51e Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Sat, 5 Aug 2023 14:19:36 +1000 Subject: [PATCH 3/6] [Feedback] Add Apache header --- .../operator/contrib/multibox_prior_test.cc | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc index 6940c65ca16..d7d921d95aa 100644 --- a/tests/cpp/operator/contrib/multibox_prior_test.cc +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -1,5 +1,24 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + // -// The goal of this class is to be the same as this +// The goal of this file multibox_prior_test.cc is to be the same as this test class MultiboxPriorTest.java // https://github.com/deepjavalibrary/djl/blob/master/integration/src/main/java/ai/djl/integration/tests/modality/cv/MultiBoxPriorTest.java // From 6ae68e39dd9269d60f6c9bd820bc459fafe07b19 Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Sat, 5 Aug 2023 14:58:10 +1000 Subject: [PATCH 4/6] [feedback] ClangFormat --- .../operator/contrib/multibox_prior_test.cc | 79 +++++++++---------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc index d7d921d95aa..463c36200e0 100644 --- a/tests/cpp/operator/contrib/multibox_prior_test.cc +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -1,24 +1,25 @@ /* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ // -// The goal of this file multibox_prior_test.cc is to be the same as this test class MultiboxPriorTest.java +// The goal of this file multibox_prior_test.cc is to be the same as this test class +// MultiboxPriorTest.java // https://github.com/deepjavalibrary/djl/blob/master/integration/src/main/java/ai/djl/integration/tests/modality/cv/MultiBoxPriorTest.java // @@ -34,18 +35,17 @@ using namespace mxnet; using DType = float; TEST(CORE_OP_RUNNER, Multibox_prior) { - - std::vector sizes = {0.2f, 0.272f}; - std::vector ratios = {1.0f, 2.0f, 0.5f}; - mxnet::Tuple steps = mxnet::Tuple({-1.0f, -1.0f}); + std::vector sizes = {0.2f, 0.272f}; + std::vector ratios = {1.0f, 2.0f, 0.5f}; + mxnet::Tuple steps = mxnet::Tuple({-1.0f, -1.0f}); std::vector offsets = {0.5f, 0.5f}; mxnet::op::MultiBoxPriorParam p1 = mxnet::op::MultiBoxPriorParam(); - p1.sizes = sizes; - p1.ratios = ratios; - p1.steps = steps; - p1.offsets = offsets; - Operator* op2 = mxnet::op::CreateOp(p1, mshadow::kFloat32); + p1.sizes = sizes; + p1.ratios = ratios; + p1.steps = steps; + p1.offsets = offsets; + Operator* op2 = mxnet::op::CreateOp(p1, mshadow::kFloat32); std::vector in_data_fwd_, in_data_bwd_; std::vector aux_data_, out_data_, in_grad_, out_grad_; @@ -53,20 +53,20 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { std::vector inputs; std::vector outputs; - int in_width = 512; + int in_width = 512; int in_height = 512; Context ctx = Context(); - long arrangeVal = 3.0f * 512.0f * 512.0f; + long arrangeVal = 3.0f * 512.0f * 512.0f; TShape arrangeShape = TShape({arrangeVal}); NDArray arangeNdArray = NDArray(arrangeShape, ctx); TShape reshapeShape = TShape({1, 3, in_height, in_width}); - NDArray ndArray = arangeNdArray.Reshape(reshapeShape); + NDArray ndArray = arangeNdArray.Reshape(reshapeShape); - TShape outShape = TShape({1, 1048576, 4}); + TShape outShape = TShape({1, 1048576, 4}); NDArray outNdArray = NDArray(outShape, ctx); std::vector in_data; @@ -74,32 +74,31 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { std::vector out_data; std::vector aux_states; - TBlob inData = ndArray.data(); + TBlob inData = ndArray.data(); inData.type_flag_ = mshadow::kFloat32; in_data = {inData}; - TBlob outData = outNdArray.data(); + TBlob outData = outNdArray.data(); outData.type_flag_ = mshadow::kFloat32; - out_data = {outData}; + out_data = {outData}; OpContext opContext = OpContext(); - op2->Forward(opContext,in_data,req,out_data,aux_states); + op2->Forward(opContext, in_data, req, out_data, aux_states); TBlob anchors = out_data.front(); int64_t resultShapeDim0 = anchors.size(0); int64_t resultShapeDim1 = anchors.size(1); int64_t resultShapeDim2 = anchors.size(2); - assert(resultShapeDim0==1); - assert(resultShapeDim1==1048576); - assert(resultShapeDim2==4); + assert(resultShapeDim0 == 1); + assert(resultShapeDim1 == 1048576); + assert(resultShapeDim2 == 4); - float *anchorFloatArray = anchors.dptr(); + float* anchorFloatArray = anchors.dptr(); std::stringstream stream; stream << std::fixed << std::setprecision(8) << anchorFloatArray[0]; std::string expectedVal = "-0.09902344"; assert(expectedVal.compare(stream.str()) == 0); - } \ No newline at end of file From 0cb1375fa7716bbe67ce26789b4e1bc060752f82 Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Sat, 5 Aug 2023 15:28:47 +1000 Subject: [PATCH 5/6] [feedback] Lint fixes --- tests/cpp/operator/contrib/multibox_prior_test.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc index 463c36200e0..c4f58fcd3a3 100644 --- a/tests/cpp/operator/contrib/multibox_prior_test.cc +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -23,14 +23,14 @@ // https://github.com/deepjavalibrary/djl/blob/master/integration/src/main/java/ai/djl/integration/tests/modality/cv/MultiBoxPriorTest.java // -#include "../include/test_op_runner.h" -#include "operator/contrib/multibox_prior-inl.h" -#include "mxnet/operator.h" - #include #include #include +#include "../include/test_op_runner.h" +#include "operator/contrib/multibox_prior-inl.h" +#include "mxnet/operator.h" + using namespace mxnet; using DType = float; @@ -58,7 +58,7 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { Context ctx = Context(); - long arrangeVal = 3.0f * 512.0f * 512.0f; + int arrangeVal = 3.0f * 512.0f * 512.0f; TShape arrangeShape = TShape({arrangeVal}); NDArray arangeNdArray = NDArray(arrangeShape, ctx); @@ -101,4 +101,4 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { stream << std::fixed << std::setprecision(8) << anchorFloatArray[0]; std::string expectedVal = "-0.09902344"; assert(expectedVal.compare(stream.str()) == 0); -} \ No newline at end of file +} From 2f929ab4f0c422f233b930f8b7c4fa352cecc8bc Mon Sep 17 00:00:00 2001 From: Julian Gamble Date: Sat, 5 Aug 2023 16:02:39 +1000 Subject: [PATCH 6/6] [feedback] Lint fixes --- tests/cpp/operator/contrib/multibox_prior_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp/operator/contrib/multibox_prior_test.cc b/tests/cpp/operator/contrib/multibox_prior_test.cc index c4f58fcd3a3..a33bd0d8765 100644 --- a/tests/cpp/operator/contrib/multibox_prior_test.cc +++ b/tests/cpp/operator/contrib/multibox_prior_test.cc @@ -58,7 +58,7 @@ TEST(CORE_OP_RUNNER, Multibox_prior) { Context ctx = Context(); - int arrangeVal = 3.0f * 512.0f * 512.0f; + int arrangeVal = 3.0f * 512.0f * 512.0f; TShape arrangeShape = TShape({arrangeVal}); NDArray arangeNdArray = NDArray(arrangeShape, ctx);