Skip to content

Error with transfroms.Scale when feeding iterable size #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ntomita opened this issue Jul 14, 2017 · 4 comments
Closed

Error with transfroms.Scale when feeding iterable size #200

ntomita opened this issue Jul 14, 2017 · 4 comments

Comments

@ntomita
Copy link
Contributor

ntomita commented Jul 14, 2017

I'm having a problem with transforms.Scale function, which complains
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
when feeding an iterable size.

It persists even after installed latest packages.
I don't see any problem in a source code, but seems like the if statement gets True for isinstance(self.size, int)

Here's how it fails.
>>> import torch
>>> import collections
>>> from torchvision.transforms import Scale
>>> from PIL import Image

>>> im = Image.open('pos19.jpg')
>>> Scale(size=(512,256))(im)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ntomita/pytorch-env/lib/python2.7/site-packages/torchvision-0.1.8-py2.7.egg/torchvision/transforms.py", line 139, in __call__
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

Now checking each portion in a source.
>>> transform = Scale(size=(512, 224))
>>> isinstance(transform.size, int)
False
>>> (isinstance(transform.size, collections.Iterable) and len(transform.size) == 2)
True
Condition terms look fine, but when calling,
>>> transform(im)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ntomita/pytorch-env/lib/python2.7/site-packages/torchvision-0.1.8-py2.7.egg/torchvision/transforms.py", line 139, in __call__
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
It fails. If I call resize directly,
>>> im.resize(transform.size, transform.interpolation)
<PIL.Image.Image image mode=L size=512x224 at 0x7F7F8C6D4F50>
works.

@ntomita
Copy link
Contributor Author

ntomita commented Jul 14, 2017

By the way here's the version of torchvision I use
torchvision: 0.1.8-py27_2 soumith
Is this the latest one?

@antspy
Copy link

antspy commented Aug 30, 2017

I have the same issue. I also use 0.1.8, which seems to be the latest version.

The problem is easy to solve, apparently they forgot to add the option of size being a tuple (even if it specified this way). In I believe something like this would solve it:

    def __call__(self, img):
        w, h = img.size
        if (w <= h and w == self.size) or (h <= w and h == self.size):
            return img
        if w < h:
            ow = self.size
            oh = int(self.size * h / w)
            return img.resize((ow, oh), self.interpolation)
        else:
            if isinstance(self.size, tuple): #<--- check if tuple, in which case just use it
                 return img.resize(self.size, self.interpolation)
            oh = self.size
            ow = int(self.size * w / h)
            return img.resize((ow, oh), self.interpolation)

@ntomita
Copy link
Contributor Author

ntomita commented Aug 31, 2017

I see, I just avoid using the function and directly call resize() for now.

def _scale(size):
    def resize(im):
        return im.resize(size, Image.BILINEAR)
    return resize

@alykhantejani
Copy link
Contributor

Hi @antspy this has been fixed in this PR. However, a new version of torchvision has not been released. If you install from source you will have this fix.

cc @fmassa I think we can close this

@fmassa fmassa closed this as completed Sep 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants