{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before shape inference, the shape info of Y is:\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import onnx\n",
    "from onnx import helper, shape_inference\n",
    "from onnx import TensorProto\n",
    "\n",
    "\n",
    "# Preprocessing: create a model with two nodes, Y's shape is unknown\n",
    "node1 = helper.make_node(\"Transpose\", [\"X\"], [\"Y\"], perm=[1, 0, 2])\n",
    "node2 = helper.make_node(\"Transpose\", [\"Y\"], [\"Z\"], perm=[1, 0, 2])\n",
    "\n",
    "graph = helper.make_graph(\n",
    "    [node1, node2],\n",
    "    \"two-transposes\",\n",
    "    [helper.make_tensor_value_info(\"X\", TensorProto.FLOAT, (2, 3, 4))],\n",
    "    [helper.make_tensor_value_info(\"Z\", TensorProto.FLOAT, (2, 3, 4))],\n",
    ")\n",
    "\n",
    "original_model = helper.make_model(graph, producer_name=\"onnx-examples\")\n",
    "\n",
    "# Check the model and print Y's shape information\n",
    "onnx.checker.check_model(original_model)\n",
    "print(\n",
    "    \"Before shape inference, the shape info of Y is:\\n{}\".format(\n",
    "        original_model.graph.value_info\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After shape inference, the shape info of Y is:\n",
      "[name: \"Y\"\n",
      "type {\n",
      "  tensor_type {\n",
      "    elem_type: 1\n",
      "    shape {\n",
      "      dim {\n",
      "        dim_value: 3\n",
      "      }\n",
      "      dim {\n",
      "        dim_value: 2\n",
      "      }\n",
      "      dim {\n",
      "        dim_value: 4\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "# Apply shape inference on the model\n",
    "inferred_model = shape_inference.infer_shapes(original_model)\n",
    "\n",
    "# Check the model and print Y's shape information\n",
    "onnx.checker.check_model(inferred_model)\n",
    "print(\n",
    "    \"After shape inference, the shape info of Y is:\\n{}\".format(\n",
    "        inferred_model.graph.value_info\n",
    "    )\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
