yolov3_loss_op.h 18.5 KB
Newer Older
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
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
   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. */

#pragma once
#include <algorithm>
#include <vector>
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
template <typename T, size_t D, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

28
using Array5 = Eigen::DSizes<int64_t, 5>;
29 30 31 32 33 34 35

template <typename T>
static inline bool isZero(T x) {
  return abs(x) < 1e-6;
}

template <typename T>
36
static inline T sigmoid(T x) {
37 38 39
  return 1.0 / (exp(-1.0 * x) + 1.0);
}

40 41 42 43 44 45 46 47 48 49 50 51
template <typename T>
static inline T CalcMaskPointNum(const Tensor& mask) {
  auto mask_t = EigenVector<int>::Flatten(mask);
  T count = 0.0;
  for (int i = 0; i < mask_t.dimensions()[0]; i++) {
    if (mask_t(i)) {
      count += 1.0;
    }
  }
  return count;
}

52 53 54 55 56
template <typename T>
static inline T CalcMSEWithMask(const Tensor& x, const Tensor& y,
                                const Tensor& mask) {
  auto x_t = EigenVector<T>::Flatten(x);
  auto y_t = EigenVector<T>::Flatten(y);
57
  auto mask_t = EigenVector<int>::Flatten(mask);
D
dengkaipeng 已提交
58 59 60 61 62 63 64 65 66 67

  T error_sum = 0.0;
  T points = 0.0;
  for (int i = 0; i < x_t.dimensions()[0]; i++) {
    if (mask_t(i)) {
      error_sum += pow(x_t(i) - y_t(i), 2);
      points += 1;
    }
  }
  return (error_sum / points);
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
template <typename T>
static void CalcMSEGradWithMask(Tensor* grad, const Tensor& x, const Tensor& y,
                                const Tensor& mask, T mf) {
  auto grad_t = EigenVector<T>::Flatten(*grad).setConstant(0.0);
  auto x_t = EigenVector<T>::Flatten(x);
  auto y_t = EigenVector<T>::Flatten(y);
  auto mask_t = EigenVector<int>::Flatten(mask);

  for (int i = 0; i < x_t.dimensions()[0]; i++) {
    if (mask_t(i)) {
      grad_t(i) = 2.0 * (x_t(i) - y_t(i)) / mf;
    }
  }
}

85 86 87 88 89
template <typename T>
static inline T CalcBCEWithMask(const Tensor& x, const Tensor& y,
                                const Tensor& mask) {
  auto x_t = EigenVector<T>::Flatten(x);
  auto y_t = EigenVector<T>::Flatten(y);
90
  auto mask_t = EigenVector<int>::Flatten(mask);
91

D
dengkaipeng 已提交
92 93 94 95 96 97 98 99 100 101
  T error_sum = 0.0;
  T points = 0.0;
  for (int i = 0; i < x_t.dimensions()[0]; i++) {
    if (mask_t(i)) {
      error_sum +=
          -1.0 * (y_t(i) * log(x_t(i)) + (1.0 - y_t(i)) * log(1.0 - x_t(i)));
      points += 1;
    }
  }
  return (error_sum / points);
102 103 104
}

template <typename T>
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static inline void CalcBCEGradWithMask(Tensor* grad, const Tensor& x,
                                       const Tensor& y, const Tensor& mask,
                                       T mf) {
  auto grad_t = EigenVector<T>::Flatten(*grad).setConstant(0.0);
  auto x_t = EigenVector<T>::Flatten(x);
  auto y_t = EigenVector<T>::Flatten(y);
  auto mask_t = EigenVector<int>::Flatten(mask);

  for (int i = 0; i < x_t.dimensions()[0]; i++) {
    if (mask_t(i)) {
      grad_t(i) = ((1.0 - y_t(i)) / (1.0 - x_t(i)) - y_t(i) / x_t(i)) / mf;
    }
  }
}

template <typename T>
static void CalcPredResult(const Tensor& input, Tensor* pred_conf,
                           Tensor* pred_class, Tensor* pred_x, Tensor* pred_y,
                           Tensor* pred_w, Tensor* pred_h, const int anchor_num,
                           const int class_num) {
125 126 127 128 129 130
  const int n = input.dims()[0];
  const int h = input.dims()[2];
  const int w = input.dims()[3];
  const int box_attr_num = 5 + class_num;

  auto input_t = EigenTensor<T, 4>::From(input);
131 132
  auto pred_conf_t = EigenTensor<T, 4>::From(*pred_conf);
  auto pred_class_t = EigenTensor<T, 5>::From(*pred_class);
133 134 135 136 137 138 139 140 141 142
  auto pred_x_t = EigenTensor<T, 4>::From(*pred_x);
  auto pred_y_t = EigenTensor<T, 4>::From(*pred_y);
  auto pred_w_t = EigenTensor<T, 4>::From(*pred_w);
  auto pred_h_t = EigenTensor<T, 4>::From(*pred_h);

  for (int i = 0; i < n; i++) {
    for (int an_idx = 0; an_idx < anchor_num; an_idx++) {
      for (int j = 0; j < h; j++) {
        for (int k = 0; k < w; k++) {
          pred_x_t(i, an_idx, j, k) =
143
              sigmoid(input_t(i, box_attr_num * an_idx, j, k));
144
          pred_y_t(i, an_idx, j, k) =
145
              sigmoid(input_t(i, box_attr_num * an_idx + 1, j, k));
146
          pred_w_t(i, an_idx, j, k) =
D
dengkaipeng 已提交
147
              input_t(i, box_attr_num * an_idx + 2, j, k);
148
          pred_h_t(i, an_idx, j, k) =
D
dengkaipeng 已提交
149
              input_t(i, box_attr_num * an_idx + 3, j, k);
150

151 152
          pred_conf_t(i, an_idx, j, k) =
              sigmoid(input_t(i, box_attr_num * an_idx + 4, j, k));
153 154

          for (int c = 0; c < class_num; c++) {
155 156
            pred_class_t(i, an_idx, j, k, c) =
                sigmoid(input_t(i, box_attr_num * an_idx + 5 + c, j, k));
157 158 159 160 161 162 163 164
          }
        }
      }
    }
  }
}

template <typename T>
D
dengkaipeng 已提交
165 166 167 168 169 170 171 172 173 174 175 176
static T CalcBoxIoU(std::vector<T> box1, std::vector<T> box2) {
  T b1_x1 = box1[0] - box1[2] / 2;
  T b1_x2 = box1[0] + box1[2] / 2;
  T b1_y1 = box1[1] - box1[3] / 2;
  T b1_y2 = box1[1] + box1[3] / 2;
  T b2_x1 = box2[0] - box2[2] / 2;
  T b2_x2 = box2[0] + box2[2] / 2;
  T b2_y1 = box2[1] - box2[3] / 2;
  T b2_y2 = box2[1] + box2[3] / 2;

  T b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1);
  T b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1);
