Azure Machine Learning Studioで作成したモデルをプログラムから再トレーニングする
2019-08-08
azblob://2022/11/11/eyecatch/2019-08-08-azure-ml-studio-retraining-000.jpg

はじめに

Azure Machine Learning StudioでWeb APIを作成しました。このWeb APIを実運用する際には、日々モデルの再トレーニングが必要となってきます。そこで公式ドキュメント(https://docs.microsoft.com/ja-jp/azure/machine-learning/studio/retrain-classic-web-service)を参考に進めていったのですが、かなり詰まってしまったのでやり方を共有しておきたいと思います。

Webサービスのデプロイ

まずはWebサービス(Web API)の作成をします。詳しいやり方は公式ドキュメント(https://docs.microsoft.com/ja-jp/azure/machine-learning/studio/create-experiment)を参照してください。

予測Webサービスの作成

中のロジックはともかく、だいたいこのような図になるかと思います。

そこでRUN -> SET UP WEB SERVICE -> Predictive Web service[Recommend]

RUN -> DEPLOY WEB SERVICE -> Deploy Web Service[Classic]

これで予測Webサービスがデプロイされました。

再トレーニングWebサービスの作成

こちらのドキュメント(https://docs.microsoft.com/ja-jp/azure/machine-learning/studio/retrain-machine-learning-model)を参考に進めました。

サイドバーのExperiments -> 始めに作成したトレーニングWebサービス(Predictiveではないほう) -> SET UP WEB SERVICE -> Retraining Web Service

RUN -> SET UP WEB SERVICE -> Deploy Web Service[Classic]

これで再トレーニングWebサービスがデプロイされました。

再トレーニングの実行

次に新しいデータセットを用いてモデルの再トレーニングを行います。

再トレーニングの流れは、

  1. 新しいデータセットをローカルからBlobへアップロードする
  2. Blobのデータセットで再トレーニングさせる
  3. 再トレーニングモデルをBlobへアップロードする

となります。 そのため、事前準備としてAzure上にBlobを作成しておく必要があります(Azure Machine Learning StudioワークスペースをAzureポータル上で作成した場合は自動的に作成されています)。

サイドバーのWeb Services -> 再トレーニングWebサービス -> test

Consume -> Sample Code -> Batch

サンプルコードを実行するため、Visual StudioからC#コンソールアプリケーションを作成します。 [新規] > [プロジェクト] > [Visual C#] > [Windows Classic Desktop] > [コンソール アプリ (.NET Framework)]

作成されたProgram.csにBatchのサンプルコードを貼り付けます。このとき、apiKeyStorageAccountName等を適切なものに置き換えます。 また、NugetパッケージのMicrosoft.AspNet.WebApi.Clientをインストールします。

const string apiKey = "abc123"; // Replace this with the API key for the web service

apiKey:再トレーニングWebサービスのAPI Key

const string StorageAccountName = "mystorageacct"; // Replace this with your Azure Storage Account name
const string StorageAccountKey = "a_storage_account_key"; // Replace this with your Azure Storage Key
const string StorageContainerName = "mycontainer"; // Replace this with your Azure Storage Container name

StorageAccountName:ストレージアカウント名

StorageAccountKey:ストレージアカウントのアクセスキー

StorageContainerName:コンテナ名

UploadFileToBlob("input1data.file_extension" /*Replace this with the location of your input file, and valid file extension (usually .csv)*/,
                    "input1datablob.file_extension" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                    StorageContainerName, storageConnectionString);
"input1",
    new AzureBlobDataReference()
    {
        ConnectionString = storageConnectionString,
        RelativeLocation = string.Format("{0}/input1datablob.file_extension", 
                           StorageContainerName)
    }

input1data.file_extension:新しいデータセットのアドレス

input1datablob.file_extension:保存するデータセットのBlob上での名前

{
    "output2",
        new AzureBlobDataReference()
        {
            ConnectionString = storageConnectionString,
                               RelativeLocation = string.Format("{0}/output2results.file_extension", StorageContainerName) /*Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)*/
        }
},
{
    "output1",
        new AzureBlobDataReference()
        {
            ConnectionString = storageConnectionString,
                               RelativeLocation = string.Format("{0}/output1results.file_extension", StorageContainerName) /*Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)*/
        }
},

output2results.file_extension:再トレーニング済みモデル名(拡張子を.ilearnerにする)

output1results.file_extension:再トレーニングのログファイル名(拡張子を.csvにする)

コンソールアプリを実行すると再トレーニングが実行され、再トレーニング済みモデルがBlob Storageに出力されます。

再トレーニングモデルの適用

新たにエンドポイントを作成し、それに対して再トレーニングモデルの適用を行います。エンドポイントは予測Webサービスに追加します。

サイドバーのWeb Services -> 予測Webサービス -> New Web Services Experience -> 戻るボタン -> NEW 適当な名前を付けてエンドポイントを作成します。

エンドポイントの名前 -> Consume -> PATCH api help

再トレーニングと同様にコンソールアプリケーションを作成し、サンプルコードを貼り付けます。

apikeyBaseLocation等も適宜置き換えます。

作成したコンソールアプリケーションを実行すると、指定したBlob上の再トレーニングモデルが作成したエンドポイントへ適用されます。

おわりに

いろいろ調べたのですが、Classic版でないとC#プログラム上からの更新はできないみたいでした(new版はPowerShellでの更新)。できるよ!ということであれば教えていただきたいです。

ともあれ、C#から更新ができたので他のAPIとの連携がしやすくなりました。

azblob://2024/04/12/eyecatch/2024-04-12-gemini-1.5-000.jpg
2024/04/11
AI/Machine Learning