模型託管協定

本文件說明在 tfhub.dev (TFJS、TF Lite 和 TensorFlow 模型) 上託管所有模型類型時使用的網址慣例。本文件也說明 tensorflow_hub 程式庫實作的 HTTP(S) 型協定,以便將 TensorFlow 模型從 tfhub.dev 和相容服務載入 TensorFlow 程式。

其主要功能是在程式碼中使用相同的網址來載入模型,並在瀏覽器中檢視模型文件。

一般網址慣例

tfhub.dev 支援下列網址格式

  • TF Hub 發布者遵循 <a href="https://tfhub.dev/">https://tfhub.dev/</a><publisher>
  • TF Hub 集合遵循 <a href="https://tfhub.dev/">https://tfhub.dev/</a><publisher>/collection/<collection_name>
  • TF Hub 模型具有版本化網址 <a href="https://tfhub.dev/">https://tfhub.dev/</a><publisher>/<model_name>/<version> 和未版本化網址 <a href="https://tfhub.dev/">https://tfhub.dev/</a><publisher>/<model_name>,後者會解析為模型的最新版本。

TF Hub 模型可以透過將網址參數附加到 tfhub.dev 模型網址來下載為壓縮資產。但是,達成此目的所需的網址參數取決於模型類型

  • TensorFlow 模型 (SavedModel 和 TF1 Hub 格式):將 ?tf-hub-format=compressed 附加到 TensorFlow 模型網址。
  • TFJS 模型:將 ?tfjs-format=compressed 附加到 TFJS 模型網址以下載壓縮檔,或附加 /model.json?tfjs-format=file 以從遠端儲存空間讀取。
  • TF lite 模型:將 ?lite-format=tflite 附加到 TF Lite 模型網址。

範例

類型 模型網址 下載類型 網址參數 下載網址
TensorFlow (SavedModel、TF1 Hub 格式) https://tfhub.dev/google/spice/2 .tar.gz ?tf-hub-format=compressed https://tfhub.dev/google/spice/2?tf-hub-format=compressed
TF Lite https://tfhub.dev/google/lite-model/spice/1 .tflite ?lite-format=tflite https://tfhub.dev/google/lite-model/spice/1?lite-format=tflite
TF.js https://tfhub.dev/google/tfjs-model/spice/2/default/1 .tar.gz ?tfjs-format=compressed https://tfhub.dev/google/tfjs-model/spice/2/default/1?tfjs-format=compressed

此外,部分模型也以可以直接從遠端儲存空間讀取的格式託管,而無需下載。如果沒有本機儲存空間可用,這特別有用,例如在瀏覽器中執行 TF.js 模型或在 Colab 上載入 SavedModel。請注意,讀取遠端託管的模型而不先在本機下載可能會增加延遲時間。

類型 模型網址 回應類型 網址參數 要求網址
TensorFlow (SavedModel、TF1 Hub 格式) https://tfhub.dev/google/spice/2 字串 (GCS 資料夾路徑,其中儲存未壓縮模型) ?tf-hub-format=uncompressed https://tfhub.dev/google/spice/2?tf-hub-format=uncompressed
TF.js https://tfhub.dev/google/tfjs-model/spice/2/default/1 .json ?tfjs-format=file https://tfhub.dev/google/tfjs-model/spice/2/default/1/model.json?tfjs-format=file

tensorflow_hub 程式庫協定

本節說明我們如何在 tfhub.dev 上託管模型,以搭配 tensorflow_hub 程式庫使用。如果您想要託管自己的模型存放區以搭配 tensorflow_hub 程式庫使用,您的 HTTP(s) 發布服務應提供此協定的實作。

請注意,本節未說明託管 TF Lite 和 TFJS 模型,因為這些模型並非透過 tensorflow_hub 程式庫下載。如需託管這些模型類型的詳細資訊,請查看上方

壓縮託管

模型在 tfhub.dev 上儲存為壓縮的 tar.gz 檔案。根據預設,tensorflow_hub 程式庫會自動下載壓縮模型。也可以手動下載模型,方法是將 ?tf-hub-format=compressed 附加到模型網址,例如

wget https://tfhub.dev/tensorflow/albert_en_xxlarge/1?tf-hub-format=compressed

封存檔的根目錄是模型目錄的根目錄,且應包含 SavedModel,如此範例所示

# Create a compressed model from a SavedModel directory.
$ tar -cz -f model.tar.gz --owner=0 --group=0 -C /tmp/export-model/ .

# Inspect files inside a compressed model
$ tar -tf model.tar.gz
./
./variables/
./variables/variables.data-00000-of-00001
./variables/variables.index
./assets/
./saved_model.pb

搭配舊版 TF1 Hub 格式 使用的 Tarball 也會包含 ./tfhub_module.pb 檔案。

當呼叫 tensorflow_hub 程式庫模型載入 API 之一 (hub.KerasLayerhub.load 等) 時,程式庫會下載模型、解壓縮模型,並在本機快取模型。tensorflow_hub 程式庫預期模型網址已版本化,且指定版本的模型內容不可變更,以便可以無限期快取。進一步瞭解快取模型

未壓縮託管

當環境變數 TFHUB_MODEL_LOAD_FORMAT 或命令列旗標 --tfhub_model_load_format 設定為 UNCOMPRESSED 時,模型會直接從遠端儲存空間 (GCS) 讀取,而不是在本機下載和解壓縮。啟用此行為時,程式庫會將 ?tf-hub-format=uncompressed 附加到模型網址。該要求會傳回 GCS 上資料夾的路徑,其中包含未壓縮的模型檔案。例如:
<a href="https://tfhub.dev/google/spice/2?tf-hub-format=uncompressed">https://tfhub.dev/google/spice/2?tf-hub-format=uncompressed</a>
傳回
gs://tfhub-modules/google/spice/2/uncompressed 在 303 回應的內文中。然後,程式庫會從該 GCS 目的地讀取模型。