TypeError: can‘t multiply sequence by non-int of type ‘list‘

bug之torchsummary总结模型出现问题:TypeError: can't multiply sequence by non-int of type 'list'

项目场景:

源码:源码是全卷积神经网络,每一层输出的特征出的宽高一致,只有通道数的变化,即每一层由不同数量和核的卷积构成,最后输出的特征图个数对应多少个关键点。整个网络评判的是,图像中每一个像素位置是某一个关键点的概率,是将图像转换成数值坐标的表示。
但这里谈的并不是这个,是torchsummary在打印模型结构的时候出现了bug

import dsntnn
import torch
from torch import nn, optim

# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


class FCN(nn.Module):
    def __init__(self):
        super().__init__()

        self.layers = nn.Sequential(
            nn.Conv2d(3, 48, kernel_size=7, padding=3),
            nn.ReLU(),
            nn.Conv2d(48, 48, kernel_size=5, padding=2),
            nn.ReLU(),
            nn.Conv2d(48, 24, kernel_size=5, padding=2),
            nn.ReLU(),
            nn.Conv2d(24, 24, kernel_size=3, padding=1),
        )

    def forward(self, x):
        return self.layers(x)


class CoordRegressionNetwork(nn.Module):
    def __init__(self, n_locations):
        super().__init__()
        self.fcn = FCN()
        self.hm_conv = nn.Conv2d(24, n_locations, kernel_size=1, bias=False)

    def forward(self, images):
        # 1. Run the images through our FCN
        fcn_out = self.fcn(images)
        # 2. Use a 1x1 conv to get one unnormalized heatmap per location
        unnormalized_heatmaps = self.hm_conv(fcn_out)
        # 3. Normalize the heatmaps
        heatmaps = dsntnn.flat_softmax(unnormalized_heatmaps)
        # 4. Calculate the coordinates
        coords = dsntnn.dsnt(heatmaps)

        return coords, heatmaps


def main():
    from torchsummary import summary
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = CoordRegressionNetwork(n_locations=24)

    summary(model.cuda(), (3, 512, 512))


if __name__ == "__main__":
    # 测试打印模型
    main()


问题描述:

提示:这里描述项目中遇到的问题:TypeError: can’t multiply sequence by non-int of type 'list’出现再torchsummary。
尺寸大小固定的网络:
在anaconda3+python3.7 +pytorch1.5.1+torchsummary环境下运行正常。
在新环境运行报错,新环境python=3.8.3

File "D:/TanHaiyan/Python_code/KeyPointsDetection_with_Torch/creat_FCN.py", line 71, in main
    summary(model.cuda(), (3, 512, 512))
  File "D:\TanHaiyan\Program Files\Anaconda3\lib\site-packages\torchsummary-1.5.1-py3.8.egg\torchsummary\torchsummary.py", line 10, in summary
  File "D:\TanHaiyan\Program Files\Anaconda3\lib\site-packages\torchsummary-1.5.1-py3.8.egg\torchsummary\torchsummary.py", line 94, in summary_string
  File "<__array_function__ internals>", line 5, in prod
  File "D:\TanHaiyan\Program Files\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2961, in prod
    return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
  File "D:\TanHaiyan\Program Files\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 90, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
TypeError: can't multiply sequence by non-int of type 'list'

原因分析:

1.环境不匹配。
2.模型输出两个量,两者之间传递信息可能不是nn.Module的子类。
在这里插入图片描述


解决方案:

方法一:将环境换回python3.7
方法二:采用新的方法读取模型,采用下列代码段替代main函数的函数体:

# Initialize model
# model = TheModelClass()
model = CoordRegressionNetwork(n_locations=24)
# Initialize optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)

print("Model's state_dict:")
# Print model's state_dict
for param_tensor in model.state_dict():
    print(param_tensor, "\t", model.state_dict()[param_tensor].size())
print("optimizer's state_dict:")
# Print optimizer's state_dict
for var_name in optimizer.state_dict():
    print(var_name, "\t", optimizer.state_dict()[var_name])

方法三:针对分析原因2,去掉一个输出。模型能正常总结summary
修改模型最后输出:
在这里插入图片描述

输出:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 48, 512, 512]           7,104
              ReLU-2         [-1, 48, 512, 512]               0
            Conv2d-3         [-1, 48, 512, 512]          57,648
              ReLU-4         [-1, 48, 512, 512]               0
            Conv2d-5         [-1, 24, 512, 512]          28,824
              ReLU-6         [-1, 24, 512, 512]               0
            Conv2d-7         [-1, 24, 512, 512]           5,208
               FCN-8         [-1, 24, 512, 512]               0
            Conv2d-9         [-1, 24, 512, 512]             576
CoordRegressionNetwork-10         [-1, 24, 512, 512]               0
================================================================
Total params: 99,360
Trainable params: 99,360
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 3.00
Forward/backward pass size (MB): 672.00
Params size (MB): 0.38
Estimated Total Size (MB): 675.38
----------------------------------------------------------------


Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/beauthy/article/details/108234095
今日推荐