Juliusの音声認識結果をソケット通信で受け取って表示・保存するPythonプログラム

前回の記事の関連として。
Juliusのdictation kitをダウンロードしておく。

以下のコマンドによりJuliusをmoduleモードで立ち上げる。

julius -n 5 -output 1 -rejectshort 800 -C main.jconf -C am-dnn.jconf -dnnconf julius.dnnconf -module &

ポート番号10500におけるTCP/IP経由でクライアントからの接続を待つ状態になる。一種のサーバーモードである。

クライアント側はJuliusと接続後、上記のポートを介して認識結果を受け取る。音声認識結果はログに保存される(example.log)。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import socket
import logging

# create logger
logger = logging.getLogger('simple_example')
logging.basicConfig(filename='example.log', level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

host = '127.0.0.1'   # IPアドレス
port = 10500         # Juliusとの通信用ポート番号

# Juliusにソケット通信で接続
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))

data = ""
try:
    while True:
        # 音声認識結果のみをXMLで取得
        while (data.find("</RECOGOUT>\n.") == -1):
            soc = sock.recv(1024)
            data = data + soc.decode('utf-8')

        # 音声認識結果のXMLから単語部分のみを抜き出して連結
        recog_text = ""
        for line in data.split('\n'):
            index = line.find('WORD="')
            if index != -1:
                line = line[index+6:line.find('"', index+6)]
                recog_text = recog_text + line

        logging.info("認識結果: " + recog_text)
        print("認識結果: " + recog_text)
        data = ""

# Command + C などで強制終了した場合、JuliusサーバーをDIEコマンドにより落とす
# https://julius.osdn.jp/juliusbook/ja/desc_module.html
except KeyboardInterrupt:
    print('finished')
    sock.send("DIE".encode('utf-8'))
    sock.close()

後ほど記事は追記・更新予定。

オウム返しなPythonプログラム(オンラインで音声認識→オフラインで音声合成)

音声認識して、その内容を音声合成で返すという、単純なもの。

Mac 10.13.6上での動作確認。
Pythonのバージョンは3.7.3。

PythonのSpeechRecognitionライブラリを利用。
Google音声認識のサービスを用いるのでオンライン環境。

pip3 install SpeechRecognition
brew install open-jtalk

の後に

#!/usr/bin/env python3

import speech_recognition as sr
import subprocess
import tempfile


def jtalk_run(message, out_wav='/tmp/voice.wav', speed=1.0):

    jtalk_dir = "/usr/local/Cellar/open-jtalk/1.10_1/"
    dic_path = jtalk_dir + "/dic"
    voice_path = jtalk_dir + "/voice/mei/mei_normal.htsvoice"

    with tempfile.NamedTemporaryFile(mode='w+') as tmp:
        tmp.write(message)
        tmp.seek(0)
        command = 'open_jtalk -x {} -m {} -r {} -ow {} {}'.format(
            dic_path, voice_path, speed, out_wav, tmp.name)
        command = command + '; afplay ' + out_wav
        # print(command)
        subprocess.run(command, shell=True)


if __name__ == "__main__":

    # マイクからの音声入力
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("話しかけてみましょう!")
        audio = r.listen(source)

    try:
        # 日本語でGoogle音声認識
        text = r.recognize_google(audio, language="ja-JP")
    except sr.UnknownValueError:
        print("Google音声認識は音声を理解できませんでした。")
    except sr.RequestError as e:
        print("Google音声認識サービスからの結果を要求できませんでした;"
              " {0}".format(e))
    else:
        print(text)
        jtalk_run(text)

pythonコードを適当な名前で保存して実行したのであった。

以下を参考にさせていただきました。
thr3a.hatenablog.com

Flow系の論文たち

サーベイ論文

  • Normalizing Flows: Introduction and Ideas (2019) URL
  • Normalizing Flows for Probabilistic Modeling and Inference (2019) URL

代表的なもの

  • NICE: Non-linear Independent Components Estimation (2014) URL
  • MADE: Masked Autoencoder for Distribution Estimation (2015) URL
  • Variational Inference with Normalizing Flows (2015) URL
  • NADE: Neural Autoregressive Density Estimator (2016) URL
  • Real NVP : Real-valued non-volume preserving transformations (2016) URL
  • IAF: Inverse Autoregressive Flow (2016) URL
  • Density estimation using Real NVP (2017) URL
  • MAF: Masked Autoregressive Flow (2017) URL
  • NAF: Neural Autoregressive Flows (2018) URL
  • Glow: Generative Flow with Invertible 1x1 Convolutions (2018) URL
  • MintNet : Building Invertible Neural Networks with Masked Convolutions (2019) URL

