当前位置:网站首页>AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
2022-06-26 09:45:00 【氵文大师】
AdaptiveAvgPool2D 不支持 onnx 导出,导出过程会告诉你,onnx不支持那个动态操作巴拉巴拉
我用的是 pp_liteseg 导出为 onnx 模型,都一样,paddle和Torch的 Adaptive Pool2D 都是动态的,onnx暂时都不支持,我根据下述公式,将 AdaptiveAvgPool2D 改了,可以用onnx导出了,但是需要指定一下原图的大小,和输出图的大小
h s t a r t = f l o o r ( i ∗ H i n / H o u t ) h e n d = c e i l ( ( i + 1 ) ∗ H i n / H o u t ) w s t a r t = f l o o r ( j ∗ W i n / W o u t ) w e n d = c e i l ( ( j + 1 ) ∗ W i n / W o u t ) O u t p u t ( i , j ) = ∑ I n p u t [ h s t a r t : h e n d , w s t a r t : w e n d ] ( h e n d − h s t a r t ) ∗ ( w e n d − w s t a r t ) \begin{aligned} hstart &= floor(i * H_{in} / H_{out})\\ hend &= ceil((i + 1) * H_{in} / H_{out})\\ wstart &= floor(j * W_{in} / W_{out}) \\ wend &= ceil((j + 1) * W_{in} / W_{out}) \\ Output(i ,j) &= \frac{\sum Input[hstart:hend, wstart:wend]}{(hend - hstart) * (wend - wstart)} \end{aligned} hstarthendwstartwendOutput(i,j)=floor(i∗Hin/Hout)=ceil((i+1)∗Hin/Hout)=floor(j∗Win/Wout)=ceil((j+1)∗Win/Wout)=(hend−hstart)∗(wend−wstart)∑Input[hstart:hend,wstart:wend]
参考自:
AdaptiveAvgPool2D
class CostomAdaptiveAvgPool2D(nn.Layer):
def __init__(self, output_size, input_size):
super(CostomAdaptiveAvgPool2D, self).__init__()
self.output_size = output_size
self.input_size = input_size
def forward(self, x):
H_in, W_in = self.input_size
H_out, W_out = [self.output_size, self.output_size] \
if isinstance(self.output_size, int) \
else self.output_size
out_i = []
for i in range(H_out):
out_j = []
for j in range(W_out):
hs = int(np.floor(i * H_in / H_out))
he = int(np.ceil((i+1) * H_in / H_out))
ws = int(np.floor(j * W_in / W_out))
we = int(np.ceil((j+1) * W_in / W_out))
# print(hs, he, ws, we)
kernel_size = [he-hs, we-ws]
out = F.avg_pool2d(x[:, :, hs:he, ws:we], kernel_size)
out_j.append(out)
out_j = paddle.concat(out_j, -1)
out_i.append(out_j)
out_i = paddle.concat(out_i, -2)
return out_i
用 CostomAdaptiveAvgPool2D 调换 AdaptiveAvgPool2D, 并且指定原图和输出图的大小即可

[14,14] 就是输入图的大小,我这个pp_liteseg 输入shape是 224 × 224 224\times 224 224×224,所以上一层的shape是
[-1, 512, 14, 14]
然后一次过三个AdaptiveAvgPool2D 变成
[-1, 512, 1, 1]
[-1, 512, 2, 2]
[-1, 512, 4, 4]
然后 paddle2onnx 导出即可
paddle2onnx --model_dir . \
--model_filename model.pdmodel \
--params_filename model.pdiparams \
--opset_version 11 \
--save_file output.onnx
边栏推荐
- Function run time
- 【LeetCode】59. 螺旋矩阵 II
- echo $?
- Global and Chinese market for baked potato chips 2022-2028: Research Report on technology, participants, trends, market size and share
- 字符串常量池、class常量池和运行时常量池
- MySQL 13th job - transaction management
- 【Leetcode】76. Minimum covering substring
- Common interview questions of binary tree
- Hcia-dhcp experiment
- MySQL Chapter 6 Summary
猜你喜欢

看我在Map<String, String>集合中,存入Integer类型数据

MySQL 9th job - connection Query & sub query

Based on Zeng Shen's explanation, the line segment tree is studied again one

The sixth MySQL job - query data - multiple conditions

Establishment of smart dialogue platform for wechat official account

36 qtextedit control input multiline text

方法区里面有什么——class文件、class文件常量池、运行时常量池

Hcia-dhcp experiment

MySQL第八次作业

Allocation de mémoire tas lors de la création d'objets
随机推荐
【LeetCode】59. 螺旋矩阵 II
MySQL第十一作業-視圖的應用
About multi table query of MySQL
1. 两数之和(LeetCode题目)
3行3列整形二维数组,求对角之和
MySQL 9th job - connection Query & sub query
leetCode-链表的中间结点
全渠道、多场景、跨平台,App如何借助数据分析渠道流量
【LeetCode】59. Spiral matrix II
Global and Chinese market of recycled paper 2022-2028: Research Report on technology, participants, trends, market size and share
Establishment of smart dialogue platform for wechat official account
Svn command
Small example of SSM project, detailed tutorial of SSM integration
Record the handling of oom problems caused by too many threads at one time
How QT uses quazip to compress and decompress files
Today's headline adaptation scheme code
String constant pool, class constant pool, and runtime constant pool
Win10安装tensorflow-quantum过程详解
Servlet learning notes II
MySQL第八次作业