177 178 179 180 181

  T inter_rect_x1 = std::max(b1_x1, b2_x1);
  T inter_rect_y1 = std::max(b1_y1, b2_y1);
  T inter_rect_x2 = std::min(b1_x2, b2_x2);
  T inter_rect_y2 = std::min(b1_y2, b2_y2);
D
dengkaipeng 已提交
182 183
  T inter_area = std::max(inter_rect_x2 - inter_rect_x1, static_cast<T>(0.0)) *
                 std::max(inter_rect_y2 - inter_rect_y1, static_cast<T>(0.0));
184

D
dengkaipeng 已提交
185
  return inter_area / (b1_area + b2_area - inter_area);
186 187 188
}

template <typename T>
D
dengkaipeng 已提交
189
static void PrePorcessGTBox(const Tensor& gt_boxes, const float ignore_thresh,
190 191 192
                            std::vector<int> anchors, const int grid_size,
                            Tensor* obj_mask, Tensor* noobj_mask, Tensor* tx,
                            Tensor* ty, Tensor* tw, Tensor* th, Tensor* tconf,
D
dengkaipeng 已提交
193
                            Tensor* tclass) {
194 195 196 197
  const int n = gt_boxes.dims()[0];
  const int b = gt_boxes.dims()[1];
  const int anchor_num = anchors.size() / 2;
  auto gt_boxes_t = EigenTensor<T, 3>::From(gt_boxes);
D
dengkaipeng 已提交
198 199
  auto obj_mask_t = EigenTensor<int, 4>::From(*obj_mask).setConstant(0);
  auto noobj_mask_t = EigenTensor<int, 4>::From(*noobj_mask).setConstant(1);
200 201 202 203 204 205 206 207 208 209 210 211 212 213
  auto tx_t = EigenTensor<T, 4>::From(*tx).setConstant(0.0);
  auto ty_t = EigenTensor<T, 4>::From(*ty).setConstant(0.0);
  auto tw_t = EigenTensor<T, 4>::From(*tw).setConstant(0.0);
  auto th_t = EigenTensor<T, 4>::From(*th).setConstant(0.0);
  auto tconf_t = EigenTensor<T, 4>::From(*tconf).setConstant(0.0);
  auto tclass_t = EigenTensor<T, 5>::From(*tclass).setConstant(0.0);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < b; j++) {
      if (isZero(gt_boxes_t(i, j, 0)) && isZero(gt_boxes_t(i, j, 1)) &&
          isZero(gt_boxes_t(i, j, 2)) && isZero(gt_boxes_t(i, j, 3))) {
        continue;
      }

214
      int gt_label = static_cast<int>(gt_boxes_t(i, j, 0));
D
dengkaipeng 已提交
215 216 217 218
      T gx = gt_boxes_t(i, j, 1) * grid_size;
      T gy = gt_boxes_t(i, j, 2) * grid_size;
      T gw = gt_boxes_t(i, j, 3) * grid_size;
      T gh = gt_boxes_t(i, j, 4) * grid_size;
219 220 221
      int gi = static_cast<int>(gx);
      int gj = static_cast<int>(gy);

222
      T max_iou = static_cast<T>(0);
223 224 225 226 227 228
      T iou;
      int best_an_index = -1;
      std::vector<T> gt_box({0, 0, gw, gh});
      for (int an_idx = 0; an_idx < anchor_num; an_idx++) {
        std::vector<T> anchor_shape({0, 0, static_cast<T>(anchors[2 * an_idx]),
                                     static_cast<T>(anchors[2 * an_idx + 1])});
D
dengkaipeng 已提交
229
        iou = CalcBoxIoU<T>(gt_box, anchor_shape);
230 231 232 233 234
        if (iou > max_iou) {
          max_iou = iou;
          best_an_index = an_idx;
        }
        if (iou > ignore_thresh) {
235
          noobj_mask_t(i, an_idx, gj, gi) = 0;
236 237
        }
      }
238 239
      obj_mask_t(i, best_an_index, gj, gi) = 1;
      noobj_mask_t(i, best_an_index, gj, gi) = 0;
240 241
      tx_t(i, best_an_index, gj, gi) = gx - gi;
      ty_t(i, best_an_index, gj, gi) = gy - gj;
D
dengkaipeng 已提交
242 243
      tw_t(i, best_an_index, gj, gi) = log(gw / anchors[2 * best_an_index]);
      th_t(i, best_an_index, gj, gi) = log(gh / anchors[2 * best_an_index + 1]);
244 245
      tclass_t(i, best_an_index, gj, gi, gt_label) = 1;
      tconf_t(i, best_an_index, gj, gi) = 1;
246 247
    }
  }
