Pytorch: Trying to apply the transform to a numpy array... fails with an error
Any help will be much appreciated. The code in transforms.py says that the transformation should/would apply to PIL images as well as ndarrays.
Given the transforms:

data_transforms = {
    'train': transforms.Compose([
        transforms.Scale(256),
        transforms.Pad(4,0),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'val': transforms.Compose([
        transforms.Scale(256),
        transforms.Pad(4,0),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}


I wish to apply the transform on ndarrays that I obtained from some other code. Let's say it is x_data, whose shape is (1000,120,160,3) where the dimensions are (total rows, width, height, channels)

doing the following fails (All I'm trying to do is apply a transformation) :

foo = data_transforms['train']
bar = foo(x_data[0])


with the following message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-93-a703e3b9c76d&gt; in &lt;module&gt;()
----&gt; 1 foo(x_data[1])

~/anaconda3/envs/pytorch/lib/python3.5/site-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py in __call__(self, img)
     32     def __call__(self, img):
     33         for t in self.transforms:
---&gt; 34             img = t(img)
     35         return img
     36 

~/anaconda3/envs/pytorch/lib/python3.5/site-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py in __call__(self, img)
    185         """
    186         if isinstance(self.size, int):
--&gt; 187             w, h = img.size
    188             if (w &lt;= h and w == self.size) or (h &lt;= w and h == self.size):
    189                 return img

TypeError: 'int' object is not iterable


Most transforms method take only PIL objects as input. But you can add another transform called transforms.ToPILImage() which takes an nd-array as input, to convert an nd-array to PIL object. So in your case, dictionary variable should become:

data_transforms = {
'train': transforms.Compose([
    transforms.ToPILImage()
    transforms.Scale(256),
    transforms.Pad(4,0),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
    transforms.Scale(256),
    transforms.Pad(4,0),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}


Note that these transformations work sequentially. So it's necessary you add the toPILImage transform as the first transformation. Hence your nd-array is first converted to PIL object, and then other transformations are applied.
