how to export librosa to onnx

Viewed 18

I've implemented a Keras model and as feature extractor i use librosa (melspectogram).

Need to convert part of librosa (or full librosa) like transformer to onnx. Example from documentation didn't work and there are not enough information. Tried use skl2onnx and register custom transformer, dut it doesn't work.

Example:

code:

class FetureExtractor(TransformerMixin, BaseEstimator):
    def __init__(self, X = None, alpha=0):
        self.mult_coeff = 2
        self._mel = np.ones((1,16,8,1))

    def fit(self, X, y = None):
        return self 


    def transform(self, X, sample_rate = 22050):
        # We extract mfcc feature from data
        mels = np.mean(librosa.feature.melspectrogram(y=X, sr=sample_rate).T,axis=0)
        self._mel = mels.reshape(1,16,8,1)

        return self._mel

custom canculator and convertor

def test_feature_extractor_calculator(operator):
    op = operator.raw_operator
    input_type = operator.inputs[0].type.__class__

    input = operator.inputs[0]      # inputs in ONNX graph
    N = input.type.shape[0]
    print(operator.inputs[0].type)

    input_dim = operator.inputs[0].get_first_dimension()
    print(input_dim)
    operator.outputs[0].type = FloatTensorType((1,16,8,1))

def test_feature_extractor_converter(scope, operator, container):
    op = operator.raw_operator
    opv = container.target_opset
    out = operator.outputs
    X = operator.inputs[0]
    print('X {}'.format(X))
    dtype = guess_numpy_type(X.type)

    N = 20        # number of observations
    C = 20   # dimension of outputs
    coef = np.full((1,16,8,1), op.mult_coeff).astype(dtype)
    Y = OnnxMatMul(X,coef, op_version=opv, output_names=out[:1])
    Y.add_to(scope, container)

If need can implement only needed method from librosa.

0 Answers
Related