ubicomplab/rPPG-Toolbox

pth to onnx error:Exporting the operator 'aten::diff' to ONNX opset version 12 is not supported.

408550969 opened this issue · 2 comments

Hi,I want to convert pth to onnx, but an error occurred during the conversion: Exporting the operator 'aten:: diff' to ONNX opset version 12 is not supported.
Here is my conversion code:

import torch
import onnx
from EfficientPhys import EfficientPhys
def pth_to_onnx(input_path,output_path):
    torch_model = EfficientPhys(frame_depth=10, img_size=72)  
    state_dict  = torch.load(input_path, map_location=torch.device('cpu'))
    new_state_dict = {}
    for key, value in state_dict.items():
        if key.startswith("module."):
            new_key = key[len("module."):]  
            new_state_dict[new_key] = value
        else:
            new_state_dict[key] = value
    torch_model.load_state_dict(new_state_dict)
    
    torch_model.eval()
    x = torch.randn(11,3,72,72)          # N, D, C, H, W
    export_onnx_file = output_path         
    torch.onnx.export(torch_model,
                      x,
                      export_onnx_file,
                      opset_version=12,   
                      input_names=["input"],       
                      output_names=["output"],      
                      dynamic_axes={"input": {0: "batch_size"},         
                                    "output": {0: "batch_size"}}
                      )
def main():
    input_path = "./UBFC_UBFC_PURE_efficientphys_Epoch29.pth"  
    output_path = "./model.onnx"  
    pth_to_onnx(input_path, output_path)

if __name__ == "__main__":
    main()

How to solve this problem?

Should I subtract during the data preprocessing stage rather than during model inference, that is, in the UBFC-rPPG_UBFC-rPPG_PURE_EFFICIENTPHYS.yaml file, change DATA_TYPE: ['Standardized'] to DATA_TYPE: ['DiffNormalized'], and remove inputs = torch.diff(inputs, dim=0) under the forward function in EfficientPhys.py?

Hi,

Are you encountering this issue only with efficientphys or with other models as well (eg. deepphys or tscan)?
The difference blocks in efficient phys are one of the improvements over prior work (eg. the efficientphys model is fully end-to-end). If you are considering applying the diff in preproecssing, I would suggest trying either deephys or tscan.