モデル適応により音声文字変換の結果を改善する

概要

モデル適応機能を使用すると、提案される可能性がある他の候補よりも、Speech-to-Text が特定の単語やフレーズをより高い頻度で認識するように設定できます。たとえば、音声データに「weather」という単語が含まれているとします。Speech-to-Text が「weather」という単語を検出した場合、「whether」よりも多く「weather」と文字変換されることが理想的です。この場合は、モデル適応を使用して「weather」と認識するように Speech-to-Text にバイアスをかけることができます。

モデルの適応は、特に次のようなユースケースに対して有効です。

  • 音声データで頻繁に使用される単語やフレーズの精度を改善する。たとえば、一般ユーザーがよく発する音声コマンドを、認識モデルに知らせることが可能です。

  • Speech-to-Text によって認識される語彙を増加させる。Speech-to-Text には大量の語彙が含まれています。ただし、音声データに一般的な言語使用においては使用頻度が極めて低い単語(正式名称や地域特有の単語など)が高頻度で含まれている場合は、モデル適応を使用してそれらの単語を追加できます。

  • 音声にノイズが含まれている場合や、鮮明でない場合に音声文字変換の精度を改善する。

必要に応じて、モデル適応ブースト機能を使用して、認識モデルのバイアスを微調整できます。

単語とフレーズの認識精度を向上させる

Speech-to-Text が音声データを文字変換するときに「weather」という単語を認識する確率を高めるには、SpeechAdaptation リソースの PhraseSet オブジェクトに含まれる「weather」という単独の単語を渡します。

フレーズを指定することで、Speech-to-Text がその単語の並びを認識しやすくなります。フレーズを提供することで、個々の単語を含むフレーズの一部を認識する確率も高まります。これらのフレーズの数とサイズの制限については、コンテンツの制限ページをご覧ください。

クラスを使用して認識精度を向上させる

クラスとは、通貨単位やカレンダー日付など、自然言語で発生する一般的なコンセプトを表します。クラスにより、共通のコンセプトに対応する大規模な単語グループの音声文字変換の精度を向上できますが、必ずしも同一の単語やフレーズを含むとは限りません。

たとえば、お住まいの地域の住所を音声で録音したとします。「My house is 123 Main Street, the fourth house on the left.」という音声が録音されていたとしましょう。この場合、Speech-to-Text により「123」という数の並びは、「百二十三」という数値としてではなく、番地として認識されることが望ましいと思われます。しかし、すべての人が同じ住所「123 Main Street」に住んでいるわけではありません。可能なすべての番地を PhraseSet リソースに一覧表示することは実用的ではありません。代わりに、クラスを使用して、実際の番号に関わりなく、番地を認識させることができます。この例において、Speech-to-Text では「123 メイン ストリート」や「987 大通り」などの語句は番地として認識されるため、より正確に文字変換されます。

クラストークン

モデル適応でクラスを使用するには、PhraseSet リソースの phrases フィールドにクラストークンを格納します。ご利用の言語で使用できるトークンを確認するには、サポートされているクラストークンの一覧をご覧ください。たとえば、ソース音声からの住所番号の音声文字変換を改善するには、PhraseSet. のフレーズ内に値 $ADDRESSNUM を指定します。

クラスを phrases 配列の単独項目として使用することも、1 つ以上のクラストークンを複数の単語からなるフレーズに埋め込むこともできます。たとえば、文字列にクラストークン ["my address is $ADDRESSNUM"] を含めることで、より多くのフレーズで住所番号を指定できます。ただし、「I am at 123 Main Street」など、類似しているが同一ではないフレーズが含まれている場合には役立ちません。類似のフレーズを認識しやすくするためには、クラストークン ["my address is $ADDRESSNUM", "$ADDRESSNUM"] を単独で追加することが重要です。無効または異常なクラストークンを使用した場合、Speech-to-Text はエラーを発生させずトークンを無視しますが、それ以外のフレーズはコンテキストで使用されます。

カスタムクラス