今後に期待

  • Sylvester Normalizing Flows for Variational Inference (2018) URL
  • Flow-GAN: Combining Maximum Likelihood and Adversarial Learning in Generative Models (2018) URL

音声系

  • WaveGlow: A Flow-based Generative Network for Speech Synthesis (2018) URL
  • FloWaveNet: A Generative Flow for Raw Audio (2018) URL
  • Blow: a single-scale hyperconditioned flow for non-parallel raw-audio voice conversion (2019) URL

各論文の読書メモを記したブログ記事は以下から。

peluigi.hatenablog.com

Flow系の生成モデルをまとめたスライドは以下から。

www.slideshare.net

その他

ブログ記事
akosiorek.github.io

Eric Jang氏によるNormalizing Flowの解説ブログ記事その1
blog.evjang.com

Eric Jang氏によるNormalizing Flowの解説ブログ記事その2
blog.evjang.com

Kingma氏の論文

Kingma氏が第1著者の論文を(独断と偏見で)抜き出してまとめておく。

そして以下がD論。必読。

書籍『統計的パターン認識と判別分析』のレビュー

最近発売された『統計的パターン認識と判別分析』のレビューを書く。
統計的パターン認識と判別分析 (シリーズ 情報科学における確率モデル 1)

各章の概要についてはコロナ社のサイトを参照されたい。
『シリーズ 情報科学における確率モデル』|書籍紹介

本書の「ご利益」を一言で表せば、回帰と識別(分類)という機械学習における2大タスクの定式化に現れる、(最小二乗誤差の意味で)最適な非線形関数の解釈を通じてこれらをより良く理解することに役立つ、と言えよう。本書では、この最適な非線形関数に現れる事後確率を「線形近似」するアプローチにより、解釈が進められる。実際に、線形回帰/識別関数が、非線形回帰/識別関数の「線形近似」となる事実が、丁寧な導出とともに具体的に示されており、読者は理解を大いに深めることができるだろう。後半の章では、主成分分析/線形判別分析→カーネル主成分分析/カーネル判別分析という流れで話が進み、これらは最終章の布石をなしている。そして最終章において、非線形判別写像に現れる事後確率を線形近似することにより、線形判別分析が導出できる、という事実が示されるのであった。事後確率の近似方法として、正規分布を仮定する場合など線形近似以外の方法についても具体的な言及がされていたのは好印象であった。最適な非線形判別分析をカーネル判別分析として再解釈する議論もまた、読者にとって有益であると思われる。

本書を通読して、確かに対象読者はパターン認識の基礎を「一通り」勉強し、微分積分線形代数に「十分に慣れた」大学院生や研究者向け、という印象を持った。本書は早い段階で積分を駆使した変分法や確率計算、行列やベクトル計算、行列の偏微分など、高度な数学的操作が現れるため、それらにある程度習熟していることが望ましい。さもなくば、とても「スラスラ」とは読めないであろう。巻末の付録には線形代数、最適化、確率統計の基礎事項がまとめてあり、読者への配慮は伺える。ただし、それらはあくまで備忘録として参照する程度に済むくらいの読者が対象といえるだろう。本書の「まえがき」では付録の存在・活用法について一切言及がないため、ここは幾分残念な点であった。

いくつかの誤植はあるが、ほとんどは軽微な修正で済む。

著者らの最近の論文(2016) は最終章の理解に役立つ(いくつかの誤植の確認はこの論文を参照しながら行った)
www.jstage.jst.go.jp

最適輸送理論の解説論文とか書籍へのリンクのまとめ

はじめに

メモとして。WGANの勉強にもなるかなと。

2023年 11月 追記

輸送計画,輸送写像,輸送経路―有限集合とℝ2の最適輸送理論の違い―
https://www.jstage.jst.go.jp/article/bjsiam/32/2/32_69/_article/-char/ja/

応用

おわりに

最適輸送理論、とても難しい。数学者はすごい。

密度比推定まわりの書籍・解説記事・論文・ソフトウェアの各種情報まとめ

はじめに

密度比推定の文献については、すでに山田氏による素晴らしいまとめ記事がある。同記事「はじめに」より、確率密度比推定の有用性を引用すれば、

