Best practice for processing a batch of sequential images with OpenCV

Viewed 41

I have a batch of sequential images each containing 5 frames with a shape of (Batch, Sequence, Height, Width, Channel). Here how it looks like with a batch size of 32:

data.shape
> (32, 5, 256, 512, 3)

Now I want to apply some OpenCV and Torch operations on these images. Some examples for these are

cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

or torchvision.transforms.compose operation as:

midas_transformer
> Compose(
    <function transforms.<locals>.<lambda> at 0x7ff5c5488a60>
    <midas.transforms.Resize object at 0x7ff5c547c0a0>
    <midas.transforms.NormalizeImage object at 0x7ff5c547c0d0>
    <midas.transforms.PrepareForNet object at 0x7ff5c547c130>
    <function transforms.<locals>.<lambda> at 0x7ff5c5488af0>

Currently my solution is nested list comprehension:

new_image = np.array([[my_function(sequence) for sequence in batch] for batch in data])

My question is: What is the best practice to apply these operations on each image frame? Is there any better way to do that?

1 Answers

For the pytorch operations, I would first .reshape() data into a (32 * 5, 3, 256, 512) tensor, then apply the transformation on this whole batch to fully take advantage of CPU/GPU parallelism, and finally reshape into a (32, 5, 256, 512, 3) tensor

Related