TensorFlow를 사용한 Azure Blob Storage

TensorFlow.org에서 보기 Google Colab에서 실행 GitHub에서 소스 보기 노트북 다운로드

개요

이 튜토리얼 쇼 방법에 대한 읽기 및 쓰기 파일을 사용하는 푸른 물방울 저장 TensorFlow IO의 푸른 파일 시스템의 통합을 통해, TensorFlow와 함께.

Azure Blob Storage에서 파일을 읽고 쓰려면 Azure Storage 계정이 필요합니다. Azure Storage 키는 환경 변수를 통해 제공되어야 합니다.

os.environ['TF_AZURE_STORAGE_KEY'] = '<key>'

스토리지 계정 이름과 컨테이너 이름은 파일 이름 uri의 일부입니다.

azfs://<storage-account-name>/<container-name>/<path>

이 튜토리얼에서는, 데모 목적을 위해 당신이 할 수있는 선택적으로 설치 아주 라이트 푸른 저장 에뮬레이터입니다. Azurite 에뮬레이터를 사용하면 TensorFlow와 함께 Azure Blob Storage 인터페이스를 통해 파일을 읽고 쓸 수 있습니다.

설정 및 사용

필요한 패키지를 설치하고 런타임을 다시 시작하십시오.

try:
  %tensorflow_version 2.x 
except Exception:
  pass

!pip install tensorflow-io

Azurite 설치 및 설정(선택 사항)

Azure Storage 계정을 사용할 수 없는 경우 Azure Storage 인터페이스를 에뮬레이트하는 Azurite를 설치 및 설정하려면 다음이 필요합니다.

npm install azurite@2.7.0
[K[?25hnpm WARN deprecated request@2.87.0: request has been deprecated, see https://github.com/request/request/issues/3142
[K[?25hnpm WARN saveError ENOENT: no such file or directory, open '/content/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/content/package.json'
npm WARN content No description
npm WARN content No repository field.
npm WARN content No README data
npm WARN content No license field.

+ azurite@2.7.0
added 116 packages from 141 contributors in 6.591s
# The path for npm might not be exposed in PATH env,
# you can find it out through 'npm bin' command
npm_bin_path = get_ipython().getoutput('npm bin')[0]
print('npm bin path: ', npm_bin_path)

# Run `azurite-blob -s` as a background process. 
# IPython doesn't recognize `&` in inline bash cells.
get_ipython().system_raw(npm_bin_path + '/' + 'azurite-blob -s &')
npm bin path:  /content/node_modules/.bin

TensorFlow를 사용하여 Azure Storage에 파일 읽기 및 쓰기

다음은 TensorFlow의 API를 사용하여 Azure Storage에 파일을 읽고 쓰는 예입니다.

그것은 한 번 TensorFlow에서 다른 파일 시스템 (예를 들어, POSIX 또는 GCS)와 같은 방식으로 작동 tensorflow-io 로 패키지를 가져, tensorflow-io 자동으로 등록합니다 azfs 사용하기위한 계획을.

푸른 저장 키를 통해 제공되어야한다 TF_AZURE_STORAGE_KEY 환경 변수입니다. 그렇지 않으면 TF_AZURE_USE_DEV_STORAGE 설정 될 수있는 True 대신 아주 라이트 에뮬레이터를 사용 :

import os
import tensorflow as tf
import tensorflow_io as tfio

# Switch to False to use Azure Storage instead:
use_emulator = True

if use_emulator:
  os.environ['TF_AZURE_USE_DEV_STORAGE'] = '1'
  account_name = 'devstoreaccount1'
else:
  # Replace <key> with Azure Storage Key, and <account> with Azure Storage Account
  os.environ['TF_AZURE_STORAGE_KEY'] = '<key>'
  account_name = '<account>'

  # Alternatively, you can use a shared access signature (SAS) to authenticate with the Azure Storage Account
  os.environ['TF_AZURE_STORAGE_SAS'] = '<your sas>'
  account_name = '<account>'
pathname = 'az://{}/aztest'.format(account_name)
tf.io.gfile.mkdir(pathname)

filename = pathname + '/hello.txt'
with tf.io.gfile.GFile(filename, mode='w') as w:
  w.write("Hello, world!")

with tf.io.gfile.GFile(filename, mode='r') as r:
  print(r.read())
Hello, world!

구성

TensorFlow의 Azure Blob Storage 구성은 항상 환경 변수를 통해 수행됩니다. 다음은 사용 가능한 구성의 전체 목록입니다.

  • TF_AZURE_USE_DEV_STORAGE : '//devstoreaccount1/container/file.txt AZ'1 세트와 같은 연결을위한 지역 개발 저장 에뮬레이터를 사용합니다. 그래서 이것은 다른 모든 설정보다 우선 걸릴 unset 다른 연결을 사용하도록
  • TF_AZURE_STORAGE_KEY : 사용중인 스토리지 계정에 대한 계정 키
  • TF_AZURE_STORAGE_USE_HTTP : 당신이 HTTPS 전송을 사용하지 않으려는 경우 임의의 값으로 설정합니다. unset HTTPS의 기본을 사용하기
  • TF_AZURE_STORAGE_BLOB_ENDPOINT : BLOB 저장소의 엔드 포인트로 설정 - 기본값은 .core.windows.net .