EfficientNet-GradCam-Visualization icon indicating copy to clipboard operation
EfficientNet-GradCam-Visualization copied to clipboard

efficientnet error

Open Kathrine94 opened this issue 6 years ago • 11 comments

When I use efficientnet to test, it has an error that "NoneType" object has no attribute "register_forward_hook". Could you help me?

Kathrine94 avatar Sep 05 '19 07:09 Kathrine94

I have a same problem

lsw8603 avatar Mar 05 '20 06:03 lsw8603

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'

zhangabner avatar Mar 20 '20 05:03 zhangabner

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 avatar Apr 03 '20 07:04 HelloAlone

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?

ChenXiao61 avatar Jun 23 '20 09:06 ChenXiao61

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)

你好,你修改后可以成功运行吗?我依然遇到这个问题。

ChenXiao61 avatar Jun 23 '20 11:06 ChenXiao61

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 avatar Jun 23 '20 13:06 HelloAlone

好的,谢谢你的回复,可以麻烦你发我一份修改后的完整文件吗?非常感谢!

------------------ 原始邮件 ------------------ 发件人: "HelloAlone"<[email protected]>; 发送时间: 2020年6月23日(星期二) 晚上9:20 收件人: "sidml/EfficientNet-GradCam-Visualization"<[email protected]>; 抄送: "&#x2740;"<[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.

ChenXiao61 avatar Jun 23 '20 13:06 ChenXiao61

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]

ChenXiao61 avatar Jun 23 '20 13:06 ChenXiao61

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.

shravan2x avatar Jan 14 '21 05:01 shravan2x

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

virilo avatar Jan 26 '21 13:01 virilo

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.

edwardcho avatar May 02 '22 02:05 edwardcho