Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_scripted_tensor_transforms.ipynb>`_
Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_scripted_tensor_transforms.ipynb>`_
or :ref:`go to the end <sphx_glr_download_auto_examples_others_plot_scripted_tensor_transforms.py>` to download the full example code.
or :ref:`go to the end <sphx_glr_download_auto_examples_others_plot_scripted_tensor_transforms.py>` to download the full example code.
This example illustrates various features that are now supported by the
This example illustrates `torchscript
:ref:`image transformations <transforms>` on Tensor images. In particular, we
<https://pytorch.org/docs/stable/jit.html>`_ support of the torchvision
show how image transforms can be performed on GPU, and how one can also script
:ref:`transforms <transforms>` on Tensor images.
them using JIT compilation.
Prior to v0.8.0, transforms in torchvision have traditionally been PIL-centric
and presented multiple limitations due to that. Now, since v0.8.0, transforms
implementations are Tensor and PIL compatible, and we can achieve the following
new features:
- transform multi-band torch tensor images (with more than 3-4 channels)
- torchscript transforms together with your model for deployment
- support for GPU acceleration
- batched transformation such as for videos
- read and decode data directly as torch tensor with torchscript support (for PNG and JPEG image formats)
.. note::
These features are only possible with **Tensor** images.
"""
"""
# %%
frompathlibimportPath
frompathlibimportPath
importmatplotlib.pyplotasplt
importmatplotlib.pyplotasplt
importnumpyasnp
importtorch
importtorch
importtorchvision.transformsasT
importtorch.nnasnn
fromtorchvision.ioimportread_image
importtorchvision.transformsasv1
fromtorchvision.ioimportread_image
plt.rcParams["savefig.bbox"]='tight'
plt.rcParams["savefig.bbox"]='tight'
torch.manual_seed(1)
torch.manual_seed(1)
# If you're trying to run that on collab, you can download the assets and the
defshow(imgs):
# helpers from https://github.com/pytorch/vision/tree/main/gallery/