[Python] ONNX export stops without error and no onnx file is generated

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to export my PyTorch model to ONNX format while ensuring that the batch size remains dynamic. the result always is Segmentation fault (core dumped) Here’s the code I’m using:

    import torch # Import PyTorch library

    # Create a dummy input tensor of shape (1, 3, 256, 256) and move it to the appropriate device
    dummy_input = torch.randn(1, 3, 256, 256).to(device)

    # Create dummy camera and view labels, initialized to zeros, and move them to the device
    dummy_cam = torch.zeros(1, dtype=torch.long).to(device)
    dummy_view = torch.zeros(1, dtype=torch.long).to(device)

    # Export the model to ONNX format
    torch.onnx.export(
    model, # The PyTorch model to be converted
    (dummy_input, dummy_cam, dummy_view), # The input tuple
    "deit_transreid_veri.onnx", # The output file name
    input_names=["input", "cam_label", "view_label"], # Naming the input tensors
    output_names=["output"], # Naming the output tensor
    dynamic_axes={ # Define dynamic batch size for inputs and outputs
    'input': {0: 'batch_size'},
    'cam_label': {0: 'batch_size'},
    'view_label': {0: 'batch_size'},
    'output': {0: 'batch_size'}
    },
    opset_version=13, # Specify the ONNX opset version
    verbose=True, # Print detailed information during export
    )

    print("Model has been successfully exported to ONNX format!") # Confirmation message

    Continue reading...

Compartilhe esta Página