MindSpore报错 `half_pixel_centers`=True only support in Ascend

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): CPU
Software Environment:
– MindSpore version (source or binary): 1.8.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 脚本

调用ResizeBilinear算子,用双线性插值调整输入Tensor为指定的大小。脚本如下:

 01 context.set_context(device_target='CPU')
 02 x = Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mindspore.float32)
 03 resize_bilinear = ops.ResizeBilinear((5, 5), half_pixel_centers=True)
 04 output = resize_bilinear(x)
 05 print(output)

1.2.2 报错

这里报错信息如下:

Traceback (most recent call last):
  File "C:/Users/l30026544/PycharmProjects/q2_map/new/ResizeBilinear.py", line 7, in <module>
    resize_bilinear = ops.ResizeBilinear((5, 5), half_pixel_centers=True)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\primitive.py", line 687, in deco
    fn(self, *args, **kwargs)
  File "C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\operations\nn_ops.py", line 3263, in __init__
    raise ValueError(f"Currently `half_pixel_centers`=True only support in Ascend device_target, "
ValueError: Currently `half_pixel_centers`=True only support in Ascend device_target, but got CPU

原因分析

我们看报错信息,在ValueError中,写到Currently half_pixel_centers=True only support in Ascend device_target, but got CPU,意思是只支持在Ascend环境下half_pixel_centers属性才能设置为True。这一点官网API作了说明:
image.png

2 解决方法

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

 01 context.set_context(device_target='Ascend')
 02 x = Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mindspore.float32)
 03 resize_bilinear = ops.ResizeBilinear((5, 5), half_pixel_centers=True)
 04 output = resize_bilinear(x)
 05 print(output)

此时执行成功,输出如下:

[[[[1. 2. 3. 4. 5.]
[1. 2. 3. 4. 5.]
[1. 2. 3. 4. 5.]
[1. 2. 3. 4. 5.]
[1. 2. 3. 4. 5.]]]]

3 总结

定位报错问题的步骤:

1、找到报错的用户代码行:* resize_bilinear = ops.ResizeBilinear((5, 5), half_pixel_centers=True)*;

2、 根据日志报错信息中的关键字,缩小分析问题的范围Currently half_pixel_centers=True only support in Ascend device_target, but got CPU ;

4 参考文档

4.1 ResizeBilinear算子API接口

posted @ 2022-07-15 15:33  Skytier  阅读(73)  评论(0编辑  收藏  举报