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

[?] How to export empty YOLOv5 labels files for images with no annotations #5396

Open
2 tasks
hassanbadawy opened this issue Jan 16, 2025 · 1 comment
Open
2 tasks
Labels
question A general question about how to use FiftyOne

Comments

@hassanbadawy
Copy link

Describe the problem

when I run
split_view.export(
export_dir=export_dir,
dataset_type=fo.types.YOLOv5Dataset,
label_field=label_field,
split=split,
classes=classes,
)
it exports only all images that have annotation, but there are some images with no annotation don't export! I need my model to train on it to learn them as a background, without this the model gives me many false positive on those empty images.

System information

  • OS Platform and Distribution (Linux Ubuntu 22.04):
  • Python version (3.10.12):
  • FiftyOne version (1.2.0):
  • FiftyOne installed from (pip):

Willingness to contribute

The FiftyOne Community encourages bug fix contributions. Would you or another
member of your organization be willing to contribute a fix for this bug to the
FiftyOne codebase?

  • [x ] Yes. I can contribute a fix for this bug independently
  • Yes. I would be willing to contribute a fix for this bug with guidance
    from the FiftyOne community
  • No. I cannot contribute a bug fix at this time
@hassanbadawy hassanbadawy added the bug Bug fixes label Jan 16, 2025
@brimoor brimoor changed the title [BUG] fo.export only images with annotations [?] How to export empty YOLOv5 labels files for images with no annotations Jan 16, 2025
@brimoor brimoor added question A general question about how to use FiftyOne and removed bug Bug fixes labels Jan 16, 2025
@brimoor
Copy link
Contributor

brimoor commented Jan 16, 2025

Hi @hassanbadawy 👋

Suppose you are exporting a field called ground_truth in YOLOv5 format. Then:

  1. If ground_truth is None, then no labels TXT file is generated
  2. If ground_truth is an empty detections object fo.Detections(), then an empty labels TXT file is generated

Sounds like you want option 2 in your case.

Here's an example:

import fiftyone as fo
import fiftyone.zoo as foz
import fiftyone.utils.random as four

dataset = foz.load_zoo_dataset("quickstart")

classes = dataset.distinct("ground_truth.detections.label")

# Create splits
dataset.untag_samples(dataset.distinct("tags"))
four.random_split(dataset, {"train": 0.7, "test": 0.3})

#
# Simulate a case where 50% of images have no annotations
#

# If `ground_truth` is `None`, no labels files are generated
# dataset.take(100).clear_sample_field("ground_truth")

# If `ground_truth` contains empty detections `fo.Detections()`, then empty
# labels files are generated
dataset.take(100).set_field("ground_truth", fo.Detections()).save()

# Export in YOLOv5 format
for split in ("train", "test"):
    split_view = dataset.match_tags(split)
    split_view.export(
        export_dir="/tmp/yolov5",
        dataset_type=fo.types.YOLOv5Dataset,
        label_field="ground_truth",
        split=split,
        classes=classes,
    )
find /tmp/yolov5/images/train -type f | wc -l
find /tmp/yolov5/labels/train -type f | wc -l
find /tmp/yolov5/images/test -type f | wc -l
find /tmp/yolov5/labels/test -type f | wc -l
140
140
60
60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A general question about how to use FiftyOne
Projects
None yet
Development

No branches or pull requests

2 participants