mean_iou_op.cc 4.3 KB
Newer Older
W
whs 已提交
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* Copyright (c) 2018 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 "paddle/fluid/operators/mean_iou_op.h"

namespace paddle {
namespace operators {

class MeanIoUOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Predictions"),
                   "Input (Predictions) of MeanIoU op should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Labels"),
                   "Input (labels) of MeanIoU op should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("OutMeanIou"),
                   "Output (OutMeanIou) of MeanIoU op should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("OutWrong"),
                   "Output (OutWrong) of MeanIoU op should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("OutCorrect"),
                   "Output (OutWrong) of MeanIoU op should not be null.");

    int64_t num_classes =
        static_cast<int64_t>(ctx->Attrs().Get<int>("num_classes"));

    ctx->SetOutputDim("OutMeanIou", {1});
    ctx->SetOutputDim("OutWrong", {num_classes});
    ctx->SetOutputDim("OutCorrect", {num_classes});
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("Predictions")->type()),
        ctx.GetPlace());
  }
};

class MeanIoUOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("Predictions",
             "(Tensor), A Tensor of prediction results for semantic labels"
             " with type int32 or int64. The rank should be greater than 1.");
    AddInput(
        "Labels",
        "(Tensor), A Tensor of ground truth labels with type int32 or int64."
        "Its shape should be the same as Input(Predictions).");
    AddInput("InWrongs",
             "(vector<Tensor>), A list of Tensor with shape "
             "[num_classes]. They are used to collect wrong number among "
             "batches. Empty list is also valid here.")
        .AsDuplicable()
        .AsDispensable();
    AddInput(
        "InCorrects",
        "(vector<Tensor>), A list of Tensor with shape "
        "[num_classes]. They are used to collect correct number among batches. "
        "Empty list is also valid here.")
        .AsDuplicable()
        .AsDispensable();
    AddInput("InMeanIou",
             "(vector<Tensor>), A list of Tensor that Output(mean_iou) should "
             "be added to. Empty list is also valid here.")
        .AsDuplicable()
        .AsDispensable();
    AddOutput("OutMeanIou",
              "(vector<Tensor>), A Tensor representing the"
              " mean intersection-over-union with shape [1].");
    AddOutput("OutWrong", "(Tensor), A Tensor with shape [num_classes]. ");
    AddOutput("OutCorrect", "(Tensor), A Tensor with shape [num_classes]. ");
    AddAttr<int>("num_classes", "(int), The possible number of labels.");

    AddComment(R"DOC(
mean-IOU Operator.
Mean Intersection-Over-Union is a common evaluation metric for
semantic image segmentation, which first computes the IOU for each
semantic class and then computes the average over classes. 
IOU is defined as follows: 
    IOU = true_positive / (true_positive + false_positive + false_negative).
It is based on pixel level area while "IOU Similarity Operator" 
is based on area of rectangle.

)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(mean_iou, ops::MeanIoUOp, ops::MeanIoUOpMaker,
                  paddle::framework::EmptyGradOpMaker);
REGISTER_OP_CPU_KERNEL(mean_iou, ops::MeanIoUKernel<int>,
                       ops::MeanIoUKernel<int32_t>,
                       ops::MeanIoUKernel<int64_t>);