DocumentationAPI Reference
Back to ConsoleLog In

How Do I Upload My PyTorch Model into Deci?

The following describes how to upload a PyTorch model into Deci by exporting/converting it into ONNX 1.8.0 format (a .onnx file that supports inferencing with dynamic batch sizes).

After you have converted the PyTorch model into ONNX format, you can continue following the instructions in Step 3 – Adding a Model in order to upload this new ONNX model file into Deci (in the same way that you would if it was an original ONNX file).

A PyTorch model can be exported either using tracing or scripting. The following is an example of exporting a PyTorch model using tracing.

To export a PyTorch model to ONNX format using tracing –

Call the torch.onnx.export() function. This function will execute the model and record a trace of the operators that are used to compute the outputs. The following is an example –

import onnx
import torch
# Input to the model 
x = torch.randn(batch_size, 3, 224, 224, requires_grad=False)

# Export the model
torch_model.eval()               # Put model into eval mode
torch.onnx.export(torch_model,               # Model being run
                  x,                         # Model input (or a tuple for multiple inputs)
                  "converted_model.onnx",    # Where to save the model (can be a file or file-like object)
                  export_params=True,        # Store the trained parameter weights inside the model file
                  opset_version=10,          # The ONNX version to export the model to
                  do_constant_folding=True,  # Whether to execute constant folding for optimization
                  input_names = ['input'],   # The model's input names
                  output_names = ['output'], # The model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # Variable                                                                          length axes
                                'output' : {0 : 'batch_size'}})

onnx_model = onnx.load("converted_model.onnx")

If the conversion is not successful, then change the opset_version value to 9, 11 or 12. If the code example is still not successful, then contact Deci support.

Did this page help you?