GCPのTPUでInvalidArgumentError: Unimplemented: File system scheme ‘[local]’ not implemented

KaggleやColabでTPUを使っていて、SavedModelを保存・復元しようとした時に表題のエラー。

TPUはGoogle Cloud Strage(GCS)のバケットから読み書きするようにデフォルトで設定されており、ローカルから読み書きできないエラーが出たのはその設定のためだと思われる。

SavedModelのオプションを見てみる。

tf.saved_model.SaveOptions(
namespace_whitelist=None, save_debug_info=False, function_aliases=None,
experimental_io_device=None, experimental_variable_policy=None
)

experimental_io_device

string. Applies in a distributed setting. Tensorflow device to use to access the filesystem. If None (default) then for each variable the filesystem is accessed from the CPU:0 device of the host where that variable is assigned. If specified, the filesystem is instead accessed from that device for all variables.

This is for example useful if you want to save to a local directory, such as “/tmp” when running in a distributed setting. In that case pass a device for the host where the “/tmp” directory is accessible.

ローカルに読み書きできそうなオプションがあった。

しかし、毎回/tmpなどのディレクトリを指定するのも面倒だなと思い、ローカル環境であることをTensorflowに伝える方法はないか調べてみた所、以下を発見。

experimental_io_device='/job:localhost'

ということで、SavedModelをローカルに保存する時は

save_locally = tf.saved_model.SaveOptions(experimental_io_device='/job:localhost')
model.save('./model', options=save_locally)

ロードする時は

load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')
model = tf.keras.models.load_model('./model', options=load_locally)

ModelCheckpointにもoptionsを設定することが可能。

これで、SavedModelをローカルに読み書きすることが出来るようになった。

参考

Cloud TPU ワークフローのトラブルシューティング  |  Google Cloud
tf.saved_model.SaveOptions  |  TensorFlow v2.16.1
Options for saving to SavedModel.
Five flowers with Keras and Xception on TPU
Explore and run machine learning code with Kaggle Notebooks | Using data from Five Flowers

コメント

タイトルとURLをコピーしました