{
  "id": 148615,
  "title": "Loading keras model failed",
  "url": "/competitions/flower-classification-with-tpus/discussion/148615",
  "author_name": "",
  "post_date": "2020-05-05T00:57:03.135711600Z",
  "votes": 1,
  "comment_count": 7,
  "views": 0,
  "content": "<p>I loaded my model with load_model func(Keras API), and the code like this:</p>\n\n<p>with strategy.scope():</p>\n\n<pre><code>print('Model loading...')\n\nmodel_path = '/kaggle/input/xxx/xxx.h5'\n\nmodel = tf.keras.models.load_model(model_path)\n</code></pre>\n\n<p>But...\nError: \"Nonetype\" has no attribute \"merge_call\"...</p>\n\n<p>If loading outside scope, it works! But using cpu...hence very very slow to predict or test..T_T\nHow  to load my model in scope again? Thank you for your reply!!</p>",
  "messages": [
    {
      "id": "833639",
      "postDate": "05/05/2020 00:57:03",
      "content": "<p>I loaded my model with load_model func(Keras API), and the code like this:</p>\n\n<p>with strategy.scope():</p>\n\n<pre><code>print('Model loading...')\n\nmodel_path = '/kaggle/input/xxx/xxx.h5'\n\nmodel = tf.keras.models.load_model(model_path)\n</code></pre>\n\n<p>But...\nError: \"Nonetype\" has no attribute \"merge_call\"...</p>\n\n<p>If loading outside scope, it works! But using cpu...hence very very slow to predict or test..T_T\nHow  to load my model in scope again? Thank you for your reply!!</p>",
      "rawMarkdown": "I loaded my model with load_model func(Keras API), and the code like this:\n\n\nwith strategy.scope():\n    \n    print('Model loading...')\n    \n    model_path = '/kaggle/input/xxx/xxx.h5'\n\n    model = tf.keras.models.load_model(model_path)\n\nBut...\nError: \"Nonetype\" has no attribute \"merge_call\"...\n\nIf loading outside scope, it works! But using cpu...hence very very slow to predict or test..T_T\nHow  to load my model in scope again? Thank you for your reply!!",
      "votes": null
    },
    {
      "id": "834265",
      "postDate": "05/05/2020 12:48:11",
      "content": "<p>Load_model call should work in both cases : with or without the use of a strategy context. and yes its a <a href=\"https://github.com/tensorflow/tensorflow/issues/38423\">bug</a> it doesnt load model inside scope in tensorflow 2.1. </p>\n\n<p>Try with tf-nightly this works as expected:</p>\n\n<pre><code> !pip install tf-nightly-gpu\n strategy_context = tf.distribute.OneDeviceStrategy(device=\"/gpu:0\") \n with strategy_context.scope():\n      model1 = tf.keras.models.load_model(model_path)\n      print('model summary after loading in a scope')\n      model1.summary()\n</code></pre>",
      "rawMarkdown": "Load_model call should work in both cases : with or without the use of a strategy context. and yes its a [bug](https://github.com/tensorflow/tensorflow/issues/38423) it doesnt load model inside scope in tensorflow 2.1. \n\n\nTry with tf-nightly this works as expected:\n\n     !pip install tf-nightly-gpu\n     strategy_context = tf.distribute.OneDeviceStrategy(device=\"/gpu:0\") \n     with strategy_context.scope():\n          model1 = tf.keras.models.load_model(model_path)\n          print('model summary after loading in a scope')\n          model1.summary()",
      "votes": null
    },
    {
      "id": "834684",
      "postDate": "05/05/2020 17:39:56",
      "content": "<p>Single GPU usage in Tensorflow is automatic. You don't need OneDeviceStrategy for it.\nIf using TPUs, please don't manually upgrade to tf-nightly. It will upgrade your VM but not the TPU and you will have communication problems between the two (weird errors).</p>",
      "rawMarkdown": "Single GPU usage in Tensorflow is automatic. You don't need OneDeviceStrategy for it.\nIf using TPUs, please don't manually upgrade to tf-nightly. It will upgrade your VM but not the TPU and you will have communication problems between the two (weird errors).",
      "votes": null
    },
    {
      "id": "834736",
      "postDate": "05/05/2020 18:32:05",
      "content": "<p>This is a known bug indeed. Here is a workaround. If you copy the model from TPU to CPU first and then save it, you will be able to reload it in the strategy scope:</p>\n\n<p>```\nmodel_copy = create_model() # whatever your model creation code is\nmodel_copy.set_weights(trained_model.get_weights()) # copy your trained weights\nmodel_copy.save(\"model.h5\")</p>\n\n<p>with strategy.scope():\n  reload_model = tf.keras.models.load_model('model.h5')\n```</p>\n\n<p>I made a notebook with this code:\n<a href=\"https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu\">https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu</a></p>",
      "rawMarkdown": "This is a known bug indeed. Here is a workaround. If you copy the model from TPU to CPU first and then save it, you will be able to reload it in the strategy scope:\n\n```\nmodel_copy = create_model() # whatever your model creation code is\nmodel_copy.set_weights(trained_model.get_weights()) # copy your trained weights\nmodel_copy.save(\"model.h5\")\n\nwith strategy.scope():\n  reload_model = tf.keras.models.load_model('model.h5')\n```\n\nI made a notebook with this code:\nhttps://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu",
      "votes": null
    },
    {
      "id": "834806",
      "postDate": "05/05/2020 19:36:27",
      "content": "<p>Thanks. Good to know. I wasn't actually aware of that.</p>",
      "rawMarkdown": "Thanks. Good to know. I wasn't actually aware of that.",
      "votes": null
    },
    {
      "id": "835027",
      "postDate": "05/06/2020 02:00:21",
      "content": "<p>Hi, 404 not found your web...</p>",
      "rawMarkdown": "Hi, 404 not found your web...",
      "votes": null
    },
    {
      "id": "836052",
      "postDate": "05/06/2020 17:25:33",
      "content": "<p>😧 Works better when I make the notebook public. You can access it now: <a href=\"https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu\">https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu</a></p>",
      "rawMarkdown": "😧 Works better when I make the notebook public. You can access it now: https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu",
      "votes": null
    },
    {
      "id": "836121",
      "postDate": "05/06/2020 18:24:17",
      "content": "<p>As an alternative to <code>model.save('model.h5')</code>, you can use <code>model.save_weights('weights.h5')</code>. Then in your inference notebook, just load the weights <code>(model.load_weights('weights.h5')</code> after you create the model.</p>",
      "rawMarkdown": "As an alternative to `model.save('model.h5')`, you can use `model.save_weights('weights.h5')`. Then in your inference notebook, just load the weights `(model.load_weights('weights.h5')` after you create the model.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 834265,
      "author_name": "starkking07",
      "author_url": "",
      "post_date": "05/05/2020 12:48:11",
      "content": "<p>Load_model call should work in both cases : with or without the use of a strategy context. and yes its a <a href=\"https://github.com/tensorflow/tensorflow/issues/38423\">bug</a> it doesnt load model inside scope in tensorflow 2.1. </p>\n\n<p>Try with tf-nightly this works as expected:</p>\n\n<pre><code> !pip install tf-nightly-gpu\n strategy_context = tf.distribute.OneDeviceStrategy(device=\"/gpu:0\") \n with strategy_context.scope():\n      model1 = tf.keras.models.load_model(model_path)\n      print('model summary after loading in a scope')\n      model1.summary()\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 834684,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "05/05/2020 17:39:56",
          "content": "<p>Single GPU usage in Tensorflow is automatic. You don't need OneDeviceStrategy for it.\nIf using TPUs, please don't manually upgrade to tf-nightly. It will upgrade your VM but not the TPU and you will have communication problems between the two (weird errors).</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 834806,
          "author_name": "starkking07",
          "author_url": "",
          "post_date": "05/05/2020 19:36:27",
          "content": "<p>Thanks. Good to know. I wasn't actually aware of that.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 834736,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "05/05/2020 18:32:05",
      "content": "<p>This is a known bug indeed. Here is a workaround. If you copy the model from TPU to CPU first and then save it, you will be able to reload it in the strategy scope:</p>\n\n<p>```\nmodel_copy = create_model() # whatever your model creation code is\nmodel_copy.set_weights(trained_model.get_weights()) # copy your trained weights\nmodel_copy.save(\"model.h5\")</p>\n\n<p>with strategy.scope():\n  reload_model = tf.keras.models.load_model('model.h5')\n```</p>\n\n<p>I made a notebook with this code:\n<a href=\"https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu\">https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 835027,
          "author_name": "astzls",
          "author_url": "",
          "post_date": "05/06/2020 02:00:21",
          "content": "<p>Hi, 404 not found your web...</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 836052,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "05/06/2020 17:25:33",
          "content": "<p>😧 Works better when I make the notebook public. You can access it now: <a href=\"https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu\">https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 836121,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "05/06/2020 18:24:17",
      "content": "<p>As an alternative to <code>model.save('model.h5')</code>, you can use <code>model.save_weights('weights.h5')</code>. Then in your inference notebook, just load the weights <code>(model.load_weights('weights.h5')</code> after you create the model.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "833639": "I loaded my model with load_model func(Keras API), and the code like this:\n\n\nwith strategy.scope():\n    \n    print('Model loading...')\n    \n    model_path = '/kaggle/input/xxx/xxx.h5'\n\n    model = tf.keras.models.load_model(model_path)\n\nBut...\nError: \"Nonetype\" has no attribute \"merge_call\"...\n\nIf loading outside scope, it works! But using cpu...hence very very slow to predict or test..T_T\nHow  to load my model in scope again? Thank you for your reply!!",
    "834265": "Load_model call should work in both cases : with or without the use of a strategy context. and yes its a [bug](https://github.com/tensorflow/tensorflow/issues/38423) it doesnt load model inside scope in tensorflow 2.1. \n\n\nTry with tf-nightly this works as expected:\n\n     !pip install tf-nightly-gpu\n     strategy_context = tf.distribute.OneDeviceStrategy(device=\"/gpu:0\") \n     with strategy_context.scope():\n          model1 = tf.keras.models.load_model(model_path)\n          print('model summary after loading in a scope')\n          model1.summary()",
    "834684": "Single GPU usage in Tensorflow is automatic. You don't need OneDeviceStrategy for it.\nIf using TPUs, please don't manually upgrade to tf-nightly. It will upgrade your VM but not the TPU and you will have communication problems between the two (weird errors).",
    "834736": "This is a known bug indeed. Here is a workaround. If you copy the model from TPU to CPU first and then save it, you will be able to reload it in the strategy scope:\n\n```\nmodel_copy = create_model() # whatever your model creation code is\nmodel_copy.set_weights(trained_model.get_weights()) # copy your trained weights\nmodel_copy.save(\"model.h5\")\n\nwith strategy.scope():\n  reload_model = tf.keras.models.load_model('model.h5')\n```\n\nI made a notebook with this code:\nhttps://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu",
    "834806": "Thanks. Good to know. I wasn't actually aware of that.",
    "835027": "Hi, 404 not found your web...",
    "836052": "😧 Works better when I make the notebook public. You can access it now: https://www.kaggle.com/mgornergoogle/five-flowers-train-save-and-reload-on-tpu",
    "836121": "As an alternative to `model.save('model.h5')`, you can use `model.save_weights('weights.h5')`. Then in your inference notebook, just load the weights `(model.load_weights('weights.h5')` after you create the model."
  },
  "source": "meta"
}