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
- 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)
Examples
Table of Contents
- Visualizing Image Tensor
- Visualizing Mask Tensors
- Visualizing Batch of Tensors
- Visualizing Channels in Feature Maps
- Visualizing Multiple Tensors with Custom Layout.
- Visualizing Tensors as Video Clip
- Display Video Animation in Jupyter Notebook
- Set Inline Display in Notebook
- Change Channel Order (RGB/BGR)
- Change Unnormalization Presets
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file torchshow-0.3.0.tar.gz
.
File metadata
- Download URL: torchshow-0.3.0.tar.gz
- Upload date:
- Size: 12.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | c14eb78034e0417b5ea1ddd1a71eef546a482f6795970ca9afd780d95f4f3f2c |
|
MD5 | 9ff854cd84cc89ed8b0a1d868e24b318 |
|
BLAKE2b-256 | b4bb96e2540ddd234c30400839e012210bdf7ac938d0f253972b70d5b61b48e6 |
File details
Details for the file torchshow-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: torchshow-0.3.0-py3-none-any.whl
- Upload date:
- Size: 12.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45c6c2e26801d1690504d65ba2e64587bf28ecfdbce9023884e2f3785ae658fa |
|
MD5 | e57b0abe2251dab73b4016c75a03d2da |
|
BLAKE2b-256 | 65d50d3fe86a769f21c8418b6bfc3d7a9ccfa2c5b2759ab91e092e35325e5866 |