Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to dramatically lower the memory usage of
.detect_video()
This PR touches #139 and #165.Previously we were relying on torch's
read_video
which always reads all video frames into memory at once. Ourskip_frames
was then used to slice the frame data during processing to speed things up. This approach is reasonable for short videos, videos with lower framerates, or smaller resolutions, but incrediby inefficient for larger files.We had hoped there would be an informative error from the torch side of things in case this process failed when running out of memory, but multiple user reports (and validated locally) show that the kernel just crashes, hangs, or even causes a computuer freeze...hardly a graceful failure.
While torch has a
VideoReader
class, it's currently in beta and using it requires compiling torch from source 🙃.I first tried storing video frame-counts and making repeated calls to
read_video
on a per-frame basis, but getting thepts
and timing right was non-trivial.So in order to avoid adding another hard-to-install dependency like openCV, this PR now uses a trick to lazy load video-frames by wrapping and slicing a
pyav
generator object; the library that torch'sread_video
uses under-the-hood.I've verified that even extremely long, high resolution videos work with
.detect_video
, and that the approach still works with batching, since we're still wrapping ourVideoDataset
in a torchDataLoader
.@ljchang @TiankangXie If you could test this branch on GPU machines or other platforms to see if we're incurring any additional unexpected overhead that would be super helpful!