Skip to content

fix: move predict_image input tensor to the model's device#1391

Open
giswqs wants to merge 1 commit into
weecology:mainfrom
giswqs:fix/predict-image-device-mismatch
Open

fix: move predict_image input tensor to the model's device#1391
giswqs wants to merge 1 commit into
weecology:mainfrom
giswqs:fix/predict-image-device-mismatch

Conversation

@giswqs
Copy link
Copy Markdown

@giswqs giswqs commented May 13, 2026

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:

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
should be the same or input should be a MKLDNN tensor and weight is a dense tensor

This PR moves the input tensor to next(model.parameters()).device before the forward pass. Guarded with try/except StopIteration to handle the (unrealistic) case of a parameter-less model gracefully.

Repro

from deepforest import main
m = main.deepforest()
m.load_model(model_name='weecology/deepforest-tree', revision='main')
m.model.to('cuda')
m.predict_image(path='some_rgb.tif')
# before this PR: RuntimeError as above
# after this PR: returns a DataFrame of detections

Changes

  • src/deepforest/predict.py: move the input tensor to the model's device inside _predict_image_.
  • tests/test_main.py: add test_predict_image_on_cuda regression test, skipped when CUDA is unavailable.

Test plan

  • ruff format --check and ruff check on the changed source files (clean, using the version pinned in .pre-commit-config.yaml).
  • CI: existing CPU predict_image tests should still pass; new CUDA test is auto-skipped on CPU-only runners.
  • Verified locally that the workaround applied downstream in opengeos/geoai#746 becomes unnecessary once this lands.

`_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
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.84%. Comparing base (f3bd776) to head (9006f50).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/deepforest/predict.py 50.00% 2 Missing ⚠️
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     
Flag Coverage Δ
unittests 86.84% <50.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bw4sz bw4sz requested a review from jveitchmichaelis May 13, 2026 16:45
@bw4sz
Copy link
Copy Markdown
Collaborator

bw4sz commented May 13, 2026

Thanks @giswqs! @jveitchmichaelis will have a look at this.

@jveitchmichaelis
Copy link
Copy Markdown
Collaborator

Thanks, this looks pretty sensible to me. I'll double check I can reproduce too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

predict_image fails on CUDA: input tensor not moved to model device

3 participants