Skip to content

[Perf] decord gpu context has no better performance than cpu context #359

@yuan-luo

Description

@yuan-luo

I encountered decord gpu context has no better performance than cpu context.
Here's my test script with gpu warm up.

import os
import time

import cv2
from tqdm import tqdm

from decord import VideoReader, cpu, gpu


def get_video_frames_cv2(video_path, frame_index, num_frames):
    cap = cv2.VideoCapture(video_path)
    video = []
    for _ in range(frame_index):
        cap.read()
    for _ in range(num_frames):
        _, frame = cap.read()
        video += [cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)]
    cap.release()

    return video


folder = "/root/luoyuan.luo/test_decord/video"


def test1():
    t0 = time.time()
    for f in tqdm(os.listdir(folder)):
        vr = VideoReader(os.path.join(folder, f), ctx=gpu(0))
        vr.get_batch([i for i in range(100)]).shape
    t = time.time() - t0
    return t


def test2():
    t0 = time.time()
    for f in tqdm(os.listdir(folder)):
        vr = VideoReader(os.path.join(folder, f), ctx=cpu(0))
        vr.get_batch([i for i in range(100)]).shape
    t = time.time() - t0
    return t


def test3():
    t0 = time.time()
    for f in tqdm(os.listdir(folder)):
        get_video_frames_cv2(os.path.join(folder, f), 0, 100)
    t = time.time() - t0
    return t

print(f"====Round1====")
print(test1())
print(test2())
print(test3())
print(f"====Round2====")
print(test1())
print(test2())
print(test3())
print(f"====Round3====")
print(test1())
print(test2())
print(test3())

Here's the result:

$python bench_decord.py
====Round1====
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  2.01it/s]
1.0047369003295898
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.85it/s]
0.5202276706695557
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.89it/s]
0.5144822597503662
====Round2====
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.88it/s]
0.5154316425323486
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  4.11it/s]
0.4865293502807617
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  4.00it/s]
0.5009441375732422
====Round3====
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.91it/s]
0.5115153789520264
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  4.21it/s]
0.4750845432281494
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:00<00:00,  3.85it/s]
0.5194442272186279

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions