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

Allow "tiff" and more extensions in DetectionDataset.from_yolo function #1636

Merged
merged 7 commits into from
Jan 8, 2025

Conversation

patel-zeel
Copy link
Contributor

@patel-zeel patel-zeel commented Oct 31, 2024

Description

Addresses #1554 as discussed with @LinasKo.

Highlights

  • Allows any image extension in DetectionDataset.from_yolo.
    Tested extensions (listed on Ultralytics predict page):
    • bmp
    • jpg
    • mpo
    • png
    • tif
    • webp
    • pfm - Pillow could not read pfm images, cv2 is able to read them. I saw here that even Ultralytics uses Pillow to read images. In that case, I am not sure if Ultralytics can "really" support pfm format, as their documentation suggests.
    • dng
  • Image must be of "RGB" type, raise ValueError otherwise.

List any dependencies that are required for this change.

  • Pillow

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How has this change been tested, please provide a testcase or example of how you tested the change?

The changes are tested with this colab notebook.

Test Case

100 images of each extension were generated along with their corresponding dummy labels and dummy data.yml. Test run checks if the function works for a particular extension and computes time taken to load the dataset.

Time taken (in seconds) to run the test case

  • Columns 0,1,2,3,4 are seed numbers.
  • -1.11 value indicates that supervision doesn't support that extension yet.

Result for supervision version 0.24.0, which uses cv2 to check the shape of images.

0 1 2 3 4 Mean Std
bmp -1.11 -1.11 -1.11 -1.11 -1.11 -1.11 0.00
jpg 0.98 1.16 0.85 0.76 0.77 0.90 0.15
mpo -1.11 -1.11 -1.11 -1.11 -1.11 -1.11 0.00
png 0.58 0.56 0.42 0.4 0.41 0.48 0.08
tif -1.11 -1.11 -1.11 -1.11 -1.11 -1.11 0.00
webp -1.11 -1.11 -1.11 -1.11 -1.11 -1.11 0.00
dng -1.11 -1.11 -1.11 -1.11 -1.11 -1.11 0.00

Result for this PR, which uses Pillow to check the shape and type (RGB or not) of images.

0 1 2 3 4 Mean Std
bmp 0.04 0.02 0.02 0.02 0.03 0.03 0.01
jpg 0.03 0.02 0.03 0.02 0.02 0.03 0.00
mpo 0.04 0.04 0.06 0.04 0.04 0.04 0.01
png 0.02 0.02 0.04 0.02 0.02 0.02 0.01
tif 0.05 0.06 0.08 0.05 0.05 0.06 0.01
webp 0.10 0.11 0.11 0.09 0.11 0.10 0.00
dng 0.06 0.06 0.05 0.06 0.06 0.06 0.00

Docs

  • Docs updated? What were the changes: Not changed, please suggest if we need changes in the docs

@LinasKo
Copy link
Contributor

LinasKo commented Oct 31, 2024

You never cease to surprise, @patel-zeel; such a thorough analysis! ⭐

Adding this to the back of my review backlog, but I already know it will be a delight.

@LinasKo LinasKo added the hacktoberfest-accepted Contribute to the notion of open-source this October! label Oct 31, 2024
@patel-zeel
Copy link
Contributor Author

Thank you for your kind words, @LinasKo. It's a pleasure to help improve a widely used library. Looking forward to your feedback!

@patel-zeel
Copy link
Contributor Author

patel-zeel commented Dec 31, 2024

@LinasKo a small correction. I recently found that Ultralytics uses this function to read pfm and other image files during yolo predict call. It uses cv2 and not Pillow. So, I was able to process pfm images with Ultralytics. However, the fact remains that Pillow does not support colored pfm images. This PR added greyscale pfm support in Pillow and it points to this issue for not supporting colored pfm images. The issue is pinned on Pillow since May 2016 and thus, looks like it is not going to be resolved in the near future. However, unless pfm format dominates some domains, we are not bottlenecked by it.

resolution_wh = (w, h)
if image.mode != "RGB":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patel-zeel looks like we can simplify the code here. There is no need for nested ifs.

if image.mode not in ("RGB", "L"):
    raise ValueError(
        f"Images must be 'RGB' or 'grayscale', but {image_path} mode is '{image.mode}'."
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

resolution_wh = (w, h)
if image.mode != "RGB":
if image.mode == "L":
image = image.convert("RGB")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that conversion to RGB is not necessary. The image is not used in the further part of the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that'd save us the time wasted in the conversion. I checked the other extreme as well. If we convert all images to RGB by default, that adds a bit of unnecessary overhead. So, this change will improve the speed further. Thank you for pointing it out, @SkalskiP.

@@ -153,7 +153,7 @@ def load_yolo_annotations(
image_paths = [
str(path)
for path in list_files_with_extensions(
directory=images_directory_path, extensions=["jpg", "jpeg", "png"]
directory=images_directory_path, extensions=["*"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a small but important side-effect of this change. If there are other files than images in the directory, we will also try to load them. For example, macOS puts a .DS_Store file in the directory. @patel-zeel I suggest putting here a list of image extensions that you have tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. On it!

@SkalskiP
Copy link
Collaborator

SkalskiP commented Jan 8, 2025

@patel-zeel I apologize for making you wait so long for a review of this PR. I was on a long break at the turn of the year. I left some comments, please address them.

@patel-zeel
Copy link
Contributor Author

patel-zeel commented Jan 8, 2025

No worries, @SkalskiP, I hope you had a good time during the break. Thank you for the review! I have addressed the comments and applied the changes.

@@ -153,7 +153,18 @@ def load_yolo_annotations(
image_paths = [
str(path)
for path in list_files_with_extensions(
directory=images_directory_path, extensions=["jpg", "jpeg", "png"]
directory=images_directory_path,
extensions=[
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patel-zeel small request: don't break file extensions onto separate lines; list them all on one line if possible

Copy link
Contributor Author

@patel-zeel patel-zeel Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept them in a single line @SkalskiP. Pre-commit (specifically ruff-format) forced them on separate lines.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eeeh that's what I thought so

@SkalskiP
Copy link
Collaborator

SkalskiP commented Jan 8, 2025

@patel-zeel one more small comment and we should be good to go

@SkalskiP
Copy link
Collaborator

SkalskiP commented Jan 8, 2025

@patel-zeel, thanks a lot! 🙏🏻 approved and merging!

@SkalskiP SkalskiP merged commit 651ed9d into roboflow:develop Jan 8, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Contribute to the notion of open-source this October!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants