dart_tensor_preprocessing library

A high-performance tensor preprocessing library for Flutter/Dart.

This library provides NumPy-like tensor transforms optimized for ONNX Runtime inference. Key features include:

Getting Started

import 'package:dart_tensor_preprocessing/dart_tensor_preprocessing.dart';

// Create a preprocessing pipeline
final pipeline = TensorPipeline([
  ResizeOp(height: 224, width: 224),
  NormalizeOp.imagenet(),
  PermuteOp.hwcToChw(),
]);

// Run synchronously
final result = pipeline.run(inputTensor);

// Or run asynchronously in an isolate
final result = await pipeline.runAsync(inputTensor);

See PipelinePresets for pre-configured pipelines for common models.

Classes

AbsOp
Computes the absolute value of each element.
AcosOp
Computes the arccosine (inverse cosine) of each element.
AddOp
Adds a scalar or tensor to the input element-wise.
AdjustBrightnessOp
Adjusts the brightness of an image tensor by an additive factor.
AdjustContrastOp
Adjusts the contrast of an image tensor.
AdjustHueOp
Adjusts the hue of an image tensor using RGB-to-HSV conversion.
AdjustSaturationOp
Adjusts the saturation of an image tensor using RGB-to-HSV conversion.
ArithmeticOp
Base class for binary arithmetic operations.
AsinOp
Computes the arcsine (inverse sine) of each element.
Atan2Op
Computes the element-wise arctangent of y/x, considering the signs of both arguments to determine the correct quadrant.
AtanOp
Computes the arctangent (inverse tangent) of each element.
BatchNormOp
Batch Normalization for inference.
BufferPool
A pool of reusable TypedData buffers for memory optimization.
CeilOp
Rounds each element up to the nearest integer (towards positive infinity).
CenterCropOp
Crops a tensor from the center to the specified dimensions.
ClipOp
Clips tensor values to a specified range.
ColorJitterOp
Randomly adjusts brightness, contrast, saturation, and hue of image tensors.
ContiguousOp
Forces a tensor to be contiguous in memory.
CosOp
Computes the cosine of each element (in radians).
DivOp
Divides the input by a scalar or tensor element-wise.
DTypeDispatcher
Utility for dispatching operations based on tensor dtype.
ELUOp
Exponential Linear Unit (ELU) activation function.
ErrorMessages
Centralized error message formatting for tensor operations.
ExpOp
Computes the exponential (e^x) of each element.
FlattenOp
Flattens dimensions in a specified range into a single dimension.
FloorOp
Rounds each element down to the nearest integer (towards negative infinity).
GatherOp
Gathers values along an axis specified by dim.
GaussianBlurOp
Applies Gaussian blur to tensor for data augmentation.
GELUOp
Gaussian Error Linear Unit (GELU) activation function.
GLUOp
Gated Linear Unit (GLU) activation function.
GroupNormOp
Group normalization operation.
HardsigmoidOp
Hard Sigmoid activation function.
HardswishOp
Hard Swish activation function.
HorizontalFlipOp
Deterministically flips a tensor left-to-right (horizontal flip).
HsvToRgbOp
Converts an HSV tensor to RGB color space.
IdentityOp
A no-op transform that returns the input unchanged.
InstanceNormOp
Instance Normalization for inference.
LayerNormOp
Layer Normalization for inference.
LayoutConvertOp
Converts a tensor between memory layout formats (NCHW/NHWC).
LeakyReLUOp
Leaky Rectified Linear Unit activation function.
LogOp
Computes the natural logarithm of each element.
LpNormalizeOp
Lp normalization along a specified dimension.
MaskedFillOp
Fills elements of the tensor where mask is non-zero with a given value.
MishOp
Mish activation function.
MulOp
Multiplies the input by a scalar or tensor element-wise.
NegOp
Negates each element.
NormalizeOp
Normalizes tensor values per channel using mean and standard deviation.
OperationCapabilities
Describes the capabilities and characteristics of a tensor operation.
PadOp
Pads tensor with specified padding on spatial dimensions.
PermuteOp
Permutes the dimensions of a tensor according to dims.
PipelinePresets
Pre-configured preprocessing pipelines for common ML models.
PositionalEncodingOp
Adds sinusoidal positional encodings to the input tensor.
PowOp
Raises each element to the given power.
RandomCropOp
Randomly crops tensor to specified dimensions for data augmentation.
RandomErasingOp
Randomly erases a rectangular region of a tensor image for data augmentation.
RandomHorizontalFlipOp
Randomly flips a tensor left-to-right with configurable probability.
RandomVerticalFlipOp
Randomly flips a tensor top-to-bottom with configurable probability.
ReLUOp
Rectified Linear Unit (ReLU) activation function.
RepeatOp
Repeats a tensor along each dimension.
ReshapeOp
Reshapes a tensor to the specified shape.
ResizeNormalizeFusedOp
Fused resize + normalize operation that eliminates the intermediate tensor.
ResizeOp
Resizes a tensor to a fixed height and width.
ResizeShortestOp
Resizes a tensor so that the shortest edge matches shortestEdge.
RgbToGrayscaleOp
Converts an RGB tensor to grayscale using ITU-R BT.601 weighted coefficients.
RgbToHsvOp
Converts an RGB tensor to HSV color space.
RMSNormOp
Root Mean Square Normalization for inference.
RollOp
Rolls a tensor along the given dimensions.
RoundOp
Rounds each element to the nearest integer (half away from zero).
ScaleOp
Scales tensor values by a constant factor.
SELUOp
Scaled Exponential Linear Unit (SELU) activation function.
SigmoidOp
Sigmoid activation function.
SiLUOp
Sigmoid Linear Unit (SiLU / Swish) activation function.
SimdOps
SIMD-accelerated tensor operations.
SinOp
Computes the sine of each element (in radians).
SliceOp
Slices tensor with Python-like syntax.
SoftmaxOp
Softmax activation function.
SqrtOp
Computes the square root of each element.
SqueezeOp
Removes size-1 dimensions from a tensor.
SubOp
Subtracts a scalar or tensor from the input element-wise.
TanhOp
Hyperbolic tangent (Tanh) activation function.
TanOp
Computes the tangent of each element (in radians).
TensorBuffer
A multi-dimensional array view over typed data with shape and stride metadata.
TensorIndexer
Utilities for tensor index calculations.
TensorPipeline
A composable sequence of tensor transform operations.
TensorStorage
An immutable wrapper around typed data that provides the physical storage for TensorBuffer.
TileOp
Tiles a tensor by repeating it along each dimension.
ToImageOp
Converts a tensor from CHW/NCHW to HWC/NHWC image format.
TopKOp
Selects the k largest or smallest values and their indices along an axis.
ToTensorOp
Converts an image tensor from HWC/NHWC to CHW/NCHW format.
TransformOp
Base class for all tensor transform operations.
TypeCastOp
Casts tensor element values to a different data type.
TypedDataViews
Utilities for working with TypedData views and zero-copy operations.
UnaryMathOp
Base class for unary math operations.
UnsqueezeOp
Inserts a size-1 dimension at the specified position.
VerticalFlipOp
Deterministically flips a tensor top-to-bottom (vertical flip).
WhereOp
TransformOp form of element-wise where.

