-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Handle empty PyTorch video predictions without crashing #3488
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
Open
deruyter92
wants to merge
20
commits into
dev
Choose a base branch
from
jaap/fix-empty-video-predictions
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
64a2f4e
fix _generate_output_data crash on empty predictions
deruyter92 510090e
Allow create_df_from_prediction() to handle an empty predictions list.
deruyter92 4b45fb9
Treat zero predictions as a distinct warning case in `video_inference()`
deruyter92 c78f439
Don't write dataframe for empty predictions
deruyter92 16a2977
update test_videos: pop `"PAFinds"` before comparison
deruyter92 f534de6
update test_videos: assert no DF is written for empty predictions
deruyter92 624b1b1
Return empty-detection inference results
C-Achard c57dd35
Use CTD predictions for video output
C-Achard 4e4dab7
Handle empty prediction outputs gracefully
C-Achard 42098ac
Reuse shared frame key width helper
C-Achard 292d068
Clarify video inference warnings
C-Achard d5ba290
Go back to ValueError for empty predictions
C-Achard bff4fea
Remove NaN fallback for empty video predictions
C-Achard 06486de
Fix CTD export guard for empty predictions
C-Achard e456aeb
Clarify empty CTD prediction handling
C-Achard a162439
Centralize test fakes
C-Achard 11ff4fd
Add test for async with no images
C-Achard a210b69
Clarify async inference test comment
C-Achard c7a8c34
Use shared frame key width helper
C-Achard 8248a10
Merge branch 'dev' into jaap/fix-empty-video-predictions
C-Achard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| import deeplabcut.pose_estimation_pytorch.apis.videos as videos | ||
|
|
||
| POSE_CFG = { | ||
| "all_joints": [[0], [1], [2]], | ||
| "all_joints_names": ["snout", "leftear", "rightear"], | ||
| "nmsradius": 5, | ||
| "minconfidence": 0.1, | ||
| "sigma": 1, | ||
| } | ||
|
|
||
|
|
||
| def test_generate_output_data_handles_empty_predictions(): | ||
| output = videos._generate_output_data(POSE_CFG, []) | ||
|
|
||
| assert output["metadata"].pop("PAFinds").tolist() == [] | ||
| assert output == { | ||
| "metadata": { | ||
| "nms radius": 5, | ||
| "minimal confidence": 0.1, | ||
| "sigma": 1, | ||
| "PAFgraph": None, | ||
| "all_joints": [[0], [1], [2]], | ||
| "all_joints_names": ["snout", "leftear", "rightear"], | ||
| "nframes": 0, | ||
| "key_str_width": 1, | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def test_create_df_from_prediction_rejects_empty_predictions(tmp_path): | ||
| with pytest.raises(ValueError, match="empty predictions list"): | ||
| videos.create_df_from_prediction( | ||
| predictions=[], | ||
| dlc_scorer="DLC_test", | ||
| multi_animal=False, | ||
| model_cfg={ | ||
| "metadata": { | ||
| "bodyparts": ["snout", "leftear", "rightear"], | ||
| "unique_bodyparts": [], | ||
| "individuals": ["animal_0"], | ||
| } | ||
| }, | ||
| output_path=tmp_path, | ||
| output_prefix="video", | ||
| save_as_csv=False, | ||
| ) | ||
|
|
||
| assert not (tmp_path / "video.h5").exists() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def patch_video_iterator(monkeypatch): | ||
| """Patches ``VideoIterator`` with a stub reporting ``n_frames`` empty frames.""" | ||
|
|
||
| def _patch(n_frames: int): | ||
| class FakeVideoIterator: | ||
| def __init__(self, video_path, cropping=None): | ||
| self.video_path = video_path | ||
| self.fps = 25 | ||
| self.dimensions = (640, 480) | ||
|
|
||
| def get_n_frames(self, robust=False): | ||
| return n_frames | ||
|
|
||
| def set_context(self, context): | ||
| pass | ||
|
|
||
| def __iter__(self): | ||
| return iter(()) | ||
|
|
||
| monkeypatch.setattr(videos, "VideoIterator", FakeVideoIterator) | ||
| return FakeVideoIterator | ||
|
|
||
| return _patch | ||
|
|
||
|
|
||
| def _pose_runner(predictions: list) -> Mock: | ||
| runner = Mock() | ||
| runner.batch_size = 2 | ||
| runner.inference.return_value = predictions | ||
| return runner | ||
|
|
||
|
|
||
| def test_video_inference_zero_predictions_warns_about_unreadable_video(patch_video_iterator, caplog): | ||
| """Without a detector, every readable frame yields a prediction, so an empty | ||
| result means the video could not be read - not that no animals were found.""" | ||
| patch_video_iterator(3) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([])) | ||
|
|
||
| assert predictions == [] | ||
| assert "No predictions were produced for video.mp4" in caplog.text | ||
| assert "the video could not be read" in caplog.text | ||
| assert "no animals were detected" not in caplog.text | ||
| # the re-encoding tip is the actionable advice in this case | ||
| assert "re-encoding your video" in caplog.text | ||
|
|
||
|
|
||
| def test_video_inference_zero_predictions_with_detector_warns_about_detections(patch_video_iterator, caplog): | ||
| patch_video_iterator(3) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([]), detector_runner=_pose_runner([])) | ||
|
|
||
| assert predictions == [] | ||
| assert "no animals were detected in any frame" in caplog.text | ||
| assert "the video could not be read" in caplog.text | ||
| assert "re-encoding your video" in caplog.text | ||
|
|
||
|
|
||
| def test_video_inference_zero_predictions_warns_when_frame_count_is_zero(patch_video_iterator, caplog): | ||
| """A video whose metadata reports 0 frames must still warn.""" | ||
| patch_video_iterator(0) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([])) | ||
|
|
||
| assert predictions == [] | ||
| assert "No predictions were produced for video.mp4" in caplog.text | ||
|
|
||
|
|
||
| def test_video_inference_warns_when_some_frames_are_missing(patch_video_iterator, caplog): | ||
| patch_video_iterator(3) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([{}, {}])) | ||
|
|
||
| assert len(predictions) == 2 | ||
| assert "there are 3 frames in the video, but only 2" in caplog.text | ||
| assert "re-encoding your video" in caplog.text | ||
| assert "No predictions were produced" not in caplog.text | ||
|
|
||
|
|
||
| def test_video_inference_does_not_warn_when_all_frames_are_predicted(patch_video_iterator, caplog): | ||
| patch_video_iterator(3) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([{}, {}, {}])) | ||
|
|
||
| assert len(predictions) == 3 | ||
| assert "No predictions were produced" not in caplog.text | ||
| assert "were able to be processed" not in caplog.text | ||
|
|
||
|
|
||
| def test_video_inference_does_not_warn_when_writing_to_a_shelf(patch_video_iterator, caplog): | ||
| """The returned list is empty by design when a shelf writer is given.""" | ||
| patch_video_iterator(3) | ||
|
|
||
| with caplog.at_level("WARNING"): | ||
| predictions = videos.video_inference("video.mp4", _pose_runner([]), shelf_writer=Mock()) | ||
|
|
||
| assert predictions == [] | ||
| assert "No predictions were produced" not in caplog.text | ||
| assert "were able to be processed" not in caplog.text | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.