TypeError: ‘NoneType‘ object is not callable

深度学习代码报错

TypeError: ‘NoneType‘ object is not callable_第1张图片
这是我在跑代码时候遇到的报错,百度之后发现应该是哪里出现了None导致了该报错。在查看代码后发现了在模块定义的时候未对None进行分类处理。
该类问题需要处理代码中出现的None 并对其进行逻辑处理。

我出现该问题的解决

self.main = nn.Sequential(
            DoConv(out_channel, out_channel, kernel, act),
            DoConv(out_channel, out_channel, kernel, None)
        )

TypeError: ‘NoneType‘ object is not callable_第2张图片
DoConv模块的部分代码如下:

    def __init__(self, in_channels, out_channels, kernel_size, act):
        super(DoConv, self).__init__()
        m_body = []
        m_body.append(nn.Conv2d(in_channels, in_channels, kernel_size=kernel_size, stride=1, padding=1, groups=in_channels,
                               bias=False))
        m_body.append(nn.BatchNorm2d(in_channels))
        m_body.append(act)
        m_body.append(nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False))
        m_body.append(nn.BatchNorm2d(out_channels))
        m_body.append(act)
        # self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1, groups=in_channels,
        #                        bias=False)
        # self.bn1 = nn.BatchNorm2d(in_channels)
        # self.conv2 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
        # self.bn2 = nn.BatchNorm2d(out_channels)

        self.m_body = nn.Sequential(*m_body)

这是我CV大法获得的代码。发现在定义DoConv模块时候有None作为参数,但是在模块中未对其进行处理。
所以更改代码如下,对None进行了判断。

    def __init__(self, in_channels, out_channels, kernel_size, act):
        super(DoConv, self).__init__()
        m_body = []
        m_body.append(nn.Conv2d(in_channels, in_channels, kernel_size=kernel_size, stride=1, padding=1, groups=in_channels,
                               bias=False))
        m_body.append(nn.BatchNorm2d(in_channels))
        if act:
            m_body.append(act)
        m_body.append(nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False))
        m_body.append(nn.BatchNorm2d(out_channels))
        if act:
            m_body.append(act)
        # self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1, groups=in_channels,
        #                        bias=False)
        # self.bn1 = nn.BatchNorm2d(in_channels)
        # self.conv2 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
        # self.bn2 = nn.BatchNorm2d(out_channels)

        self.m_body = nn.Sequential(*m_body)

该报错消失。(新的bug出现=-=)

你可能感兴趣的:(python,人工智能,深度学习)