Enums

CoordinateTransformMode
ONNX-compatible coordinate transformation modes for resize operations.
DType
Represents the data type of tensor elements.
InterpolationMode
Interpolation algorithm used for image resizing.
MemoryFormat
Defines the memory layout format for tensor data.
PadMode
Padding modes for PadOp.

Mixins

InPlaceTransform
Mixin for transforms that can modify tensors in place.
RequiresContiguous
Mixin for transforms that require contiguous tensor input.

Extensions

BufferPoolExtension on BufferPool
Extension to add pool-aware operations to TensorBuffer.
MemoryFormatExtension on MemoryFormat
Extension providing utility methods for MemoryFormat.
TensorBufferFactory on TensorBuffer
Extension on TensorBuffer for factory methods.
TensorBufferReduce on TensorBuffer
Extension providing reduction operations for TensorBuffer.
TensorViewExtension on TensorBuffer
Extension methods for creating tensor views efficiently.
TopKExtension on TensorBuffer
Provides a convenient topk method on TensorBuffer.

Functions

chunk(TensorBuffer tensor, int chunks, {int dim = 0}) List<TensorBuffer>
Splits a tensor into a specified number of chunks.
concat(List<TensorBuffer> tensors, {int axis = 0}) TensorBuffer
Concatenates multiple tensors along a specified axis.
hsvToRgb(double h, double s, double v) → (double, double, double)
Converts HSV values to RGB.
rgbToHsv(double r, double g, double b) → (double, double, double)
Converts RGB values to HSV.
split(TensorBuffer tensor, List<int> splitSizes, {int dim = 0}) List<TensorBuffer>
Splits a tensor into multiple sub-tensors along a dimension.
stack(List<TensorBuffer> tensors, {int dim = 0}) TensorBuffer
Stacks a sequence of tensors along a new dimension.
tensorWhere(TensorBuffer condition, TensorBuffer x, TensorBuffer y) TensorBuffer
Element-wise conditional selection between two tensors.

Typedefs

SwishOp = SiLUOp
Alias for SiLUOp - Swish activation function.
TopKResult = (TensorBuffer, TensorBuffer)
The result of a TopK operation: (values, indices).

Exceptions / Errors

DTypeMismatchException
Thrown when tensor data types do not match.
EmptyPipelineException
Thrown when attempting to create an empty pipeline.
IndexOutOfBoundsException
Thrown when an index or axis is out of valid range.
InvalidParameterException
Thrown when a transform operation receives an invalid parameter value.
NonContiguousException
Thrown when an operation requires a contiguous tensor but receives a non-contiguous one.
ShapeMismatchException
Thrown when tensor shapes do not match expected dimensions.
TensorException
Base class for all tensor-related exceptions.