From 3b381aac4eb6caff1a94c62a0385cfbaffff59cc Mon Sep 17 00:00:00 2001 From: 0x45f <23097963+0x45f@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:43:43 +0800 Subject: [PATCH] Use _C_ops.yolov3_loss in eager mode for test_yolov3.py (#40831) * Use _C_ops.yolov3_loss in eager mode for test_yolov3.py * fix code for test_yolov3_loss_op * remove useless import * Fix dygraph_mode flag --- paddle/fluid/pybind/op_function_generator.h | 1 + python/paddle/fluid/layers/detection.py | 11 +++++++++-- python/paddle/vision/ops.py | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pybind/op_function_generator.h b/paddle/fluid/pybind/op_function_generator.h index a4e56d15080..2bfc16c7d5b 100644 --- a/paddle/fluid/pybind/op_function_generator.h +++ b/paddle/fluid/pybind/op_function_generator.h @@ -96,6 +96,7 @@ std::map> op_ins_map = { {"nce", {"Input", "Label", "Weight", "Bias", "SampleWeight", "CustomDistProbs", "CustomDistAlias", "CustomDistAliasProbs"}}, + {"yolov3_loss", {"X", "GTBox", "GTLabel", "GTScore"}}, {"check_finite_and_unscale", {"X", "Scale", "FloatStatus"}}, {"group_norm", {"X", "Scale", "Bias"}}, {"linear_chain_crf", {"Emission", "Transition", "Label", "Length"}}, diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index cbd9712720c..75b2b26fb9d 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -1072,7 +1072,6 @@ def yolov3_loss(x, anchor_mask=anchor_mask, class_num=80, ignore_thresh=0.7, downsample_ratio=32) """ - helper = LayerHelper('yolov3_loss', **locals()) if not isinstance(x, Variable): raise TypeError("Input x of yolov3_loss must be Variable") @@ -1095,8 +1094,16 @@ def yolov3_loss(x, raise TypeError( "Attr use_label_smooth of yolov3_loss must be a bool value") - loss = helper.create_variable_for_type_inference(dtype=x.dtype) + if _non_static_mode(): + attrs = ("anchors", anchors, "anchor_mask", anchor_mask, "class_num", + class_num, "ignore_thresh", ignore_thresh, "downsample_ratio", + downsample_ratio, "use_label_smooth", use_label_smooth, + "scale_x_y", scale_x_y) + loss, _, _ = _C_ops.yolov3_loss(x, gt_box, gt_label, gt_score, *attrs) + return loss + helper = LayerHelper('yolov3_loss', **locals()) + loss = helper.create_variable_for_type_inference(dtype=x.dtype) objectness_mask = helper.create_variable_for_type_inference(dtype='int32') gt_match_mask = helper.create_variable_for_type_inference(dtype='int32') diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 7836545c738..00bd6ed38a3 100644 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -194,10 +194,10 @@ def yolo_loss(x, scale_x_y=1.) """ - if _non_static_mode() and gt_score is None: + if _non_static_mode(): loss, _, _ = _C_ops.yolov3_loss( - x, gt_box, gt_label, 'anchors', anchors, 'anchor_mask', anchor_mask, - 'class_num', class_num, 'ignore_thresh', ignore_thresh, + x, gt_box, gt_label, gt_score, 'anchors', anchors, 'anchor_mask', + anchor_mask, 'class_num', class_num, 'ignore_thresh', ignore_thresh, 'downsample_ratio', downsample_ratio, 'use_label_smooth', use_label_smooth, 'scale_x_y', scale_x_y) return loss -- GitLab