add Global Pool2d & Adaptive Pool2d (#210)

This commit is contained in:
Antkillerfarm 2021-11-09 20:25:02 +08:00 committed by GitHub
parent 23ec5e9da5
commit 214cbe5874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 2 deletions

View File

@ -36,6 +36,8 @@ namespace ops {
/** /**
* ## Pool2d * ## Pool2d
* *
* ### Classic Pool2d
*
* Performs an 2-D pooling operation. * Performs an 2-D pooling operation.
* *
* - type : MAX, AVG, L2 or AVG_ANDROID. * - type : MAX, AVG, L2 or AVG_ANDROID.
@ -43,10 +45,27 @@ namespace ops {
* - 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.
*
* ### Global Pool2d
*
* - type : MAX, AVG, L2 or AVG_ANDROID.
* - input_size : input size(only [W H])
* - round_type : CEILING or FLOOR.
*
* ### Adaptive Pool2d
*
* Same as torch.nn.AdaptiveXXXPool2d.
*
* - type : MAX, AVG, L2 or AVG_ANDROID.
* - input_size : input size(only [W H])
* - output_size : output size(only [W H])
* - round_type : CEILING or FLOOR.
*
*/ */
class Pool2d : public Operation { class Pool2d : public Operation {
public: public:
// for Classic Pool2d
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,
@ -59,13 +78,27 @@ class Pool2d : public Operation {
RoundType round_type = RoundType::FLOOR, RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN); DataLayout layout = DataLayout::WHCN);
// for Global Pool2d
Pool2d(Graph* graph, PoolType type,
const std::array<uint32_t, 2>& input_size,
RoundType round_type = RoundType::FLOOR,
DataLayout layout = DataLayout::WHCN);
// for Adaptive Pool2d
Pool2d(Graph* graph, PoolType type,
const std::array<uint32_t, 2>& input_size,
const std::array<uint32_t, 2>& output_size,
RoundType round_type = RoundType::FLOOR,
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();
protected: protected:
const PoolType type_; const PoolType type_;
const PadType padding_; const PadType padding_;
const std::array<uint32_t, 2> ksize_; std::array<uint32_t, 2> ksize_;
const std::array<uint32_t, 2> stride_; std::array<uint32_t, 2> stride_;
const RoundType round_type_; const RoundType round_type_;
const std::array<uint32_t, 4> pad_; const std::array<uint32_t, 4> pad_;
}; };

View File

@ -31,6 +31,7 @@ namespace tim {
namespace vx { namespace vx {
namespace ops { namespace ops {
// for Classic Pool2d
Pool2d::Pool2d(Graph* graph, PoolType type, PadType padding, Pool2d::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, RoundType round_type, const std::array<uint32_t, 2>& stride, RoundType round_type,
@ -61,6 +62,37 @@ Pool2d::Pool2d(Graph* graph, PoolType type,
: Operation(graph, VSI_NN_OP_POOL, 1, 1, layout), : Operation(graph, VSI_NN_OP_POOL, 1, 1, layout),
type_(type), padding_(PadType::AUTO), ksize_(ksize), stride_(stride), type_(type), padding_(PadType::AUTO), ksize_(ksize), stride_(stride),
round_type_(round_type), pad_(pad) { round_type_(round_type), pad_(pad) {
Init();
}
// for Global Pool2d
Pool2d::Pool2d(Graph* graph, PoolType type,
const std::array<uint32_t, 2>& input_size,
RoundType round_type,
DataLayout layout)
: Operation(graph, VSI_NN_OP_POOL, 1, 1, layout),
type_(type), padding_(PadType::AUTO), ksize_(input_size), stride_(input_size),
round_type_(round_type), pad_({0, 0, 0, 0}) {
Init();
}
// for Adaptive Pool2d
Pool2d::Pool2d(Graph* graph, PoolType type,
const std::array<uint32_t, 2>& input_size,
const std::array<uint32_t, 2>& output_size,
RoundType round_type,
DataLayout layout)
: Operation(graph, VSI_NN_OP_POOL, 1, 1, layout),
type_(type), padding_(PadType::AUTO),
round_type_(round_type), pad_({0, 0, 0, 0}) {
stride_[0] = floor(input_size[0] / (float)(output_size[0]));
stride_[1] = floor(input_size[1] / (float)(output_size[1]));
ksize_[0] = input_size[0] - (output_size[0] - 1) * stride_[0];
ksize_[1] = input_size[1] - (output_size[1] - 1) * stride_[1];
Init();
}
void Pool2d::Init() {
this->impl()->node()->nn_param.pool.type = TranslatePoolType(type_); this->impl()->node()->nn_param.pool.type = TranslatePoolType(type_);
this->impl()->node()->nn_param.pool.round_type = this->impl()->node()->nn_param.pool.round_type =
TranslateRoundType(round_type_); TranslateRoundType(round_type_);