简体   繁体   English

无法在ML Engine中导入Google Cloud Storage库

[英]Unable to import Google Cloud Storage library in ML Engine

As a quick solution to get some images processed in batch within ML Engine I am using the Cloud Storage Python library to download the images. 作为在ML Engine中批量处理某些图像的快速解决方案,我正在使用Cloud Storage Python库下载图像。

Unfortunately it seems that when the job is sent to ML Engine, the library import fails with the following stack trace: 不幸的是,当将作业发送到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

I am pretty sure that the library is included within the ML engine image (it would be weird if it wasn't) so I am at a loss here, the program runs fine locally. 我很确定该库包含在ML引擎映像中(如果没有的话会很奇怪),所以我在这里很茫然,该程序在本地运行良好。

The container does not contain this package because typically you use TensorFlow's file_io module, which works with GCS. 该容器不包含此软件包,因为通常您使用TensorFlow的file_io模块,该模块可与GCS一起使用。

Two options. 两种选择。 Assuming you already know how to use and/or have code for google.cloud.storage , you can just add it as a requirement in your setup.py file ( instructions ), for example: 假设您已经知道如何使用google.cloud.storage和/或拥有google.cloud.storage代码,则可以根据需要将其添加到setup.py文件( 说明 )中,例如:

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.'
)

Or, you can use file_io , which is especially useful if you don't actually need copies of the data but want to read them directly: 或者,您可以使用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

Finally, note that if you're using TensorFlow operations, TensorFlow's readers already know how to read from GCS and so there is no need to manually manipulate the files. 最后,请注意,如果您正在使用TensorFlow操作,则TensorFlow的读者已经知道如何从GCS进行读取,因此无需手动操作文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM