MindSpore报错TypeError: Do not support to get attribute from object Namespace*** 以及解决方案

1.系统环境

硬件环境(Ascend/GPU/CPU): Ascend

执行模式:静态图

Python版本:3.7

操作系统平台:Linux

2. 报错信息

2.1 问题描述

动态图可以正常运行,静态图下报错

TypeError: Do not support to get attribute from object Namespace***

2.2 报错信息

File "/root/*/mindspore/common/api.py", line 1131, in compile

     result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())

TypeError: Do not support to get attribute from <class 'argparse.Namespace'> object Namespace(batch_size=96, beta_loss_weight=0.1, checkpoint=None, checkpoint_dir='/root/ckpt', checkpoints_steps=5000, from_json=None)

--------------------------------------------------

- The Traceback of Net construct Code:

--------------------------------------------------

The function call stack (See file '/root/*/tran_parallel0/rank_0/om/analyze_fail.det' for more details. Get instructions about 'analyze_fail.dat' at https://www.mindspore.cn/search?inputValue=analyze_fail.dat):

# 0 In file'/root/*cell_wrapper.py: 380

           loss = self.network(*inputs)

#1 In file /root/*/hmr.py:476

          2 * self.focal_length/(self.options.img_res * pred_camera[:, 0] + qe-9)], -1)

------------------------------------------------------------

- C++ Call Stack: (For framework developers)

-----------------------------------------------------------

mindspore/ccrc/pipeline/jit/static_analysis/prime.cc:1702 StaticGetter

3. 根因分析

当前报错是TypeError类型,在图模式下不支持从<class, 'argparse.Namespace'>类型中获取参数属性,从下面调用栈可以知道,报错的关键点在于self.options.img_res。self.options是参数类的对象,其在construct中使用,

4. 解决方案

建议修改方式:从参数类中获取的参数在init函数中进行初始化然后再在construct进行使用初始化的参数,而不是直接在construct里面直接获取

class Nets(nn.Cell):
    def __init__(self, options):
        super(Nets, self).__init__()
        self.options = options
        self.images_res = self.options.img_res
    def construct(self,*args):
        ....
        2 * self.focal_length / (self.images_res * pred_camera[:, 0] + qe - 9)], -1)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值