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

[InferenceSlicer] - it is hard to set specific tile dimensions #1415

Closed
SkalskiP opened this issue Jul 29, 2024 · 8 comments
Closed

[InferenceSlicer] - it is hard to set specific tile dimensions #1415

SkalskiP opened this issue Jul 29, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@SkalskiP
Copy link
Collaborator

SkalskiP commented Jul 29, 2024

Description

I tried to use InferenceSlicer to divide the frame in four equally sized tiles and it turned out to be hard to do.

import numpy as np
import supervision as sv
from inference import get_model

model = get_model(model_id="football-ball-detection-rejhg/3", api_key=ROBOFLOW_API_KEY)

frame_generator = sv.get_video_frames_generator(source_path='/content/2e57b9_0.mp4')
frame = next(frame_generator)

def callback(patch: np.ndarray) -> sv.Detections:
    print(patch.shape)
    result = model.infer(patch, confidence=0.3)[0]
    return sv.Detections.from_inference(result)

slicer = sv.InferenceSlicer(
    callback=callback,
    overlap_filter_strategy=sv.OverlapFilter.NONE,
    slice_wh=(
        (1920 // 2) * 1.1, 
        (1080 // 2) * 1.1
    ),
    overlap_ratio_wh=(0.1, 0.1)
)

detections = slicer(frame).with_nms(threshold=0.1)

I was expecting the code above to produce 4 tiles with 10% overlap, but it created 9. This ended up being very wasteful as InferenceSlicer is expensive to run.

(594, 1056, 3)
(594, 969, 3)
(594, 18, 3)
(545, 1056, 3)
(545, 969, 3)
(545, 18, 3)
(10, 1056, 3)
(10, 969, 3)
(10, 18, 3)

Additional

  • Note: Please share a Google Colab with minimal code to test the new feature. We know it's additional work, but it will speed up the review process. The reviewer must test each change. Setting up a local environment to do this is time-consuming. Please ensure that Google Colab can be accessed without any issues (make it public). Thank you! 🙏🏻
@SkalskiP SkalskiP added the bug Something isn't working label Jul 29, 2024
@eric220
Copy link
Contributor

eric220 commented Aug 7, 2024

@SkalskiP I've created a generate_grid_offset function that takes a grid shape argument (i.e. (2,2) in your case) and returns the required offsets. I can throw together a colab if you're interested.

@SkalskiP
Copy link
Collaborator Author

SkalskiP commented Aug 7, 2024

I'd love to see it! I already started to work on somehow related topics in this PR: #1434

@eric220
Copy link
Contributor

eric220 commented Aug 7, 2024

@SkalskiP Here's the colab:
https://colab.research.google.com/drive/1syvRehgUFfu4jt7M8C7KcbSITakI8olt?usp=sharing

It's a rough draft, but you can get the idea. Just set the frame size and grid argument to whatever you want and it slices and dices into even sized tiles.

@SkalskiP
Copy link
Collaborator Author

SkalskiP commented Aug 7, 2024

Colab is private :)

@eric220
Copy link
Contributor

eric220 commented Aug 7, 2024 via email

@eric220
Copy link
Contributor

eric220 commented Aug 23, 2024

@SkalskiP I have circled back to the grid slicer. I recalculated, refactored and brought it into Numpy land. I also added a plotting script for proof of concept. Please check it out and let me know if this is something you think might be nice to add to the InferenceSlicer repo. It solves your issue nicely.
https://colab.research.google.com/drive/1syvRehgUFfu4jt7M8C7KcbSITakI8olt?usp=sharing

@eric220
Copy link
Contributor

eric220 commented Aug 28, 2024

So I am pretty sure the correct stride calculation for equisized tiles is:
side_len = 1920
num_divs = 3
overlap_ratio = .15
#STRIDE IS DERIVED FROM
#overlap = strideoverlap_ratio
#side_len = 2(stride-(.5
overlap))+(num_divs-2)(stride-overlap)
stride = int((side_len/(2-overlap_ratio+(num_divs-2)(1-overlap_ratio))))
overlap = int(stride
overlap_ratio)

@LinasKo
Copy link
Collaborator

LinasKo commented Oct 1, 2024

Closing after implementation in #1434

@LinasKo LinasKo closed this as completed Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants