Skip to content
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

Fix PyTorch Object Detection Estimators Missing Gradients Bug #2249

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions art/estimators/object_detection/pytorch_object_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ def _get_losses(
x_preprocessed = x_preprocessed.to(self.device)
y_preprocessed = [{k: v.to(self.device) for k, v in y_i.items()} for y_i in y_preprocessed]

# Set gradients again after inputs are moved to another device
if x_preprocessed.is_leaf:
x_preprocessed.requires_grad = True
else:
x_preprocessed.retain_grad()

loss_components = self._model(x_preprocessed, y_preprocessed)

return loss_components, x_preprocessed
Expand Down
6 changes: 6 additions & 0 deletions art/estimators/object_detection/pytorch_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ def _get_losses(
x_preprocessed = x_preprocessed.to(self.device)
y_preprocessed_yolo = y_preprocessed_yolo.to(self.device)

# Set gradients again after inputs are moved to another device
if x_preprocessed.is_leaf:
x_preprocessed.requires_grad = True
else:
x_preprocessed.retain_grad()

# Calculate loss components
loss_components = self._model(x_preprocessed, y_preprocessed_yolo)

Expand Down