Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

transforms.Compose not working as expected. #10906

@dwSun

Description

@dwSun
#!/usr/bin/env python3
# %% import libs
import mxnet as mx
from mxnet.gluon.data.vision import transforms

# %% set random seed
mx.random.seed(123)

# %% the image
img = mx.nd.random_uniform(0,255,(1024,1024,3))
img.shape #(1024, 1024, 3)
img.mean().asscalar() # 127.47173

# %%
transforms.Resize((224,320))(img).shape#(320, 224, 3)
transforms.Resize((224,320))(img).mean().asscalar()#127.2181

transforms.ToTensor()(img).shape#(3, 1024, 1024)
transforms.ToTensor()(img).mean().asscalar()#0.49988914

# %% This is confusing...
transform1 = transforms.Compose(
    [transforms.ToTensor(), # should be (1024, 1024, 3) => (3, 1024, 1024)
    transforms.Resize((224, 320))]) # should be (3, 1024, 1024) => (320, 224, 1024)

# I am confused with this 2 lines
transform1(img).shape # got (1024, 320, 224) with mxnet-mkl-1.1.0; got (320, 224, 1024) with mxnet-mkl-1.2.0b20180508
transform1(img).mean().asscalar() #got 0.00097802561 with mxnet-mkl-1.1.0;  got 0.24939652 with mxnet-mkl-1.2.0b20180508

# %% This is the one works as expected.
transform2 = transforms.Compose(
    [transforms.Resize((224, 320)),
    transforms.ToTensor()])

transform2(img).shape#(3, 320, 224)
transform2(img).mean().asscalar() # 0.49889451

# %% Guess there is a ToTensor at the end implicitly.
transform3 = transforms.Compose(
    [transforms.Resize((224, 320))])

# Actually not!
transform3(img).shape#(320, 224, 3)
transform3(img).mean().asscalar() # 127.2181

# %% those lines works exactlly as transform1 with mxnet-mkl-1.1.0, but not with mxnet-mkl-1.2.0b20180508, why?
img_mod = transforms.ToTensor()(img)
img_mod = transforms.Resize((224, 320))(img_mod)
img_mod = transforms.ToTensor()(img_mod)

img_mod.shape#(1024, 320, 224)
img_mod.mean().asscalar() # 0.00097802561

tested both with opencv-python 3.4.0.12 from pypi.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions