5

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)
<ipython-input-93-a703e3b9c76d> in <module>()
----> 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:
---> 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):
--> 187             w, h = img.size
    188             if (w <= h and w == self.size) or (h <= w and h == self.size):
    189                 return img

TypeError: 'int' object is not iterable

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't think you can apply transformation on numpy arrays. Scale (now Resize) works on PIL Image similar to many other transformations.

The source code is really easy to understand look here: https://github.com/pytorch/vision/blob/master/torchvision/transforms.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.