generated from xinetzone/xyzstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liuxinwei
committed
Apr 23, 2024
1 parent
9c0124b
commit a5029c0
Showing
24 changed files
with
1,038 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# ONNX GatherND\n", | ||
"\n", | ||
"参考:[GatherND](https://onnx.ai/onnx/operators/onnx__GatherND.html#gathernd)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# onnx L2 范数" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"from torch import nn\n", | ||
"\n", | ||
"class Model(nn.Module):\n", | ||
" def forward(self, x):\n", | ||
" x = torch.norm(x, p=2)\n", | ||
" return x\n", | ||
"\n", | ||
"x = torch.rand(1, 3, 32, 32)\n", | ||
"\n", | ||
"torch_model = Model()\n", | ||
"# 导出模型\n", | ||
"torch.onnx.export(\n", | ||
" torch_model, # torch 模型\n", | ||
" x, # 模型输入或者对于多个输入,使用元组\n", | ||
" \"L2.onnx\", # 模型保存的位置(可以是文件或类似文件的对象)\n", | ||
" export_params=True, # 将训练后的参数权重存储在模型文件内\n", | ||
" opset_version=17, # 导出模型的 ONNX 版本\n", | ||
" do_constant_folding=True, # 是否执行常量折叠以进行优化\n", | ||
" input_names = ['data'], # 模型的输入名称\n", | ||
" output_names = ['output'], # 模型的输出名称\n", | ||
" dynamic_axes={'data' : {0 : 'batch_size'}, # 可变长度的轴\n", | ||
" 'output' : {0 : 'batch_size'}}\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"![](images/L2.jpg)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "xin", | ||
"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.12.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# onnx ScatterND\n", | ||
"\n", | ||
"参考:[ScatterND](https://onnx.ai/onnx/operators/onnx__ScatterND.html)\n", | ||
"\n", | ||
"ScatterND 算子涉及三个输入:秩为 `r`(`r >= 1`)的 `data` 张量,秩为 `q`(`q >= 1`)的 `indices` 张量,以及秩为 `q + r - indices.shape[-1] - 1` 的 `updates` 张量。该算子的输出是通过创建 `data` 输入的副本,然后根据 ``indices`` 指定的特定位置,将其值更新为 `updates` 张量中指定的值来产生的。它的输出形状与 `data` 的形状相同。\n", | ||
"\n", | ||
"`indices` 是整数张量。设 `k` 为索引形状的最后一维,即 `indices.shape[-1]`。`indices` 被视为由 `k` 元组组成的 `(q-1)` 维张量,其中每个 `k` 元组都是对 `data` 的偏索引(partial-index)。因此, `k` 的值最多可以等于数据的秩。当 `k` 等于 `data` 的秩时,每个更新项指定了对张量单个元素的更新。当 `k` 小于 `data` 的秩时,每个更新项指定了对张量切片的更新。索引值可以是负数,按照从末尾开始倒数的通常惯例,但需要在有效范围内。\n", | ||
"\n", | ||
"`updates` 被视为替换切片值的(q-1)维张量。因此,`updates` 形状的前(q-1)个维度必须与索引形状的前(q-1)个维度匹配。`updates` 的其余维度对应于替换切片值的维度。每个替换切片值是 (r-k) 维张量,对应于 `data` 的尾部 (r-k) 个维度。因此,`updates` 的形状必须等于 `indices.shape[0:q-1] ++ data.shape[k:r-1]`,其中 `++` 表示形状的连接。\n", | ||
"\n", | ||
"输出通过以下方程计算:\n", | ||
"\n", | ||
"```\n", | ||
"output = np.copy(data)\n", | ||
"update_indices = indices.shape[:-1]\n", | ||
"for idx in np.ndindex(update_indices):\n", | ||
" output[indices[idx]] = updates[idx]\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"上述循环中的迭代顺序未指定。特别是,索引中不应有重复项:也就是说,如果 `idx1 != idx2`,那么 `indices[idx1] != indices[idx2]`。这确保了输出值不依赖于迭代顺序。\n", | ||
"\n", | ||
"缩减(`reduction`)允许指定可选的缩减操作,该操作将所有 `updates` 张量中的值应用于指定 `indices` 的 `output`。在将 `reduction` 设置为“none”的情况下,索引中不应有重复项:也就是说,如果 `idx1 != idx2`,那么 `indices[idx1] != indices[idx2]`。这确保了输出值不依赖于迭代顺序。\n", | ||
"\n", | ||
"当 `reduction` 设置为某个缩减函数 `f` 时,`output` 按以下方式计算:\n", | ||
"\n", | ||
"```\n", | ||
"output = np.copy(data)\n", | ||
"update_indices = indices.shape[:-1]\n", | ||
"for idx in np.ndindex(update_indices):\n", | ||
" output[indices[idx]] = f(output[indices[idx]], updates[idx])\n", | ||
"```\n", | ||
"\n", | ||
"其中 `f` 是指定的加法(`+`),乘法(`*`),最大值(`max`)或最小值(`min`)。\n", | ||
"\n", | ||
"这个算子是 `GatherND` 的逆运算。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"示例1:\n", | ||
"```\n", | ||
"data = [1, 2, 3, 4, 5, 6, 7, 8]\n", | ||
"indices = [[4], [3], [1], [7]]\n", | ||
"updates = [9, 10, 11, 12]\n", | ||
"output = [1, 11, 3, 10, 9, 6, 7, 12]\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"示例2:\n", | ||
"\n", | ||
"```\n", | ||
"data = [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n", | ||
" [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n", | ||
" [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n", | ||
" [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n", | ||
"indices = [[0], [2]]\n", | ||
"updates = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n", | ||
" [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]]\n", | ||
"output = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n", | ||
" [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n", | ||
" [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n", | ||
" [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"from torch import nn\n", | ||
"\n", | ||
"class Model(nn.Module):\n", | ||
" def forward(self, x):\n", | ||
" x[0] = x[0] + 1\n", | ||
" return x\n", | ||
"\n", | ||
"x = torch.rand(192, 36)\n", | ||
"\n", | ||
"torch_model = Model()\n", | ||
"# 导出模型\n", | ||
"torch.onnx.export(\n", | ||
" torch_model, # torch 模型\n", | ||
" x, # 模型输入或者对于多个输入,使用元组\n", | ||
" \"ScatterND.onnx\", # 模型保存的位置(可以是文件或类似文件的对象)\n", | ||
" export_params=True, # 将训练后的参数权重存储在模型文件内\n", | ||
" opset_version=17, # 导出模型的 ONNX 版本\n", | ||
" do_constant_folding=True, # 是否执行常量折叠以进行优化\n", | ||
" input_names = ['data'], # 模型的输入名称\n", | ||
" output_names = ['output'], # 模型的输出名称\n", | ||
" # dynamic_axes={'data' : {0 : 'batch_size'}, # 可变长度的轴\n", | ||
" # 'output' : {0 : 'batch_size'}}\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"![模型结构](images/ScatterND.jpg)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "xin", | ||
"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.12.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# BN" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"from torch import nn\n", | ||
"\n", | ||
"class Model(nn.Module):\n", | ||
" def __init__(self):\n", | ||
" super().__init__()\n", | ||
" self.conv1 = nn.Conv2d(3, 16, 3, 1, 1, bias=True, groups=1)\n", | ||
" self.bn = nn.BatchNorm2d(16)\n", | ||
"\n", | ||
" def forward(self, x):\n", | ||
" x = self.conv1(x)\n", | ||
" x = self.bn(x)\n", | ||
" return x\n", | ||
"\n", | ||
"x = torch.rand(1, 3, 32, 32)\n", | ||
"\n", | ||
"torch_model = Model()\n", | ||
"# 导出模型\n", | ||
"torch.onnx.export(\n", | ||
" torch_model, # torch 模型\n", | ||
" x, # 模型输入或者对于多个输入,使用元组\n", | ||
" \"bn.onnx\", # 模型保存的位置(可以是文件或类似文件的对象)\n", | ||
" export_params=True, # 将训练后的参数权重存储在模型文件内\n", | ||
" opset_version=17, # 导出模型的 ONNX 版本\n", | ||
" do_constant_folding=False, # 是否执行常量折叠以进行优化\n", | ||
" input_names = ['data'], # 模型的输入名称\n", | ||
" output_names = ['output'], # 模型的输出名称\n", | ||
" dynamic_axes={'data' : {0 : 'batch_size'}, # 可变长度的轴\n", | ||
" 'output' : {0 : 'batch_size'}}\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "xin", | ||
"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.12.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.