MindSpore报错: Conv2D第三维输出数据类型必须是正整数或者SHP_ANY, but got -59

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): Ascend Software Environment: -- MindSpore version (source or binary): 1.6.0 -- Python version (e.g., Python 3.7.5): 3.7.6 -- OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic -- GCC/Compiler version (if compiled from source):

1.2 基本信息

1.2.1 脚本

训练脚本是通过构建Conv2D的单算子网络,来实现2d卷积操作的。脚本如下:

01 class Net(nn.Cell):
02     def __init__(self, in_channels, out_channels, kernel_size):
03         super(Net, self).__init__()
04         self.in_channels = in_channels
05         self.out_channels = out_channels
06         self.kernel_size = kernel_size
07         # self.pad_mode = "valid"
08         self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode="valid")
09 
10     def construct(self, x):
11         return self.conv2d(x)
12 
13 net = Net(in_channels=2, out_channels=10, kernel_size=64)
14 x = Tensor(np.random.randn(1, 2, 4, 64), mindspore.float32)
15 y = net(x).shape
16 print("otput shape: ", y)

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
  File "C:/Users/user1/PycharmProjects/q2_map/new/I4GYSL.py", line 20, in <module>
    y = net(x).shape
  File "C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 586, in __call__
    out = self.compile_and_run(*args)
  File "C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 964, in compile_and_run
    self.compile(*inputs)
  File "C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py", line 937, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File "C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\common\api.py", line 1006, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
ValueError: build\mindspore\merge\mindspore\core\ops_merge.cc:6605 CheckShapeAnyAndPositive] Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59
WARNING: Logging before InitGoogleLogging() is written to STDERR
[CRITICAL] CORE(2540,1,?):2022-6-28 21:12:34 [build\mindspore\merge\mindspore\core\ops_merge.cc:6605] CheckShapeAnyAndPositive] Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59

2 原因分析

我们看报错信息,在ValueError中,写到Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59,意思是Conv2d的输出形状的第三维也就是H-out应该是大于0的整数或者-1, 而实际得到的是-59. 结合官网API对Conv2 D的output描述,当pad_mode为“valid”时,选择第二组公式代入数据得到H-out为-59. 因此解决的方法有调整传入的参数大小或者将pad_mode设置为“same”

3 解决方法

基于上面已知的原因,很容易做出如下修改:

01 class Net(nn.Cell):
02     def __init__(self, in_channels, out_channels, kernel_size):
03         super(Net, self).__init__()
04         self.in_channels = in_channels
05         self.out_channels = out_channels
06         self.kernel_size = kernel_size
07         # self.pad_mode = "valid"
08         self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode="valid")
09 
10     def construct(self, x):
11         return self.conv2d(x)
12 
13 net = Net(in_channels=2, out_channels=10, kernel_size=64)
14 x = Tensor(np.random.randn(1, 2, 64, 64), mindspore.float32)
15 y = net(x).shape
16 print("otput shape: ", y)

此时执行成功,输出如下:
otput shape: (1, 10, 1, 1)

4 总结

定位报错问题的步骤:
1、找到报错的用户代码行: y = net(x).shape ;
2、 根据日志报错信息中的关键字,缩小分析问题的范围 Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59 ;
3、需要重点关注变量定义、初始化的正确性。

5 参考文档

5.1 Conv2D算子API接口