我在 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'))
Run Code Online (Sandbox Code Playgroud)
我在 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')
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call …Run Code Online (Sandbox Code Playgroud)