S3 对象存储的使用越来越广泛,其中的好处就不多说了,这里用 Tensorflow 举个例子。
https://github.com/tensorflow/examples/blob/master/community/en/docs/deploy/s3.md
Tensorflow 本身就支持从 S3 中读写数据的。在 TenC 弹性计算平台上,用户可以通过指定 AWS_ACCESS_KEY_ID
和 AWS_SECRET_ACCESS_KEY
两个环境变量来校验(有些场景下,已经由平台自动绑定了环境变量)。
# Credentials only needed if connecting to a private endpoint
AWS_ACCESS_KEY_ID=XXXXX
AWS_SECRET_ACCESS_KEY=XXXXX
# Region for the S3 bucket, this is not always needed. Default is us-east-1.
AWS_REGION=us-east-1
# The S3 API Endpoint to connect to. This is specified in a HOST:PORT format.
S3_ENDPOINT=s3.us-east-1.amazonaws.com
# Whether or not to use HTTPS. Disable with 0.
S3_USE_HTTPS=1
# If HTTPS is used, controls if SSL should be enabled. Disable with 0.
S3_VERIFY_SSL=1
下面是基于镜像 tensorflow/tensorflow:1.15.2-py3
的一个 demo。
export AWS_ACCESS_KEY_ID=runzhliu-demo-key
export AWS_SECRET_ACCESS_KEY=runzhliu-demo-secret
export S3_ENDPOINT=9.25.151.xxx:7480
export S3_USE_HTTPS=0
测试的程序。
代码语言:javascript复制from tensorflow.python.lib.io import file_io
print(file_io.stat('s3://runzhliu__demo/Tensorflow_On_S3/adobegc.log'))
详细的过程。
代码语言:javascript复制# export AWS_ACCESS_KEY_ID=runzhliu-demo-08ff2955
# export AWS_SECRET_ACCESS_KEY=runzhliu-demo-b4068ecf
# export S3_ENDPOINT=9.25.151.198:7480
# export S3_USE_HTTPS=0
# python
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow.python.lib.io import file_io
>>> print(file_io.stat('s3://runzhliu__demo/Tensorflow_On_S3/adobegc.log'))
2020-02-11 01:22:56.004679: I tensorflow/core/platform/s3/aws_logging.cc:54] Found secret key
2020-02-11 01:22:56.004794: I tensorflow/core/platform/s3/aws_logging.cc:54] Connection has been released. Continuing.
2020-02-11 01:22:56.007371: I tensorflow/core/platform/s3/aws_logging.cc:54] Found secret key
2020-02-11 01:22:56.007489: I tensorflow/core/platform/s3/aws_logging.cc:54] Connection has been released. Continuing.
<tensorflow.python.pywrap_tensorflow_internal.FileStatistics; proxy of <Swig Object of type 'tensorflow::FileStatistics *' at 0x7fabbbc52180> >
下面是常见的两种错误原因。
错误情况 | 原因 | 其他 |
---|---|---|
Curl returned error code 28 | S3_ENDPOINT没有配置好 | |
Curl returned error code 35 | S3_USE_HTTPS需要设置为0 |
读取数据。
代码语言:javascript复制import tensorflow as tf
filenames = ["s3://bucketname/path/to/file1.tfrecord",
"s3://bucketname/path/to/file2.tfrecord"]
dataset = tf.data.TFRecordDataset(filenames)
Tensorboard 的测试。
代码语言:javascript复制tensorboard --logdir s3://bucketname/path/to/model/
tensorflow_model_server --port=9000 --model_name=model --model_base_path=s3://bucketname/path/to/model/export/
在 Tensorflow on Kubernetes 训练的过程中,一般会指定一个 Chief Worker 作为输出 Checkpoint 和 SaveModel 的功能节点。可以配置两个 bucket,分别收集 Checkpoint 和 Model 数据,当需要部署服务的时候,将模型的 bucket 表示的目录,挂载到服务的 Pod。
关于 Tensorflow 中 file_io 的详细用法,包括了 copy
和 stat
等方法,建议参考官方文档。
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/lib/io/file_io.py