{
  "id": 148930,
  "title": "How to save model in Tensorflow with TPU running?",
  "url": "/competitions/jigsaw-multilingual-toxic-comment-classification/discussion/148930",
  "author_name": "",
  "post_date": "2020-05-06T06:34:23.526677100Z",
  "votes": 4,
  "comment_count": 7,
  "views": 0,
  "content": "<p>I am saving mode with \n<code>tf.saved_model.save(model, model_save_path)</code>\n but got error Unimplemented: File system scheme '[local]' not implemented</p>\n\n<p>Then how to save now?</p>",
  "messages": [
    {
      "id": "835254",
      "postDate": "05/06/2020 06:34:23",
      "content": "<p>I am saving mode with \n<code>tf.saved_model.save(model, model_save_path)</code>\n but got error Unimplemented: File system scheme '[local]' not implemented</p>\n\n<p>Then how to save now?</p>",
      "rawMarkdown": "I am saving mode with \n`tf.saved_model.save(model, model_save_path)`\n but got error Unimplemented: File system scheme '[local]' not implemented\n\nThen how to save now?",
      "votes": null
    },
    {
      "id": "835591",
      "postDate": "05/06/2020 11:23:24",
      "content": "<p><a href=\"/lucca9211\">@lucca9211</a>  I use <code>model.save_weights()</code> but I guess it's the same for <code>tf.saved_model.save()</code>\nIt looks like the tpu strategy can only save *.tf paths to google storage, so you have to point to a gs:// address.\nHowever, you can save *.h5 paths locally. I.e. your <code>model_save_path</code> should end with <code>h5</code></p>",
      "rawMarkdown": "lucca9211  I use `model.save_weights()` but I guess it's the same for `tf.saved_model.save()`\nIt looks like the tpu strategy can only save *.tf paths to google storage, so you have to point to a gs:// address.\nHowever, you can save *.h5 paths locally. I.e. your `model_save_path` should end with `h5`",
      "votes": null
    },
    {
      "id": "835656",
      "postDate": "05/06/2020 12:29:44",
      "content": "<p>Its working.\n```\nimport h5py</p>\n\n<p>model_json = model.to_json()\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)</p>\n\n<p>model.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")`\n<code>\n</code>\nfrom tensorflow.keras.models import model_from_json\njson_file = open('model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\nloaded_model = model_from_json(loaded_model_json)\n```</p>\n\n<p><code>\nloaded_model.load_weights(\"model.h5\")\nprint(\"Loaded model from disk\")\n</code></p>",
      "rawMarkdown": "Its working.\n```\nimport h5py\n\nmodel_json = model.to_json()\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)\n\nmodel.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")`\n```\n```\nfrom tensorflow.keras.models import model_from_json\njson_file = open('model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\nloaded_model = model_from_json(loaded_model_json)\n```\n\n```\nloaded_model.load_weights(\"model.h5\")\nprint(\"Loaded model from disk\")\n```",
      "votes": null
    },
    {
      "id": "835685",
      "postDate": "05/06/2020 12:50:14",
      "content": "<p>It could be that Kaggle disk space limit does not allow to save a large model together with its optimizer state, while saving weights only works. I had this problem and couldn't resolve it. </p>",
      "rawMarkdown": "It could be that Kaggle disk space limit does not allow to save a large model together with its optimizer state, while saving weights only works. I had this problem and couldn't resolve it.",
      "votes": null
    },
    {
      "id": "836066",
      "postDate": "05/06/2020 17:34:42",
      "content": "<p>Sorry, this a bit buggy right now. Saving works in HDF5 format.</p>\n\n<p><code>keras_model.save(..., save_format='h5')</code>\nor\n<code>tf.saved_model.save(model, 'model_save_path.h5')</code></p>",
      "rawMarkdown": "Sorry, this a bit buggy right now. Saving works in HDF5 format.\n\n`keras_model.save(..., save_format='h5')`\nor\n`tf.saved_model.save(model, 'model_save_path.h5')`",
      "votes": null
    },
    {
      "id": "836173",
      "postDate": "05/06/2020 19:29:05",
      "content": "<blockquote>\n  <p><strong>Martin Görner wrote:</strong></p>\n  \n  <p>Sorry, this a bit buggy right now. Saving works in HDF5 format.</p>\n  \n  <p><code>keras_model.save(..., save_format='h5')</code>\n  or\n  <code>tf.saved_model.save(model, 'model_save_path.h5')</code></p>\n</blockquote>\n\n<p>Hi <a href=\"/mgornergoogle\">@mgornergoogle</a>  , saving weights (or model) doesn't work for me when I have custom <code>tf.keras</code> layers. Do you have any tip about that ? Thx !</p>",
      "rawMarkdown": "&gt; **Martin Görner wrote:**\n&gt; \n&gt; Sorry, this a bit buggy right now. Saving works in HDF5 format.\n&gt; \n&gt; `keras_model.save(..., save_format='h5')`\n&gt; or\n&gt; `tf.saved_model.save(model, 'model_save_path.h5')`\n\n\nHi @mgornergoogle  , saving weights (or model) doesn't work for me when I have custom `tf.keras` layers. Do you have any tip about that ? Thx !",
      "votes": null
    },
    {
      "id": "836276",
      "postDate": "05/06/2020 21:19:27",
      "content": "<p>Can you first check if it works in TF 2.2 (On Colab TPU). There have been improvements in this Area in TF 2.2.</p>\n\n<p>If not, please share your code.</p>",
      "rawMarkdown": "Can you first check if it works in TF 2.2 (On Colab TPU). There have been improvements in this Area in TF 2.2.\n\nIf not, please share your code.",
      "votes": null
    },
    {
      "id": "836345",
      "postDate": "05/06/2020 22:37:35",
      "content": "<p>Thanks <a href=\"/mgornergoogle\">@mgornergoogle</a> .  I will check if it works   in TF 2.2....\nI used only kaggle kernels so far (with TF 2.1)</p>",
      "rawMarkdown": "Thanks @mgornergoogle .  I will check if it works   in TF 2.2....\nI used only kaggle kernels so far (with TF 2.1)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 835591,
      "author_name": "hmendonca",
      "author_url": "",
      "post_date": "05/06/2020 11:23:24",
      "content": "<p><a href=\"/lucca9211\">@lucca9211</a>  I use <code>model.save_weights()</code> but I guess it's the same for <code>tf.saved_model.save()</code>\nIt looks like the tpu strategy can only save *.tf paths to google storage, so you have to point to a gs:// address.\nHowever, you can save *.h5 paths locally. I.e. your <code>model_save_path</code> should end with <code>h5</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 835656,
          "author_name": "lucca9211",
          "author_url": "",
          "post_date": "05/06/2020 12:29:44",
          "content": "<p>Its working.\n```\nimport h5py</p>\n\n<p>model_json = model.to_json()\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)</p>\n\n<p>model.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")`\n<code>\n</code>\nfrom tensorflow.keras.models import model_from_json\njson_file = open('model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\nloaded_model = model_from_json(loaded_model_json)\n```</p>\n\n<p><code>\nloaded_model.load_weights(\"model.h5\")\nprint(\"Loaded model from disk\")\n</code></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 835685,
      "author_name": "isakev",
      "author_url": "",
      "post_date": "05/06/2020 12:50:14",
      "content": "<p>It could be that Kaggle disk space limit does not allow to save a large model together with its optimizer state, while saving weights only works. I had this problem and couldn't resolve it. </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 836066,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "05/06/2020 17:34:42",
      "content": "<p>Sorry, this a bit buggy right now. Saving works in HDF5 format.</p>\n\n<p><code>keras_model.save(..., save_format='h5')</code>\nor\n<code>tf.saved_model.save(model, 'model_save_path.h5')</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 836173,
          "author_name": "serigne",
          "author_url": "",
          "post_date": "05/06/2020 19:29:05",
          "content": "<blockquote>\n  <p><strong>Martin Görner wrote:</strong></p>\n  \n  <p>Sorry, this a bit buggy right now. Saving works in HDF5 format.</p>\n  \n  <p><code>keras_model.save(..., save_format='h5')</code>\n  or\n  <code>tf.saved_model.save(model, 'model_save_path.h5')</code></p>\n</blockquote>\n\n<p>Hi <a href=\"/mgornergoogle\">@mgornergoogle</a>  , saving weights (or model) doesn't work for me when I have custom <code>tf.keras</code> layers. Do you have any tip about that ? Thx !</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 836276,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "05/06/2020 21:19:27",
          "content": "<p>Can you first check if it works in TF 2.2 (On Colab TPU). There have been improvements in this Area in TF 2.2.</p>\n\n<p>If not, please share your code.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 836345,
          "author_name": "serigne",
          "author_url": "",
          "post_date": "05/06/2020 22:37:35",
          "content": "<p>Thanks <a href=\"/mgornergoogle\">@mgornergoogle</a> .  I will check if it works   in TF 2.2....\nI used only kaggle kernels so far (with TF 2.1)</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "835254": "I am saving mode with \n`tf.saved_model.save(model, model_save_path)`\n but got error Unimplemented: File system scheme '[local]' not implemented\n\nThen how to save now?",
    "835591": "lucca9211  I use `model.save_weights()` but I guess it's the same for `tf.saved_model.save()`\nIt looks like the tpu strategy can only save *.tf paths to google storage, so you have to point to a gs:// address.\nHowever, you can save *.h5 paths locally. I.e. your `model_save_path` should end with `h5`",
    "835656": "Its working.\n```\nimport h5py\n\nmodel_json = model.to_json()\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)\n\nmodel.save_weights(\"model.h5\")\nprint(\"Saved model to disk\")`\n```\n```\nfrom tensorflow.keras.models import model_from_json\njson_file = open('model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\nloaded_model = model_from_json(loaded_model_json)\n```\n\n```\nloaded_model.load_weights(\"model.h5\")\nprint(\"Loaded model from disk\")\n```",
    "835685": "It could be that Kaggle disk space limit does not allow to save a large model together with its optimizer state, while saving weights only works. I had this problem and couldn't resolve it.",
    "836066": "Sorry, this a bit buggy right now. Saving works in HDF5 format.\n\n`keras_model.save(..., save_format='h5')`\nor\n`tf.saved_model.save(model, 'model_save_path.h5')`",
    "836173": "&gt; **Martin Görner wrote:**\n&gt; \n&gt; Sorry, this a bit buggy right now. Saving works in HDF5 format.\n&gt; \n&gt; `keras_model.save(..., save_format='h5')`\n&gt; or\n&gt; `tf.saved_model.save(model, 'model_save_path.h5')`\n\n\nHi @mgornergoogle  , saving weights (or model) doesn't work for me when I have custom `tf.keras` layers. Do you have any tip about that ? Thx !",
    "836276": "Can you first check if it works in TF 2.2 (On Colab TPU). There have been improvements in this Area in TF 2.2.\n\nIf not, please share your code.",
    "836345": "Thanks @mgornergoogle .  I will check if it works   in TF 2.2....\nI used only kaggle kernels so far (with TF 2.1)"
  },
  "source": "meta"
}