248 249 250 251 252 253 254 255 256 257 258 259 260 261
}

static void ExpandObjMaskByClassNum(Tensor* obj_mask_expand,
                                    const Tensor& obj_mask) {
  const int n = obj_mask_expand->dims()[0];
  const int an_num = obj_mask_expand->dims()[1];
  const int h = obj_mask_expand->dims()[2];
  const int w = obj_mask_expand->dims()[3];
  const int class_num = obj_mask_expand->dims()[4];
  auto obj_mask_expand_t = EigenTensor<int, 5>::From(*obj_mask_expand);
  auto obj_mask_t = EigenTensor<int, 4>::From(obj_mask);

  obj_mask_expand_t = obj_mask_t.reshape(Array5(n, an_num, h, w, 1))
                          .broadcast(Array5(1, 1, 1, 1, class_num));
262 263
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
template <typename T>
static void AddAllGradToInputGrad(
    Tensor* grad, T loss, const Tensor& pred_x, const Tensor& pred_y,
    const Tensor& pred_conf, const Tensor& pred_class, const Tensor& grad_x,
    const Tensor& grad_y, const Tensor& grad_w, const Tensor& grad_h,
    const Tensor& grad_conf_obj, const Tensor& grad_conf_noobj,
    const Tensor& grad_class, const int class_num) {
  const int n = pred_x.dims()[0];
  const int an_num = pred_x.dims()[1];
  const int h = pred_x.dims()[2];
  const int w = pred_x.dims()[3];
  const int attr_num = class_num + 5;
  auto grad_t = EigenTensor<T, 4>::From(*grad).setConstant(0.0);
  auto pred_x_t = EigenTensor<T, 4>::From(pred_x);
  auto pred_y_t = EigenTensor<T, 4>::From(pred_y);
  auto pred_conf_t = EigenTensor<T, 4>::From(pred_conf);
  auto pred_class_t = EigenTensor<T, 5>::From(pred_class);
  auto grad_x_t = EigenTensor<T, 4>::From(grad_x);
  auto grad_y_t = EigenTensor<T, 4>::From(grad_y);
  auto grad_w_t = EigenTensor<T, 4>::From(grad_w);
  auto grad_h_t = EigenTensor<T, 4>::From(grad_h);
  auto grad_conf_obj_t = EigenTensor<T, 4>::From(grad_conf_obj);
  auto grad_conf_noobj_t = EigenTensor<T, 4>::From(grad_conf_noobj);
  auto grad_class_t = EigenTensor<T, 5>::From(grad_class);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < an_num; j++) {
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          grad_t(i, j * attr_num, k, l) = grad_x_t(i, j, k, l) *
                                          pred_x_t(i, j, k, l) *
                                          (1.0 - pred_x_t(i, j, k, l)) * loss;
          grad_t(i, j * attr_num + 1, k, l) =
              grad_y_t(i, j, k, l) * pred_y_t(i, j, k, l) *
              (1.0 - pred_y_t(i, j, k, l)) * loss;
          grad_t(i, j * attr_num + 2, k, l) = grad_w_t(i, j, k, l) * loss;
          grad_t(i, j * attr_num + 3, k, l) = grad_h_t(i, j, k, l) * loss;
          grad_t(i, j * attr_num + 4, k, l) =
              grad_conf_obj_t(i, j, k, l) * pred_conf_t(i, j, k, l) *
              (1.0 - pred_conf_t(i, j, k, l)) * loss;
          grad_t(i, j * attr_num + 4, k, l) +=
              grad_conf_noobj_t(i, j, k, l) * pred_conf_t(i, j, k, l) *
              (1.0 - pred_conf_t(i, j, k, l)) * loss;

          for (int c = 0; c < class_num; c++) {
            grad_t(i, j * attr_num + 5 + c, k, l) =
                grad_class_t(i, j, k, l, c) * pred_class_t(i, j, k, l, c) *
                (1.0 - pred_class_t(i, j, k, l, c)) * loss;
          }
        }
      }
    }
  }
}

