pesi
/

File size: 1,400 Bytes
c4f84a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import argparse
import onnx
from onnx import numpy_helper
import numpy as np

def fix_batch_dimension(input_model_path, output_model_path, batch_size=1):
    # Load the input ONNX model
    model = onnx.load(input_model_path)

    # Iterate through the model's inputs
    for input_tensor in model.graph.input:
        # Get the tensor shape
        tensor_shape = input_tensor.type.tensor_type.shape
        
        # Modify the batch dimension
        if len(tensor_shape.dim) > 0:
            tensor_shape.dim[0].dim_value = batch_size

    # Save the modified model to the output path
    onnx.save(model, output_model_path)
    print(f"Model saved with updated batch size of {batch_size} to {output_model_path}")

if __name__ == "__main__":
    # Setup command line arguments
    parser = argparse.ArgumentParser(description="Fix batch dimension of an ONNX model.")
    parser.add_argument("input_model_path", type=str, help="Path to the input ONNX model.")
    parser.add_argument("output_model_path", type=str, help="Path to save the output ONNX model with fixed batch dimension.")
    parser.add_argument("--batch_size", type=int, default=1, help="Value of batch size to assign (default is 1).")

    # Parse arguments
    args = parser.parse_args()

    # Call the function to fix the batch dimension
    fix_batch_dimension(args.input_model_path, args.output_model_path, args.batch_size)