【问题标题】:How to attach hooks to ReLUs in Inception V3 from torchvision如何在 Torchvision 的 Inception V3 中将钩子附加到 ReLU
【发布时间】:2020-09-23 06:49:21
【问题描述】:

我正在使用Inception v3 from torchvision。我试图在模型中找到 ReLU:

def recursively_find_submodules(model, submodule_type):
    module_list = []
    q = [model]    
    while q:
        child = q.pop()
        if isinstance(child, submodule_type):
            module_list.append(child)
        q.extend(list(child.children()))                 
    return module_list

inception = torch.hub.load('pytorch/vision:v0.6.0', 'inception_v3', pretrained=True)
l = recursively_find_submodules(inception, torch.nn.ReLU) # l is empty!

因此,ReLU 不是 torch 模型中任何模块的子级。经过仔细检查,我在torchvision 的源代码中发现了ReLU,但不是模块。在inception.py 我发现了以下内容:

class BasicConv2d(nn.Module):

    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
        self.bn = nn.BatchNorm2d(out_channels, eps=0.001)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return F.relu(x, inplace=True)

所以BasicConv2d 模块使用 ReLU 函数而不是模块 (torch.nn.ReLU) 来钳制它的输出。我想没有办法在不修改整个模型以使用 ReLU 模块的情况下连接到 ReLU 函数并修改它们的输入/输出,或者有没有办法做到这一点?

【问题讨论】:

    标签: pytorch torchvision


    【解决方案1】:

    考虑到您观察的是 ReLU 的输入,而不是激活后的特征,您可以挂钩到 ReLU 之前的批处理规范层并附加到那里。

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 2022-08-16
      • 2019-12-16
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2018-01-06
      相关资源
      最近更新 更多