Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix onnx Log10 export error #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

atgdms
Copy link

@atgdms atgdms commented May 27, 2021

When trying to export to the ONNX format some NN (as those of the https://github.com/qiuqiangkong/audioset_tagging_cnn examples in readme.md) that uses the LogmelFilterBank, torch.onnx.export gives

RuntimeError: Exporting the operator log10 to ONNX opset version 9 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

This can be easily fixed by simply doing log10(x) = np.log(x)/np.log(10.0)

@phipag
Copy link

phipag commented Jun 7, 2021

I also need this. Would be nice to have!

@WuTUT
Copy link

WuTUT commented Dec 15, 2021

I have the same problem. It's very useful!

@phipag
Copy link

phipag commented Dec 15, 2021

Hey @WuTUT,

I worked this around by doing this:

melspec_extractor = nn.Sequential(
    tlibrosa.Spectrogram(
        hop_length=512,
        win_length=2048
    ), 
    tlibrosa.LogmelFilterBank(
        sr=sample_rate,
        n_mels=128,
        is_log=False, # log10 is not supported by ONNX
    ))

# Extract the mel spectrogram with torchlibrosa
melspec = melspec_extractor(waveform)
# This is the librosa.power_to_db function implemented with torch log2 only operations
log_melspec = 10.0 * torch.log(torch.clamp(melspec, min=1e-10)) / torch.log(torch.tensor(10.0))
log_melspec -= 10.0 * torch.log(torch.max(torch.tensor(1e-10), melspec.max())) / torch.log(torch.tensor(10.0))
log_melspec = torch.max(log_melspec, log_melspec.max() - torch.tensor(80.0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants