-
Notifications
You must be signed in to change notification settings - Fork 21
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
Conversation
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() |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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.
There was a problem hiding this 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)) | |||
|
There was a problem hiding this comment.
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. " + |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is grammar issues?
@@ -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'] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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? |
Well, seems torchvision has a
ToPILImage():
|
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. |
#7 (comment) |
There was a problem hiding this 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!
Please also release to pypi 🙏 |
@toyama0919 Released v0.5.0 to pypi. |
support pytorch tensor.
(work as same numpy.)
example.