Rainbow Engine

IT技術を分かりやすく簡潔にまとめることによる学習の効率化、また日常の気付きを記録に残すことを目指します。

Azure Microsoft

Azure Machine LearningからAzure StorageのFile Shares(ファイル共有)のデータにアクセスする方法

投稿日:2023年5月26日 更新日:

 

<目次>

(1) Azure Machine LearningからAzure StorageのFile Shares(ファイル共有)のデータにアクセスする方法
 やりたいこと
 (1-1) STEP1:前提条件(例)
 (1-2) STEP2:「Manage Indentity」を持つCompute Clusterの設定
 (1-3) STEP3:File Sharesへのデータアクセス処理の記述&実行
 (1-4) STEP4:実行と結果確認

(1) Azure Machine LearningからAzure StorageのFile Shares(ファイル共有)のデータにアクセスする方法

やりたいこと

・Azure Machine LearningからAzure Storage File Shares(ファイル共有)のファイル(例:画像データ)にアクセスしたい。
・それに際して、Azure Storageの権限設定など、必要となる設定を明らかにする(Contributer権限やReader権限など)
 
(図100)

(1-1) STEP1:前提条件(例)

●STEP1-1:Azure Machine Learning

・Azure Machine Learningのリソースが作成済
・Compute clustersも作成済
(図111)

●STEP1-2:Azure Storage

・Azure Storage File Shares(ファイル共有)が作成済
・上記がAzure Machine Learningと紐づいている(リソース作成時に紐づけ)
 
(図112)

(1-2) STEP2:「Manage Indentity」を持つCompute Clusterの設定

(概要)
ラベル 説明
設定操作 Azure Machine LearningのCompute clusterに対して「Manage Identity」の設定を行います。
サポートするアイデンティティの種類 「one system-assigned identity」または「multiple user-assigned identities」をサポートします。
デフォルト設定 デフォルトは「one system-assigned identity」を使用します。
用途説明 Azure MLにおけるストレージのマウント、データストア、コンテナレジストリ等で使用されます。

●STEP2-1:Managed Identityの作成

・①Azure PortalからManaged Identityを開き「+作成」を押下
(図121)
・②必要情報を入力して作成
(図122)
(図123)
・③「プロパティ」ブレードの「ID」値をコピーします。
⇒次のサンプルプログラムの「resource_id」に代入します。
(図124)

●STEP2-2:Managed Identityを持つCompute Clusterの作成

・Azure Machine LearningのNoteBooksにデータアクセス用のPythonプログラムを記述します。
・CELL1⇒CELL2⇒CELL3の順番に実行します。
 
・CELL1(サンプル)
# Handle to the workspace
from azure.ai.ml import MLClient
# Authentication package
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
・CELL2(サンプル)
# Get a handle to the workspace
ml_client = MLClient(
    credential=credential,
    subscription_id="[ご自身のSubscription ID]",
    resource_group_name="rainbow-machine-learning",
    workspace_name="rainbow-ml-workspace",
)
(図125①)
 
・CELL3(サンプル)
※resource_id:STEP2-1の最後でコピーした値を代入します。
# (参考)https://learn.microsoft.com/en-us/answers/questions/1148536/cannot-access-azure-storage-file-shares-from-azure.html
from azure.ai.ml.entities import ManagedIdentityConfiguration, IdentityConfiguration, AmlCompute
from azure.ai.ml.constants import ManagedServiceIdentityType

# Create an identity configuration from the user-assigned managed identity
managed_identity = ManagedIdentityConfiguration(
    resource_id="/subscriptions/[ご自身のSubscription ID]/resourcegroups/rainbow-machine-learning/providers/Microsoft.ManagedIdentity/userAssignedIdentities/[ご自身のManaged IDのリソース名]"
    )
identity_config = IdentityConfiguration(type = ManagedServiceIdentityType.USER_ASSIGNED, user_assigned_identities=[managed_identity])

# specify aml compute name.
gpu_compute_target = "gpu-cluster-rainbow2"

try:
    ml_client.compute.get(gpu_compute_target)
except Exception:
    print("Creating a new gpu compute target...")
    # Pass the identity configuration
    compute = AmlCompute(
        name=gpu_compute_target, size="Standard_NC4as_T4_v3", min_instances=0, max_instances=4, identity=identity_config
    )
    ml_client.compute.begin_create_or_update(compute)
(図125②)

●STEP2-3:Compute Clusterの作成結果確認

「Managed Identity」に値が入った状態で、Compute Clusterが作成されているはずです。
(図126)
(図127)

(1-3) STEP3:File Sharesへのデータアクセス処理の記述&実行

●STEP3-1:Azure Machine LearningのNotebooksの実装

Azure Machine LearningのNoteBooksにデータアクセス用のPythonプログラムを記述して、実行します。
 
・①下記コードをAzure Machine LearningのNotebooksのセルに入力します。
 
CELL4
from azure.ai.ml import command
from azure.ai.ml import UserIdentityConfiguration
from azure.ai.ml import Input

gpu_compute_target = "gpu-cluster-rainbow2"
custom_env_name = "keras-env"
# ↓正常終了
web_path = "./data"

