# 报错ValueError: Currently \`half\_pixel\_centers\`=True only support in Ascend device\_target, but got CPU

**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 &quot;C:/Users/user1/PycharmProjects/q2_map/new/ResizeBilinear.py&quot;, line 7, in &lt;module&gt;
    resize_bilinear = ops.ResizeBilinear((5, 5), half_pixel_centers=True)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\primitive.py&quot;, line 687, in deco
    fn(self, *args, **kwargs)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\ops\operations\nn_ops.py&quot;, line 3263, in __init__
    raise ValueError(f&quot;Currently `half_pixel_centers`=True only support in Ascend device_target, &quot;
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作了说明：

**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接口](https://www.mindspore.cn/docs/zh-CN/r1.7/api_python/nn/mindspore.nn.ResizeBilinear.html)

