fix: move predict_image input tensor to the model's device#1391
Open
giswqs wants to merge 1 commit into
Open
Conversation
`_predict_image_` built the input tensor on CPU and never moved it to
the model's device, so a model placed on CUDA (`m.model.to('cuda')` or
via a Lightning trainer that has been on a GPU) crashed with
"Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
should be the same".
Move the image tensor to `next(model.parameters()).device` before the
forward pass, and add a regression test that loads the model onto CUDA
and runs `predict_image` (skipped when CUDA is unavailable).
Closes weecology#1390.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1391 +/- ##
==========================================
- Coverage 86.96% 86.84% -0.12%
==========================================
Files 26 26
Lines 3712 3717 +5
==========================================
Hits 3228 3228
- Misses 484 489 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Collaborator
|
Thanks @giswqs! @jveitchmichaelis will have a look at this. |
Collaborator
|
Thanks, this looks pretty sensible to me. I'll double check I can reproduce too. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1390.
deepforest.predict._predict_image_builds the input tensor on CPU and never moves it to the model's device, so any caller that has placed the model on CUDA (model.model.to('cuda'), or a Lightning trainer that has trained on GPU) gets:This PR moves the input tensor to
next(model.parameters()).devicebefore the forward pass. Guarded withtry/except StopIterationto handle the (unrealistic) case of a parameter-less model gracefully.Repro
Changes
src/deepforest/predict.py: move the input tensor to the model's device inside_predict_image_.tests/test_main.py: addtest_predict_image_on_cudaregression test, skipped when CUDA is unavailable.Test plan
ruff format --checkandruff checkon the changed source files (clean, using the version pinned in.pre-commit-config.yaml).predict_imagetests should still pass; new CUDA test is auto-skipped on CPU-only runners.