A tool to fix shape information in ONNX models
Project description
ONNX Shape Fix
A Python package for fixing shape information in ONNX models, with special support for ParticleTransformer models.
Features
- Propagate shape information through an ONNX model graph
- Track batch dimension movement throughout the model
- Optionally remove batch dimensions from model shapes for deployment to batch-less platforms
- Special support for ParticleTransformer models
- Command-line interface for easy use
- Python API for integration with other tools
Installation
You can install the package directly from GitHub:
from PyPI (once published):
pip install onnx-shape-fix
Command-line Usage
# Basic usage
onnx-shape-fix model.onnx --input-shapes "input_1:[1,3,224,224];input_2:[1,10]"
# Using JSON file for input shapes
onnx-shape-fix model.onnx --input-shapes-json shapes.json
# Remove batch dimension
onnx-shape-fix model.onnx --input-shapes "input:[1,3,224,224]" --remove-batch
# Special handling for ParticleTransformer models
onnx-shape-fix model.onnx --input-shapes "points:[32,128,4];features:[32,128,8]" --particle-transformer
# Verbose output
onnx-shape-fix model.onnx --input-shapes "input:[1,3,224,224]" --verbose
Example Scripts
The package includes example scripts to demonstrate its usage:
Generic ONNX Model Example
# Run the generic example script
python examples/generic_example.py model.onnx -i "input:[1,3,224,224]" -v
ParticleTransformer Model Example
# Run the ParticleTransformer example script (uses pre-defined input shapes)
python examples/particle_transformer_example.py model.onnx -r -v
Both example scripts demonstrate:
- Loading an ONNX model
- Propagating shapes through the model
- Optionally removing batch dimensions
- Saving the model with fixed shapes
Python API
from onnx_shape_fix import ShapePropagator
# Basic usage with method chaining
ShapePropagator(
model_path="model.onnx",
input_shapes={"input": [1, 3, 224, 224]},
verbose=True
).propagate().save_model_with_shapes("model_fixed.onnx")
# More detailed usage
propagator = ShapePropagator(
model_path="model.onnx",
input_shapes={"input": [1, 3, 224, 224]},
verbose=True,
track_batch=True
)
# Propagate shapes through the model
propagator.propagate()
# Access the shape dictionary
shapes = propagator.shape_dict
print(f"Shape of output tensor: {shapes['output']}")
# Save model with fixed shapes
propagator.save_model_with_shapes("model_fixed.onnx", remove_batch=False)
# For ParticleTransformer models
from onnx_shape_fix import ParticleTransformerShapePropagator
# Method chaining for cleaner code
ParticleTransformerShapePropagator(
model_path="particle_transformer.onnx",
input_shapes={
"pf_features": [1, 7, 128],
"pf_vectors": [1, 4, 128],
"pf_mask": [1, 1, 128]
},
verbose=True
).propagate().save_model_with_shapes("pt_fixed.onnx", remove_batch=True)
Batch Dimension Tracking and Removal
The package automatically tracks the batch dimension throughout the model graph, allowing for:
- Understanding where the batch dimension is located for each tensor
- Removing batch dimensions for deployment to batch-less platforms
- Handling operations that reorder dimensions (like Transpose or Reshape)
from onnx_shape_fix import ShapePropagator
# Create propagator and run shape propagation
propagator = ShapePropagator(
model_path="model.onnx",
input_shapes={"input": [1, 3, 224, 224]},
track_batch=True,
verbose=True
).propagate()
# Get batch dimension index for a tensor
tensor_name = "some_intermediate_tensor"
batch_idx = propagator.batch_tracker.get_batch_dim(tensor_name)
print(f"Batch dimension for {tensor_name} is at index {batch_idx}")
# Save model with batch dimensions removed
propagator.save_model_with_shapes("model_batchless.onnx", remove_batch=True)
How Batch Removal Works
When remove_batch=True, the package:
- Identifies the batch dimension for each tensor
- Removes that dimension from the tensor shape
- Updates all shape information in the ONNX model
This is particularly useful for:
- Deploying models to hardware accelerators that don't support batching
- Simplifying models for single-sample inference
- Fixing shapes in models where the batch dimension is dynamic but hardware requires fixed dimensions
License
MIT
Extending the Package
Adding Custom Operation Handlers
You can extend the package to support custom ONNX operations by creating your own handlers:
from onnx_shape_fix.handlers import BaseHandler, register_handler
from typing import List, Optional
from onnx import NodeProto
# Create a custom handler for your operation
class MyCustomOpHandler(BaseHandler):
def __init__(self, verbose: bool = False):
super().__init__(verbose)
def handle_node(self, node: NodeProto, input_shapes: List[List[int]], propagator) -> Optional[List[int]]:
# Implement your shape handling logic here
# Return the output shape as a list of integers
return input_shapes[0] # Example: pass-through shape
# Register your handler
register_handler("MyCustomOp", MyCustomOpHandler)
# Now use the propagator as usual
from onnx_shape_fix import ShapePropagator
ShapePropagator(
model_path="model_with_custom_ops.onnx",
input_shapes={"input": [1, 3, 224, 224]},
verbose=True
).propagate().save_model_with_shapes("fixed_model.onnx")
Contributing
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch
- Add your feature or fix
- Add tests for your changes
- Submit a pull request
When adding support for new operations, place them in the appropriate handler file based on operation type:
elementwise.py- Element-wise operations (Add, Mul, etc.)shape_ops.py- Shape manipulation operations (Reshape, Transpose, etc.)math_ops.py- Mathematical operations (MatMul, Conv, etc.)nn_ops.py- Neural network operations (BatchNormalization, Dropout, etc.)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file onnx_shape_fix-0.2.1.tar.gz.
File metadata
- Download URL: onnx_shape_fix-0.2.1.tar.gz
- Upload date:
- Size: 55.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0356774e00240f3259f69383ee8255a8d28f10d81cdd1e83a4578230ca5de1fa
|
|
| MD5 |
aeea36dd3edf978ea5c34ad6898aa8f2
|
|
| BLAKE2b-256 |
02a3955e6e308913ef2c494ca1d99a8ceb9328cfdc44cf4543bdf36ab7d1c209
|
File details
Details for the file onnx_shape_fix-0.2.1-py3-none-any.whl.
File metadata
- Download URL: onnx_shape_fix-0.2.1-py3-none-any.whl
- Upload date:
- Size: 61.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54c02ce8d3cf6b542995f16f7d3ace314a5e8c30de117e14a158d423462900c0
|
|
| MD5 |
3852c7eff0f5bdbfc589fb0de376ab6c
|
|
| BLAKE2b-256 |
825a4a589ea644c5bdf28bf6f96af098b13d07fcc42bc9171454472702982504
|