319 320 321 322 323 324
template <typename DeviceContext, typename T>
class Yolov3LossKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<Tensor>("X");
    auto* gt_boxes = ctx.Input<Tensor>("GTBox");
D
dengkaipeng 已提交
325
    auto* loss = ctx.Output<Tensor>("Loss");
326 327 328 329 330 331 332 333 334 335
    auto anchors = ctx.Attr<std::vector<int>>("anchors");
    int class_num = ctx.Attr<int>("class_num");
    float ignore_thresh = ctx.Attr<float>("ignore_thresh");

    const int n = input->dims()[0];
    const int h = input->dims()[2];
    const int w = input->dims()[3];
    const int an_num = anchors.size() / 2;

    Tensor pred_x, pred_y, pred_w, pred_h;
336
    Tensor pred_conf, pred_class;
337 338 339 340
    pred_x.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_y.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_w.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_h.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
341 342 343 344
    pred_conf.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_class.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
    CalcPredResult<T>(*input, &pred_conf, &pred_class, &pred_x, &pred_y,
                      &pred_w, &pred_h, an_num, class_num);
345

D
dengkaipeng 已提交
346
    Tensor obj_mask, noobj_mask;
347
    Tensor tx, ty, tw, th, tconf, tclass;
D
dengkaipeng 已提交
348 349
    obj_mask.mutable_data<int>({n, an_num, h, w}, ctx.GetPlace());
    noobj_mask.mutable_data<int>({n, an_num, h, w}, ctx.GetPlace());
350 351 352 353 354 355
    tx.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    ty.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tw.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    th.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tconf.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tclass.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
356 357
    PrePorcessGTBox<T>(*gt_boxes, ignore_thresh, anchors, h, &obj_mask,
                       &noobj_mask, &tx, &ty, &tw, &th, &tconf, &tclass);
D
dengkaipeng 已提交
358

359 360 361 362 363
    Tensor obj_mask_expand;
    obj_mask_expand.mutable_data<int>({n, an_num, h, w, class_num},
                                      ctx.GetPlace());
    ExpandObjMaskByClassNum(&obj_mask_expand, obj_mask);

D
dengkaipeng 已提交
364 365 366 367
    T loss_x = CalcMSEWithMask<T>(pred_x, tx, obj_mask);
    T loss_y = CalcMSEWithMask<T>(pred_y, ty, obj_mask);
    T loss_w = CalcMSEWithMask<T>(pred_w, tw, obj_mask);
    T loss_h = CalcMSEWithMask<T>(pred_h, th, obj_mask);
