Added unit test for maxpool (#361)

https://github.com/VeriSilicon/TIM-VX/issues/318

Signed-off-by: Chen Xin <jack.chen@verisilicon.com>
This commit is contained in:
chxin66 2022-04-13 22:16:47 +08:00 committed by GitHub
parent 12746cb4d7
commit 0dc38eac2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 15 deletions

View File

@ -42,6 +42,7 @@ namespace ops {
* *
* - type : MAX, AVG, L2 or AVG_ANDROID. * - type : MAX, AVG, L2 or AVG_ANDROID.
* - padding : AUTO, VALID or SAME. * - padding : AUTO, VALID or SAME.
* - pad : Specify the number of pad values for left, right, top, and bottom.
* - ksize : filter size. * - ksize : filter size.
* - stride : stride along each spatial axis. * - stride : stride along each spatial axis.
* - round_type : CEILING or FLOOR. * - round_type : CEILING or FLOOR.
@ -65,33 +66,32 @@ namespace ops {
class Pool2d : public DirectMapOp { class Pool2d : public DirectMapOp {
public: public:
// for Classic Pool2d /* for Classic Pool2d, pool does not support auto-completion of pad value,
you need to specify pad size explicitly, it is recommended to use the second api.*/
Pool2d(Graph* graph, PoolType type, PadType padding, Pool2d(Graph* graph, PoolType type, PadType padding,
const std::array<uint32_t, 2>& ksize, const std::array<uint32_t, 2>& ksize,
const std::array<uint32_t, 2>& stride, const std::array<uint32_t, 2>& stride,
RoundType round_type = RoundType::FLOOR, RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
Pool2d(Graph* graph, PoolType type, Pool2d(Graph* graph, PoolType type, const std::array<uint32_t, 4>& pad,
const std::array<uint32_t, 4>& pad,
const std::array<uint32_t, 2>& ksize, const std::array<uint32_t, 2>& ksize,
const std::array<uint32_t, 2>& stride, const std::array<uint32_t, 2>& stride,
RoundType round_type = RoundType::FLOOR, RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
// for Global Pool2d // for Global Pool2d
Pool2d(Graph* graph, PoolType type, Pool2d(Graph* graph, PoolType type, const std::array<uint32_t, 2>& input_size,
const std::array<uint32_t, 2>& input_size,
RoundType round_type = RoundType::FLOOR, RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
// for Adaptive Pool2d // for Adaptive Pool2d
Pool2d(Graph* graph, PoolType type, Pool2d(Graph* graph, PoolType type, const std::array<uint32_t, 2>& input_size,
const std::array<uint32_t, 2>& input_size,
const std::array<uint32_t, 2>& output_size, const std::array<uint32_t, 2>& output_size,
RoundType round_type = RoundType::FLOOR, RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override; std::shared_ptr<Operation> Clone(
std::shared_ptr<Graph>& graph) const override;
void Init(); void Init();
protected: protected:

View File

@ -0,0 +1,52 @@
#include "tim/vx/context.h"
#include "tim/vx/graph.h"
#include "tim/vx/ops/pool2d.h"
#include <iostream>
#include "gtest/gtest.h"
#include "test_utils.h"
TEST(MAX, shape_6_6_1_1_fp32_kernel_3_stride_2) {
auto ctx = tim::vx::Context::Create();
auto graph = ctx->CreateGraph();
tim::vx::ShapeType in_shape({6, 6, 1, 1});
tim::vx::ShapeType out_shape({3, 3, 1, 1});
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, in_shape,
tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, out_shape,
tim::vx::TensorAttribute::OUTPUT);
auto input_tensor = graph->CreateTensor(input_spec);
auto output_tensor = graph->CreateTensor(output_spec);
std::vector<float> in_data = {
-1, -2, -3, -4, -5, -5,
-6, -7, -8, -9, -10, -10,
-11, -12, -13, -14, -15, -15,
-16, -17, -18, -19, -20, -20,
-21, -22, -23, -24, -25, -20,
-21, -22, -23, -24, -25, -20,
};
std::vector<float> golden = {
-1, -3, -5,
-11, -13, -15,
-21, -23, -20,
};
EXPECT_TRUE(
input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4));
std::array<uint32_t, 4> pad = {0, 1, 0, 1};
std::array<uint32_t, 2> ksize = {3, 3};
std::array<uint32_t, 2> stride = {2, 2};
auto round_type = tim::vx::RoundType::CEILING;
auto op = graph->CreateOperation<tim::vx::ops::Pool2d>(
tim::vx::PoolType::MAX, pad, ksize, stride, round_type);
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(golden.size());
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
EXPECT_EQ(golden, output);
}