パターン認識ドメイン適応、外れ値検出、変化点検出、次元削減、因果推論等の様々な機械学習の問題が確率密度比(確率密度関数の比)の問題として定式化できることから、近年、確率密度比に基づいた機械学習の研究が機械学習およびデータマイニングの分野において大変注目されている。

というわけである。しかしながら、同記事は2012年に書かれたもので、本記事の執筆時点の2018年ではリンク切れなど、一部の情報が古くなっている。そのため、これら情報を更新したいということ。また、2012年以降、いくつか研究の進展が見られたので個人的に気になった論文を備忘録としてまとめておきたいということ。以上が本記事の動機である。

以下、山田氏のまとめ記事からも情報を引っ張りつつまとめる。編集方針として、各文献のリンクの情報はできるだけ「本家」のものを採用した(つまり公式ページ)。また会議論文よりは雑誌論文を優先した。
専門家から見れば、あの論文が足りない、などの不満はあろうが、ご容赦願いたい。

書籍

  • Sugiyama et al., Density Ratio Estimation in Machine Learning
  • 井手 et al., 異常検知と変化検知 Amazon
    • 密度比推定に基づく異常検知や変化点検出のトピックが扱われている。

解説論文・解説記事

密度比推定

  • Sugiyama et al, "A Density-ratio Framework for Statistical Data Processing," vol. 1, pp. 183-208, 2009. Link
  • 杉山, "密度比に基づく機械学習の新たなアプローチ," 統計数理, vo. 58, no. 2, pp. 141–155, 2010. PDF
  • Sugiyama et al, "Density Ratio Estimation: A Comprehensive Review," 2010. Link
  • 統計的機械学習の新展開:確率密度比に基づくアプローチ PDF
  • 杉山, "密度比推定によるビッグデータ解析," 電子情報通信学会誌, vol. 97, no. 5, pp. 353–358, 2014. PDF
  • 金森, "密度比によるダイバージェンス推定とその応用, " 日本統計学会誌, vol. 44, no. 1, pp. 97-111, 2014. PDF
  • 杉山 将, 山田 誠, ドゥ・プレシ マーティヌス・クリストフェル, リウ ソン, "非定常環境下での学習:共変量シフト適応,クラスバランス変化適応,変化検知", 日本統計学会誌, vol. 44, no. 1, pp. 113-136, 2014. PDF
  • S. Mohamed, "Machine Learning Trick of the Day (7): Density Ratio Trick", 2018. Link

ダイバージェンス

  • Sugiyama et al, "Density-ratio matching under the Bregman divergence: a unified framework of density-ratio estimation," Annals of the Institute of Statistical Mathematics, vol. 64, no. 5, pp. 1009–1044, 2012. Link
  • Sugiyama, "Machine Learning with Squared-Loss Mutual Information," Entropy, vol. 15, no. 1, pp. 80-112, 2013. Link
  • Sugiyama et al., "Direct Divergence Approximation between Probability Distributions and Its Applications in Machine Learning," Journal of Computing Science and Engineering, vol. 7, no. 2, pp. 99-111, 2013. Link
  • 杉山, "確率分布間の距離推定 : 機械学習分野における最新動向," 日本応用数理学会論文誌, vol. 23, no. 3, pp. 439-452, 2013. Link

論文

密度比推定法

  • Kanamori et al., "Theoretical Analysis of Density Ratio Estimation," IEICE Transactions on Fundamentals of Electronics, Communications and Computer Sciences, vol. E93.A, no. 4, pp. 787-798, 2010. Link
  • Benjamin Rhodes, Kai Xu, Michael U. Gutmann, "Telescoping Density-Ratio Estimation," https://papers.nips.cc/paper/2020/hash/33d3b157ddc0896addfb22fa2a519097-Abstract.html
  • Akinori F. Ebihara, Taiki Miyagawa, Kazuyuki Sakurai, Hitoshi Imaoka, "Sequential Density Ratio Estimation for Simultaneous Optimization of Speed and Accuracy," ICLR 2021. " Link
  • Masahiro Kato, Takeshi Teshima, "Non-Negative Bregman Divergence Minimization for Deep Direct Density Ratio Estimation," Proceedings of the 38th International Conference on Machine Learning, PMLR 139:5320-5333, 2021. Link
