Skip to content

Conversation

@wuyefeilin
Copy link
Contributor

@wuyefeilin wuyefeilin commented Mar 25, 2022

PR types

Others

PR changes

OPs

Describe

move yolov3_loss to phi

@paddle-bot-old
Copy link

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@wuyefeilin wuyefeilin closed this Mar 27, 2022
@wuyefeilin wuyefeilin reopened this Mar 27, 2022
@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Mar 28, 2022
@PaddlePaddle PaddlePaddle unlocked this conversation Mar 28, 2022
const T* loss_grad_data = loss_grad.data<T>();
const T* obj_mask_data = objness_mask->data<T>();
const int* gt_match_mask_data = gt_match_mask.data<int>();
T* input_grad_data =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以先调用resize,再使用dev_ctx的Alloc方法进行内存分配

const T* gt_score_data;
DenseTensor gtscore;
if (!(gt_score.is_initialized())) {
gtscore.mutable_data<T>({n, b}, dev_ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

const T* input_data = input->data<T>();
const T* gt_box_data = gt_box.data<T>();
const int* gt_label_data = gt_label.data<int>();
T* loss_data = loss->mutable_data<T>({n}, dev_ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

T* loss_data = loss->mutable_data<T>({n}, dev_ctx.GetPlace());
memset(loss_data, 0, loss->numel() * sizeof(T));
T* obj_mask_data =
objness_mask->mutable_data<T>({n, mask_num, h, w}, dev_ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

objness_mask->mutable_data<T>({n, mask_num, h, w}, dev_ctx.GetPlace());
memset(obj_mask_data, 0, objness_mask->numel() * sizeof(T));
int* gt_match_mask_data =
gt_match_mask->mutable_data<int>({n, b}, dev_ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

const T* gt_score_data;
DenseTensor gtscore;
if (!(gt_score.is_initialized())) {
gtscore.mutable_data<T>({n, b}, dev_ctx.GetPlace());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上


// calc valid gt box mask, avoid calc duplicately in following code
DenseTensor gt_valid_mask;
bool* gt_valid_mask_data =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/yolov3_loss_grad_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yolov3_loss_grad_kernel.h放在开头

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/yolov3_loss_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

const DenseTensor& x,
const DenseTensor& gt_box,
const DenseTensor& gt_label,
const paddle::optional<const DenseTensor&> gt_score,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前optional外层不用再加const

Comment on lines 80 to 131
static inline Box<T> GetGtBox(const T* gt, int batch, int max_boxes, int idx) {
Box<T> 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 <typename T>
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 <typename T>
static inline T CalcBoxIoU(Box<T> b1, Box<T> 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 <typename T>
static void CalcBoxLocationLoss(T* loss,
const T* input,
Box<T> gt,
std::vector<int> anchors,
int an_idx,
int box_idx,
int gi,
int gj,
int grid_size,
int input_size,
int stride,
T score) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

像GetGtBox这些前反向都用到的函数可以统一写到一个cpu目录下的yoloxxx_functor.h文件中,避免维护两份重复的代码

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix as review

@wuyefeilin
Copy link
Contributor Author

fix as review

zyfncg
zyfncg previously approved these changes Mar 28, 2022
@zyfncg zyfncg merged commit fb93bd5 into PaddlePaddle:develop Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants