Export pth (without network code) to onnx


I would like to export pth file (without network code) to onnx, how to do that?

Could you describe that this pth file would contain? Would it be the state_dict or the model or any tensors?

@ptrblck ,
Thank you!
There are 2 cases:

  1. the pth file contains only state_dict,
  2. the pth file contains the model.

how to export those 2 kinds of pth to onnx?

  1. The state_dict contains all parameters and buffers and neither the model definition not any model code, so you can’t directly export it and would need to create a model instance first.
  2. Assuming “contains the model” means a scripted model, you would again need to create the model object first and export it to ONNX as described here.

Thank you!

if the pth file contains only state_dict, is there a way to export the model to onnx?

If the pth file contains the model, I should export the model to onnx like this:

model = torch.load(PATH)
torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)

  1. No, I don’t think so, since the state_dict only contains parameters and buffers and no model definition.
  2. Yes, this might work assuming you can properly load the model.