onnx

Форк
0
/
shape_inference.ipynb 
111 строк · 2.8 Кб
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 1,
6
   "metadata": {},
7
   "outputs": [
8
    {
9
     "name": "stdout",
10
     "output_type": "stream",
11
     "text": [
12
      "Before shape inference, the shape info of Y is:\n",
13
      "[]\n"
14
     ]
15
    }
16
   ],
17
   "source": [
18
    "import onnx\n",
19
    "from onnx import helper, shape_inference\n",
20
    "from onnx import TensorProto\n",
21
    "\n",
22
    "\n",
23
    "# Preprocessing: create a model with two nodes, Y's shape is unknown\n",
24
    "node1 = helper.make_node(\"Transpose\", [\"X\"], [\"Y\"], perm=[1, 0, 2])\n",
25
    "node2 = helper.make_node(\"Transpose\", [\"Y\"], [\"Z\"], perm=[1, 0, 2])\n",
26
    "\n",
27
    "graph = helper.make_graph(\n",
28
    "    [node1, node2],\n",
29
    "    \"two-transposes\",\n",
30
    "    [helper.make_tensor_value_info(\"X\", TensorProto.FLOAT, (2, 3, 4))],\n",
31
    "    [helper.make_tensor_value_info(\"Z\", TensorProto.FLOAT, (2, 3, 4))],\n",
32
    ")\n",
33
    "\n",
34
    "original_model = helper.make_model(graph, producer_name=\"onnx-examples\")\n",
35
    "\n",
36
    "# Check the model and print Y's shape information\n",
37
    "onnx.checker.check_model(original_model)\n",
38
    "print(\n",
39
    "    \"Before shape inference, the shape info of Y is:\\n{}\".format(\n",
40
    "        original_model.graph.value_info\n",
41
    "    )\n",
42
    ")"
43
   ]
44
  },
45
  {
46
   "cell_type": "code",
47
   "execution_count": 2,
48
   "metadata": {},
49
   "outputs": [
50
    {
51
     "name": "stdout",
52
     "output_type": "stream",
53
     "text": [
54
      "After shape inference, the shape info of Y is:\n",
55
      "[name: \"Y\"\n",
56
      "type {\n",
57
      "  tensor_type {\n",
58
      "    elem_type: 1\n",
59
      "    shape {\n",
60
      "      dim {\n",
61
      "        dim_value: 3\n",
62
      "      }\n",
63
      "      dim {\n",
64
      "        dim_value: 2\n",
65
      "      }\n",
66
      "      dim {\n",
67
      "        dim_value: 4\n",
68
      "      }\n",
69
      "    }\n",
70
      "  }\n",
71
      "}\n",
72
      "]\n"
73
     ]
74
    }
75
   ],
76
   "source": [
77
    "# Apply shape inference on the model\n",
78
    "inferred_model = shape_inference.infer_shapes(original_model)\n",
79
    "\n",
80
    "# Check the model and print Y's shape information\n",
81
    "onnx.checker.check_model(inferred_model)\n",
82
    "print(\n",
83
    "    \"After shape inference, the shape info of Y is:\\n{}\".format(\n",
84
    "        inferred_model.graph.value_info\n",
85
    "    )\n",
86
    ")"
87
   ]
88
  }
89
 ],
90
 "metadata": {
91
  "kernelspec": {
92
   "display_name": "Python 3",
93
   "language": "python",
94
   "name": "python3"
95
  },
96
  "language_info": {
97
   "codemirror_mode": {
98
    "name": "ipython",
99
    "version": 3
100
   },
101
   "file_extension": ".py",
102
   "mimetype": "text/x-python",
103
   "name": "python",
104
   "nbconvert_exporter": "python",
105
   "pygments_lexer": "ipython3",
106
   "version": "3.9.2"
107
  }
108
 },
109
 "nbformat": 4,
110
 "nbformat_minor": 2
111
}
112

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.