p(x)/q(x)の推定
  • ロジスティック回帰
    • Qin, "Inferences for Case-Control and Semiparametric Two-Sample Density Ratio Models," Biometrika vol. 85, no. 3, pp. 619-630, 1998. Link
    • Bickel et al, "Discriminative learning for differing training and test distributions," Proceedings of the 24th international conference on Machine learning (ICML 2007), pp. 81-87, 2007. PDF
  • Kernel Mean Matching (KMM): 確率密度比のモデルをb(x)とした時に、p(x)とb(x)q(x)のモーメントが一致するようにモデルを学習。
    • Huang et al., "Correcting Sample Selection Bias by Unlabeled Data," Advances in Neural Information Processing Systems 19 (NIPS 2006), pp. 601-608, 2006. Link
  • Kullback-Leibler Importance Estimation Procedure (KLIEP): 確率密度比を線形モデルで直接推定する手法。真の確率密度比と線形モデルとのカルバックライブラー距離が最小になるように、モデルパラメータを学習。
    • Sugiyama et al., "Direct Importance Estimation with Model Selection and Its Application to Covariate Shift Adaptation," Advances in Neural Information Processing Systems 20 (NIPS 2007), pp. 1433-1440, 2007. Link
    • Nguyen et al., "Estimating Divergence Functionals and the Likelihood Ratio by Convex Risk Minimization," IEEE Transactions on Information Theory, vol. 56, no. 11, 2010. Link
  • Unconstrained Least-Squares Importance Fitting (uLSIF): 確率密度比を線形モデルで直接推定する手法。真の確率密度比と線形モデルの二乗距離が最小になるように、モデルパラメータを学習。線型方程式を解くことによりモデルパラメータを推定できるため大変高速。
    • Kanamori et al., "A Least-squares Approach to Direct Importance Estimation," The Journal of Machine Learning Research, vol. 10, 2009. Link
    • Kanamori et al., "Statistical analysis of kernel-based least-squares density-ratio estimation," Mach. Learn. vol. 86, pp. 335–367, 2012. Link
  • Relative uLSIF (RuLSIF): 相対密度比 {p(x)/(a p(x) + (1-a)q(x)), 0 <= a < 1}を推定する手法。a = 0の時はuLSIFと同じになる。
    • Yamada et al., "Relative Density-Ratio Estimation for Robust Distribution Comparison," Neural Computation, vol. 25, no. 5, pp. 1324–1370, 2013. Link
p(x,y)/(p(x)p(y))の推定
  • Maximum Likelihood Mutual Information (MLMI): KLIEPの相互情報量版。相互情報量の推定に有用。
    • Suzuki et al., "Mutual information approximation via maximum likelihood estimation of density ratio," 2009 IEEE International Symposium on Information Theory, pp. 463-467, 2009. Link
  • Least-Squares Mutual Information (LSMI): uLSIFの相互情報量版。二乗損失相互情報量を高速に推定可能。
    • Suzuki et al., "Mutual information estimation reveals global associations between stimuli and biological processes," BMC Bioinformatics, vol. 10, no. 1, 2009. Link
p(x,y)/p(y)の推定
  • Least-Squares Conditional Density Estimation (LSCDE): yが連続値の場合の条件付き確率を直接推定する手法。
    • Sugiyama et al., "Least-Squares Conditional Density Estimation," IEICE Transactions on Information and Systems, vol. E93-D, no. 3, pp. 583-594, 2010. Link
  • Least-Squares Probabilistic Classifier (LSPC): yが離散値の場合(識別問題)の条件付き確率を直接推定する手法。
    • Sugiyama et al., "Superfast-Trainable Multi-Class Probabilistic Classifier by Least-Squares Posterior Fitting," IEICE Transactions on Information and Systems, vol. E93-D, no. 10, pp. 2690-2701, 2010. Link

応用

共変量シフト適応

  • Sugiyama et al., "Covariate Shift Adaptation by Importance Weighted Cross Validation," The Journal of Machine Learning Research, vol.8, pp. 985-1005, 2007. Link
  • Shimodaira, "Improving predictive inference under covariate shift by weighting the log-likelihood function," Journal of Statistical Planning and Inference, vol. 90, no. 2, pp. 227-244, 2010. Link

外れ値検出

  • Smola et al., "Relative Novelty Detection," Proceedings of the 12th International Conference on Artificial Intelligence and Statistics (AISTATS) 2009, pp. 536-543, 2009. Link
  • Hido et al., "Statistical outlier detection using direct density ratio estimation," Knowledge and Information Systems, vol. 26, no. 2, pp. 309-336, 2011. Link
  • Nam et al., "Direct Density Ratio Estimation with Convolutional Neural Networks with Application in Outlier Detection," IEICE Transactions on Information and Systems, vol. E98-D, no. 5, pp. 1073-1079, 2015. Link
  • Plessis et al., "Online Direct Density-Ratio Estimation Applied to Inlier-Based Outlier Detection," Neural Computation, vol. 27, no.9, pp. 1899-1914, 2015. PDF

