簡體   English   中英

無法訪問 Azure 機器學習服務筆記本上安裝的數據集

[英]Can't access mounted Dataset on Azure Machine Learning Service Notebook

我正在使用 Azure 機器學習服務的筆記本功能。 在這個筆記本中,我連接到工作區,檢索了相應的數據存儲,並將我的文件檢索為文件數據集 object。到目前為止一切正常。

from azureml.core import Workspace, Datastore, Dataset
import pandas as pd
import os

workspace = Workspace.from_config()
container="cnt_name"
file_path = 'actual_path'
# get datstore and dataset
datastore = Datastore.get(workspace, container)
datastore_path = [(datastore, file_path )]
dataset = Dataset.File.from_files(datastore_path)

現在我嘗試掛載這個 file_dataset

mounted_path = "/tmp/test_dir4"
dataset_mounted = dataset.mount(mounted_path)

一切似乎都很好。 快速 ls 給出以下 output:

    ls -ltr /tmp/
    prwx------ 1 azureuser azureuser    0 May 12 13:29 clr-debug-pipe-14801-259046-out
    prwx------ 1 azureuser azureuser    0 May 12 13:29 clr-debug-pipe-14801-259046-in
    d--------- 0 root      root         0 May 12 13:29 test_dir4
    drwx------ 3 azureuser azureuser 4096 May 12 13:29 tmpjrb2tx8g
    -rw------- 1 azureuser azureuser  364 May 12 13:29 tmp5w_ikt6j
    drwx------ 2 azureuser azureuser 4096 May 12 13:29 pyright-14886-W3YT3PTdzoIO

但這是我的問題:掛載的文件夾是由 root 用戶掛載的。 我無法訪問它 - 無論是從筆記本還是從 shell。ls 產生典型的錯誤path not foundpermission denied

你快到了! dataset.mount(mounted_path)有點令人不安,但它實際上會返回一個mount context ,您需要在之后啟動它才能使其工作如下:

# mount dataset onto the mounted_path of a Linux-based compute
mount_context = dataset.mount(mounted_path)

mount_context.start()

之后,您可以使用以下代碼檢查您確實可以訪問這些文件:

import os
print(os.listdir(mounted_path))

我發現最好避免明確說明安裝位置,如下所示:

dataset_mounted = dataset.mount()

這將返回一個必須使用 .start() 和 .stop() 方法(顯式或隱式使用 Python 上下文)激活的安裝上下文。

當掛載上下文處於活動狀態時,您可以使用dataset_mounted.mount_point就像在文件操作中使用文本字符串指定目錄一樣。 例如,如果您的文件數據集包含一個名為 spar.png 的圖像,您可以在 Jupyter notebook 中使用以下代碼顯示它:

from IPython.display import Image, display

dataset_mounted = dataset.mount()
dataset_mounted.start()
test_image = Image(dataset_mounted.mount_point + '/spar.png')
display(test_image)
dataset_mounted.stop()

我還鼓勵將 Python上下文管理器一起使用,以使代碼更清晰並減少掛載未關閉的可能性:

from IPython.display import Image, display

with dataset.mount() as mount_context:
    test_image = Image(mount_context.mount_point + '/spar.png')
    display(test_image)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM