Google Colab TPU 中未实现文件系统方案“[本地]”
Posted
技术标签:
【中文标题】Google Colab TPU 中未实现文件系统方案“[本地]”【英文标题】:File system scheme '[local]' not implemented in Google Colab TPU 【发布时间】:2020-11-02 08:12:33 【问题描述】:我在 Google Colab 中使用 TPU 运行时,但在读取文件时遇到问题(不确定)。我使用以下方法初始化 TPU:
import tensorflow as tf
import os
import tensorflow_datasets as tfds
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(resolver)
# This is the TPU initialization code that has to be at the beginning.
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
我在 Google Colab 存储中的一个文件夹中有许多图像(例如 '/content/train2017/000000000009.jpg'
)。我运行以下代码:
import tensorflow as tf
def load_image(image_path):
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize(img, (299, 299))
img = tf.keras.applications.inception_v3.preprocess_input(img)
return img, image_path
load_image('/content/train2017/000000000009.jpg')
但是,我收到以下错误:
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-33-a7fbb45f3b76> in <module>()
----> 1 load_image('/content/train2017/000000000009.jpg')
5 frames
<ipython-input-7-862c73d29b96> in load_image(image_path)
2 img = tf.io.read_file(image_path)
3 img = tf.image.decode_jpeg(img, channels=3)
----> 4 img = tf.image.resize(img, (299, 299))
5 img = tf.keras.applications.inception_v3.preprocess_input(img)
6 return img, image_path
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/image_ops_impl.py in resize_images_v2(images, size, method, preserve_aspect_ratio, antialias, name)
1515 preserve_aspect_ratio=preserve_aspect_ratio,
1516 name=name,
-> 1517 skip_resize_if_same=False)
1518
1519
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/image_ops_impl.py in _resize_images_common(images, resizer_fn, size, preserve_aspect_ratio, name, skip_resize_if_same)
1183 with ops.name_scope(name, 'resize', [images, size]):
1184 images = ops.convert_to_tensor(images, name='images')
-> 1185 if images.get_shape().ndims is None:
1186 raise ValueError('\'images\' contains no shape.')
1187 # TODO(shlens): Migrate this functionality to the underlying Op's.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in get_shape(self)
1071 def get_shape(self):
1072 """Alias of Tensor.shape."""
-> 1073 return self.shape
1074
1075 def _shape_as_list(self):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in shape(self)
1065 self._tensor_shape = tensor_shape.TensorShape(self._shape_tuple())
1066 except core._NotOkStatusException as e:
-> 1067 six.raise_from(core._status_to_exception(e.code, e.message), None)
1068
1069 return self._tensor_shape
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
UnimplementedError: File system scheme '[local]' not implemented (file: '/content/train2017/000000000009.jpg')
我应该如何解决?我找到了类似 gs 桶的东西,但它是有偿的。有没有其他方法可以解决这个问题?
【问题讨论】:
【参考方案1】:Cloud TPU 只能访问 GCS 中的数据,因为仅注册了 GCS 文件系统。详情请见:https://cloud.google.com/tpu/docs/troubleshooting#cannot_use_local_filesystem。
尽管对于从 TF 2.3 版本开始的检查点,您应该能够使用 experimental_io_device='/job:localhost'
选项 (https://www.tensorflow.org/api_docs/python/tf/train/CheckpointOptions) 将检查点存储到 Colab 运行时或从 Colab 运行时加载/加载。即使使用该 API,您也需要从 GCS 加载数据。
例子:
checkpoint = tf.train.Checkpoint(model=model)
local_device_option = tf.train.CheckpointOptions(experimental_io_device="/job:localhost")
checkpoint.write(checkpoint_path, options=local_device_option)
【讨论】:
【参考方案2】:使用 TPU 时从本地文件加载文件 - 将它们作为普通的 python file.read() (不是 tf.io)读取。在你的情况下:
def load_image(image_path):
with open(image_path, "rb") as local_file: # <= change here
img = local_file.read()
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.resize(img, (299, 299))
img = tf.keras.applications.inception_v3.preprocess_input(img)
return img, image_path
load_image('/content/train2017/000000000009.jpg')
【讨论】:
这里的part_size是什么? 以字节为单位的大小(例如,如果整个文件无法读入内存)。我相应地更新了答案 其实我有一个大概有8100张图片的文件夹,想分批传过来训练一个模型。 那么,我应该如何告诉它读取文件夹。抱歉,我没有收到您的代码。 我的图像嵌入到 TFRecords 中,我得到了同样的错误。那么,我该如何解决呢?以上是关于Google Colab TPU 中未实现文件系统方案“[本地]”的主要内容,如果未能解决你的问题,请参考以下文章
Google Colab 中的 Keras 调谐器和 TPU