Pytorch image normalization

Pytorch image normalization is very slow in total code and my computer. But if only run normalization code is fast normally in my computer . My friend computer i9 9700K cpu and 2080 gpu. torch image normalization is very fast more than 25 times me in total code. My computer i7 9700K 2070 nvidia. Our code (torch.cuda.floattensor(image)/255-0.5)/0.5

Could you share the code snippet you’ve used to profile this operation?
Note that CUDA operations are executed asynchronously, so you would have to synchronize the code before starting and stopping the timer (or use the torch.utils.benchmark utilities, which will synchronize the code for you).

1 Like


Sorry for my late return. I am using torch.cuda.synchronize() also this code running fast alone. But if I use with other pipeline code timing is slow.I agree with you but i cant find where is slow. Actually this process should be very fast.

in the code snippet that you are providing, you are normalizing the image on the CPU then converting it to a CUDA tensor. While in the line of code that you have provided in your comment

you are converting to CUDA tensor and then doing the normalization on the GPU which is obviously faster.

Is it correct that you convert [0-255] range to [-0.5; 0.5] and then divide by 0.5 and get [-0.25; 0.25] range?