{
  "id": 131235,
  "title": "TensorFlow Hub with TPUs",
  "url": "/competitions/flower-classification-with-tpus/discussion/131235",
  "author_name": "",
  "post_date": "2020-02-18T23:23:52.032396200Z",
  "votes": 4,
  "comment_count": 13,
  "views": 0,
  "content": "<p>Hello,</p>\n\n<p>I have been trying to use TensorFlow Hub with the TPUs, but I have been unable to get it to work.</p>\n\n<p>```\nmodel_url = \"<a href=\"https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4\">https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4</a>\"\ninput_shape=IMAGE_SIZE+(3,)</p>\n\n<p>with strategy.scope():\n    model = tf.keras.Sequential([\n         hub.KerasLayer(model_url, trainable=True, input_shape=input_shape),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])</p>\n\n<p>model.compile(\n    optimizer='adam',\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['sparse_categorical_accuracy']\n)</p>\n\n<p>model.summary()\n```</p>\n\n<p>I'm getting the error below. Has anyone managed to get the a tensorflow hub model working on a TPU?\nAny help or advice would be appreciated.</p>\n\n<p>Edit:\nStack trace is in a post further down.</p>",
  "messages": [
    {
      "id": "749823",
      "postDate": "02/18/2020 23:23:52",
      "content": "<p>Hello,</p>\n\n<p>I have been trying to use TensorFlow Hub with the TPUs, but I have been unable to get it to work.</p>\n\n<p>```\nmodel_url = \"<a href=\"https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4\">https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4</a>\"\ninput_shape=IMAGE_SIZE+(3,)</p>\n\n<p>with strategy.scope():\n    model = tf.keras.Sequential([\n         hub.KerasLayer(model_url, trainable=True, input_shape=input_shape),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])</p>\n\n<p>model.compile(\n    optimizer='adam',\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['sparse_categorical_accuracy']\n)</p>\n\n<p>model.summary()\n```</p>\n\n<p>I'm getting the error below. Has anyone managed to get the a tensorflow hub model working on a TPU?\nAny help or advice would be appreciated.</p>\n\n<p>Edit:\nStack trace is in a post further down.</p>",
      "rawMarkdown": "Hello,\n\nI have been trying to use TensorFlow Hub with the TPUs, but I have been unable to get it to work.\n\n```\nmodel_url = \"https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4\"\ninput_shape=IMAGE_SIZE+(3,)\n\nwith strategy.scope():\n    model = tf.keras.Sequential([\n         hub.KerasLayer(model_url, trainable=True, input_shape=input_shape),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n\nmodel.compile(\n    optimizer='adam',\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['sparse_categorical_accuracy']\n)\n\nmodel.summary()\n```\n\nI'm getting the error below. Has anyone managed to get the a tensorflow hub model working on a TPU?\nAny help or advice would be appreciated.\n\nEdit:\nStack trace is in a post further down.",
      "votes": null
    },
    {
      "id": "749859",
      "postDate": "02/19/2020 00:10:25",
      "content": "<p>Your code is correct at first glance and I confirm that i have been able to train models from TFHub on TPU using the <code>hub.KerasLayer</code> API. Maybe you can share the stack trace ? InvalidArgumentError is the most generic error there is.</p>",
      "rawMarkdown": "Your code is correct at first glance and I confirm that i have been able to train models from TFHub on TPU using the `hub.KerasLayer` API. Maybe you can share the stack trace ? InvalidArgumentError is the most generic error there is.",
      "votes": null
    },
    {
      "id": "750222",
      "postDate": "02/19/2020 07:54:06",
      "content": "<p>Thanks for the reply. Sorry I tried too, it appears it truncated the post. I'll try again.</p>",
      "rawMarkdown": "Thanks for the reply. Sorry I tried too, it appears it truncated the post. I'll try again.",
      "votes": null
    },
    {
      "id": "750269",
      "postDate": "02/19/2020 08:41:58",
      "content": "<p>OK. I have attached the stack trace as a txt file as kaggle keeps truncating my post when I paste it in. Sorry about that.</p>",
      "rawMarkdown": "OK. I have attached the stack trace as a txt file as kaggle keeps truncating my post when I paste it in. Sorry about that.",
      "votes": null
    },
    {
      "id": "750945",
      "postDate": "02/19/2020 21:28:40",
      "content": "<p>I confirm this is currently broken on TPU. Sorry.\nIt is not a problem with TFHub. The problem is with loading models in TF's \"SavedModel\" format on TPU.\nModels is Keras' HD5 format can be loaded on TPU in TF 2.1 but the TF-standard \"SavedModel\" loading path has a very unfortunate bug.</p>",
      "rawMarkdown": "I confirm this is currently broken on TPU. Sorry.\nIt is not a problem with TFHub. The problem is with loading models in TF's \"SavedModel\" format on TPU.\nModels is Keras' HD5 format can be loaded on TPU in TF 2.1 but the TF-standard \"SavedModel\" loading path has a very unfortunate bug.",
      "votes": null
    },
    {
      "id": "750947",
      "postDate": "02/19/2020 21:31:41",
      "content": "<p>Thanks for the feedback.</p>",
      "rawMarkdown": "Thanks for the feedback.",
      "votes": null
    },
    {
      "id": "760131",
      "postDate": "02/29/2020 22:15:16",
      "content": "<p>A potential fix (if you want to try this in Colab) is to upload the SavedModel to your GCS bucket and instruct the Hub module to use the bucket's cache directory (instead of the local one) via the <code>TFHUB_CACHE_DIR</code> environment variable. You do this by setting this env variable to the location of the remote GCS Hub cache directory where your SavedModel is living.</p>\n\n<p>More details here.\n<a href=\"https://www.tensorflow.org/hub/api_docs/python/hub/load\">https://www.tensorflow.org/hub/api_docs/python/hub/load</a></p>",
      "rawMarkdown": "A potential fix (if you want to try this in Colab) is to upload the SavedModel to your GCS bucket and instruct the Hub module to use the bucket's cache directory (instead of the local one) via the `TFHUB_CACHE_DIR` environment variable. You do this by setting this env variable to the location of the remote GCS Hub cache directory where your SavedModel is living.\n\nMore details here.\nhttps://www.tensorflow.org/hub/api_docs/python/hub/load",
      "votes": null
    },
    {
      "id": "761732",
      "postDate": "03/02/2020 22:07:26",
      "content": "<p>Unfortunately, this is not going to work on Kaggle. Kaggle does not offer a writable GCS bucket to be used as <code>TFHUB_CACHE_DIR</code>.</p>",
      "rawMarkdown": "Unfortunately, this is not going to work on Kaggle. Kaggle does not offer a writable GCS bucket to be used as `TFHUB_CACHE_DIR`.",
      "votes": null
    },
    {
      "id": "866349",
      "postDate": "05/29/2020 10:14:16",
      "content": "<p>TPUs support the datasets and models which are avaliable on GCS buckets .\nMaybe the HUB model wasn't compatible with TPU try tf.keras.applications for transfer learning on TPU <br>\nIt'll work.</p>",
      "rawMarkdown": "TPUs support the datasets and models which are avaliable on GCS buckets .\nMaybe the HUB model wasn't compatible with TPU try tf.keras.applications for transfer learning on TPU  \nIt'll work.",
      "votes": null
    },
    {
      "id": "866980",
      "postDate": "05/29/2020 21:05:47",
      "content": "<p>You can load from TFHub on TPU using the following code:</p>\n\n<p><code>\nwith strategy.scope(): # TPUStrategy\n    # copy model from TF Hub into a Kaggle dataset your-dataset then:\n    GCS_PATH_TO_SAVEDMODEL = KaggleDatasets().get_gcs_path('your-dataset')\n    loaded_model = tf.saved_model.load(GCS_PATH_TO_SAVEDMODEL)\n    # Cast the loaded model to a TFHub KerasLayer.\n    layer = hub.KerasLayer(loaded_model, trainable=True)\n    # then use keras layer normally in a model\n</code></p>",
      "rawMarkdown": "You can load from TFHub on TPU using the following code:\n\n```\nwith strategy.scope(): # TPUStrategy\n    # copy model from TF Hub into a Kaggle dataset your-dataset then:\n    GCS_PATH_TO_SAVEDMODEL = KaggleDatasets().get_gcs_path('your-dataset')\n    loaded_model = tf.saved_model.load(GCS_PATH_TO_SAVEDMODEL)\n    # Cast the loaded model to a TFHub KerasLayer.\n    layer = hub.KerasLayer(loaded_model, trainable=True)\n    # then use keras layer normally in a model\n```",
      "votes": null
    },
    {
      "id": "921635",
      "postDate": "07/09/2020 12:45:28",
      "content": "<p>GCS_PATH_TO_SAVEDMODEL?? We have tfhub link..how to load the model</p>",
      "rawMarkdown": "GCS_PATH_TO_SAVEDMODEL?? We have tfhub link..how to load the model",
      "votes": null
    },
    {
      "id": "1157165",
      "postDate": "01/17/2021 17:48:03",
      "content": "<p>Is this considered the solution to this issue? I'd been halted by this issue for several days until I came across this post. Is Kaggle still not compatible with TF Hub models? This code above seems to reference the dataset, NOT the model so I'm not sure how the code snippet relates…</p>",
      "rawMarkdown": "Is this considered the solution to this issue? I'd been halted by this issue for several days until I came across this post. Is Kaggle still not compatible with TF Hub models? This code above seems to reference the dataset, NOT the model so I'm not sure how the code snippet relates...",
      "votes": null
    },
    {
      "id": "1164090",
      "postDate": "01/22/2021 06:35:56",
      "content": "<p>Hi John, the reason the code snippet references the dataset is because using this workaround requires you to upload a copy of the model from TF Hub into a Kaggle dataset first. Once you've done that, you should be able to use the rest of the code snippet to read it in and use within your notebook. Hope this helps!</p>",
      "rawMarkdown": "Hi John, the reason the code snippet references the dataset is because using this workaround requires you to upload a copy of the model from TF Hub into a Kaggle dataset first. Once you've done that, you should be able to use the rest of the code snippet to read it in and use within your notebook. Hope this helps!",
      "votes": null
    },
    {
      "id": "1183257",
      "postDate": "02/02/2021 21:13:00",
      "content": "<p>As of TF 2.4, this now works on TPU:</p>\n<pre><code>import tensorflow_hub as hub\nwith strategy.scope():\n    load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')\npretrained_model = hub.KerasLayer('https://tfhub.dev/tensorflow/efficientnet/b6/feature-vector/1', trainable=True, input_shape=[512,512,3], load_options=load_locally)\n</code></pre>\n<p>More info in <a href=\"https://www.kaggle.com/product-feedback/216256\" target=\"_blank\">this post</a>.</p>",
      "rawMarkdown": "As of TF 2.4, this now works on TPU:\n\n```\nimport tensorflow_hub as hub\nwith strategy.scope():\n    load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')\npretrained_model = hub.KerasLayer('https://tfhub.dev/tensorflow/efficientnet/b6/feature-vector/1', trainable=True, input_shape=[512,512,3], load_options=load_locally)\n```\n\nMore info in [this post](https://www.kaggle.com/product-feedback/216256).",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1183257,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "02/02/2021 21:13:00",
      "content": "<p>As of TF 2.4, this now works on TPU:</p>\n<pre><code>import tensorflow_hub as hub\nwith strategy.scope():\n    load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')\npretrained_model = hub.KerasLayer('https://tfhub.dev/tensorflow/efficientnet/b6/feature-vector/1', trainable=True, input_shape=[512,512,3], load_options=load_locally)\n</code></pre>\n<p>More info in <a href=\"https://www.kaggle.com/product-feedback/216256\" target=\"_blank\">this post</a>.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 749859,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "02/19/2020 00:10:25",
      "content": "<p>Your code is correct at first glance and I confirm that i have been able to train models from TFHub on TPU using the <code>hub.KerasLayer</code> API. Maybe you can share the stack trace ? InvalidArgumentError is the most generic error there is.</p>",
      "votes": null,
      "replies": [
        {
          "id": 750222,
          "author_name": "ghostskipper",
          "author_url": "",
          "post_date": "02/19/2020 07:54:06",
          "content": "<p>Thanks for the reply. Sorry I tried too, it appears it truncated the post. I'll try again.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 750269,
          "author_name": "ghostskipper",
          "author_url": "",
          "post_date": "02/19/2020 08:41:58",
          "content": "<p>OK. I have attached the stack trace as a txt file as kaggle keeps truncating my post when I paste it in. Sorry about that.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 866349,
          "author_name": "prudhvi9999",
          "author_url": "",
          "post_date": "05/29/2020 10:14:16",
          "content": "<p>TPUs support the datasets and models which are avaliable on GCS buckets .\nMaybe the HUB model wasn't compatible with TPU try tf.keras.applications for transfer learning on TPU <br>\nIt'll work.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 750945,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "02/19/2020 21:28:40",
      "content": "<p>I confirm this is currently broken on TPU. Sorry.\nIt is not a problem with TFHub. The problem is with loading models in TF's \"SavedModel\" format on TPU.\nModels is Keras' HD5 format can be loaded on TPU in TF 2.1 but the TF-standard \"SavedModel\" loading path has a very unfortunate bug.</p>",
      "votes": null,
      "replies": [
        {
          "id": 750947,
          "author_name": "ghostskipper",
          "author_url": "",
          "post_date": "02/19/2020 21:31:41",
          "content": "<p>Thanks for the feedback.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 760131,
          "author_name": "ranik40",
          "author_url": "",
          "post_date": "02/29/2020 22:15:16",
          "content": "<p>A potential fix (if you want to try this in Colab) is to upload the SavedModel to your GCS bucket and instruct the Hub module to use the bucket's cache directory (instead of the local one) via the <code>TFHUB_CACHE_DIR</code> environment variable. You do this by setting this env variable to the location of the remote GCS Hub cache directory where your SavedModel is living.</p>\n\n<p>More details here.\n<a href=\"https://www.tensorflow.org/hub/api_docs/python/hub/load\">https://www.tensorflow.org/hub/api_docs/python/hub/load</a></p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 761732,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "03/02/2020 22:07:26",
          "content": "<p>Unfortunately, this is not going to work on Kaggle. Kaggle does not offer a writable GCS bucket to be used as <code>TFHUB_CACHE_DIR</code>.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 866980,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "05/29/2020 21:05:47",
      "content": "<p>You can load from TFHub on TPU using the following code:</p>\n\n<p><code>\nwith strategy.scope(): # TPUStrategy\n    # copy model from TF Hub into a Kaggle dataset your-dataset then:\n    GCS_PATH_TO_SAVEDMODEL = KaggleDatasets().get_gcs_path('your-dataset')\n    loaded_model = tf.saved_model.load(GCS_PATH_TO_SAVEDMODEL)\n    # Cast the loaded model to a TFHub KerasLayer.\n    layer = hub.KerasLayer(loaded_model, trainable=True)\n    # then use keras layer normally in a model\n</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 921635,
          "author_name": "gyanaluckydas",
          "author_url": "",
          "post_date": "07/09/2020 12:45:28",
          "content": "<p>GCS_PATH_TO_SAVEDMODEL?? We have tfhub link..how to load the model</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1157165,
          "author_name": "jsukup",
          "author_url": "",
          "post_date": "01/17/2021 17:48:03",
          "content": "<p>Is this considered the solution to this issue? I'd been halted by this issue for several days until I came across this post. Is Kaggle still not compatible with TF Hub models? This code above seems to reference the dataset, NOT the model so I'm not sure how the code snippet relates…</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1164090,
          "author_name": "devvret",
          "author_url": "",
          "post_date": "01/22/2021 06:35:56",
          "content": "<p>Hi John, the reason the code snippet references the dataset is because using this workaround requires you to upload a copy of the model from TF Hub into a Kaggle dataset first. Once you've done that, you should be able to use the rest of the code snippet to read it in and use within your notebook. Hope this helps!</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "749823": "Hello,\n\nI have been trying to use TensorFlow Hub with the TPUs, but I have been unable to get it to work.\n\n```\nmodel_url = \"https://tfhub.dev/google/imagenet/inception_resnet_v2/feature_vector/4\"\ninput_shape=IMAGE_SIZE+(3,)\n\nwith strategy.scope():\n    model = tf.keras.Sequential([\n         hub.KerasLayer(model_url, trainable=True, input_shape=input_shape),\n        tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n    ])\n\nmodel.compile(\n    optimizer='adam',\n    loss = 'sparse_categorical_crossentropy',\n    metrics=['sparse_categorical_accuracy']\n)\n\nmodel.summary()\n```\n\nI'm getting the error below. Has anyone managed to get the a tensorflow hub model working on a TPU?\nAny help or advice would be appreciated.\n\nEdit:\nStack trace is in a post further down.",
    "749859": "Your code is correct at first glance and I confirm that i have been able to train models from TFHub on TPU using the `hub.KerasLayer` API. Maybe you can share the stack trace ? InvalidArgumentError is the most generic error there is.",
    "750222": "Thanks for the reply. Sorry I tried too, it appears it truncated the post. I'll try again.",
    "750269": "OK. I have attached the stack trace as a txt file as kaggle keeps truncating my post when I paste it in. Sorry about that.",
    "750945": "I confirm this is currently broken on TPU. Sorry.\nIt is not a problem with TFHub. The problem is with loading models in TF's \"SavedModel\" format on TPU.\nModels is Keras' HD5 format can be loaded on TPU in TF 2.1 but the TF-standard \"SavedModel\" loading path has a very unfortunate bug.",
    "750947": "Thanks for the feedback.",
    "760131": "A potential fix (if you want to try this in Colab) is to upload the SavedModel to your GCS bucket and instruct the Hub module to use the bucket's cache directory (instead of the local one) via the `TFHUB_CACHE_DIR` environment variable. You do this by setting this env variable to the location of the remote GCS Hub cache directory where your SavedModel is living.\n\nMore details here.\nhttps://www.tensorflow.org/hub/api_docs/python/hub/load",
    "761732": "Unfortunately, this is not going to work on Kaggle. Kaggle does not offer a writable GCS bucket to be used as `TFHUB_CACHE_DIR`.",
    "866349": "TPUs support the datasets and models which are avaliable on GCS buckets .\nMaybe the HUB model wasn't compatible with TPU try tf.keras.applications for transfer learning on TPU  \nIt'll work.",
    "866980": "You can load from TFHub on TPU using the following code:\n\n```\nwith strategy.scope(): # TPUStrategy\n    # copy model from TF Hub into a Kaggle dataset your-dataset then:\n    GCS_PATH_TO_SAVEDMODEL = KaggleDatasets().get_gcs_path('your-dataset')\n    loaded_model = tf.saved_model.load(GCS_PATH_TO_SAVEDMODEL)\n    # Cast the loaded model to a TFHub KerasLayer.\n    layer = hub.KerasLayer(loaded_model, trainable=True)\n    # then use keras layer normally in a model\n```",
    "921635": "GCS_PATH_TO_SAVEDMODEL?? We have tfhub link..how to load the model",
    "1157165": "Is this considered the solution to this issue? I'd been halted by this issue for several days until I came across this post. Is Kaggle still not compatible with TF Hub models? This code above seems to reference the dataset, NOT the model so I'm not sure how the code snippet relates...",
    "1164090": "Hi John, the reason the code snippet references the dataset is because using this workaround requires you to upload a copy of the model from TF Hub into a Kaggle dataset first. Once you've done that, you should be able to use the rest of the code snippet to read it in and use within your notebook. Hope this helps!",
    "1183257": "As of TF 2.4, this now works on TPU:\n\n```\nimport tensorflow_hub as hub\nwith strategy.scope():\n    load_locally = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost')\npretrained_model = hub.KerasLayer('https://tfhub.dev/tensorflow/efficientnet/b6/feature-vector/1', trainable=True, input_shape=[512,512,3], load_options=load_locally)\n```\n\nMore info in [this post](https://www.kaggle.com/product-feedback/216256)."
  },
  "source": "meta"
}