无法在 ML Engine 中导入 Google 云存储库
Unable to import Google Cloud Storage library in ML Engine
作为在 ML Engine 中批量处理一些图像的快速解决方案,我使用云存储 Python 库下载图像。
不幸的是,当作业发送到 ML Engine 时,库导入失败并显示以下堆栈跟踪:
Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/root/.local/lib/python2.7/site-packages/trainer/task.py", line 1, in <module> from google.cloud import storage ImportError: cannot import name storage
我很确定该库包含在 ML 引擎映像中(如果不是的话会很奇怪)所以我在这里不知所措,程序在本地运行良好。
容器不包含此包,因为通常您使用 TensorFlow 的 file_io
模块,该模块与 GCS 配合使用。
两种选择。假设您已经知道如何使用 and/or 并拥有 google.cloud.storage
的代码,您可以将其作为要求添加到 setup.py
文件 (instructions) 中,例如:
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['google-cloud-storage']
setup(
name='trainer',
version='0.1',
install_requires=REQUIRED_PACKAGES,
packages=find_packages(),
include_package_data=True,
description='My trainer application package.'
)
或者,您可以使用 file_io
,如果您实际上不需要数据副本但想直接读取它们,这将特别有用:
import tensorflow as tf
from tensorflow.python.lib.io import file_io
# Copy
file_io.copy("gs://my-bucket/myfiles/*", "/tmp")
# Glob and read
for file in file_io.get_matching_files("gs://my-bucket/myfiles/*"):
with file_io.FileIO(file) as f:
# Do something
最后请注意,如果您使用的是 TensorFlow 操作,TensorFlow 的读者已经知道如何从 GCS 读取数据,因此无需手动操作文件。
作为在 ML Engine 中批量处理一些图像的快速解决方案,我使用云存储 Python 库下载图像。
不幸的是,当作业发送到 ML Engine 时,库导入失败并显示以下堆栈跟踪:
Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/root/.local/lib/python2.7/site-packages/trainer/task.py", line 1, in <module> from google.cloud import storage ImportError: cannot import name storage
我很确定该库包含在 ML 引擎映像中(如果不是的话会很奇怪)所以我在这里不知所措,程序在本地运行良好。
容器不包含此包,因为通常您使用 TensorFlow 的 file_io
模块,该模块与 GCS 配合使用。
两种选择。假设您已经知道如何使用 and/or 并拥有 google.cloud.storage
的代码,您可以将其作为要求添加到 setup.py
文件 (instructions) 中,例如:
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['google-cloud-storage']
setup(
name='trainer',
version='0.1',
install_requires=REQUIRED_PACKAGES,
packages=find_packages(),
include_package_data=True,
description='My trainer application package.'
)
或者,您可以使用 file_io
,如果您实际上不需要数据副本但想直接读取它们,这将特别有用:
import tensorflow as tf
from tensorflow.python.lib.io import file_io
# Copy
file_io.copy("gs://my-bucket/myfiles/*", "/tmp")
# Glob and read
for file in file_io.get_matching_files("gs://my-bucket/myfiles/*"):
with file_io.FileIO(file) as f:
# Do something
最后请注意,如果您使用的是 TensorFlow 操作,TensorFlow 的读者已经知道如何从 GCS 读取数据,因此无需手动操作文件。