mean_iou_op.cc 4.2 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
/* 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 {
25 26
    OP_INOUT_CHECK(
        ctx->HasInput("Predictions"), "Input", "Predictions", "MeanIoU");
27
    OP_INOUT_CHECK(ctx->HasInput("Labels"), "Input", "Labels", "MeanIoU");
28 29
    OP_INOUT_CHECK(
        ctx->HasOutput("OutMeanIou"), "Output", "OutMeanIou", "MeanIoU");
30
    OP_INOUT_CHECK(ctx->HasOutput("OutWrong"), "Output", "OutWrong", "MeanIoU");
31 32
    OP_INOUT_CHECK(
        ctx->HasOutput("OutCorrect"), "Output", "OutCorrect", "MeanIoU");
W
whs 已提交
33 34 35 36

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

37
    ctx->SetOutputDim("OutMeanIou", phi::make_ddim({}));
W
whs 已提交
38 39 40 41 42
    ctx->SetOutputDim("OutWrong", {num_classes});
    ctx->SetOutputDim("OutCorrect", {num_classes});
  }

 protected:
43
  phi::KernelKey GetExpectedKernelType(
W
whs 已提交
44
      const framework::ExecutionContext& ctx) const override {
45
    return phi::KernelKey(
46 47
        OperatorWithKernel::IndicateVarDataType(ctx, "Predictions"),
        ctx.GetPlace());
W
whs 已提交
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
  }
};

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"
81
              " mean intersection-over-union with shape [].");
W
whs 已提交
82 83 84 85 86 87 88 89
    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
90 91
semantic class and then computes the average over classes.
IOU is defined as follows:
W
whs 已提交
92
    IOU = true_positive / (true_positive + false_positive + false_negative).
93
It is based on pixel level area while "IOU Similarity Operator"
W
whs 已提交
94 95 96 97 98 99 100 101 102 103
is based on area of rectangle.

)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
104
REGISTER_OPERATOR(
105 106 107
    mean_iou,
    ops::MeanIoUOp,
    ops::MeanIoUOpMaker,
H
hong 已提交
108 109
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
110 111 112

PD_REGISTER_STRUCT_KERNEL(
    mean_iou, CPU, ALL_LAYOUT, ops::MeanIoUKernel, int, int64_t) {}