CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-tensorboardx

TensorBoardX lets you watch Tensors Flow without Tensorflow

Overview
Eval results
Files

utilities.mddocs/

Utility Functions

Data conversion and tensor processing utilities for preparing data for TensorBoard visualization. These functions handle format conversion, figure processing, tensor manipulation, and data validation for optimal TensorBoard compatibility.

Capabilities

Figure Conversion

Convert matplotlib figures and plots to image arrays suitable for TensorBoard visualization.

def figure_to_image(figures, close: bool = True):
    """
    Convert matplotlib figure(s) to numpy image array(s).

    Parameters:
    - figures: Matplotlib figure object or list of figures
    - close: Whether to close the figure(s) after conversion (default: True)

    Returns:
    numpy.ndarray: Image array in CHW format (channels, height, width)
                   If input is list, returns list of arrays
    """

Tensor Conversion

Convert various tensor types and formats to numpy arrays with proper shape and data type handling.

def make_np(x):
    """
    Convert various tensor types to numpy arrays.
    Handles PyTorch tensors, numpy arrays, lists, and scalar values.

    Parameters:
    - x: Input data (torch.Tensor, numpy.ndarray, list, or scalar)

    Returns:
    numpy.ndarray: Converted numpy array
    """

def check_nan(array):
    """
    Check for NaN and Inf values in array and handle them.

    Parameters:
    - array: Input numpy array

    Returns:
    numpy.ndarray: Array with NaN/Inf values handled (replaced with 0)
    """

Format Conversion

Convert tensors between different data formats required by TensorBoard visualization components.

def convert_to_HWC(tensor, input_format: str):
    """
    Convert tensor to HWC (Height, Width, Channels) format.

    Parameters:
    - tensor: Input tensor (numpy.ndarray or torch.Tensor)
    - input_format: Current format ('CHW', 'HW', 'NCHW', 'NHWC', etc.)

    Returns:
    numpy.ndarray: Tensor in HWC format
    """

def convert_to_NTCHW(tensor, input_format: str):
    """
    Convert tensor to NTCHW (Batch, Time, Channels, Height, Width) format for videos.

    Parameters:
    - tensor: Input tensor (numpy.ndarray or torch.Tensor)
    - input_format: Current format ('NTHWC', 'NCHW', 'TCHW', etc.)

    Returns:
    numpy.ndarray: Tensor in NTCHW format
    """

Image Grid Creation

Create image grids from batches of images for compact visualization.

def make_grid(I, ncols: int = 8):
    """
    Create a grid of images from a batch of images.

    Parameters:
    - I: Input images tensor/array (N, C, H, W) or (N, H, W)
    - ncols: Number of columns in the grid (default: 8)

    Returns:
    numpy.ndarray: Image grid array
    """

Embedding Utilities

Utilities for creating embedding visualizations with metadata and sprite images.

def make_mat(matlist, save_path: str):
    """
    Create embedding matrix file for TensorBoard projector.

    Parameters:
    - matlist: List of embedding vectors
    - save_path: Path to save the matrix file
    """

def make_sprite(label_img, save_path: str):
    """
    Create sprite image for embedding visualization.

    Parameters:
    - label_img: Images corresponding to embedding points
    - save_path: Path to save the sprite image
    """

def make_tsv(metadata, save_path: str, metadata_header=None):
    """
    Create metadata TSV file for embedding labels.

    Parameters:
    - metadata: List of labels/metadata for each embedding point
    - save_path: Path to save the TSV file
    - metadata_header: Optional header for metadata columns
    """

def append_pbtxt(metadata_path: str, save_path: str):
    """
    Append projector configuration to pbtxt file.

    Parameters:
    - metadata_path: Path to metadata TSV file
    - save_path: Path to save projector config
    """

Usage Examples

Figure to Image Conversion

import matplotlib.pyplot as plt
import numpy as np
from tensorboardX.utils import figure_to_image
from tensorboardX import SummaryWriter

# Create matplotlib figure
fig, ax = plt.subplots(figsize=(8, 6))
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)
ax.set_title('Sine Wave')

# Convert to image array
img_array = figure_to_image(fig, close=True)

# Log to TensorBoard
writer = SummaryWriter()
writer.add_image('Plot', img_array, 0)
writer.close()

Tensor Format Conversion

import torch
import numpy as np
from tensorboardX.utils import convert_to_HWC, make_np

# PyTorch tensor in CHW format
tensor = torch.randn(3, 64, 64)

# Convert to numpy
np_array = make_np(tensor)

# Convert to HWC format for certain visualizations
hwc_array = convert_to_HWC(np_array, 'CHW')

print(f"Original shape: {tensor.shape}")     # torch.Size([3, 64, 64])
print(f"NumPy shape: {np_array.shape}")      # (3, 64, 64)
print(f"HWC shape: {hwc_array.shape}")       # (64, 64, 3)

