v2.Resize returns result faster if from_numpy is not used first

Came across an oddity when preprocessing some high resolution image data. Resizing directly from a numpy array returns the desired tensor result faster than if from_numpy is used beforehand. Just curious for an explanation for this behavior (multiple copies being made?). Thanks in advanced!

import numpy as np
import time
import torch
from torchvision.transforms import v2
image = np.random.randint(255, size=(3, 50000, 50000), dtype='uint8')

print('\nDirect Resize')
t0 = time.perf_counter()
reData = v2.Resize((224,224))(image)
tDiff = time.perf_counter()-t0
print('Numpy->Resize; Time:  ', tDiff)

print('\nIndirect Resize')
t0 = time.perf_counter()
inData = torch.from_numpy(image)
tDiff = time.perf_counter()-t0
print('Numpy->Tensor;  Time: ', tDiff)

t0 = time.perf_counter()
inData = v2.Resize((224,224))(inData)
tDiff = time.perf_counter()-t0
print('Tensor->Resize; Time: ', tDiff)

yields:

Direct Resize
Numpy->Resize; Time:   0.00033349916338920593

Indirect Resize
Numpy->Tensor;  Time:  0.00025670044124126434
Tensor->Resize; Time:  2.8171700993552804

inData is undefined and thus your code applies the same operation twice (if the invalid block is removed).

Apologies, corrected typo in example; same result and question though.

While no error/warning was generated, reData was not being resized. :sweat_smile: