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 missing instance frame #108

Merged
merged 2 commits into from
Feb 4, 2025
Merged
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
19 changes: 16 additions & 3 deletions dreem/datasets/sleap_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ def __init__(
# for label in self.labels:
# label.remove_empty_instances(keep_empty_frames=False)

self.frame_idx = [torch.arange(len(labels)) for labels in self.labels]
# note if slp is missing frames, taking last frame idx is safer than len(labels)
# as there will be fewer labeledframes than actual frames
self.frame_idx = [
torch.arange(labels[-1].frame_idx + 1) for labels in self.labels
]
self.skipped_frame_ct = [0 for labels in self.labels]
# self.frame_idx = [torch.arange(len(labels)) for labels in self.labels]
# Method in BaseDataset. Creates label_idx and chunked_frame_idx to be
# used in call to get_instances()
self.create_chunks()
Expand All @@ -168,7 +174,6 @@ def get_instances(self, label_idx: list[int], frame_idx: list[int]) -> list[Fram

"""
video = self.labels[label_idx]

video_name = self.video_files[label_idx]

# get the correct crop size based on the video
Expand Down Expand Up @@ -201,7 +206,15 @@ def get_instances(self, label_idx: list[int], frame_idx: list[int]) -> list[Fram

frame_ind = int(frame_ind)

lf = video[frame_ind]
# if slp is missing instances in some frames, frame_ind will be smaller than lf.frame_idx
lf = video[frame_ind - self.skipped_frame_ct[label_idx]]
if frame_ind < lf.frame_idx:
logger.warning(
f"Frame index {frame_ind} is trying to access frame {lf.frame_idx} of the slp file {video_name}. "
f"This likely means there are no labelled instances in this frame. Skipping frame."
)
self.skipped_frame_ct[label_idx] += 1
continue

try:
img = vid_reader.get_data(int(lf.frame_idx))
Expand Down
Loading