/* 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" #include "paddle/fluid/operators/math/math_function.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; template using EigenTensor = framework::EigenTensor; template using EigenVector = framework::EigenVector; template static inline bool LessEqualZero(T x) { return x < 1e-6; } template static T SCE(T x, T label) { return (x > 0 ? x : 0.0) - x * label + std::log(1.0 + std::exp(-std::abs(x))); } template static T L1Loss(T x, T y) { return std::abs(y - x); } template static T SCEGrad(T x, T label) { return 1.0 / (1.0 + std::exp(-x)) - label; } template static T L1LossGrad(T x, T y) { return x > y ? 1.0 : -1.0; } static int GetMaskIndex(std::vector mask, int val) { for (size_t i = 0; i < mask.size(); i++) { if (mask[i] == val) { return i; } } return -1; } template struct Box { float x, y, w, h; }; template static inline T sigmoid(T x) { return 1.0 / (1.0 + std::exp(-x)); } template static inline Box GetYoloBox(const T* x, std::vector anchors, int i, int j, int an_idx, int grid_size, int input_size, int index, int stride) { Box b; b.x = (i + sigmoid(x[index])) / grid_size; b.y = (j + sigmoid(x[index + stride])) / grid_size; b.w = std::exp(x[index + 2 * stride]) * anchors[2 * an_idx] / input_size; b.h = std::exp(x[index + 3 * stride]) * anchors[2 * an_idx + 1] / input_size; return b; } template static inline Box GetGtBox(const T* gt, int batch, int max_boxes, int idx) { Box b; b.x = gt[(batch * max_boxes + idx) * 4]; b.y = gt[(batch * max_boxes + idx) * 4 + 1]; b.w = gt[(batch * max_boxes + idx) * 4 + 2]; b.h = gt[(batch * max_boxes + idx) * 4 + 3]; return b; } template static inline T BoxOverlap(T c1, T w1, T c2, T w2) { T l1 = c1 - w1 / 2.0; T l2 = c2 - w2 / 2.0; T left = l1 > l2 ? l1 : l2; T r1 = c1 + w1 / 2.0; T r2 = c2 + w2 / 2.0; T right = r1 < r2 ? r1 : r2; return right - left; } template static inline T CalcBoxIoU(Box b1, Box b2) { T w = BoxOverlap(b1.x, b1.w, b2.x, b2.w); T h = BoxOverlap(b1.y, b1.h, b2.y, b2.h); T inter_area = (w < 0 || h < 0) ? 0.0 : w * h; T union_area = b1.w * b1.h + b2.w * b2.h - inter_area; return inter_area / union_area; } static inline int GetEntryIndex(int batch, int an_idx, int hw_idx, int an_num, int an_stride, int stride, int entry) { return (batch * an_num + an_idx) * an_stride + entry * stride + hw_idx; } template static void CalcBoxLocationLoss(T* loss, const T* input, Box gt, std::vector anchors, int an_idx, int box_idx, int gi, int gj, int grid_size, int input_size, int stride) { T tx = gt.x * grid_size - gi; T ty = gt.y * grid_size - gj; T tw = std::log(gt.w * input_size / anchors[2 * an_idx]); T th = std::log(gt.h * input_size / anchors[2 * an_idx + 1]); T scale = 2.0 - gt.w * gt.h; loss[0] += SCE(input[box_idx], tx) * scale; loss[0] += SCE(input[box_idx + stride], ty) * scale; loss[0] += L1Loss(input[box_idx + 2 * stride], tw) * scale; loss[0] += L1Loss(input[box_idx + 3 * stride], th) * scale; } template static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input, Box gt, std::vector anchors, int an_idx, int box_idx, int gi, int gj, int grid_size, int input_size, int stride) { T tx = gt.x * grid_size - gi; T ty = gt.y * grid_size - gj; T tw = std::log(gt.w * input_size / anchors[2 * an_idx]); T th = std::log(gt.h * input_size / anchors[2 * an_idx + 1]); T scale = 2.0 - gt.w * gt.h; input_grad[box_idx] = SCEGrad(input[box_idx], tx) * scale * loss; input_grad[box_idx + stride] = SCEGrad(input[box_idx + stride], ty) * scale * loss; input_grad[box_idx + 2 * stride] = L1LossGrad(input[box_idx + 2 * stride], tw) * scale * loss; input_grad[box_idx + 3 * stride] = L1LossGrad(input[box_idx + 3 * stride], th) * scale * loss; } template static inline void CalcLabelLoss(T* loss, const T* input, const int index, const int label, const int class_num, const int stride) { for (int i = 0; i < class_num; i++) { loss[0] += SCE(input[index + i * stride], (i == label) ? 1.0 : 0.0); } } template static inline void CalcLabelLossGrad(T* input_grad, const T loss, const T* input, const int index, const int label, const int class_num, const int stride) { for (int i = 0; i < class_num; i++) { input_grad[index + i * stride] = SCEGrad(input[index + i * stride], (i == label) ? 1.0 : 0.0) * loss; } } template static inline void CalcObjnessLoss(T* loss, const T* input, const int* objness, const int n, const int an_num, const int h, const int w, const int stride, const int an_stride) { 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++) { int obj = objness[k * w + l]; if (obj >= 0) { loss[i] += SCE(input[k * w + l], static_cast(obj)); } } } objness += stride; input += an_stride; } } } template static inline void CalcObjnessLossGrad(T* input_grad, const T* loss, const T* input, const int* objness, const int n, const int an_num, const int h, const int w, const int stride, const int an_stride) { 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++) { int obj = objness[k * w + l]; if (obj >= 0) { input_grad[k * w + l] = SCEGrad(input[k * w + l], static_cast(obj)) * loss[i]; } } } objness += stride; input += an_stride; input_grad += an_stride; } } } 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"); auto anchor_mask = ctx.Attr>("anchor_mask"); int class_num = ctx.Attr("class_num"); float ignore_thresh = ctx.Attr("ignore_thresh"); int downsample = ctx.Attr("downsample"); 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; const int mask_num = anchor_mask.size(); const int b = gt_box->dims()[1]; int input_size = downsample * h; const T* input_data = input->data(); const T* gt_box_data = gt_box->data(); const int* gt_label_data = gt_label->data(); T* loss_data = loss->mutable_data({n}, ctx.GetPlace()); memset(loss_data, 0, loss->numel() * sizeof(T)); Tensor objness; int* objness_data = objness.mutable_data({n, mask_num, h, w}, ctx.GetPlace()); memset(objness_data, 0, objness.numel() * sizeof(int)); const int stride = h * w; const int an_stride = (class_num + 5) * stride; for (int i = 0; i < n; i++) { for (int j = 0; j < mask_num; j++) { for (int k = 0; k < h; k++) { for (int l = 0; l < w; l++) { int box_idx = GetEntryIndex(i, j, k * w + l, mask_num, an_stride, stride, 0); Box pred = GetYoloBox(input_data, anchors, l, k, anchor_mask[j], h, input_size, box_idx, stride); T best_iou = 0; for (int t = 0; t < b; t++) { Box gt = GetGtBox(gt_box_data, i, b, t); if (LessEqualZero(gt.w) || LessEqualZero(gt.h)) { continue; } T iou = CalcBoxIoU(pred, gt); if (iou > best_iou) { best_iou = iou; } } if (best_iou > ignore_thresh) { int obj_idx = (i * mask_num + j) * stride + k * w + l; objness_data[obj_idx] = -1; } } } } for (int t = 0; t < b; t++) { Box gt = GetGtBox(gt_box_data, i, b, t); if (LessEqualZero(gt.w) || LessEqualZero(gt.h)) { continue; } int gi = static_cast(gt.x * w); int gj = static_cast(gt.y * h); Box gt_shift = gt; gt_shift.x = 0.0; gt_shift.y = 0.0; T best_iou = 0.0; int best_n = 0; for (int an_idx = 0; an_idx < an_num; an_idx++) { Box an_box; an_box.x = 0.0; an_box.y = 0.0; an_box.w = anchors[2 * an_idx] / static_cast(input_size); an_box.h = anchors[2 * an_idx + 1] / static_cast(input_size); float iou = CalcBoxIoU(an_box, gt_shift); // TO DO: iou > 0.5 ? if (iou > best_iou) { best_iou = iou; best_n = an_idx; } } int mask_idx = GetMaskIndex(anchor_mask, best_n); if (mask_idx >= 0) { int box_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 0); CalcBoxLocationLoss(loss_data + i, input_data, gt, anchors, best_n, box_idx, gi, gj, h, input_size, stride); int obj_idx = (i * mask_num + mask_idx) * stride + gj * w + gi; objness_data[obj_idx] = 1; int label = gt_label_data[i * b + t]; int label_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 5); CalcLabelLoss(loss_data + i, input_data, label_idx, label, class_num, stride); } } } CalcObjnessLoss(loss_data, input_data + 4 * stride, objness_data, n, mask_num, h, w, stride, an_stride); } }; 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* input_grad = ctx.Output(framework::GradVarName("X")); auto* loss_grad = ctx.Input(framework::GradVarName("Loss")); auto anchors = ctx.Attr>("anchors"); auto anchor_mask = ctx.Attr>("anchor_mask"); int class_num = ctx.Attr("class_num"); float ignore_thresh = ctx.Attr("ignore_thresh"); int downsample = ctx.Attr("downsample"); 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; const int mask_num = anchor_mask.size(); const int b = gt_box->dims()[1]; int input_size = downsample * h; const T* input_data = input->data(); const T* gt_box_data = gt_box->data(); const int* gt_label_data = gt_label->data(); const T* loss_grad_data = loss_grad->data(); T* input_grad_data = input_grad->mutable_data({n, c, h, w}, ctx.GetPlace()); memset(input_grad_data, 0, input_grad->numel() * sizeof(T)); Tensor objness; int* objness_data = objness.mutable_data({n, mask_num, h, w}, ctx.GetPlace()); memset(objness_data, 0, objness.numel() * sizeof(int)); const int stride = h * w; const int an_stride = (class_num + 5) * stride; for (int i = 0; i < n; i++) { for (int j = 0; j < mask_num; j++) { for (int k = 0; k < h; k++) { for (int l = 0; l < w; l++) { int box_idx = GetEntryIndex(i, j, k * w + l, mask_num, an_stride, stride, 0); Box pred = GetYoloBox(input_data, anchors, l, k, anchor_mask[j], h, input_size, box_idx, stride); T best_iou = 0; for (int t = 0; t < b; t++) { Box gt = GetGtBox(gt_box_data, i, b, t); if (LessEqualZero(gt.w) || LessEqualZero(gt.h)) { continue; } T iou = CalcBoxIoU(pred, gt); if (iou > best_iou) { best_iou = iou; } } if (best_iou > ignore_thresh) { int obj_idx = (i * mask_num + j) * stride + k * w + l; objness_data[obj_idx] = -1; } } } } for (int t = 0; t < b; t++) { Box gt = GetGtBox(gt_box_data, i, b, t); if (LessEqualZero(gt.w) || LessEqualZero(gt.h)) { continue; } int gi = static_cast(gt.x * w); int gj = static_cast(gt.y * h); Box gt_shift = gt; gt_shift.x = 0.0; gt_shift.y = 0.0; T best_iou = 0.0; int best_n = 0; for (int an_idx = 0; an_idx < an_num; an_idx++) { Box an_box; an_box.x = 0.0; an_box.y = 0.0; an_box.w = anchors[2 * an_idx] / static_cast(input_size); an_box.h = anchors[2 * an_idx + 1] / static_cast(input_size); float iou = CalcBoxIoU(an_box, gt_shift); // TO DO: iou > 0.5 ? if (iou > best_iou) { best_iou = iou; best_n = an_idx; } } int mask_idx = GetMaskIndex(anchor_mask, best_n); if (mask_idx >= 0) { int box_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 0); CalcBoxLocationLossGrad(input_grad_data, loss_grad_data[i], input_data, gt, anchors, best_n, box_idx, gi, gj, h, input_size, stride); int obj_idx = (i * mask_num + mask_idx) * stride + gj * w + gi; objness_data[obj_idx] = 1; int label = gt_label_data[i * b + t]; int label_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 5); CalcLabelLossGrad(input_grad_data, loss_grad_data[i], input_data, label_idx, label, class_num, stride); } } } CalcObjnessLossGrad(input_grad_data + 4 * stride, loss_grad_data, input_data + 4 * stride, objness_data, n, mask_num, h, w, stride, an_stride); } }; } // namespace operators } // namespace paddle