Skip to content

Commit 79e2d13

Browse files
Fix: Correct YOLO loss gradient for x,y coordinates (#3088)
* Fix: Correct YOLO loss gradient for x,y coordinates The gradient calculation for bounding box x and y coordinates in the `loss_yolo_` helper `impl::yolo_helper_impl::tensor_to_loss` was missing a factor of 2.0. This factor arises from the chain rule due to the coordinate transformation `output_scaled = network_output * 2.0 - 0.5`, where the loss is calculated based on `output_scaled`, but the gradient needs to be with respect to `network_output`. This commit multiplies the affected gradient terms by 2.0f to correctly apply the chain rule. * scale the loss --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 81cd1f1 commit 79e2d13

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

dlib/dnn/loss.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3827,8 +3827,8 @@ namespace dlib
38273827
const auto y_idx = tensor_index(output_tensor, n, k + 1, r, c);
38283828
const auto w_idx = tensor_index(output_tensor, n, k + 2, r, c);
38293829
const auto h_idx = tensor_index(output_tensor, n, k + 3, r, c);
3830-
g[x_idx] = scale_box * put_in_range(-1, 1, (out_data[x_idx] * 2.0 - 0.5 - tx));
3831-
g[y_idx] = scale_box * put_in_range(-1, 1, (out_data[y_idx] * 2.0 - 0.5 - ty));
3830+
g[x_idx] = scale_box * put_in_range(-1, 1, (out_data[x_idx] * 2.0 - 0.5 - tx)) * 2.0f;
3831+
g[y_idx] = scale_box * put_in_range(-1, 1, (out_data[y_idx] * 2.0 - 0.5 - ty)) * 2.0f;
38323832
g[w_idx] = scale_box * put_in_range(-1, 1, (out_data[w_idx] - tw));
38333833
g[h_idx] = scale_box * put_in_range(-1, 1, (out_data[h_idx] - th));
38343834

@@ -3863,7 +3863,7 @@ namespace dlib
38633863
}
38643864

38653865
// The loss is the squared norm of the gradient
3866-
loss += length_squared(rowm(mat(grad), n));
3866+
loss += 0.5 * length_squared(rowm(mat(grad), n));
38673867
}
38683868
};
38693869
}

0 commit comments

Comments
 (0)