関連する項目または値のカスタムリストで構成された独自の CustomClass クラスを作成することもできます。たとえば、地域に存在する数百軒のレストランのうち、いずれかの名前が含まれる可能性がある音声データを文字変換する必要がある場合が考えられます。レストラン名は一般的な音声での検出頻度は極めて低く、認識モデルによって「正しい」回答として選択される可能性は低くなります。カスタムクラスを使用すると、音声に出現した際にそれらの名前を正しく認識するように認識モデルにバイアスをかけることができます。

カスタムクラスを使用するには、ClassItem として各レストラン名を含む CustomClass リソースを作成します。カスタムクラスは、事前ビルドされたクラストークンと同じ方法で機能します。phrase には、事前ビルドされたクラストークンとカスタムクラスの両方を含めることができます。

ブーストを使用して音声文字変換の結果をファイン チューニングする

デフォルトでは、モデル適応から得られる効果は、特に 1 語句の場合は比較的小さなものです。モデル適応のブースト機能を使用すると、一部のフレーズに対する重み付けの割り当てを他のフレーズよりも引き上げることで、認識モデルのバイアスを引き上げられます。次の条件をすべて満たす場合は、ブーストを実装することをおすすめします。

  1. モデル適応はすでに実装済み。
  2. 音声文字変換の結果に対するモデル適応効果の強度をさらに調整する必要がある。ブーストの機能がお使いの言語で利用可能かどうかを確認するには、言語サポートのページをご覧ください。

たとえば、「fare」よりも「fair」という単語が頻繁に出現する状況で、「fare to get into the county fair」と尋ねる録音が多くあるとします。この場合は、モデル適応を使用して、PhraseSet リソースに phrases として追加することにより、モデルが「fair」と「fare」の両方を認識する確率を向上させることができます。これにより、「hare」や「lair」などよりも高い頻度で「fair」と「fare」と認識するよう Speech-to-Text に指示されます。

ただし、「fair」は音声においてより頻繁に出現するため、「fare」よりも高い頻度で認識されなければなりません。すでに、Speech-to-Text API を使用して音声を文字変換し、正しい単語(「fair」)を認識し損ねるエラーを多数見つけているかもしれません。この場合、ブースト機能を使用して、「fare」よりも「fair」により高いブーストを割り当てることをおすすめします。「fair」に割り当てる重み付け値を高くすると、Speech-to-Text API が「fare」よりも「fair」を頻繁に選択するようにバイアスが適用されます。ブースト値を割り当てていない場合、認識モデルは「fair」と「fare」を同じ確率で認識します。

ブーストの基礎

ブーストを使用する場合は、PhraseSet リソースの phrase 項目に重み付け値を割り当てます。Speech-to-Text は、音声データ内の単語の文字変換候補を選択するときに、この重み付け値を参照します。この値が大きいほど、Speech-to-Text が文字変換候補からその単語や語句を選択する可能性が高くなります。

複数単語のフレーズにブースト値を適用すると、ブーストはフレーズ全体に(フレーズ全体にのみ)適用されます。たとえば、「My favorite extract in American Museum of Natural History is the blue wheat」というフレーズにブースト値を割り当てます。このフレーズを phrase オブジェクトに追加し、ブースト値を割り当てると、認識モデルはそのフレーズを全体として逐語的に認識する可能性が高くなります。

複数単語のフレーズにブーストを適用しても、期待する結果が得られない場合は、そのフレーズ内のすべてのバイグラム(順序のある 2 単語)を phrase 項目として追加し、それぞれにブースト値を割り当てます。上述の例の場合、バイグラムとエンドグラム(3 つ以上の単語)をさらに追加して、確認してください。たとえば、「my favorite」、「my favorite exhibit」、「favorite exhibit」、「my favorite exhibit at the American Museum of Natural History」、「American Museum of Natural History」、「blue whale」などです。これにより、STT 認識モデルは、元のブーストされたフレーズの一部を含むものの、逐語的には一致しない音声内の関連フレーズを認識する可能性が高くなります。

ブースト値の設定

強調数値には 0 より大きい浮動小数点数値を指定してください。ブースト値の実質的な上限値は 20 です。最適な結果を得るには、ブースト値を調整して音声文字変換結果を確認して、正しい音声文字変換結果が得られるようにします。