# ジョブの最新環境を取得
job_env = ml_client.environments.get(name=custom_env_name, version=str(len(list(ml_client.environments.list(name=custom_env_name)))))
# ジョブの定義
job = command(
    # 入力パラメータの設定
    inputs=dict(
        data_folder=Input(type="uri_folder", path=web_path),
    ), 
    # CPU/GPUリソース
    compute=gpu_compute_target,
    # 実行環境
    environment=f"{job_env.name}:{job_env.version}",
    # 実行対象モジュールの格納先
    code="./src/",
    # 実行対象モジュールと引数
    command="python fileshare_test_get_image.py --data-folder ${{inputs.data_folder}}",
    experiment_name="fileshare_test_get_image",
    display_name="fileshare_test_get_image",
)
# ジョブの実行
ml_client.jobs.create_or_update(job)
CPUクラスター、実行環境、データ格納先などを指定して、データアクセス用のPythonプログラムを呼び出しする処理です。実行するとJOBが生成され、JOBメニューから状況をモニタリングできます。
 
また、web_pathで指定したパスは環境(Dockerコンテナ)側にもマウントされて、アクセスが可能になります。
 
(図141)

(補足)備忘メモ
web_pathを下記のように指定すると「key: “NonCompliantReason”, value: “DataAccessError(Unknown(\”unsuccessful status code 400 Bad Request, body \”, None))エラーとなった。

web_path = "https://rainbowmlstorage.file.core.windows.net/code-391ff5ac-6576-460f-ba4d-7e03433c68b6/Users/xxxx/datastore_test/data"
↓参考
 

●STEP3-2:NoteBooks経由で呼び出すPythonプログラムの実装

ここで、ジョブとして実行する処理(画像の処理や、モデル学習など)を記述します。今回は疎通確認用の処理として以下を行います。
 
(例)引数を受け取って、画像を(Azure MLワークスペースから)取得して、環境内(Dockerコンテナ)に保存してみる
 
(図142)

・①下記コードをAzure Machine LearningのNotebooksのセルに入力します。
 
(サンプルプログラム)fileshare_test_get_image.py
from os import listdir
import os
import argparse
import matplotlib.image as mpimg
import mlflow

print("--- 引数の取得START")
parser = argparse.ArgumentParser()
# データセットの格納場所を設定
parser.add_argument(
    "--data-folder",
    type=str,
    dest="data_folder",
    default="data",
    help="data folder mounting point",
)
args = parser.parse_args()

# Start Logging
mlflow.start_run()

data_folder = args.data_folder
print("--- 引数の取得END")

photos, labels = list(), list()
# ディレクトリ内のサブフォルダをループ
print("===== LISTDIR: "+str(listdir(data_folder)))
for sub_folder in listdir(data_folder):
    
    # サブフォルダ内の子フォルダをループ
    print("===== SUBFOLDER: "+data_folder+"/"+sub_folder)

    # data配下が「ディレクトリか?」「ファイルか?」のチェック
    if os.path.isdir(data_folder+"/"+sub_folder):
        
        # サブディレクトリの場合、その中のファイルをループ 
        for file in listdir(data_folder+"/"+sub_folder):
            # サブフォルダ内の子フォルダをループ
            print("===== FILE: "+file)
            if file.startswith('.amlignore'):
                continue
            else:
                # データを表示する
                img = mpimg.imread(data_folder +"/"+ sub_folder +"/"+ file)
                mlflow.log_image(img, str(data_folder +"/"+ sub_folder +"/"+ file))
                
    else:
        # サブフォルダ内の子フォルダをループ
        print("===== FILE: "+sub_folder)
        # データを表示する
        img = mpimg.imread(data_folder +"/"+sub_folder)
        mlflow.log_image(img, str(data_folder +"/"+sub_folder))

mlflow.end_run()


これはAzure MLのワークスペース内にある画像を、Pythonプログラム内で読み取り、機械学習の環境であるDockerコンテナ内に保存する処理です(特に意味ナシ、疎通目的)。

(図143)

目次にもどる

(1-4) STEP4:実行と結果確認

●STEP4-1:セルを順番に実行

・認証 ⇒ DataStore作成 ⇒ ジョブ作成と順番に実行していきます。
(図144)

●STEP4-2:結果確認

⇒Dockerコンテナ内にも、犬と猫の画像が保存されました
(図145)

Adsense審査用広告コード


Adsense審査用広告コード


-Azure, Microsoft
-

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

Azure Blob Storage上にあるPDFファイルをテキスト変換する方法

  <目次> (1) Azure Blob Storage上にあるPDFファイルをテキスト変換する方法  やりたいこと  (1-0) STEP0:前提条件  (1-1) STEP1:Pyth …

Azure Cosmos DBとは?特徴や構造およびデータ形式について

  <目次> (1) Azure Cosmos DBとは?特徴や構造およびデータ形式について  (1-1) Azure Cosmos DBとは?  (1-2) Azure Cosmos DB …

AzureのAZ900の試験対策について(Microsoft Azure Fundamentals)

  <目次> (1) AzureのAZ900の試験対策について(Microsoft Azure Fundamentals)  (1-1) AZ900(Microsoft Azure Fund …

Azure Machine LearningでDataStoreを作成してAzure Storage File Shares(ファイル共有)のデータを操作する方法

  <目次> (1) Azure Machine LearningでDataStoreを作成してAzure Storage File Shares(ファイル共有)のデータを操作する方法  ( …

Azure Machine Learningで「Failed to pull Docker image」が出る原因と対処法について

  <目次> (1) Azure Machine Learningで「Failed to pull Docker image」が出る原因と対処法について  (1-1) エラー概要  (1-2 …

  • English (United States)
  • 日本語
Top