Can not apply normalize in compose

Hi there,

I am a new learner to Pytorch. I am trying to customize my first Pytorch model. Currently, I am stuck in the normalization part. My codes are:

and the error I got is:

File “/home/wei/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 3045, in normalize
denom = input.norm(p, dim, True).clamp_min(eps).expand_as(input)
AttributeError: ‘dict’ object has no attribute ‘norm’

The rescale, randomcorp and ToTensor parts all work very well. I know the problem is about the input. It should not be in a dict format. But I just have no idea how to fix it…

The reason I did not use “transforms.Normalize”, it’s because it also reports errors:

File “/home/wei/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py”, line 210, in normalize
raise TypeError(‘tensor is not a torch image.’)

TypeError: tensor is not a torch image.

I just copied the "Class Normalize(object)"codes from: [https://pytorch.org/docs/stable/_modules/torchvision/transforms/transforms.html#Normalize](source codes) into my MyUtils.py file with Rescale, RandomCrop, ToTensor together.

First time post, any help would highly appreciated!

I think the problem is the ToTensor function in MyUtils.

problem_5

it returned a dict, but the Normalize function only takes tensor as input. In this case, is there anyway I can only assign the ‘image’ tensor in the dictionary to the normalization function?

Since you’ve already created custom version of these transformations, you could unpack the dict in MyUtils.Normalize and apply the normalization only on the image.

Thanks for your kind help! I have not solved this problem yet. I just walked around this problem by using ImageFolder, which is super helpful. I prepared my dataset as the requirement of ImageFolder, then every transform function worked smoothly!