MiDaS icon indicating copy to clipboard operation
MiDaS copied to clipboard

why need np.transpose(image,(2,0,1)) before feed image to model

Open bigtree2020 opened this issue 3 years ago • 4 comments

Hi I wonder why we need a np.transpose(image,(2,0,1)) before feed image to the model? https://github.com/intel-isl/MiDaS/blob/master/midas/transforms.py#L219

bigtree2020 avatar Jan 08 '21 06:01 bigtree2020

@bigtree2020 It's a pytorch thing, unrelated to MiDaS; see, for example, https://discuss.pytorch.org/t/swap-axes-in-pytorch/970.

dfrumkin avatar Jan 08 '21 07:01 dfrumkin

@bigtree2020

OpenCV img = cv2.imread(path) loads an image with HWC-layout (height, width, channels), while Pytorch requires CHW-layout. So we have to do np.transpose(image,(2,0,1)) for HWC->CHW transformation.

AlexeyAB avatar Jan 10 '21 14:01 AlexeyAB

Thanks.

mathmax12 avatar Jan 13 '21 01:01 mathmax12

@bigtree2020

OpenCV img = cv2.imread(path) loads an image with HWC-layout (height, width, channels), while Pytorch requires CHW-layout. So we have to do np.transpose(image,(2,0,1)) for HWC->CHW transformation.

Thank you, it helped me a lot!

Tar0kan avatar Aug 10 '22 12:08 Tar0kan