-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[phi] move yolov3_loss to phi #40944
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
b7be90c
to
c9b0bd6
Compare
c9b0bd6
to
4a6651b
Compare
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 = |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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 = |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前optional外层不用再加const
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) { |
There was a problem hiding this comment.
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文件中,避免维护两份重复的代码
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix as review
fix as review |
PR types
Others
PR changes
OPs
Describe
move yolov3_loss to phi