Skip to content
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

Support torch tensors #7

Merged
merged 7 commits into from
Mar 10, 2020
Merged

Conversation

toyama0919
Copy link
Contributor

support pytorch tensor.
(work as same numpy.)

example.

from PIL import Image
import torchvision
from imgcat import imgcat

path = 'evaluation/car1.jpg'
img = Image.open(path)
to_tensor = torchvision.transforms.ToTensor()
imgcat(to_tensor(img))

imgcat/imgcat.py Outdated
im = im.mul(255).byte().squeeze().numpy()
elif im.shape[0] == 3:
mode = None # RGB/RGBA
im = im.mul(255).byte().permute(1, 2, 0).numpy()
Copy link
Owner

@wookayin wookayin Feb 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One should not assume anything about RGB/BGR order and datatype (uint8 or float). But why don't we just do im.numpy()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, You are right.
fix use torchvision.

Copy link
Owner

@wookayin wookayin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me (despite some nits), but we should deal with failing tests before we are able to merge this. + Conventions on commit messages

imgcat/imgcat.py Outdated
@@ -105,7 +105,6 @@ def to_content_buf(data):
else:
raise ValueError("Expected a 3D ndarray (RGB/RGBA image) or 2D (grayscale image), "
"but given shape: {}".format(im.shape))

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

from torchvision import transforms
except ImportError as e:
raise ImportError(e.msg +
"\nTo draw torch tensor, we require torchvision. " +
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix grammar issues here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is grammar issues?

@toyama0919
Copy link
Contributor Author

hmm.. python3.4 is not support.
Do you have a good idea?

image

@@ -39,6 +41,10 @@ def read_version():
else: # <= Python 3.4
tests_requires += ['matplotlib<3.0', 'Pillow<6.0']

# pytorch: python 2.7 require future
if sys.version_info < (3, 0):
tests_requires += ['future']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate on why future is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python2.7 import error.
No module named builtins
for avoid this, Install future.

see.
https://discuss.pytorch.org/t/building-pytorch-from-source-in-conda-fails-in-pytorch-caffe2-operators-crash-op-cc/42859/3
hyperopt/hyperopt#273

root@1f2b2ea83553:~# pip install torch
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting torch
  Downloading torch-1.4.0-cp27-cp27mu-manylinux1_x86_64.whl (753.4 MB)
     |████████████████████████████████| 753.4 MB 2.5 kB/s
Installing collected packages: torch
Successfully installed torch-1.4.0
root@1f2b2ea83553:~# python
Python 2.7.17 (default, Feb 26 2020, 17:18:08)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/torch/__init__.py", line 19, in <module>
    from ._six import string_classes as _string_classes
  File "/usr/local/lib/python2.7/site-packages/torch/_six.py", line 23, in <module>
    import builtins
ImportError: No module named builtins

@wookayin
Copy link
Owner

wookayin commented Mar 8, 2020

This works for me (numpy):

❯❯❯ python -c 'import imgcat; from skimage.data import chelsea; imgcat.imgcat(chelsea())'

But this does not (torch), not showing any images:

❯❯❯ python -c 'import imgcat, torch; from skimage.data import chelsea; imgcat.imgcat(torch.tensor(chelsea()))'

Any idea?

@wookayin
Copy link
Owner

wookayin commented Mar 8, 2020

Well, seems torchvision has a critical bug. Its data spec is CHW, not HWC.

>>> a = torch.tensor(skimage.data.chelsea())
>>> a.shape
torch.Size([300, 451, 3])

>>> transforms.ToPILImage()(a)
<PIL.Image.Image image mode=RGB size=3x451 at 0x12F0C57D0>

ToPILImage():

Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape
    H x W x C to a PIL Image while preserving the value range.

@wookayin
Copy link
Owner

wookayin commented Mar 8, 2020

Sorry, I now realize that torch always use CHW convention, so it's not imgcat's fault and we may not need to convert them.

from PIL import Image
import torchvision.transforms.functional as TF
image = Image.open('screenshot.png')
x = TF.to_tensor(image)    # [3, H, W]
imgcat(x)

and it works.

@toyama0919
Copy link
Contributor Author

#7 (comment)
What about this problem?
(Though the part of the torch can skip 3.4 etc..

Copy link
Owner

@wookayin wookayin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some commits to disable pytorch tests failing on Python 3.4. Thanks!

@wookayin wookayin changed the title support torch tensor. Support torch tensors Mar 10, 2020
@wookayin wookayin merged commit 04c5f2e into wookayin:master Mar 10, 2020
@toyama0919
Copy link
Contributor Author

Please also release to pypi 🙏

@wookayin
Copy link
Owner

@toyama0919 Released v0.5.0 to pypi.

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

Successfully merging this pull request may close these issues.

2 participants