efficientnet error
When I use efficientnet to test, it has an error that "NoneType" object has no attribute "register_forward_hook". Could you help me?
I have a same problem
same problem here.
16 vis = GradCam(module, device)
17
---> 18 model_outs[name] = list(map(lambda x: tensor2img(vis(x, None,postprocessing=image_net_postprocessing)[0]), inputs))
19 del module
20 torch.cuda.empty_cache()
~\Documents\A-journey-into-Convolutional-Neural-Network-visualization-\visualisation\core\GradCam.py in call(self, input_image, layer, guide, target_class, postprocessing, regression) 51 layer = module 52 ---> 53 self.store_outputs_and_grad(layer) 54 55 if guide: self.guide(self.module)
~\Documents\A-journey-into-Convolutional-Neural-Network-visualization-\visualisation\core\GradCam.py in store_outputs_and_grad(self, layer) 26 self.conv_outputs = outputs 27 ---> 28 self.handles.append(layer.register_forward_hook(store_outputs)) 29 self.handles.append(layer.register_backward_hook(store_grads)) 30
AttributeError: 'NoneType' object has no attribute 'register_forward_hook'
just edit module2traced in utils.py:
def traverse(module):
if isinstance(module, torch.nn.Conv2d):
handles.append(module.register_forward_hook(trace))
return
for m in module.children():
traverse(m)
is_leaf = len(list(module.children())) == 0
if is_leaf: handles.append(module.register_forward_hook(trace))
这个函数试图把叶子节点记录下来,然后外部再去叶子节点里寻找Conv2D卷积层。然而Conv2dStaticSamePadding是一个继承了Conv2D的复合层,代码把它当作了一个有着叶子节点(ZeroPad2d)的层,所以跟踪到的叶子节点列表里没有Conv2D。其实考虑到继承关系,Conv2dStaticSamePadding==Conv2D it seems that it treat Conv2dStaticSamePadding not as same as Conv2d, but a layer with a leaf(ZeroPad2d)
same problem here. 16 vis = GradCam(module, device) 17 ---> 18 model_outs[name] = list(map(lambda x: tensor2img(vis(x, None,postprocessing=image_net_postprocessing)[0]), inputs)) 19 del module 20 torch.cuda.empty_cache()
~\Documents\A-journey-into-Convolutional-Neural-Network-visualization-\visualisation\core\GradCam.py in call(self, input_image, layer, guide, target_class, postprocessing, regression) 51 layer = module 52 ---> 53 self.store_outputs_and_grad(layer) 54 55 if guide: self.guide(self.module)
~\Documents\A-journey-into-Convolutional-Neural-Network-visualization-\visualisation\core\GradCam.py in store_outputs_and_grad(self, layer) 26 self.conv_outputs = outputs 27 ---> 28 self.handles.append(layer.register_forward_hook(store_outputs)) 29 self.handles.append(layer.register_backward_hook(store_grads)) 30
AttributeError: 'NoneType' object has no attribute 'register_forward_hook'
I got the same problem, did you address it?
just edit module2traced in utils.py:
def traverse(module): if isinstance(module, torch.nn.Conv2d): handles.append(module.register_forward_hook(trace)) return for m in module.children(): traverse(m) is_leaf = len(list(module.children())) == 0 if is_leaf: handles.append(module.register_forward_hook(trace))这个函数试图把叶子节点记录下来,然后外部再去叶子节点里寻找Conv2D卷积层。然而Conv2dStaticSamePadding是一个继承了Conv2D的复合层,代码把它当作了一个有着叶子节点(ZeroPad2d)的层,所以跟踪到的叶子节点列表里没有Conv2D。其实考虑到继承关系,Conv2dStaticSamePadding==Conv2D it seems that it treat Conv2dStaticSamePadding not as same as Conv2d, but a layer with a leaf(ZeroPad2d)
你好,你修改后可以成功运行吗?我依然遇到这个问题。
just edit module2traced in utils.py:
def traverse(module): if isinstance(module, torch.nn.Conv2d): handles.append(module.register_forward_hook(trace)) return for m in module.children(): traverse(m) is_leaf = len(list(module.children())) == 0 if is_leaf: handles.append(module.register_forward_hook(trace))这个函数试图把叶子节点记录下来,然后外部再去叶子节点里寻找Conv2D卷积层。然而Conv2dStaticSamePadding是一个继承了Conv2D的复合层,代码把它当作了一个有着叶子节点(ZeroPad2d)的层,所以跟踪到的叶子节点列表里没有Conv2D。其实考虑到继承关系,Conv2dStaticSamePadding==Conv2D it seems that it treat Conv2dStaticSamePadding not as same as Conv2d, but a layer with a leaf(ZeroPad2d)
你好,你修改后可以成功运行吗?我依然遇到这个问题。
就是成功运行了才发出来的。。
好的,谢谢你的回复,可以麻烦你发我一份修改后的完整文件吗?非常感谢!
------------------ 原始邮件 ------------------ 发件人: "HelloAlone"<[email protected]>; 发送时间: 2020年6月23日(星期二) 晚上9:20 收件人: "sidml/EfficientNet-GradCam-Visualization"<[email protected]>; 抄送: "❀"<[email protected]>; "Comment"<[email protected]>; 主题: Re: [sidml/EfficientNet-GradCam-Visualization] efficientnet error (#2)
just edit module2traced in utils.py:
def traverse(module): if isinstance(module, torch.nn.Conv2d): handles.append(module.register_forward_hook(trace)) return for m in module.children(): traverse(m) is_leaf = len(list(module.children())) == 0 if is_leaf: handles.append(module.register_forward_hook(trace))
这个函数试图把叶子节点记录下来,然后外部再去叶子节点里寻找Conv2D卷积层。然而Conv2dStaticSamePadding是一个继承了Conv2D的复合层,代码把它当作了一个有着叶子节点(ZeroPad2d)的层,所以跟踪到的叶子节点列表里没有Conv2D。其实考虑到继承关系,Conv2dStaticSamePadding==Conv2D
it seems that it treat Conv2dStaticSamePadding not as same as Conv2d, but a layer with a leaf(ZeroPad2d)
你好,你修改后可以成功运行吗?我依然遇到这个问题。
就是成功运行了才发出来的。。
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
just edit module2traced in utils.py:
def traverse(module): if isinstance(module, torch.nn.Conv2d): handles.append(module.register_forward_hook(trace)) return for m in module.children(): traverse(m) is_leaf = len(list(module.children())) == 0 if is_leaf: handles.append(module.register_forward_hook(trace))这个函数试图把叶子节点记录下来,然后外部再去叶子节点里寻找Conv2D卷积层。然而Conv2dStaticSamePadding是一个继承了Conv2D的复合层,代码把它当作了一个有着叶子节点(ZeroPad2d)的层,所以跟踪到的叶子节点列表里没有Conv2D。其实考虑到继承关系,Conv2dStaticSamePadding==Conv2D it seems that it treat Conv2dStaticSamePadding not as same as Conv2d, but a layer with a leaf(ZeroPad2d)
你好,你修改后可以成功运行吗?我依然遇到这个问题。
就是成功运行了才发出来的。。
可以发我一份修改后的文件吗?非常感谢,我的邮箱[email protected]
FYI that fix mentioned above should be made in /visualisation/core/utils.py, not /utils.py. The same module2traced function seems to exist in two utils.py files in the same project.
I solved this issue by using model._blocks[-1]._project_conv as layer parameter, instead of None. But this solution is better. Although both gives me the same results.
BTW @HelloAlone @shravan2x @ all ... how are you results with EfficentNet? My results with EfficientNet B0 are so bad: https://github.com/sidml/EfficientNet-GradCam-Visualization/issues/5
Hello Sir,
I think that this problem was not solve yet because this problem is occurred in my test. When predict on efficient_b0, I met error message. "'NoneType' object has no attribute 'register_forward_hook'"
How to solve it??
Thanks.