Skip to main content

Visualizing pytorch tensor in one line of code.

Project description

TorchShow

Torchshow visualizes your data in one line of code. It is designed to help debugging Computer Vision project.

Torchshow automatically infers the type of a tensor such as RGB images, grayscale images, binary masks, categorical masks (automatically apply color palette), etc. and perform necessary unnormalization if needed.

Supported Type:

  • RGB Images
  • Grayscale Images
  • Binary Mask
  • Categorical Mask (Integer Labels)
  • Multiple Images
  • Videos
  • Multiple Videos
  • Optical Flows (powered by flow_vis)

Changelogs

  • [2021-08-23] v0.3.2 Adding ts.save(tensor) API for saving figs instead of showing them. This is more convenient compared to the headless mode. Remove white spaces of the saved figures. ts.headless() is now removed.
  • [2021-06-14] v0.3.1 Fixes some bugs. Now support headless mode useful for running on server without display. After setting ts.headless(True), calling ts.show(tensor) will save the figure under ./_torchshow/.
  • [2021-04-25] v0.3.0 Adding optical flow support.

Installation

pip install torchshow

Basic Usage

The usage of TorchShow is extremely simple. Simply import the package and visualize your data in one line:

import torchshow as ts
ts.show(tensor)

If you work on a headless server without display. You can use ts.save(tensor) command (since version 0.3.2).

import torchshow as ts
ts.save(tensor) # Figure will be saved under ./_torchshow/***.png
ts.save(tensor, './vis/test.jpg') # You can specify the save path.

Examples

Table of Contents

1. Visualizing Image Tensor

Visualizing an image-like tensor is not difficult but could be very tedious. You usually need to convert the tensor to numpy array with proper shapes. In many cases images were normalized during dataloader, which means that you have to unnormalize it so it can be displayed correctly.

If you need to frequently verify how your image tensors look like, TorchShow is a very helpful tool.

Using Matplotlib Using TorchShow
The image tensor has been normalized so Matlotlib cannot display it correctly. TorchShow does the conversion automatically.

2. Visualizing Mask Tensors

For projects related to Semantic Segmentation or Instance Segmentation, we often need to visualize mask tensors -- either ground truth annotations or model's prediction. This can be easily done using TorchShow.

Using Matplotlib Using TorchShow
Different instances have same colors. Some categories are missing. TorchShow automatically apply color palletes during visualization.

3. Visualizing Batch of Tensors

When the tensor is a batch of images, TorchShow will automatically create grid layout to visualize them. It is also possible to manually control the number of rows and columns.

4. Visualizing Feature Maps

If the input tensor has more than 3 channels, TorchShow will visualize each of the channel similar to batch visualization. This is useful to visualize a feature map.

5. Visualizing Multiple Tensors with Custom Layout.

TorchShow has more flexibility to visualize multiple tensor using a custom layout.

To control the layout, put the tensors in list of list as an 2D array. The following example will create a 2 x 3 grid layout.

ts.show([[tensor1, tensor2, tensor3],
         [tensor4, tensor5, tensor6]])

It is worth mentioning that it is necessary to fill up all the places in the grid. The following example visualizes 5 tensors in a 2 x 3 grid layout.

ts.show([[tensor1, tensor2],
         [tensor3, tensor4, tensor5]])

6. Visualizing Tensors as Video Clip

Tensors can be visualized as video clips, which very helpful if the tensor is a sequence of frames. This can be done using show_video function.

ts.show_video(video_tensor)

It is also possible to visualize multiple videos in a custom grid layout.

7. Display Video Animation in Jupyter Notebook

TorchShow visualizes video clips as an matplotlib.func_animation object and may not display in a notebook by default. The following example shows a simple trick to display it.

import torchshow as ts
from IPython.display import HTML

ani = ts.show_video(video_tensor)
HTML(ani.to_jshtml())

8. Set Inline Display in Notebook

By default the ts.show() will call plt.show() to display the image. When using notebook environment, this step may not be needed. Running the following code will tell TorchShow to not run plt.show().

import torchshow as ts
ts.use_inline(True)

9. Change Channel Order (RGB/BGR)

By default tensorflow visualize image tensor in the RGB mode, you can switch the setting to BGR in case you are using opencv to load the image.

ts.set_color_mode('bgr)

10. Change Unnormalization Presets

The image tensor may have been preprocessed with a normalization function. If not specified, torchshow will automatically rescale it to 0-1.

To change the preset to imagenet normalization. Use the following code.

ts.show(tensor, unnormalize='imagenet')

To use a customize mean and std value, use the following command.

ts.set_image_mean([0., 0., 0.])
ts.set_image_std([1., 1., 1.])

Note that once this is set, torchshow will use this value for the following visualization. This is useful because usually only a single normalization preset will be used for the entire project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchshow-0.3.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

torchshow-0.3.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file torchshow-0.3.2.tar.gz.

File metadata

  • Download URL: torchshow-0.3.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for torchshow-0.3.2.tar.gz
Algorithm Hash digest
SHA256 7147a72e52ea7ae0e806688e4ff527abce4504d34241b89164573b6c2648fa66
MD5 3b98f26ba70490f6ea5b5188fc12687b
BLAKE2b-256 46fd3827728e4cecfe8ecc95fbf2475da892749ef7d58a57e2cc5d03fa5667d0

See more details on using hashes here.

File details

Details for the file torchshow-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: torchshow-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for torchshow-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 157f4c40f9e824ab8707f03c5e8430df5ad2bdcca204972931d7f214249c3cd3
MD5 c7f194d200395325358ddf41126d008e
BLAKE2b-256 071cf5dca1eab15379211e7cc38efc5d1b6b8664b2612ba64ed9a0845c5e80a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page