-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support audio tagging using zipformer (#747)
- Loading branch information
1 parent
c9ae759
commit f20291c
Showing
30 changed files
with
927 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
log() { | ||
# This function is from espnet | ||
local fname=${BASH_SOURCE[1]##*/} | ||
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*" | ||
} | ||
|
||
echo "EXE is $EXE" | ||
echo "PATH: $PATH" | ||
|
||
which $EXE | ||
|
||
log "------------------------------------------------------------" | ||
log "Run zipformer for audio tagging " | ||
log "------------------------------------------------------------" | ||
|
||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
tar xvf sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
rm sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
repo=sherpa-onnx-zipformer-audio-tagging-2024-04-09 | ||
ls -lh $repo | ||
|
||
for w in 1.wav 2.wav 3.wav 4.wav; do | ||
$EXE \ | ||
--zipformer-model=$repo/model.onnx \ | ||
--labels=$repo/class_labels_indices.csv \ | ||
$repo/test_wavs/$w | ||
done | ||
rm -rf $repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go.sum | ||
vad-asr-paraformer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// sherpa-onnx/csrc/audio-tagging-impl.cc | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
|
||
#include "sherpa-onnx/csrc/audio-tagging-impl.h" | ||
|
||
#include "sherpa-onnx/csrc/audio-tagging-zipformer-impl.h" | ||
#include "sherpa-onnx/csrc/macros.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
std::unique_ptr<AudioTaggingImpl> AudioTaggingImpl::Create( | ||
const AudioTaggingConfig &config) { | ||
if (!config.model.zipformer.model.empty()) { | ||
return std::make_unique<AudioTaggingZipformerImpl>(config); | ||
} | ||
|
||
SHERPA_ONNX_LOG( | ||
"Please specify an audio tagging model! Return a null pointer"); | ||
return nullptr; | ||
} | ||
|
||
} // namespace sherpa_onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// sherpa-onnx/csrc/audio-tagging-impl.h | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
#ifndef SHERPA_ONNX_CSRC_AUDIO_TAGGING_IMPL_H_ | ||
#define SHERPA_ONNX_CSRC_AUDIO_TAGGING_IMPL_H_ | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "sherpa-onnx/csrc/audio-tagging.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
class AudioTaggingImpl { | ||
public: | ||
virtual ~AudioTaggingImpl() = default; | ||
|
||
static std::unique_ptr<AudioTaggingImpl> Create( | ||
const AudioTaggingConfig &config); | ||
|
||
virtual std::unique_ptr<OfflineStream> CreateStream() const = 0; | ||
|
||
virtual std::vector<AudioEvent> Compute(OfflineStream *s, | ||
int32_t top_k = -1) const = 0; | ||
}; | ||
|
||
} // namespace sherpa_onnx | ||
|
||
#endif // SHERPA_ONNX_CSRC_AUDIO_TAGGING_IMPL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// sherpa-onnx/csrc/audio-tagging-label-file.cc | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
|
||
#include "sherpa-onnx/csrc/audio-tagging-label-file.h" | ||
|
||
#include <fstream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include "sherpa-onnx/csrc/macros.h" | ||
#include "sherpa-onnx/csrc/text-utils.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
AudioTaggingLabels::AudioTaggingLabels(const std::string &filename) { | ||
std::ifstream is(filename); | ||
Init(is); | ||
} | ||
|
||
// Format of a label file | ||
/* | ||
index,mid,display_name | ||
0,/m/09x0r,"Speech" | ||
1,/m/05zppz,"Male speech, man speaking" | ||
*/ | ||
void AudioTaggingLabels::Init(std::istream &is) { | ||
std::string line; | ||
std::getline(is, line); // skip the header | ||
|
||
std::string index; | ||
std::string tmp; | ||
std::string name; | ||
|
||
while (std::getline(is, line)) { | ||
index.clear(); | ||
name.clear(); | ||
std::istringstream input2(line); | ||
|
||
std::getline(input2, index, ','); | ||
std::getline(input2, tmp, ','); | ||
std::getline(input2, name); | ||
|
||
std::size_t pos{}; | ||
int32_t i = std::stoi(index, &pos); | ||
if (index.size() == 0 || pos != index.size()) { | ||
SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str()); | ||
exit(-1); | ||
} | ||
|
||
if (i != names_.size()) { | ||
SHERPA_ONNX_LOGE( | ||
"Index should be sorted and contiguous. Expected index: %d, given: " | ||
"%d.", | ||
static_cast<int32_t>(names_.size()), i); | ||
} | ||
if (name.empty() || name.front() != '"' || name.back() != '"') { | ||
SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str()); | ||
exit(-1); | ||
} | ||
|
||
names_.emplace_back(name.begin() + 1, name.end() - 1); | ||
} | ||
} | ||
|
||
const std::string &AudioTaggingLabels::GetEventName(int32_t index) const { | ||
return names_.at(index); | ||
} | ||
|
||
} // namespace sherpa_onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// sherpa-onnx/csrc/audio-tagging-label-file.h | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
#ifndef SHERPA_ONNX_CSRC_AUDIO_TAGGING_LABEL_FILE_H_ | ||
#define SHERPA_ONNX_CSRC_AUDIO_TAGGING_LABEL_FILE_H_ | ||
|
||
#include <istream> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace sherpa_onnx { | ||
|
||
class AudioTaggingLabels { | ||
public: | ||
explicit AudioTaggingLabels(const std::string &filename); | ||
|
||
// Return the event name for the given index. | ||
// The returned reference is valid as long as this object is alive | ||
const std::string &GetEventName(int32_t index) const; | ||
int32_t NumEventClasses() const { return names_.size(); } | ||
|
||
private: | ||
void Init(std::istream &is); | ||
|
||
private: | ||
std::vector<std::string> names_; | ||
}; | ||
|
||
} // namespace sherpa_onnx | ||
|
||
#endif // SHERPA_ONNX_CSRC_AUDIO_TAGGING_LABEL_FILE_H_ |
Oops, something went wrong.