ブースト値を大きくすると、認識漏れ(音声に出現する単語や語句が Speech-to-Text で正しく認識されない)が少なくなります。ただし、ブーストを使用すると過剰検出(音声に出現していない単語や語句が音声文字変換に出力される)の可能性は高くなります。

モデル適応を使用したユースケース例

次の例は、「The word is fare」という音声録音を、モデル適応を使って音声文字変換するプロセスを示しています。この場合、音声適応を使用しないと、Speech-to-Text は単語「fair」と認識します。音声適応を使用すると、Speech-to-Text は単語「fare」と認識できます。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. Speech-to-Text API を有効にします。

    API を有効にする

  5. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role colunn to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      [IAM] に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
    8. Install the Google Cloud CLI.
    9. To initialize the gcloud CLI, run the following command:

      gcloud init
    10. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

      プロジェクト セレクタに移動

    11. Google Cloud プロジェクトで課金が有効になっていることを確認します

    12. Speech-to-Text API を有効にします。

      API を有効にする

    13. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

      Check for the roles

      1. In the Google Cloud console, go to the IAM page.

        Go to IAM
      2. Select the project.
      3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

      4. For all rows that specify or include you, check the Role colunn to see whether the list of roles includes the required roles.

      Grant the roles

      1. In the Google Cloud console, go to the IAM page.

        [IAM] に移動
      2. プロジェクトを選択します。
      3. [ アクセスを許可] をクリックします。
      4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

      5. [ロールを選択] リストでロールを選択します。
      6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
      7. [保存] をクリックします。
      8. Install the Google Cloud CLI.
      9. To initialize the gcloud CLI, run the following command:

        gcloud init
      10. クライアント ライブラリは、アプリケーションのデフォルト認証情報を使用することによって、Google API で簡単に認証を行い、これらの API にリクエストを送信できます。アプリケーションのデフォルト認証情報を使用すると、ベースとなるコードを変更することなく、ローカルでのアプリケーションのテストやアプリケーションのデプロイが可能です。詳しくは、<atrack-type="commonincludes" l10n-attrs-original-order="href,track-type,track-name" l10n-encrypted-href="WDE63JFVMK0YqIWBqG8nCycgwkRfOeEqRvzYs1N+2tJUEhcZvE5VtDH5LoWw0lj/" track-name="referenceLink">クライアント ライブラリを使用して認証する</atrack-type="commonincludes">をご覧ください。

      11. Create local authentication credentials for your user account:

        gcloud auth application-default login

      また、クライアント ライブラリがインストールされていることを確認してください。

      PhraseSet を使用して音声文字変換を改善する

      1. 次のサンプルでは、「fare」というフレーズを含む PhraseSet を作成し、それを認識リクエストに inline_phrase_set として追加します。

      Python

      import os
      
      from google.cloud.speech_v2 import SpeechClient
      from google.cloud.speech_v2.types import cloud_speech
      
      PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
      
      
      def adaptation_v2_inline_phrase_set(audio_file: str) -> cloud_speech.RecognizeResponse:
          """Enhances speech recognition accuracy using an inline phrase set.
          The inline custom phrase set helps the recognizer produce more accurate transcriptions for specific terms.
          Phrases are given a boost to increase their chances of being recognized correctly.
          Args:
              audio_file (str): Path to the local audio file to be transcribed.
          Returns:
              cloud_speech.RecognizeResponse: The full response object which includes the transcription results.
          """
      
          # Instantiates a client
          client = SpeechClient()
      
          # Reads a file as bytes
          with open(audio_file, "rb") as f:
              audio_content = f.read()
      
          # Build inline phrase set to produce a more accurate transcript
          phrase_set = cloud_speech.PhraseSet(
              phrases=[{"value": "fare", "boost": 10}, {"value": "word", "boost": 20}]
          )
          adaptation = cloud_speech.SpeechAdaptation(
              phrase_sets=[
                  cloud_speech.SpeechAdaptation.AdaptationPhraseSet(
                      inline_phrase_set=phrase_set
                  )
              ]
          )
          config = cloud_speech.RecognitionConfig(
              auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
              adaptation=adaptation,
              language_codes=["en-US"],
              model="short",
          )
      
          # Prepare the request which includes specifying the recognizer, configuration, and the audio content
          request = cloud_speech.RecognizeRequest(
              recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
              config=config,
              content=audio_content,
          )
      
          # Transcribes the audio into text
          response = client.recognize(request=request)
      
          for result in response.results:
              print(f"Transcript: {result.alternatives[0].transcript}")
      
          return response
      
      
      1. このサンプルでは、同じフレーズを含む PhraseSet リソースを作成し、認識リクエストでそれを参照します。

      Python

      import os
      
      from google.cloud.speech_v2 import SpeechClient
      from google.cloud.speech_v2.types import cloud_speech
      
      PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
      
      
      def adaptation_v2_phrase_set_reference(
          audio_file: str,
          phrase_set_id: str,
      ) -> cloud_speech.RecognizeResponse:
          """Transcribe audio files using a PhraseSet.
          Args:
              audio_file (str): Path to the local audio file to be transcribed.
              phrase_set_id (str): The unique ID of the PhraseSet to use.
          Returns:
              cloud_speech.RecognizeResponse: The full response object which includes the transcription results.
          """
      
          # Instantiates a client
          client = SpeechClient()
      
          # Reads a file as bytes
          with open(audio_file, "rb") as f:
              audio_content = f.read()
      
          # Creating operation of creating the PhraseSet on the cloud.
          operation = client.create_phrase_set(
              parent=f"projects/{PROJECT_ID}/locations/global",
              phrase_set_id=phrase_set_id,
              phrase_set=cloud_speech.PhraseSet(phrases=[{"value": "fare", "boost": 10}]),
          )
          phrase_set = operation.result()
      
          # Add a reference of the PhraseSet into the recognition request
          adaptation = cloud_speech.SpeechAdaptation(
              phrase_sets=[
                  cloud_speech.SpeechAdaptation.AdaptationPhraseSet(
                      phrase_set=phrase_set.name
                  )
              ]
          )
      
          # Automatically detect audio encoding. Use "short" model for short utterances.
          config = cloud_speech.RecognitionConfig(
              auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
              adaptation=adaptation,
              language_codes=["en-US"],
              model="short",
          )
          #  Prepare the request which includes specifying the recognizer, configuration, and the audio content
          request = cloud_speech.RecognizeRequest(
              recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
              config=config,
              content=audio_content,
          )
          # Transcribes the audio into text
          response = client.recognize(request=request)
      
          for result in response.results:
              print(f"Transcript: {result.alternatives[0].transcript}")
      
          return response
      
      

      CustomClass を使用して音声文字変換の結果を改善する

      1. 次のサンプルでは、項目「fare」と名前「fare」を含む CustomClass を作成します。次に、認識リクエストで inline_phrase_set 内の CustomClass を参照します。

      Python

      import os
      
      from google.cloud.speech_v2 import SpeechClient
      from google.cloud.speech_v2.types import cloud_speech
      
      PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
      
      
      def adaptation_v2_inline_custom_class(
          audio_file: str,
      ) -> cloud_speech.RecognizeResponse:
          """Transcribe audio file using inline custom class.
          The inline custom class helps the recognizer produce more accurate transcriptions for specific terms.
          Args:
              audio_file (str): Path to the local audio file to be transcribed.
          Returns:
              cloud_speech.RecognizeResponse: The response object which includes the transcription results.
          """
          # Instantiates a client
          client = SpeechClient()
      
          # Reads a file as bytes
          with open(audio_file, "rb") as f:
              audio_content = f.read()
      
          # Define an inline custom class to enhance recognition accuracy with specific items like "fare" etc.
          custom_class_name = "your-class-name"
          custom_class = cloud_speech.CustomClass(
              name=custom_class_name,
              items=[{"value": "fare"}],
          )
      
          # Build inline phrase set to produce a more accurate transcript
          phrase_set = cloud_speech.PhraseSet(
              phrases=[{"value": custom_class_name, "boost": 20}]
          )
          adaptation = cloud_speech.SpeechAdaptation(
              phrase_sets=[
                  cloud_speech.SpeechAdaptation.AdaptationPhraseSet(
                      inline_phrase_set=phrase_set
                  )
              ],
              custom_classes=[custom_class],
          )
          config = cloud_speech.RecognitionConfig(
              auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
              adaptation=adaptation,
              language_codes=["en-US"],
              model="short",
          )
      
          # Prepare the request which includes specifying the recognizer, configuration, and the audio content
          request = cloud_speech.RecognizeRequest(
              recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
              config=config,
              content=audio_content,
          )
      
          # Transcribes the audio into text
          response = client.recognize(request=request)
      
          for result in response.results:
              print(f"Transcript: {result.alternatives[0].transcript}")
      
          return response
      
      
      1. このサンプルでは、同じアイテムを含む CustomClass リソースを作成します。次に、リソース名 CustomClass を参照するフレーズを含む PhraseSet リソースを作成します。次に、認識リクエストで PhraseSet リソースを参照します。

      Python

      import os
      
      from google.cloud.speech_v2 import SpeechClient
      from google.cloud.speech_v2.types import cloud_speech
      
      PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
      
      
      def adaptation_v2_custom_class_reference(
          audio_file: str, phrase_set_id: str, custom_class_id: str
      ) -> cloud_speech.RecognizeResponse:
          """Transcribe audio file using a custom class.
          Args:
              audio_file (str): Path to the local audio file to be transcribed.
              phrase_set_id (str): The unique ID of the phrase set to use.
              custom_class_id (str): The unique ID of the custom class to use.
          Returns:
              cloud_speech.RecognizeResponse: The full response object which includes the transcription results.
          """
          # Instantiates a speech client
          client = SpeechClient()
      
          # Reads a file as bytes
          with open(audio_file, "rb") as f:
              audio_content = f.read()
      
          # Create a custom class to improve recognition accuracy for specific terms
          custom_class = cloud_speech.CustomClass(items=[{"value": "fare"}])
          operation = client.create_custom_class(
              parent=f"projects/{PROJECT_ID}/locations/global",
              custom_class_id=custom_class_id,
              custom_class=custom_class,
          )
          custom_class = operation.result()
      
          # Create a persistent PhraseSet to reference in a recognition request
          created_phrase_set = cloud_speech.PhraseSet(
              phrases=[
                  {
                      "value": f"${{{custom_class.name}}}",
                      "boost": 20,
                  },  # Using custom class reference
              ]
          )
          operation = client.create_phrase_set(
              parent=f"projects/{PROJECT_ID}/locations/global",
              phrase_set_id=phrase_set_id,
              phrase_set=created_phrase_set,
          )
          phrase_set = operation.result()
      
          # Add a reference of the PhraseSet into the recognition request
          adaptation = cloud_speech.SpeechAdaptation(
              phrase_sets=[
                  cloud_speech.SpeechAdaptation.AdaptationPhraseSet(
                      phrase_set=phrase_set.name
                  )
              ]
          )
          # Automatically detect the audio's encoding with short audio model
          config = cloud_speech.RecognitionConfig(
              auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
              adaptation=adaptation,
              language_codes=["en-US"],
              model="short",
          )
      
          # Create a custom class to reference in a PhraseSet
          request = cloud_speech.RecognizeRequest(
              recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
              config=config,
              content=audio_content,
          )
      
          # Transcribes the audio into text
          response = client.recognize(request=request)
      
          for result in response.results:
              print(f"Transcript: {result.alternatives[0].transcript}")
      
          return response
      
      

      クリーンアップ

      このページで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、次の操作を行います。

      1. 作成した認証情報を取り消して、ローカル認証情報ファイルを削除します。

        gcloud auth application-default revoke
      2. (省略可)gcloud CLI から認証情報を取り消します。

        gcloud auth revoke

      コンソール

    14. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    15. In the project list, select the project that you want to delete, and then click Delete.
    16. In the dialog, type the project ID, and then click Shut down to delete the project.
    17. gcloud

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

      次のステップ