Committed by
GitHub
Add score function to speaker identification (#775)
正在显示
3 个修改的文件
包含
32 行增加
和
0 行删除
| @@ -151,6 +151,23 @@ class SpeakerEmbeddingManager::Impl { | @@ -151,6 +151,23 @@ class SpeakerEmbeddingManager::Impl { | ||
| 151 | return true; | 151 | return true; |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | + float Score(const std::string &name, const float *p) { | ||
| 155 | + if (!name2row_.count(name)) { | ||
| 156 | + // Setting a default value if the name is not found | ||
| 157 | + return -2.0; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + int32_t row_idx = name2row_.at(name); | ||
| 161 | + | ||
| 162 | + Eigen::VectorXf v = | ||
| 163 | + Eigen::Map<Eigen::VectorXf>(const_cast<float *>(p), dim_); | ||
| 164 | + v.normalize(); | ||
| 165 | + | ||
| 166 | + float score = embedding_matrix_.row(row_idx) * v; | ||
| 167 | + | ||
| 168 | + return score; | ||
| 169 | + } | ||
| 170 | + | ||
| 154 | bool Contains(const std::string &name) const { | 171 | bool Contains(const std::string &name) const { |
| 155 | return name2row_.count(name) > 0; | 172 | return name2row_.count(name) > 0; |
| 156 | } | 173 | } |
| @@ -206,6 +223,11 @@ bool SpeakerEmbeddingManager::Verify(const std::string &name, const float *p, | @@ -206,6 +223,11 @@ bool SpeakerEmbeddingManager::Verify(const std::string &name, const float *p, | ||
| 206 | return impl_->Verify(name, p, threshold); | 223 | return impl_->Verify(name, p, threshold); |
| 207 | } | 224 | } |
| 208 | 225 | ||
| 226 | +float SpeakerEmbeddingManager::Score(const std::string &name, | ||
| 227 | + const float *p) const { | ||
| 228 | + return impl_->Score(name, p); | ||
| 229 | +} | ||
| 230 | + | ||
| 209 | int32_t SpeakerEmbeddingManager::NumSpeakers() const { | 231 | int32_t SpeakerEmbeddingManager::NumSpeakers() const { |
| 210 | return impl_->NumSpeakers(); | 232 | return impl_->NumSpeakers(); |
| 211 | } | 233 | } |
| @@ -74,6 +74,8 @@ class SpeakerEmbeddingManager { | @@ -74,6 +74,8 @@ class SpeakerEmbeddingManager { | ||
| 74 | */ | 74 | */ |
| 75 | bool Verify(const std::string &name, const float *p, float threshold) const; | 75 | bool Verify(const std::string &name, const float *p, float threshold) const; |
| 76 | 76 | ||
| 77 | + float Score(const std::string &name, const float *p) const; | ||
| 78 | + | ||
| 77 | // Return true if the given speaker already exists; return false otherwise. | 79 | // Return true if the given speaker already exists; return false otherwise. |
| 78 | bool Contains(const std::string &name) const; | 80 | bool Contains(const std::string &name) const; |
| 79 | 81 |
| @@ -60,6 +60,14 @@ void PybindSpeakerEmbeddingManager(py::module *m) { | @@ -60,6 +60,14 @@ void PybindSpeakerEmbeddingManager(py::module *m) { | ||
| 60 | return self.Verify(name, v.data(), threshold); | 60 | return self.Verify(name, v.data(), threshold); |
| 61 | }, | 61 | }, |
| 62 | py::arg("name"), py::arg("v"), py::arg("threshold"), | 62 | py::arg("name"), py::arg("v"), py::arg("threshold"), |
| 63 | + py::call_guard<py::gil_scoped_release>()) | ||
| 64 | + .def( | ||
| 65 | + "score", | ||
| 66 | + [](const PyClass &self, const std::string &name, | ||
| 67 | + const std::vector<float> &v) -> float { | ||
| 68 | + return self.Score(name, v.data()); | ||
| 69 | + }, | ||
| 70 | + py::arg("name"), py::arg("v"), | ||
| 63 | py::call_guard<py::gil_scoped_release>()); | 71 | py::call_guard<py::gil_scoped_release>()); |
| 64 | } | 72 | } |
| 65 | 73 |
-
请 注册 或 登录 后发表评论