368 369 370
    T loss_conf_obj = CalcBCEWithMask<T>(pred_conf, tconf, obj_mask);
    T loss_conf_noobj = CalcBCEWithMask<T>(pred_conf, tconf, noobj_mask);
    T loss_class = CalcBCEWithMask<T>(pred_class, tclass, obj_mask_expand);
D
dengkaipeng 已提交
371 372

    auto* loss_data = loss->mutable_data<T>({1}, ctx.GetPlace());
373 374
    loss_data[0] = loss_x + loss_y + loss_w + loss_h + loss_conf_obj +
                   loss_conf_noobj + loss_class;
375 376 377 378 379 380 381
  }
};

template <typename DeviceContext, typename T>
class Yolov3LossGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    auto* input = ctx.Input<Tensor>("X");
    auto* gt_boxes = ctx.Input<Tensor>("GTBox");
    auto anchors = ctx.Attr<std::vector<int>>("anchors");
    int class_num = ctx.Attr<int>("class_num");
    float ignore_thresh = ctx.Attr<float>("ignore_thresh");
    auto* input_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto* output_grad = ctx.Input<Tensor>(framework::GradVarName("Loss"));
    const T loss = output_grad->data<T>()[0];

    const int n = input->dims()[0];
    const int c = input->dims()[1];
    const int h = input->dims()[2];
    const int w = input->dims()[3];
    const int an_num = anchors.size() / 2;

    Tensor pred_x, pred_y, pred_w, pred_h;
    Tensor pred_conf, pred_class;
    pred_x.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_y.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_w.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_h.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_conf.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    pred_class.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
    CalcPredResult<T>(*input, &pred_conf, &pred_class, &pred_x, &pred_y,
                      &pred_w, &pred_h, an_num, class_num);

    Tensor obj_mask, noobj_mask;
    Tensor tx, ty, tw, th, tconf, tclass;
    obj_mask.mutable_data<int>({n, an_num, h, w}, ctx.GetPlace());
    noobj_mask.mutable_data<int>({n, an_num, h, w}, ctx.GetPlace());
    tx.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    ty.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tw.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    th.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tconf.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    tclass.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
    PrePorcessGTBox<T>(*gt_boxes, ignore_thresh, anchors, h, &obj_mask,
                       &noobj_mask, &tx, &ty, &tw, &th, &tconf, &tclass);

    Tensor obj_mask_expand;
    obj_mask_expand.mutable_data<int>({n, an_num, h, w, class_num},
                                      ctx.GetPlace());
    ExpandObjMaskByClassNum(&obj_mask_expand, obj_mask);

    Tensor grad_x, grad_y, grad_w, grad_h;
    Tensor grad_conf_obj, grad_conf_noobj, grad_class;
    grad_x.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_y.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_w.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_h.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_conf_obj.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_conf_noobj.mutable_data<T>({n, an_num, h, w}, ctx.GetPlace());
    grad_class.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
    T obj_mf = CalcMaskPointNum<int>(obj_mask);
    T noobj_mf = CalcMaskPointNum<int>(noobj_mask);
    T obj_expand_mf = CalcMaskPointNum<int>(obj_mask_expand);
    CalcMSEGradWithMask<T>(&grad_x, pred_x, tx, obj_mask, obj_mf);
    CalcMSEGradWithMask<T>(&grad_y, pred_y, ty, obj_mask, obj_mf);
    CalcMSEGradWithMask<T>(&grad_w, pred_w, tw, obj_mask, obj_mf);
    CalcMSEGradWithMask<T>(&grad_h, pred_h, th, obj_mask, obj_mf);
    CalcBCEGradWithMask<T>(&grad_conf_obj, pred_conf, tconf, obj_mask, obj_mf);
    CalcBCEGradWithMask<T>(&grad_conf_noobj, pred_conf, tconf, noobj_mask,
                           noobj_mf);
    CalcBCEGradWithMask<T>(&grad_class, pred_class, tclass, obj_mask_expand,
                           obj_expand_mf);

    input_grad->mutable_data<T>({n, c, h, w}, ctx.GetPlace());
    AddAllGradToInputGrad<T>(
        input_grad, loss, pred_x, pred_y, pred_conf, pred_class, grad_x, grad_y,
        grad_w, grad_h, grad_conf_obj, grad_conf_noobj, grad_class, class_num);
452 453 454 455 456
  }
};

}  // namespace operators
}  // namespace paddle