RajeshKumar S
RajeshKumar S

Reputation: 1

CuPy takes more time to preprocess the image?

import cv2
import numpy as np
import cupy as cp
import time


def op_image(image):
    start_time = time.time()
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (640, 480))
    image_mean = np.array([127, 127, 127])
    image = (image - image_mean) / 128
    image = np.transpose(image, [2, 0, 1])
    image = np.expand_dims(image, axis=0)
    image = image.astype(np.float32)
    print("Opencv End time : ",time.time() - start_time)


def Cu_image(image):
    start_time = time.time()
    image = cp.array(image)
    image = cp.ascontiguousarray(image[:, :, ::-1])
    image = cp.resize(image, (640, 480,3))
    image_mean = cp.array([127, 127, 127])
    image = (image - image_mean) / 128
    image = cp.transpose(image, (2, 0, 1))
    image = cp.expand_dims(image, axis=0)
    image = cp.expand_dims(image, axis=0)
    image = image.astype(cp.float32)
    print("Cupy End time : ",time.time() - start_time)


image = cv2.imread("/home/nextbrain-ssd/Vision/cctv-server/assets/event_created_images/camera_0_9_509_2024-05-23T04:14:32.931834Z.jpg")
op_image(image=image)
image = cv2.imread("/home/nextbrain-ssd/Vision/cctv-server/assets/event_created_images/camera_0_9_509_2024-05-23T04:14:32.931834Z.jpg")
Cu_image(image=image)

Output:

Opencv End time :  0.00825643539428711
Cupy End time :  0.10329556465148926

The above code preprocesses an image using two different libraries: OpenCV (CPU) and CuPy (GPU). Surprisingly, CuPy takes more time compared to OpenCV. OpenCV processes the image using the CPU, while CuPy uses the GPU. I expected CuPy to be faster, but it's the opposite. Why does CuPy take longer than OpenCV?

Upvotes: 0

Views: 346

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60761

The processing you do here is rather trivial, and can be done very efficiently on CPU. On most computers, using the GPU means transferring data to it, and then transferring the result back. This is a large overhead. The time saving of performing the computation on the GPU must be sufficiently large to compensate for this data transfer overhead.

Besides this, your timing code is inadequate. Anything that is faster than a second or so should be run in a loop for timing. Remove the calls to time.time() from your functions, and then call your functions with timeit:

timeit.timeit(lambda: op_image(image=image), number=1000)

Upvotes: 0

Related Questions