torchlibrosa
torchlibrosa copied to clipboard
Fix onnx Log10 export error
trafficstars
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)
I also need this. Would be nice to have!
I have the same problem. It's very useful!
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))