提交 db8ff57a 编写于 作者: D dengkaipeng

remove useless code and update doc. test=develop

上级 577a92d9
...@@ -138,17 +138,23 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -138,17 +138,23 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
thresh, the confidence score loss of this anchor box will be ignored. thresh, the confidence score loss of this anchor box will be ignored.
Therefore, the yolov3 loss consist of three major parts, box location loss, Therefore, the yolov3 loss consist of three major parts, box location loss,
confidence score loss, and classification loss. The MSE loss is used for confidence score loss, and classification loss. The L1 loss is used for
box location, and binary cross entropy loss is used for confidence score box coordinates (w, h), and sigmoid cross entropy loss is used for box
loss and classification loss. coordinates (x, y), confidence score loss and classification loss.
In order to trade off box coordinate losses between big boxes and small
boxes, box coordinate losses will be mutiplied by scale weight, which is
calculated as follow.
$$
weight_{box} = 2.0 - t_w * t_h
$$
Final loss will be represented as follow. Final loss will be represented as follow.
$$ $$
loss = \loss_weight_{xy} * loss_{xy} + \loss_weight_{wh} * loss_{wh} loss = (loss_{xy} + loss_{wh}) * weight_{box}
+ \loss_weight_{conf_target} * loss_{conf_target} + loss_{conf} + loss_{class}
+ \loss_weight_{conf_notarget} * loss_{conf_notarget}
+ \loss_weight_{class} * loss_{class}
$$ $$
)DOC"); )DOC");
} }
...@@ -204,11 +210,7 @@ namespace ops = paddle::operators; ...@@ -204,11 +210,7 @@ namespace ops = paddle::operators;
REGISTER_OPERATOR(yolov3_loss, ops::Yolov3LossOp, ops::Yolov3LossOpMaker, REGISTER_OPERATOR(yolov3_loss, ops::Yolov3LossOp, ops::Yolov3LossOpMaker,
ops::Yolov3LossGradMaker); ops::Yolov3LossGradMaker);
REGISTER_OPERATOR(yolov3_loss_grad, ops::Yolov3LossOpGrad); REGISTER_OPERATOR(yolov3_loss_grad, ops::Yolov3LossOpGrad);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(yolov3_loss, ops::Yolov3LossKernel<float>,
yolov3_loss, ops::Yolov3LossKernel<double>);
ops::Yolov3LossKernel<paddle::platform::CPUDeviceContext, float>, REGISTER_OP_CPU_KERNEL(yolov3_loss_grad, ops::Yolov3LossGradKernel<float>,
ops::Yolov3LossKernel<paddle::platform::CPUDeviceContext, double>); ops::Yolov3LossGradKernel<double>);
REGISTER_OP_CPU_KERNEL(
yolov3_loss_grad,
ops::Yolov3LossGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::Yolov3LossGradKernel<paddle::platform::CPUDeviceContext, double>);
...@@ -260,26 +260,18 @@ static void CalcYolov3Loss(T* loss_data, const Tensor& input, const Tensor& tx, ...@@ -260,26 +260,18 @@ static void CalcYolov3Loss(T* loss_data, const Tensor& input, const Tensor& tx,
const int class_num = tclass.dims()[4]; const int class_num = tclass.dims()[4];
const int grid_num = h * w; const int grid_num = h * w;
// T l = 0.0;
CalcSCE<T>(loss_data, input_data, tx_data, tweight_data, obj_mask_data, n, CalcSCE<T>(loss_data, input_data, tx_data, tweight_data, obj_mask_data, n,
an_num, grid_num, class_num, 1); an_num, grid_num, class_num, 1);
CalcSCE<T>(loss_data, input_data + grid_num, ty_data, tweight_data, CalcSCE<T>(loss_data, input_data + grid_num, ty_data, tweight_data,
obj_mask_data, n, an_num, grid_num, class_num, 1); obj_mask_data, n, an_num, grid_num, class_num, 1);
// LOG(ERROR) << "C++ xy: " << loss_data[0] - l;
// l = loss_data[0];
CalcL1Loss<T>(loss_data, input_data + 2 * grid_num, tw_data, tweight_data, CalcL1Loss<T>(loss_data, input_data + 2 * grid_num, tw_data, tweight_data,
obj_mask_data, n, an_num, grid_num, class_num); obj_mask_data, n, an_num, grid_num, class_num);
CalcL1Loss<T>(loss_data, input_data + 3 * grid_num, th_data, tweight_data, CalcL1Loss<T>(loss_data, input_data + 3 * grid_num, th_data, tweight_data,
obj_mask_data, n, an_num, grid_num, class_num); obj_mask_data, n, an_num, grid_num, class_num);
// LOG(ERROR) << "C++ wh: " << loss_data[0] - l;
// l = loss_data[0];
CalcSCE<T>(loss_data, input_data + 4 * grid_num, tconf_data, conf_mask_data, CalcSCE<T>(loss_data, input_data + 4 * grid_num, tconf_data, conf_mask_data,
conf_mask_data, n, an_num, grid_num, class_num, 1); conf_mask_data, n, an_num, grid_num, class_num, 1);
// LOG(ERROR) << "C++ conf: " << loss_data[0] - l;
// l = loss_data[0];
CalcSCE<T>(loss_data, input_data + 5 * grid_num, tclass_data, obj_mask_data, CalcSCE<T>(loss_data, input_data + 5 * grid_num, tclass_data, obj_mask_data,
obj_mask_data, n, an_num, grid_num, class_num, class_num); obj_mask_data, n, an_num, grid_num, class_num, class_num);
// LOG(ERROR) << "C++ class: " << loss_data[0] - l;
} }
template <typename T> template <typename T>
...@@ -329,7 +321,7 @@ static void CalcYolov3LossGrad(T* input_grad_data, const Tensor& loss_grad, ...@@ -329,7 +321,7 @@ static void CalcYolov3LossGrad(T* input_grad_data, const Tensor& loss_grad,
obj_mask_data, n, an_num, grid_num, class_num, class_num); obj_mask_data, n, an_num, grid_num, class_num, class_num);
} }
template <typename DeviceContext, typename T> template <typename T>
class Yolov3LossKernel : public framework::OpKernel<T> { class Yolov3LossKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& ctx) const override { void Compute(const framework::ExecutionContext& ctx) const override {
...@@ -359,24 +351,24 @@ class Yolov3LossKernel : public framework::OpKernel<T> { ...@@ -359,24 +351,24 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
tconf.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()); tclass.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
math::SetConstant<DeviceContext, T> constant; math::SetConstant<platform::CPUDeviceContext, T> constant;
constant(ctx.template device_context<DeviceContext>(), &conf_mask, constant(ctx.template device_context<platform::CPUDeviceContext>(),
static_cast<T>(1.0)); &conf_mask, static_cast<T>(1.0));
constant(ctx.template device_context<DeviceContext>(), &obj_mask, constant(ctx.template device_context<platform::CPUDeviceContext>(),
static_cast<T>(0.0)); &obj_mask, static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tx, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tx,
static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &ty,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tw, constant(ctx.template device_context<platform::CPUDeviceContext>(), &ty,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &th, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tw,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tweight, constant(ctx.template device_context<platform::CPUDeviceContext>(), &th,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tconf, constant(ctx.template device_context<platform::CPUDeviceContext>(),
&tweight, static_cast<T>(0.0));
constant(ctx.template device_context<platform::CPUDeviceContext>(), &tconf,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tclass, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tclass,
static_cast<T>(0.0)); static_cast<T>(0.0));
PreProcessGTBox<T>(*gt_box, *gt_label, ignore_thresh, anchors, input_size, PreProcessGTBox<T>(*gt_box, *gt_label, ignore_thresh, anchors, input_size,
...@@ -390,7 +382,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> { ...@@ -390,7 +382,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
} }
}; };
template <typename DeviceContext, typename T> template <typename T>
class Yolov3LossGradKernel : public framework::OpKernel<T> { class Yolov3LossGradKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& ctx) const override { void Compute(const framework::ExecutionContext& ctx) const override {
...@@ -422,24 +414,24 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> { ...@@ -422,24 +414,24 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
tconf.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()); tclass.mutable_data<T>({n, an_num, h, w, class_num}, ctx.GetPlace());
math::SetConstant<DeviceContext, T> constant; math::SetConstant<platform::CPUDeviceContext, T> constant;
constant(ctx.template device_context<DeviceContext>(), &conf_mask, constant(ctx.template device_context<platform::CPUDeviceContext>(),
static_cast<T>(1.0)); &conf_mask, static_cast<T>(1.0));
constant(ctx.template device_context<DeviceContext>(), &obj_mask, constant(ctx.template device_context<platform::CPUDeviceContext>(),
static_cast<T>(0.0)); &obj_mask, static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tx, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tx,
static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &ty,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tw, constant(ctx.template device_context<platform::CPUDeviceContext>(), &ty,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &th, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tw,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tweight, constant(ctx.template device_context<platform::CPUDeviceContext>(), &th,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tconf, constant(ctx.template device_context<platform::CPUDeviceContext>(),
&tweight, static_cast<T>(0.0));
constant(ctx.template device_context<platform::CPUDeviceContext>(), &tconf,
static_cast<T>(0.0)); static_cast<T>(0.0));
constant(ctx.template device_context<DeviceContext>(), &tclass, constant(ctx.template device_context<platform::CPUDeviceContext>(), &tclass,
static_cast<T>(0.0)); static_cast<T>(0.0));
PreProcessGTBox<T>(*gt_box, *gt_label, ignore_thresh, anchors, input_size, PreProcessGTBox<T>(*gt_box, *gt_label, ignore_thresh, anchors, input_size,
......
...@@ -485,19 +485,6 @@ def yolov3_loss(x, ...@@ -485,19 +485,6 @@ def yolov3_loss(x,
"input_size": input_size, "input_size": input_size,
} }
# if loss_weight_xy is not None and isinstance(loss_weight_xy, float):
# self.attrs['loss_weight_xy'] = loss_weight_xy
# if loss_weight_wh is not None and isinstance(loss_weight_wh, float):
# self.attrs['loss_weight_wh'] = loss_weight_wh
# if loss_weight_conf_target is not None and isinstance(
# loss_weight_conf_target, float):
# self.attrs['loss_weight_conf_target'] = loss_weight_conf_target
# if loss_weight_conf_notarget is not None and isinstance(
# loss_weight_conf_notarget, float):
# self.attrs['loss_weight_conf_notarget'] = loss_weight_conf_notarget
# if loss_weight_class is not None and isinstance(loss_weight_class, float):
# self.attrs['loss_weight_class'] = loss_weight_class
helper.append_op( helper.append_op(
type='yolov3_loss', type='yolov3_loss',
inputs={"X": x, inputs={"X": x,
......
...@@ -157,11 +157,6 @@ def YoloV3Loss(x, gtbox, gtlabel, attrs): ...@@ -157,11 +157,6 @@ def YoloV3Loss(x, gtbox, gtlabel, attrs):
loss_obj = sce(pred_conf, tconf, conf_mask) loss_obj = sce(pred_conf, tconf, conf_mask)
loss_class = sce(pred_cls, tcls, obj_mask_expand) loss_class = sce(pred_cls, tcls, obj_mask_expand)
# print("python loss_xy: ", loss_x + loss_y)
# print("python loss_wh: ", loss_w + loss_h)
# print("python loss_obj: ", loss_obj)
# print("python loss_class: ", loss_class)
return loss_x + loss_y + loss_w + loss_h + loss_obj + loss_class return loss_x + loss_y + loss_w + loss_h + loss_obj + loss_class
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册