{
  "id": 400551,
  "title": "pytorch to onnx conversion question",
  "url": "/competitions/asl-signs/discussion/400551",
  "author_name": "",
  "post_date": "2023-04-09T02:29:06.471965Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>while converting pytorch to onnx and when using a version of below code snippet:</p>\n<pre><code> = torch.rand((, , )) \ntorch.onnx.export(\n    model,                  \n    ,  ...\n</code></pre>\n<p>what if there is an 'if statement' in the code, that depends on shape of the input?<br>\nAs an example, assume we are resizing the input </p>\n<pre><code> frames&gt;:\n    ...\n</code></pre>\n<p>How 'robust' is the onnx graph created when the sample input does only one branch of the if statement? For example in the example above, because input frame size is 20, the 'if branch' will not be executed. </p>\n<p>Here the solution might be not to use the if statement at all and resize in all circumstances, or take the first 30 frames, so the question is more on how 'to-onnx converters' handle these. </p>",
  "messages": [
    {
      "id": "2215011",
      "postDate": "04/09/2023 02:29:06",
      "content": "<p>while converting pytorch to onnx and when using a version of below code snippet:</p>\n<pre><code> = torch.rand((, , )) \ntorch.onnx.export(\n    model,                  \n    ,  ...\n</code></pre>\n<p>what if there is an 'if statement' in the code, that depends on shape of the input?<br>\nAs an example, assume we are resizing the input </p>\n<pre><code> frames&gt;:\n    ...\n</code></pre>\n<p>How 'robust' is the onnx graph created when the sample input does only one branch of the if statement? For example in the example above, because input frame size is 20, the 'if branch' will not be executed. </p>\n<p>Here the solution might be not to use the if statement at all and resize in all circumstances, or take the first 30 frames, so the question is more on how 'to-onnx converters' handle these. </p>",
      "rawMarkdown": "while converting pytorch to onnx and when using a version of below code snippet:\n\n```python\ninput = torch.rand((20, 543, 3)) # frame x landmarks x XYZ\ntorch.onnx.export(\n    model,                  \n    input,  ...\n```\nwhat if there is an 'if statement' in the code, that depends on shape of the input?\nAs an example, assume we are resizing the input \n```python\nif frames>30:\n    ...\n```\nHow 'robust' is the onnx graph created when the sample input does only one branch of the if statement? For example in the example above, because input frame size is 20, the 'if branch' will not be executed. \n\nHere the solution might be not to use the if statement at all and resize in all circumstances, or take the first 30 frames, so the question is more on how 'to-onnx converters' handle these.",
      "votes": null
    },
    {
      "id": "2215157",
      "postDate": "04/09/2023 05:59:07",
      "content": "<p>The ONNX graph wont deal with the 'if-else' statement directly. One way to deal with this is by using dynamic input shapes. If your model can handle dynamic input shapes, you can specify dynamic axes during the ONNX export process. This way, the ONNX graph will be able to adapt to different input shapes at runtime. </p>\n<p><strong>Here's an example:</strong></p>\n<pre><code>.\n.\n.\ndynamic_axes = {\n    : {: , : , : },\n}\n\n\ntorch.onnx.export(\n    model,\n    ,\n    ,\n    input_names=[],\n    dynamic_axes=dynamic_axes,\n)\n</code></pre>\n<p>Or learn more about <code>dynamic_axes</code> in <a href=\"https://pytorch.org/docs/stable/onnx.html\" target=\"_blank\">Pytorch official document</a></p>\n<p>Hope this helps 😁</p>",
      "rawMarkdown": "The ONNX graph wont deal with the 'if-else' statement directly. One way to deal with this is by using dynamic input shapes. If your model can handle dynamic input shapes, you can specify dynamic axes during the ONNX export process. This way, the ONNX graph will be able to adapt to different input shapes at runtime. \n\n**Here's an example:**\n\n```python\n.\n.\n.\ndynamic_axes = {\n    \"input\": {0: \"frame\", 1: \"landmarks\", 2: \"xyz\"},\n}\n\n\ntorch.onnx.export(\n    model,\n    input,\n    \"model.onnx\",\n    input_names=[\"input\"],\n    dynamic_axes=dynamic_axes,\n)\n```\n\nOr learn more about `dynamic_axes` in [Pytorch official document](https://pytorch.org/docs/stable/onnx.html)\n\nHope this helps 😁",
      "votes": null
    },
    {
      "id": "2215383",
      "postDate": "04/09/2023 09:01:54",
      "content": "<p>Thank You,</p>",
      "rawMarkdown": "Thank You,",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2215157,
      "author_name": "thaweewatboy",
      "author_url": "",
      "post_date": "04/09/2023 05:59:07",
      "content": "<p>The ONNX graph wont deal with the 'if-else' statement directly. One way to deal with this is by using dynamic input shapes. If your model can handle dynamic input shapes, you can specify dynamic axes during the ONNX export process. This way, the ONNX graph will be able to adapt to different input shapes at runtime. </p>\n<p><strong>Here's an example:</strong></p>\n<pre><code>.\n.\n.\ndynamic_axes = {\n    : {: , : , : },\n}\n\n\ntorch.onnx.export(\n    model,\n    ,\n    ,\n    input_names=[],\n    dynamic_axes=dynamic_axes,\n)\n</code></pre>\n<p>Or learn more about <code>dynamic_axes</code> in <a href=\"https://pytorch.org/docs/stable/onnx.html\" target=\"_blank\">Pytorch official document</a></p>\n<p>Hope this helps 😁</p>",
      "votes": null,
      "replies": [
        {
          "id": 2215383,
          "author_name": "abdulkadirguner",
          "author_url": "",
          "post_date": "04/09/2023 09:01:54",
          "content": "<p>Thank You,</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2215011": "while converting pytorch to onnx and when using a version of below code snippet:\n\n```python\ninput = torch.rand((20, 543, 3)) # frame x landmarks x XYZ\ntorch.onnx.export(\n    model,                  \n    input,  ...\n```\nwhat if there is an 'if statement' in the code, that depends on shape of the input?\nAs an example, assume we are resizing the input \n```python\nif frames>30:\n    ...\n```\nHow 'robust' is the onnx graph created when the sample input does only one branch of the if statement? For example in the example above, because input frame size is 20, the 'if branch' will not be executed. \n\nHere the solution might be not to use the if statement at all and resize in all circumstances, or take the first 30 frames, so the question is more on how 'to-onnx converters' handle these.",
    "2215157": "The ONNX graph wont deal with the 'if-else' statement directly. One way to deal with this is by using dynamic input shapes. If your model can handle dynamic input shapes, you can specify dynamic axes during the ONNX export process. This way, the ONNX graph will be able to adapt to different input shapes at runtime. \n\n**Here's an example:**\n\n```python\n.\n.\n.\ndynamic_axes = {\n    \"input\": {0: \"frame\", 1: \"landmarks\", 2: \"xyz\"},\n}\n\n\ntorch.onnx.export(\n    model,\n    input,\n    \"model.onnx\",\n    input_names=[\"input\"],\n    dynamic_axes=dynamic_axes,\n)\n```\n\nOr learn more about `dynamic_axes` in [Pytorch official document](https://pytorch.org/docs/stable/onnx.html)\n\nHope this helps 😁",
    "2215383": "Thank You,"
  },
  "source": "meta"
}