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)