[模型部署] 算子等价替换

[模型部署] 算子等价替换

  • ReduceMean
  • ReduceSum

ReduceMean

在PyTorch中可用torch.mean表示,该算子可被卷积等价替换;
存在输入张量的形状为[1,48,56,56],对其2,3维度进行ReduceMean,形状变为[1,48,1,1],可用特征图大小的卷积核,padding为0,stride设置为1或56,均可

代码如下:

import torch
a = torch.randn(1, 48, 56, 56)
conv = torch.nn.Conv2d(in_channels=48, out_channels=48, 
		kernel_size=(56, 56), stride=(1, 1), padding=(0, 0), groups=48, bias=False)
weight = torch.ones(48, 1, 56, 56) / (56 * 56)
conv.weight = torch.nn.parameter.Parameter(weight)
b = conv(a)
c = torch.mean(a, dim=(2, 3), keepdim=True)
print(b.detach().numpy().flatten())
print(c.detach().numpy().flatten())

ReduceSum

你可能感兴趣的:(模型部署,深度学习,python,pytorch)