Video Format Conversion

import numpy as np
from tensorboardX.utils import convert_to_NTCHW
from tensorboardX import SummaryWriter

# Video tensor in NTHWC format (batch, time, height, width, channels)
video = np.random.rand(2, 10, 64, 64, 3)

# Convert to NTCHW format required by TensorBoard
video_ntchw = convert_to_NTCHW(video, 'NTHWC')

# Log video to TensorBoard
writer = SummaryWriter()
writer.add_video('Sample_Video', video_ntchw[0:1], fps=5)  # First video in batch
writer.close()

print(f"Original shape: {video.shape}")        # (2, 10, 64, 64, 3)
print(f"NTCHW shape: {video_ntchw.shape}")     # (2, 10, 3, 64, 64)

Image Grid Creation

import numpy as np
from tensorboardX.utils import make_grid
from tensorboardX import SummaryWriter

# Batch of images (N, C, H, W)
images = np.random.rand(16, 3, 32, 32)

# Create grid with 4 columns
grid = make_grid(images, ncols=4)

# Log grid to TensorBoard
writer = SummaryWriter()
writer.add_image('Image_Grid', grid, 0)
writer.close()

print(f"Original batch shape: {images.shape}")  # (16, 3, 32, 32)
print(f"Grid shape: {grid.shape}")              # Grid dimensions

NaN/Inf Handling

import numpy as np
from tensorboardX.utils import check_nan, make_np

# Array with problematic values
data = np.array([1.0, 2.0, np.nan, np.inf, -np.inf, 5.0])

# Clean the data
clean_data = check_nan(data)

print(f"Original: {data}")      # [1.0, 2.0, nan, inf, -inf, 5.0]
print(f"Cleaned: {clean_data}") # [1.0, 2.0, 0.0, 0.0, 0.0, 5.0]

Embedding Visualization Setup

import numpy as np
from tensorboardX.embedding import make_mat, make_sprite, make_tsv, append_pbtxt
from tensorboardX import SummaryWriter

# Generate embedding data
embeddings = np.random.randn(1000, 128)  # 1000 points, 128 dimensions
labels = [f'class_{i%10}' for i in range(1000)]
images = np.random.rand(1000, 28, 28)  # Corresponding images

# Save embedding files
make_mat(embeddings, 'embeddings.tsv')
make_tsv(labels, 'metadata.tsv')
make_sprite(images, 'sprite.png')
append_pbtxt('metadata.tsv', 'projector_config.pbtxt')

# Add to TensorBoard
writer = SummaryWriter('logs/embeddings')
writer.add_embedding(
    embeddings,
    metadata=labels,
    label_img=images,
    tag='MNIST_Embeddings'
)
writer.close()

Batch Processing with Utilities

import numpy as np
from tensorboardX.utils import make_np, convert_to_HWC, figure_to_image
from tensorboardX import SummaryWriter
import matplotlib.pyplot as plt

def process_batch_for_tensorboard(data_batch, format='CHW'):
    """Process a batch of data for TensorBoard logging."""
    processed = []
    
    for item in data_batch:
        # Convert to numpy
        np_item = make_np(item)
        
        # Convert format if needed
        if format == 'HWC':
            np_item = convert_to_HWC(np_item, 'CHW')
        
        processed.append(np_item)
    
    return processed

# Example usage
batch = [np.random.rand(3, 64, 64) for _ in range(5)]
processed_batch = process_batch_for_tensorboard(batch, format='HWC')

writer = SummaryWriter()
for i, img in enumerate(processed_batch):
    writer.add_image(f'Processed_Image_{i}', img, 0, dataformats='HWC')
writer.close()

Type Compatibility

The utility functions handle multiple input types:

Tensor Types

  • PyTorch tensors: torch.Tensor (CPU and CUDA)
  • NumPy arrays: numpy.ndarray
  • Lists: Python lists and nested lists
  • Scalars: Python floats, integers

Format Strings

  • Image formats: 'CHW', 'HWC', 'HW'
  • Batch formats: 'NCHW', 'NHWC', 'NHW'
  • Video formats: 'NTCHW', 'NTHWC', 'TCHW', 'THWC'

Data Type Handling

  • Automatic dtype conversion for TensorBoard compatibility
  • Normalization for image data (0-1 or 0-255 ranges)
  • Proper handling of edge cases (empty arrays, single pixels, etc.)

Performance Notes

  • Memory efficiency: Functions avoid unnecessary copying when possible
  • Format detection: Automatic format detection for common tensor shapes
  • Batch processing: Optimized for processing multiple items efficiently
  • Error handling: Graceful handling of malformed input data

Install with Tessl CLI

npx tessl i tessl/pypi-tensorboardx

docs

file-writer.md

global-writer.md

index.md

record-writer.md

summary-writer.md

torchvis.md

utilities.md

tile.json