{
  "id": 268414,
  "title": "Exporting model in Onnx",
  "url": "/competitions/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/268414",
  "author_name": "",
  "post_date": "2021-08-27T09:50:15.921099700Z",
  "votes": 6,
  "comment_count": 2,
  "views": 0,
  "content": "<h1>Exporting model in ONNX</h1>\n<h3>1) Pytorch</h3>\n<p>Export the model</p>\n<pre><code>    torch.onnx.export(model, dummy_input, \"ImageClassifier.onnx\", export_params=True,   \n         opset_version=10, do_constant_folding=True, input_names = ['modelInput'], \n         output_names = ['modelOutput'], dynamic_axes={'modelInput' : {0 : 'batch_size'}, \n         'modelOutput' : {0 : 'batch_size'}}) \n    print(\" \") \n    print('Model has been converted to ONNX')\n</code></pre>\n<h3>2) Tensorflow</h3>\n<pre><code>import tensorflow as tf\nimport tf2onnx\nfrom onnx import helper\n\n_TENSORFLOW_DOMAIN = \"ai.onnx.converters.tensorflow\"\n\n\ndef print_handler(ctx, node, name, args):\n    # replace tf.Print() with Identity\n    #   T output = Print(T input, data, @list(type) U, @string message, @int first_n, @int summarize)\n    # becomes:\n    #   T output = Identity(T Input)\n    node.domain = _TENSORFLOW_DOMAIN\n    del node.input[1:]\n    return node\n\n\nwith tf.Session() as sess:\n    x = tf.placeholder(tf.float32, [2, 3], name=\"input\")\n    x_ = tf.add(x, x)\n    x_ = tf.Print(x, [x], \"hello\")\n    _ = tf.identity(x_, name=\"output\")\n    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph,\n                                                 custom_op_handlers={\"Print\": (print_handler, [\"Identity\", \"mode\"])},\n                                                 extra_opset=[helper.make_opsetid(_TENSORFLOW_DOMAIN, 1)],\n                                                 input_names=[\"input:0\"],\n                                                 output_names=[\"output:0\"])\n    model_proto = onnx_graph.make_model(\"test\")\n    with open(\"/tmp/model.onnx\", \"wb\") as f:\n        f.write(model_proto.SerializeToString())\n</code></pre>",
  "messages": [
    {
      "id": "1492606",
      "postDate": "08/27/2021 09:50:15",
      "content": "<h1>Exporting model in ONNX</h1>\n<h3>1) Pytorch</h3>\n<p>Export the model</p>\n<pre><code>    torch.onnx.export(model, dummy_input, \"ImageClassifier.onnx\", export_params=True,   \n         opset_version=10, do_constant_folding=True, input_names = ['modelInput'], \n         output_names = ['modelOutput'], dynamic_axes={'modelInput' : {0 : 'batch_size'}, \n         'modelOutput' : {0 : 'batch_size'}}) \n    print(\" \") \n    print('Model has been converted to ONNX')\n</code></pre>\n<h3>2) Tensorflow</h3>\n<pre><code>import tensorflow as tf\nimport tf2onnx\nfrom onnx import helper\n\n_TENSORFLOW_DOMAIN = \"ai.onnx.converters.tensorflow\"\n\n\ndef print_handler(ctx, node, name, args):\n    # replace tf.Print() with Identity\n    #   T output = Print(T input, data, @list(type) U, @string message, @int first_n, @int summarize)\n    # becomes:\n    #   T output = Identity(T Input)\n    node.domain = _TENSORFLOW_DOMAIN\n    del node.input[1:]\n    return node\n\n\nwith tf.Session() as sess:\n    x = tf.placeholder(tf.float32, [2, 3], name=\"input\")\n    x_ = tf.add(x, x)\n    x_ = tf.Print(x, [x], \"hello\")\n    _ = tf.identity(x_, name=\"output\")\n    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph,\n                                                 custom_op_handlers={\"Print\": (print_handler, [\"Identity\", \"mode\"])},\n                                                 extra_opset=[helper.make_opsetid(_TENSORFLOW_DOMAIN, 1)],\n                                                 input_names=[\"input:0\"],\n                                                 output_names=[\"output:0\"])\n    model_proto = onnx_graph.make_model(\"test\")\n    with open(\"/tmp/model.onnx\", \"wb\") as f:\n        f.write(model_proto.SerializeToString())\n</code></pre>",
      "rawMarkdown": "# Exporting model in ONNX\n\n### 1) Pytorch\n\nExport the model\n\n```   \n    torch.onnx.export(model, dummy_input, \"ImageClassifier.onnx\", export_params=True,   \n         opset_version=10, do_constant_folding=True, input_names = ['modelInput'], \n         output_names = ['modelOutput'], dynamic_axes={'modelInput' : {0 : 'batch_size'}, \n         'modelOutput' : {0 : 'batch_size'}}) \n    print(\" \") \n    print('Model has been converted to ONNX')\n\n``` \n\n### 2) Tensorflow\n\n```\nimport tensorflow as tf\nimport tf2onnx\nfrom onnx import helper\n\n_TENSORFLOW_DOMAIN = \"ai.onnx.converters.tensorflow\"\n\n\ndef print_handler(ctx, node, name, args):\n    # replace tf.Print() with Identity\n    #   T output = Print(T input, data, @list(type) U, @string message, @int first_n, @int summarize)\n    # becomes:\n    #   T output = Identity(T Input)\n    node.domain = _TENSORFLOW_DOMAIN\n    del node.input[1:]\n    return node\n\n\nwith tf.Session() as sess:\n    x = tf.placeholder(tf.float32, [2, 3], name=\"input\")\n    x_ = tf.add(x, x)\n    x_ = tf.Print(x, [x], \"hello\")\n    _ = tf.identity(x_, name=\"output\")\n    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph,\n                                                 custom_op_handlers={\"Print\": (print_handler, [\"Identity\", \"mode\"])},\n                                                 extra_opset=[helper.make_opsetid(_TENSORFLOW_DOMAIN, 1)],\n                                                 input_names=[\"input:0\"],\n                                                 output_names=[\"output:0\"])\n    model_proto = onnx_graph.make_model(\"test\")\n    with open(\"/tmp/model.onnx\", \"wb\") as f:\n        f.write(model_proto.SerializeToString())\n\n```",
      "votes": null
    },
    {
      "id": "1495227",
      "postDate": "08/29/2021 11:53:00",
      "content": "<p>Onnx is essential for the transfer learning, Thank you for sharing </p>",
      "rawMarkdown": "Onnx is essential for the transfer learning, Thank you for sharing",
      "votes": null
    },
    {
      "id": "1495298",
      "postDate": "08/29/2021 12:55:37",
      "content": "<p>Thank you so much! 🙂</p>",
      "rawMarkdown": "Thank you so much! 🙂",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1495227,
      "author_name": "kumariricha",
      "author_url": "",
      "post_date": "08/29/2021 11:53:00",
      "content": "<p>Onnx is essential for the transfer learning, Thank you for sharing </p>",
      "votes": null,
      "replies": [
        {
          "id": 1495298,
          "author_name": "priteshraj10",
          "author_url": "",
          "post_date": "08/29/2021 12:55:37",
          "content": "<p>Thank you so much! 🙂</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1492606": "# Exporting model in ONNX\n\n### 1) Pytorch\n\nExport the model\n\n```   \n    torch.onnx.export(model, dummy_input, \"ImageClassifier.onnx\", export_params=True,   \n         opset_version=10, do_constant_folding=True, input_names = ['modelInput'], \n         output_names = ['modelOutput'], dynamic_axes={'modelInput' : {0 : 'batch_size'}, \n         'modelOutput' : {0 : 'batch_size'}}) \n    print(\" \") \n    print('Model has been converted to ONNX')\n\n``` \n\n### 2) Tensorflow\n\n```\nimport tensorflow as tf\nimport tf2onnx\nfrom onnx import helper\n\n_TENSORFLOW_DOMAIN = \"ai.onnx.converters.tensorflow\"\n\n\ndef print_handler(ctx, node, name, args):\n    # replace tf.Print() with Identity\n    #   T output = Print(T input, data, @list(type) U, @string message, @int first_n, @int summarize)\n    # becomes:\n    #   T output = Identity(T Input)\n    node.domain = _TENSORFLOW_DOMAIN\n    del node.input[1:]\n    return node\n\n\nwith tf.Session() as sess:\n    x = tf.placeholder(tf.float32, [2, 3], name=\"input\")\n    x_ = tf.add(x, x)\n    x_ = tf.Print(x, [x], \"hello\")\n    _ = tf.identity(x_, name=\"output\")\n    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph,\n                                                 custom_op_handlers={\"Print\": (print_handler, [\"Identity\", \"mode\"])},\n                                                 extra_opset=[helper.make_opsetid(_TENSORFLOW_DOMAIN, 1)],\n                                                 input_names=[\"input:0\"],\n                                                 output_names=[\"output:0\"])\n    model_proto = onnx_graph.make_model(\"test\")\n    with open(\"/tmp/model.onnx\", \"wb\") as f:\n        f.write(model_proto.SerializeToString())\n\n```",
    "1495227": "Onnx is essential for the transfer learning, Thank you for sharing",
    "1495298": "Thank you so much! 🙂"
  },
  "source": "meta"
}