Can not apply normalize in compose

Wei_Meng: <p>Hi there,</p>
<p>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:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b28d7b6da90d2522800b4b872da3ada6eb953a87.png" data-download-href="https://discuss.pytorch.org/uploads/default/b28d7b6da90d2522800b4b872da3ada6eb953a87" title="problem_1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b28d7b6da90d2522800b4b872da3ada6eb953a87_2_690x277.png" alt="problem_1" data-base62-sha1="pty73zaKduwq6kH2XN3QvZ6Zqv5" width="690" height="277" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b28d7b6da90d2522800b4b872da3ada6eb953a87_2_690x277.png, https://discuss.pytorch.org/uploads/default/original/3X/b/2/b28d7b6da90d2522800b4b872da3ada6eb953a87.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/2/b28d7b6da90d2522800b4b872da3ada6eb953a87.png 2x" data-dominant-color="434546"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">problem_1</span><span class="informations">934×375 47 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>and the error I got is:</p>
<blockquote>
<p>File “/home/wei/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py”, line 3045, in normalize<br>
denom = input.norm(p, dim, True).clamp_min(eps).expand_as(input)<br>
AttributeError: ‘dict’ object has no attribute ‘norm’</p>
</blockquote>
<p>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…</p>
<p>The reason I did not use “transforms.Normalize”, it’s because it also reports errors:</p>
<blockquote>
<p>File “/home/wei/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py”, line 210, in normalize<br>
raise TypeError(‘tensor is not a torch image.’)</p>
<p>TypeError: tensor is not a torch image.</p>
</blockquote>
<p>I just copied the "Class Normalize(object)"codes from: [<a href="https://pytorch.org/docs/stable/_modules/torchvision/transforms/transforms.html#Normalize" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/_modules/torchvision/transforms/transforms.html#Normalize</a>](source codes) into my MyUtils.py file with Rescale, RandomCrop, ToTensor together.</p>
<p>First time post, any help would highly appreciated!</p>

Wei_Meng: <p>I think the problem is the ToTensor function in MyUtils.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/7/379b63e0cf7f087a05852bb8cd9521580c19ed4e.png" alt="problem_5" data-base62-sha1="7VVbkqyuWv5r91Pmonvrk6UyDLw" width="547" height="199"></p>
<p>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?</p>

ptrblck: <p>Since you’ve already created custom version of these transformations, you could unpack the <code>dict</code> in <code>MyUtils.Normalize</code> and apply the normalization only on the image.</p>

Wei_Meng: <p>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!</p>