/* 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 #include #include "paddle/fluid/framework/op_registry.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenTensor = framework::EigenTensor; template using EigenVector = framework::EigenVector; using Array5 = Eigen::DSizes; template static inline bool isZero(T x) { return fabs(x) < 1e-6; } template static inline T CalcMSEWithWeight(const Tensor& x, const Tensor& y, const Tensor& weight, const T mf) { int numel = static_cast(x.numel()); const T* x_data = x.data(); const T* y_data = y.data(); const T* weight_data = weight.data(); T error_sum = 0.0; for (int i = 0; i < numel; i++) { T xi = x_data[i]; T yi = y_data[i]; T weighti = weight_data[i]; error_sum += pow(yi - xi, 2) * weighti; } return error_sum / mf; } template static void CalcMSEGradWithWeight(Tensor* grad, const Tensor& x, const Tensor& y, const Tensor& weight, const T mf) { int numel = static_cast(grad->numel()); T* grad_data = grad->data(); const T* x_data = x.data(); const T* y_data = y.data(); const T* weight_data = weight.data(); for (int i = 0; i < numel; i++) { grad_data[i] = 2.0 * weight_data[i] * (x_data[i] - y_data[i]) / mf; } } template struct SigmoidCrossEntropyForward { T operator()(const T& x, const T& label) const { T term1 = (x > 0) ? x : 0; T term2 = x * label; T term3 = std::log(static_cast(1.0) + std::exp(-(std::abs(x)))); return term1 - term2 + term3; } }; template struct SigmoidCrossEntropyBackward { T operator()(const T& x, const T& label) const { T sigmoid_x = static_cast(1.0) / (static_cast(1.0) + std::exp(-1.0 * x)); return sigmoid_x - label; } }; template static inline T CalcSCEWithWeight(const Tensor& x, const Tensor& labels, const Tensor& weight, const T mf) { int numel = x.numel(); const T* x_data = x.data(); const T* labels_data = labels.data(); const T* weight_data = weight.data(); T loss = 0.0; for (int i = 0; i < numel; i++) { T xi = x_data[i]; T labeli = labels_data[i]; T weighti = weight_data[i]; loss += ((xi > 0.0 ? xi : 0.0) - xi * labeli + std::log(1.0 + std::exp(-1.0 * std::abs(xi)))) * weighti; } return loss / mf; } template static inline void CalcSCEGradWithWeight(Tensor* grad, const Tensor& x, const Tensor& labels, const Tensor& weight, const T mf) { int numel = grad->numel(); T* grad_data = grad->data(); const T* x_data = x.data(); const T* labels_data = labels.data(); const T* weight_data = weight.data(); for (int i = 0; i < numel; i++) { grad_data[i] = (1.0 / (1.0 + std::exp(-1.0 * x_data[i])) - labels_data[i]) * weight_data[i] / mf; } } template 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) { 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::From(input); auto pred_conf_t = EigenTensor::From(*pred_conf); auto pred_class_t = EigenTensor::From(*pred_class); auto pred_x_t = EigenTensor::From(*pred_x); auto pred_y_t = EigenTensor::From(*pred_y); auto pred_w_t = EigenTensor::From(*pred_w); auto pred_h_t = EigenTensor::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) = input_t(i, box_attr_num * an_idx, j, k); pred_y_t(i, an_idx, j, k) = input_t(i, box_attr_num * an_idx + 1, j, k); pred_w_t(i, an_idx, j, k) = input_t(i, box_attr_num * an_idx + 2, j, k); pred_h_t(i, an_idx, j, k) = input_t(i, box_attr_num * an_idx + 3, j, k); pred_conf_t(i, an_idx, j, k) = input_t(i, box_attr_num * an_idx + 4, j, k); for (int c = 0; c < class_num; c++) { pred_class_t(i, an_idx, j, k, c) = input_t(i, box_attr_num * an_idx + 5 + c, j, k); } } } } } } template static T CalcBoxIoU(std::vector box1, std::vector 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); 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); T inter_area = std::max(inter_rect_x2 - inter_rect_x1, static_cast(0.0)) * std::max(inter_rect_y2 - inter_rect_y1, static_cast(0.0)); return inter_area / (b1_area + b2_area - inter_area); } template static void PreProcessGTBox(const Tensor& gt_box, const Tensor& gt_label, const float ignore_thresh, std::vector anchors, const int input_size, const int grid_size, Tensor* obj_mask, Tensor* noobj_mask, Tensor* tx, Tensor* ty, Tensor* tw, Tensor* th, Tensor* tweight, Tensor* tconf, Tensor* tclass) { const int n = gt_box.dims()[0]; const int b = gt_box.dims()[1]; const int anchor_num = anchors.size() / 2; auto gt_box_t = EigenTensor::From(gt_box); auto gt_label_t = EigenTensor::From(gt_label); auto obj_mask_t = EigenTensor::From(*obj_mask).setConstant(0); auto noobj_mask_t = EigenTensor::From(*noobj_mask).setConstant(1); auto tx_t = EigenTensor::From(*tx).setConstant(0.0); auto ty_t = EigenTensor::From(*ty).setConstant(0.0); auto tw_t = EigenTensor::From(*tw).setConstant(0.0); auto th_t = EigenTensor::From(*th).setConstant(0.0); auto tweight_t = EigenTensor::From(*tweight).setConstant(0.0); auto tconf_t = EigenTensor::From(*tconf).setConstant(0.0); auto tclass_t = EigenTensor::From(*tclass).setConstant(0.0); for (int i = 0; i < n; i++) { for (int j = 0; j < b; j++) { if (isZero(gt_box_t(i, j, 0)) && isZero(gt_box_t(i, j, 1)) && isZero(gt_box_t(i, j, 2)) && isZero(gt_box_t(i, j, 3))) { continue; } int cur_label = gt_label_t(i, j); T gx = gt_box_t(i, j, 0) * grid_size; T gy = gt_box_t(i, j, 1) * grid_size; T gw = gt_box_t(i, j, 2) * input_size; T gh = gt_box_t(i, j, 3) * input_size; int gi = static_cast(gx); int gj = static_cast(gy); T max_iou = static_cast(0); T iou; int best_an_index = -1; std::vector gt_box_shape({0, 0, gw, gh}); for (int an_idx = 0; an_idx < anchor_num; an_idx++) { std::vector anchor_shape({0, 0, static_cast(anchors[2 * an_idx]), static_cast(anchors[2 * an_idx + 1])}); iou = CalcBoxIoU(gt_box_shape, anchor_shape); if (iou > max_iou) { max_iou = iou; best_an_index = an_idx; } if (iou > ignore_thresh) { noobj_mask_t(i, an_idx, gj, gi) = static_cast(0.0); } } obj_mask_t(i, best_an_index, gj, gi) = static_cast(1.0); noobj_mask_t(i, best_an_index, gj, gi) = static_cast(0.0); tx_t(i, best_an_index, gj, gi) = gx - gi; ty_t(i, best_an_index, gj, gi) = gy - gj; 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]); tweight_t(i, best_an_index, gj, gi) = 2.0 - gt_box_t(i, j, 2) * gt_box_t(i, j, 3); tclass_t(i, best_an_index, gj, gi, cur_label) = 1; tconf_t(i, best_an_index, gj, gi) = 1; } } } 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::From(*obj_mask_expand); auto obj_mask_t = EigenTensor::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)); } template 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_target, const Tensor& grad_conf_notarget, const Tensor& grad_class, const int class_num, const float loss_weight_xy, const float loss_weight_wh, const float loss_weight_conf_target, const float loss_weight_conf_notarget, const float loss_weight_class) { 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::From(*grad).setConstant(0.0); auto pred_x_t = EigenTensor::From(pred_x); auto pred_y_t = EigenTensor::From(pred_y); auto pred_conf_t = EigenTensor::From(pred_conf); auto pred_class_t = EigenTensor::From(pred_class); auto grad_x_t = EigenTensor::From(grad_x); auto grad_y_t = EigenTensor::From(grad_y); auto grad_w_t = EigenTensor::From(grad_w); auto grad_h_t = EigenTensor::From(grad_h); auto grad_conf_target_t = EigenTensor::From(grad_conf_target); auto grad_conf_notarget_t = EigenTensor::From(grad_conf_notarget); auto grad_class_t = EigenTensor::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) * loss * loss_weight_xy; grad_t(i, j * attr_num + 1, k, l) = grad_y_t(i, j, k, l) * loss * loss_weight_xy; grad_t(i, j * attr_num + 2, k, l) = grad_w_t(i, j, k, l) * loss * loss_weight_wh; grad_t(i, j * attr_num + 3, k, l) = grad_h_t(i, j, k, l) * loss * loss_weight_wh; grad_t(i, j * attr_num + 4, k, l) = grad_conf_target_t(i, j, k, l) * loss * loss_weight_conf_target; grad_t(i, j * attr_num + 4, k, l) += grad_conf_notarget_t(i, j, k, l) * loss * loss_weight_conf_notarget; 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) * loss * loss_weight_class; } } } } } } template class Yolov3LossKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* input = ctx.Input("X"); auto* gt_box = ctx.Input("GTBox"); auto* gt_label = ctx.Input("GTLabel"); auto* loss = ctx.Output("Loss"); auto anchors = ctx.Attr>("anchors"); int class_num = ctx.Attr("class_num"); int input_size = ctx.Attr("input_size"); float ignore_thresh = ctx.Attr("ignore_thresh"); float loss_weight_xy = ctx.Attr("loss_weight_xy"); float loss_weight_wh = ctx.Attr("loss_weight_wh"); float loss_weight_conf_target = ctx.Attr("loss_weight_conf_target"); float loss_weight_conf_notarget = ctx.Attr("loss_weight_conf_notarget"); float loss_weight_class = ctx.Attr("loss_weight_class"); 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; Tensor pred_conf, pred_class; pred_x.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_y.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_w.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_h.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_conf.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_class.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); CalcPredResult(*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, tweight, tconf, tclass; obj_mask.mutable_data({n, an_num, h, w}, ctx.GetPlace()); noobj_mask.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tx.mutable_data({n, an_num, h, w}, ctx.GetPlace()); ty.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tw.mutable_data({n, an_num, h, w}, ctx.GetPlace()); th.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tweight.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tconf.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tclass.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); PreProcessGTBox(*gt_box, *gt_label, ignore_thresh, anchors, input_size, h, &obj_mask, &noobj_mask, &tx, &ty, &tw, &th, &tweight, &tconf, &tclass); Tensor obj_weight; obj_weight.mutable_data({n, an_num, h, w}, ctx.GetPlace()); auto obj_weight_t = EigenTensor::From(obj_weight); auto obj_mask_t = EigenTensor::From(obj_mask); auto tweight_t = EigenTensor::From(tweight); obj_weight_t = obj_mask_t * tweight_t; Tensor obj_mask_expand; obj_mask_expand.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); auto obj_mask_expand_t = EigenTensor::From(obj_mask_expand); obj_mask_expand_t = obj_mask_t.reshape(Array5(n, an_num, h, w, 1)) .broadcast(Array5(1, 1, 1, 1, class_num)); T box_f = static_cast(an_num * h * w); T class_f = static_cast(an_num * h * w * class_num); T loss_x = CalcSCEWithWeight(pred_x, tx, obj_weight, box_f); T loss_y = CalcSCEWithWeight(pred_y, ty, obj_weight, box_f); T loss_w = CalcMSEWithWeight(pred_w, tw, obj_weight, box_f); T loss_h = CalcMSEWithWeight(pred_h, th, obj_weight, box_f); T loss_conf_target = CalcSCEWithWeight(pred_conf, tconf, obj_mask, box_f); T loss_conf_notarget = CalcSCEWithWeight(pred_conf, tconf, noobj_mask, box_f); T loss_class = CalcSCEWithWeight(pred_class, tclass, obj_mask_expand, class_f); auto* loss_data = loss->mutable_data({1}, ctx.GetPlace()); loss_data[0] = loss_weight_xy * (loss_x + loss_y) + loss_weight_wh * (loss_w + loss_h) + loss_weight_conf_target * loss_conf_target + loss_weight_conf_notarget * loss_conf_notarget + loss_weight_class * loss_class; } }; template class Yolov3LossGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* input = ctx.Input("X"); auto* gt_box = ctx.Input("GTBox"); auto* gt_label = ctx.Input("GTLabel"); auto anchors = ctx.Attr>("anchors"); int class_num = ctx.Attr("class_num"); float ignore_thresh = ctx.Attr("ignore_thresh"); auto* input_grad = ctx.Output(framework::GradVarName("X")); auto* output_grad = ctx.Input(framework::GradVarName("Loss")); const T loss = output_grad->data()[0]; int input_size = ctx.Attr("input_size"); float loss_weight_xy = ctx.Attr("loss_weight_xy"); float loss_weight_wh = ctx.Attr("loss_weight_wh"); float loss_weight_conf_target = ctx.Attr("loss_weight_conf_target"); float loss_weight_conf_notarget = ctx.Attr("loss_weight_conf_notarget"); float loss_weight_class = ctx.Attr("loss_weight_class"); 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({n, an_num, h, w}, ctx.GetPlace()); pred_y.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_w.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_h.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_conf.mutable_data({n, an_num, h, w}, ctx.GetPlace()); pred_class.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); CalcPredResult(*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, tweight, tconf, tclass; obj_mask.mutable_data({n, an_num, h, w}, ctx.GetPlace()); noobj_mask.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tx.mutable_data({n, an_num, h, w}, ctx.GetPlace()); ty.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tw.mutable_data({n, an_num, h, w}, ctx.GetPlace()); th.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tweight.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tconf.mutable_data({n, an_num, h, w}, ctx.GetPlace()); tclass.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); PreProcessGTBox(*gt_box, *gt_label, ignore_thresh, anchors, input_size, h, &obj_mask, &noobj_mask, &tx, &ty, &tw, &th, &tweight, &tconf, &tclass); Tensor obj_weight; obj_weight.mutable_data({n, an_num, h, w}, ctx.GetPlace()); auto obj_weight_t = EigenTensor::From(obj_weight); auto obj_mask_t = EigenTensor::From(obj_mask); auto tweight_t = EigenTensor::From(tweight); obj_weight_t = obj_mask_t * tweight_t; Tensor obj_mask_expand; obj_mask_expand.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); auto obj_mask_expand_t = EigenTensor::From(obj_mask_expand); obj_mask_expand_t = obj_mask_t.reshape(Array5(n, an_num, h, w, 1)) .broadcast(Array5(1, 1, 1, 1, class_num)); Tensor grad_x, grad_y, grad_w, grad_h; Tensor grad_conf_target, grad_conf_notarget, grad_class; grad_x.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_y.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_w.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_h.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_conf_target.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_conf_notarget.mutable_data({n, an_num, h, w}, ctx.GetPlace()); grad_class.mutable_data({n, an_num, h, w, class_num}, ctx.GetPlace()); T box_f = static_cast(an_num * h * w); T class_f = static_cast(an_num * h * w * class_num); CalcSCEGradWithWeight(&grad_x, pred_x, tx, obj_weight, box_f); CalcSCEGradWithWeight(&grad_y, pred_y, ty, obj_weight, box_f); CalcMSEGradWithWeight(&grad_w, pred_w, tw, obj_weight, box_f); CalcMSEGradWithWeight(&grad_h, pred_h, th, obj_weight, box_f); CalcSCEGradWithWeight(&grad_conf_target, pred_conf, tconf, obj_mask, box_f); CalcSCEGradWithWeight(&grad_conf_notarget, pred_conf, tconf, noobj_mask, box_f); CalcSCEGradWithWeight(&grad_class, pred_class, tclass, obj_mask_expand, class_f); input_grad->mutable_data({n, c, h, w}, ctx.GetPlace()); AddAllGradToInputGrad( input_grad, loss, pred_x, pred_y, pred_conf, pred_class, grad_x, grad_y, grad_w, grad_h, grad_conf_target, grad_conf_notarget, grad_class, class_num, loss_weight_xy, loss_weight_wh, loss_weight_conf_target, loss_weight_conf_notarget, loss_weight_class); } }; } // namespace operators } // namespace paddle