How to convert Pytorch model (AlignTTS) to ONNX?

Elias_Hossain: <p>I’m converting my model that is saved into .pth to ONNX. The model is AlignTTS (text-to-speech) and it was trained on Bangla data (speech and corresponding transcribe). Here is my script below, can you please advice me what went wrong with my code, as I failed to export Pytorch model to ONNX.</p>
<pre><code class="lang-auto">
args = AlignTTSArgs(num_chars=105, out_channels=80, hidden_channels=256, hidden_channels_dp=256, 
                    encoder_type='fftransformer', encoder_params={"hidden_channels_ffn": 1024, "num_heads": 2, "num_layers": 6, "dropout_p": 0.1},
                    decoder_type='fftransformer', decoder_params={"hidden_channels_ffn": 1024, "num_heads": 2, "num_layers": 6, "dropout_p": 0.1},
                    length_scale=1.0, num_speakers=0, use_speaker_embedding=False, use_d_vector_file=False, d_vector_dim=0)

model = AlignTTS(config=args)

def converting_onnx(checkpoint_path):
    
    checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    
    model.load_state_dict(checkpoint['model'])

    model.eval()

    input_shape = (8, 105, 105)
    output_path = 'align_tts_model.onnx'
    dummy_input = torch.randn(input_shape)
    print(dummy_input)
    y_lengths = torch.LongTensor([105, 102, 98, 101, 100, 103, 97, 104])  # example values for y_lengths
    print(y_lengths)
    torch.onnx.export(model, (dummy_input, y_lengths), output_path, verbose=True)
    

if __name__ == '__main__':
    # Call the function with the checkpoint path
    
    new_checkpoint_path='/home/elias/modified_checkpoint.pth'
    converting_onnx(new_checkpoint_path) ```

Running the above code shows the following error:

  outs.append(self.inner(*trace_inputs))
  File "/home/elias/miniconda3/envs/dev-elias/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/elias/miniconda3/envs/dev-elias/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/elias/miniconda3/envs/dev-elias/lib/python3.10/site-packages/TTS/tts/models/align_tts.py", line 246, in forward
    y = y.transpose(1, 2)
AttributeError: 'dict' object has no attribute 'transpose'

</code></pre>
<p>In addition to that, I have used another approach to convert my model to ONNX:</p>
<pre><code class="lang-auto">def convert(checkpoint_path):
# The model definition comes from the torchvision. The model file generated in the example is based on the ResNet-50 model.
    checkpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))
    # load the state dict into the model
    model.load_state_dict(checkpoint['model'])
 
    batch_size = 8 # Size of the batch processing
    input_shape = (8, 105, 105) # Input data. Replace it with the actual shape.

    # Set the model to inference mode.
    model.eval()

    dummy_input = torch.randn(batch_size, *input_shape) # Define the input shape.
    torch.onnx.export(model, 
                      dummy_input, 
                      "test_model.onnx", 
                      input_names = ["input"], # Construct the input name.
                      output_names = ["output"], # Construct the output name.
                      opset_version=11, # Currently, the ATC tool supports only opset_version=11.
                      dynamic_axes={"input":{0:"batch_size"}, "output":{0:"batch_size"}}) # Dynamic axes of the output is supported.) 
    
</code></pre>
<p>However, none of the solutions have worked for me. Could you please help me in this regard, I checked similar blog and Github issues, but I cannot figure out the reason of failing the exporting to ONNX.</p>