zenseact / zod

Software Development Kit for the Zenseact Open Dataset (ZOD)
https://zod.zenseact.com
MIT License
92 stars 13 forks source link

Fix ValueError for get_annotation on frames with empty project data #9

Closed AlexVialaBellander closed 1 year ago

AlexVialaBellander commented 1 year ago

Summary

A value error is often raised from Information.get_annotation_frame(self, time, project) when accessing EGO_ROAD, but it seems that for many images. The min() function in the above functions tries to execute on an empty sequence.

Reproduction

from zod import ZodFrames
zod_frames = ZodFrames("/mnt/ZOD", "full")

from zod.constants import AnnotationProject
zod_frames["048405"].get_annotation(AnnotationProject.EGO_ROAD)

Error

ValueError                                Traceback (most recent call last)
Cell In[16], line 5
      2 zod_frames = ZodFrames("/mnt/ZOD", "full")
      4 from zod.constants import AnnotationProject
----> 5 zod_frames["048405"].get_annotation(AnnotationProject.EGO_ROAD)

File [~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/frame.py:54](https://vscode-remote+ssh-002dremote-002bai-002dswe.vscode-resource.vscode-cdn.net/home/alex/swarm-learning-master-thesis/platform/~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/frame.py:54), in ZodFrame.get_annotation(self, project)
     52 """Get the annotation for a given project."""
     53 assert project in self.info.annotation_frames, f"Project {project} not available."
---> 54 return self.info.get_key_annotation_frame(project).read()

File [~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/info.py:83](https://vscode-remote+ssh-002dremote-002bai-002dswe.vscode-resource.vscode-cdn.net/home/alex/swarm-learning-master-thesis/platform/~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/info.py:83), in Information.get_key_annotation_frame(self, project)
     81     return self.annotation_frames[project][0]
     82 else:
---> 83     return self.get_annotation_frame(self.keyframe_time, project)

File [~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/info.py:112](https://vscode-remote+ssh-002dremote-002bai-002dswe.vscode-resource.vscode-cdn.net/home/alex/swarm-learning-master-thesis/platform/~/miniconda3/envs/swarm/lib/python3.10/site-packages/zod/data_classes/info.py:112), in Information.get_annotation_frame(self, time, project)
    110 if project not in self.annotation_frames:
    111     raise ValueError(f"No annotations found for project {project.value}.")
--> 112 return min(
    113     self.annotation_frames[project],
    114     key=lambda annotation_frame: abs(annotation_frame.time - time),
    115 )

ValueError: min() arg is an empty sequence

The issue

The issue lies in [here: (L53 in frame.py)] assert project in self.info.annotation_frames, f"Project {project} not available."

I assume that the intentions here were to catch this error. However, the project exists in Information. However, the value for the key is the empty list. As such, the assertion passes and the empty list is passed on to downstream functions.

Proposed Solution

Add a line which checks if the value for the key-value pair is the empty list assert self.info.annotation_frames.get(project), f"Project {project} is empty."

Example

zod_frames["048405"].info.annotation_frames
> {<AnnotationProject.EGO_ROAD: 'ego_road'>: [], ..... }

AnnotationProject.EGO_ROAD 
> <AnnotationProject.EGO_ROAD: 'ego_road'>

zod_frames["048405"].info.annotation_frames.get(AnnotationProject.EGO_ROAD)
> []

AnnotationProject.EGO_ROAD  in zod_frames["048405"].info.annotation_frames
> True
atonderski commented 1 year ago

This should be fixed now.

For instance, the following code triggers the assert:

for zod_frame in zod_frames:
    polygon_annotations = zod_frame.get_annotation(AnnotationProject.EGO_ROAD)

And the following code doesn't

frame_ids = zod_frames.get_split("train", AnnotationProject.EGO_ROAD)
for frame_id in frame_ids:
    zod_frame = zod_frames[frame_id]
    polygon_annotations = zod_frame.get_annotation(AnnotationProject.EGO_ROAD)

Are you happy with this resolution? If so, I will close this issue

AlexVialaBellander commented 1 year ago

Perfect, closing!