fix: move predict_image input tensor to the model's device#1391
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:
|
|
Thanks @giswqs! @jveitchmichaelis will have a look at this. |
|
Thanks, this looks pretty sensible to me. I'll double check I can reproduce too. |
|
@giswqs thanks for raising this, I can replicate although I was surprised that it failed. The additional cause is that the model isn't moved to the device until a trainer uses it and Alternatively, you could use So I'd propose:
I'll leave some comments on the test case in the review. @bw4sz any thoughts there? On the one hand I feel we shouldn't encourage manually moving the model, because |
|
We can allow this for now, but definitely let's get predict_image into the trainer context that feels like the right pattern. |
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.