変化点検出

  • Kawahara et al., "Sequential change-point detection based on direct density-ratio estimation," Statistical Analysis and Data Mining, vol. 5, no. 2, 2011. Link
  • Song et al., "Change-point detection in time-series data by relative density-ratio estimation," Neural Networks, vol. 43, pp. 72-83, 2013. Link
  • Yamada et al, "Change-point detection with feature selection in high-dimensional time-series data," Proceedings of International Joint Conference on Artificial Intelligence (IJCAI 2013), pp. 1827-1833, 2013. PDF

マルコフネットワークの構造変化検知

  • Song et al., "Direct Learning of Sparse Changes in Markov Networks by Density Ratio Estimation," Neural Computation, vol. 26, no. 6, pp.1169-1197, 2014. Link

十分次元削減

  • Yamada et al., "Computationally Efficient Sufficient Dimension Reduction via Squared-Loss Mutual Information," Proceedings of the Second Asian Conference on Machine Learning (ACML2011), pp. 247-262, 2011. Link
  • Suzuki et al., "Sufficient Dimension Reduction via Squared-Loss Mutual Information Estimation," Neural Computation, vol. 25, no. 3, pp. 725-758, 2013. Link

次元削減

  • Sugiyama et al., "Direct density-ratio estimation with dimensionality reduction via least-squares hetero-distributional subspace search," Neural Networks, vol. 24, no. 2, pp. 183-198, 2011. Link

独立成分分析

  • Suzuki et al., "Least-Squares Independent Component Analysis," Neural Computation, vol. 23, no. 1, pp. 284-301, 2011. Link

クラスタリング

  • Sainui et al., "Direct Approximation of Quadratic Mutual Information and Its Application to Dependence-Maximization Clustering," IEICE Transactions on Information and Systems, vol. E96-D, no. 10, pp. 2282-2285, 2013. Link

二標本検定

  • Sugiyama et al, "Least-squares two-sample test," Neural Networks, vol. 24, no. 7, pp. 735-751, 2011. Link

Generative adversarial network

  • Uehara et al., "Generative Adversarial Nets from a Density Ratio Estimation Perspective," arXiv preprint arXiv:1610.02920, 2016. Link
  • Mohamed et al., "Learning in Implicit Generative Models," arXiv preprint arXiv:1610.03483, 2016. Link

音声区間検出

  • 太刀岡 et al., "音声と騒音の密度比推定を用いた音声区間検出法,'' 電気学会論文誌C, vol. 133, no. 8, pp. 1549-1555, 2013. Link

話者認識

  • Yamada, "Kernel Methods and Frequency Domain Independent Component Analysis for Robust Speaker Identification," Doctor Thesis, Department of Statistical Science, The Graduate University for Advanced Studies, Hayama, Japan, 2010. Link
  • Yamada et al., "Semi-supervised speaker identification under covariate shift," Signal Processing, vol.90, no.8, pp.2353-2361, 2010. Link

二乗損失相互情報量

  • Yamada et al., "Cross-Domain Matching with Squared-Loss Mutual Information," IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 37, no. 9, pp. 1764-1776, 2015. Link
  • Sakai et al., "Estimation of Squared-Loss Mutual Information from Positive and Unlabeled Data," arXiv preprint, arXiv:1710.05359, 2017. Link

その他(必ずしも密度比推定ではないが)

  • Song et al., "Trimmed Density Ratio Estimation," Advances in Neural Information Processing Systems 19 (NIPS 2017), pp. 4518-4528, 2017. Link
密度微分推定
  • Sasaki et al., "Direct Density Derivative Estimation," Neural Computation, vol. 28, no. 6, pp. 1101-1140, 2016. Link
  • 佐々木 et al., "確率密度微分の直接推定と機械学習への応用", 数理解析研究所講究録, vol. 1999, pp. 154-173, 2016. Link
密度差推定
  • Sugiyama et al., "Density-Difference Estimation," Neural Computation, vol. 25, no. 10, pp. 2734-2775, 2013. Link
スライド

ソフトウェア

おわりに

確率密度比推定は勉強していて楽しい。