collect_fpn_proposals_op.cc 2.5 KB
Newer Older
J
juncaipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.

#include "lite/operators/collect_fpn_proposals_op.h"
#include "lite/core/op_lite.h"
#include "lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace operators {

bool CollectFpnProposalsOpLite::CheckShape() const {
  CHECK_OR_FALSE(!param_.multi_level_rois.empty());
  CHECK_OR_FALSE(!param_.multi_level_scores.empty());
  CHECK_OR_FALSE(param_.fpn_rois);

  for (auto item : param_.multi_level_rois) {
    auto dims = item->dims();
    CHECK_OR_FALSE(dims[1] == 4);
  }
  for (auto item : param_.multi_level_scores) {
    auto dims = item->dims();
J
juncaipeng 已提交
34
    CHECK_OR_FALSE(dims[1] == 1);
J
juncaipeng 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
  }
  for (int i = 0; i < param_.multi_level_rois.size(); i++) {
    auto roi = param_.multi_level_rois[i];
    auto roi_lod = roi->lod();
    auto score = param_.multi_level_scores[i];
    auto score_lod = score->lod();
    CHECK_OR_FALSE(roi_lod == score_lod);
  }
  return true;
}

bool CollectFpnProposalsOpLite::InferShape() const {
  param_.fpn_rois->Resize({param_.post_nms_topN, 4});
J
juncaipeng 已提交
48

J
juncaipeng 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  return true;
}

bool CollectFpnProposalsOpLite::AttachImpl(const cpp::OpDesc& op_desc,
                                           lite::Scope* scope) {
  auto rois_names = op_desc.Input("MultiLevelRois");
  for (const auto& var_name : rois_names) {
    param_.multi_level_rois.push_back(
        scope->FindVar(var_name)->GetMutable<lite::Tensor>());
  }
  auto scores_names = op_desc.Input("MultiLevelScores");
  for (const auto& var_name : scores_names) {
    param_.multi_level_scores.push_back(
        scope->FindVar(var_name)->GetMutable<lite::Tensor>());
  }

  auto fpn_rois = op_desc.Output("FpnRois").front();
  param_.fpn_rois = scope->FindVar(fpn_rois)->GetMutable<lite::Tensor>();

  param_.post_nms_topN = op_desc.GetAttr<int>("post_nms_topN");
  return true;
}

}  // namespace operators
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_OP(collect_fpn_proposals,
                 paddle::lite::operators::CollectFpnProposalsOpLite);