|
@ -11,19 +11,34 @@ |
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
# See the License for the specific language governing permissions and |
|
|
# See the License for the specific language governing permissions and |
|
|
# limitations under the License. |
|
|
# limitations under the License. |
|
|
|
|
|
# Copyright 2021 Zilliz. All rights reserved. |
|
|
|
|
|
# |
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
|
# you may not use this file except in compliance with the License. |
|
|
|
|
|
# You may obtain a copy of the License at |
|
|
|
|
|
# |
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
|
|
|
|
# |
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
|
|
|
# See the License for the specific language governing permissions and |
|
|
|
|
|
# limitations under the License. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import os |
|
|
import sys |
|
|
import sys |
|
|
import logging |
|
|
import logging |
|
|
from pathlib import Path |
|
|
from pathlib import Path |
|
|
from typing import Union |
|
|
|
|
|
|
|
|
from typing import List |
|
|
|
|
|
|
|
|
import torchaudio |
|
|
|
|
|
|
|
|
import resampy |
|
|
import torch |
|
|
import torch |
|
|
import numpy |
|
|
import numpy |
|
|
|
|
|
|
|
|
from towhee.operator import NNOperator |
|
|
from towhee.operator import NNOperator |
|
|
from towhee import register |
|
|
from towhee import register |
|
|
|
|
|
from towhee.types.audio_frame import AudioFrame |
|
|
|
|
|
|
|
|
sys.path.append(str(Path(__file__).parent)) |
|
|
sys.path.append(str(Path(__file__).parent)) |
|
|
from clmr_checkpoint import load_encoder_checkpoint |
|
|
from clmr_checkpoint import load_encoder_checkpoint |
|
@ -56,30 +71,57 @@ class ClmrMagnatagatune(NNOperator): |
|
|
self.model.eval() |
|
|
self.model.eval() |
|
|
self.model.to(self.device) |
|
|
self.model.to(self.device) |
|
|
|
|
|
|
|
|
def __call__(self, audio: Union[str, numpy.ndarray], sample_rate: int = None) -> numpy.ndarray: |
|
|
|
|
|
|
|
|
def __call__(self, data: List[AudioFrame]) -> numpy.ndarray: |
|
|
|
|
|
audio_tensors = self.preprocess(data).to(self.device) |
|
|
|
|
|
features = self.model(audio_tensors) |
|
|
|
|
|
outs = features.to("cpu") |
|
|
|
|
|
return outs.detach().numpy() |
|
|
|
|
|
|
|
|
|
|
|
def __call__(self, data: List[AudioFrame]) -> numpy.ndarray: |
|
|
_sr = 22050 |
|
|
_sr = 22050 |
|
|
audio_length = 59049 |
|
|
audio_length = 59049 |
|
|
|
|
|
|
|
|
if isinstance(audio, str): |
|
|
|
|
|
source = os.path.abspath(audio) |
|
|
|
|
|
audio, sr = torchaudio.load(source) |
|
|
|
|
|
elif isinstance(audio, numpy.ndarray): |
|
|
|
|
|
sr = sample_rate |
|
|
|
|
|
audio = torch.tensor(audio).to(torch.float32) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sr = data[0].sample_rate |
|
|
|
|
|
layout = data[0].layout |
|
|
|
|
|
if layout == 'stereo': |
|
|
|
|
|
frames = [frame.reshape(-1, 2) for frame in data] |
|
|
|
|
|
audio = numpy.vstack(frames).transpose() |
|
|
|
|
|
# audio = numpy.mean(audio, axis=0) |
|
|
|
|
|
# audio = numpy.expand_dims(audio, 0) |
|
|
|
|
|
else: |
|
|
|
|
|
audio = numpy.hstack(data) |
|
|
|
|
|
audio = numpy.expand_dims(audio, 0) |
|
|
|
|
|
|
|
|
|
|
|
audio = self.int2float(audio).astype('float32') |
|
|
if sr != _sr: |
|
|
if sr != _sr: |
|
|
transform = torchaudio.transforms.Resample(orig_freq=sr, new_freq=_sr) |
|
|
|
|
|
audio = transform(audio) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
audio = resampy.resample(audio, sr, _sr) |
|
|
with torch.no_grad(): |
|
|
with torch.no_grad(): |
|
|
|
|
|
audio = torch.from_numpy(audio) |
|
|
batch = torch.split(audio, audio_length, dim=1) |
|
|
batch = torch.split(audio, audio_length, dim=1) |
|
|
batch = torch.cat(batch[:-1]) |
|
|
batch = torch.cat(batch[:-1]) |
|
|
batch = batch.unsqueeze(dim=1) |
|
|
batch = batch.unsqueeze(dim=1) |
|
|
batch = batch.to(self.device) |
|
|
batch = batch.to(self.device) |
|
|
features = numpy.squeeze(self.model(batch)) |
|
|
features = numpy.squeeze(self.model(batch)) |
|
|
|
|
|
|
|
|
embeddings = features.to("cpu") |
|
|
|
|
|
return embeddings.detach().numpy() |
|
|
|
|
|
|
|
|
return features.to('cpu').detach().numpy() |
|
|
|
|
|
|
|
|
|
|
|
def int2float(self, wav: numpy.ndarray, dtype: str = 'float64'): |
|
|
|
|
|
""" |
|
|
|
|
|
Convert audio data from int to float. |
|
|
|
|
|
The input dtype must be integers. |
|
|
|
|
|
The output dtype is controlled by the parameter `dtype`, defaults to 'float64'. |
|
|
|
|
|
|
|
|
|
|
|
The code is inspired by https://github.com/mgeier/python-audio/blob/master/audio-files/utility.py |
|
|
|
|
|
""" |
|
|
|
|
|
dtype = numpy.dtype(dtype) |
|
|
|
|
|
assert dtype.kind == 'f' |
|
|
|
|
|
if wav.dtype.kind in 'iu': |
|
|
|
|
|
ii = numpy.iinfo(wav.dtype) |
|
|
|
|
|
abs_max = 2 ** (ii.bits - 1) |
|
|
|
|
|
offset = ii.min + abs_max |
|
|
|
|
|
return (wav.astype(dtype) - offset) / abs_max |
|
|
|
|
|
else: |
|
|
|
|
|
return wav.astype(dtype) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if __name__ == "__main__": |
|
|
# if __name__ == "__main__": |
|
|