dmlc / decord

An efficient video loader for deep learning with smart shuffling that's super easy to digest
Apache License 2.0
1.86k stars 160 forks source link

VideoReader: Fastest way to get all the frames? #265

Open ggregoire opened 1 year ago

ggregoire commented 1 year ago

Hi there,

I'm using get_batch to get all the frames from a 1 min video and I wanted to ask you if there is a fastest method to achieve this? I read the code from video_reader.py and I don't think so, but I just wanted to confirm.

Here is my code:

vr = VideoReader(video, ctx=gpu(0))
n_frames = len(vr) # 1800
frames_indexes = list(range(0, n_frames)) # [0, ..., 1799]
frames = vr.get_batch(frames_indexes).asnumpy()

Thank you.

gcanat commented 1 year ago

It's not faster but it is more concise:

vr = VideoReader(video, ctx=gpu(0))
frames = vr[:].asnumpy()

Also do you really need ctx=gpu(0) ? Isn't numpy always on cpu anyway ?

ggregoire commented 1 year ago

I'm not a numpy expert but we see our script appearing in nvidia-smi when we use ctx=gpu(0) so I guess it works.

The weird thing though: when we hash the frames, the hashes are different when we use ctx=cpu(0) and ctx=gpu(0).

For instance:

vr_gpu = VideoReader(video, ctx=gpu(0))
vr_cpu = VideoReader(video) # defaults to ctx=cpu(0)
hash(vr_gpu[0].asnumpy()) == hash(vr_cpu[0].asnumpy()) # => False