The bug's torchsummary summarizes the problem with the model: TypeError: can't multiply sequence by non-int of type'list'
Project scene:
Source code: The source code is a fully convolutional neural network. The width and height of the output features of each layer are the same, and only the number of channels changes, that is, each layer is composed of convolutions of different numbers and cores, and the number of feature maps finally output corresponds to How many key points. The whole network judges the probability that each pixel position in the image is a certain key point, which is the representation of converting the image into numerical coordinates.
But this is not what we are talking about here, it is that torchsummary has a bug when printing the model structure.
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()
Problem Description:
Tip: Describe the problems encountered in the project here: TypeError: can't multiply sequence by non-int of type'list' appears before torchsummary.
Fixed size network: It
runs normally under anaconda3+python3.7+pytorch1.5.1+torchsummary environment.
Run in the new environment and report an error, the new environment 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'
Cause Analysis:
1. The environment does not match.
2. The model outputs two quantities, and the information passed between the two may not be a subclass of nn.Module.
solution:
Method 1: Change the environment back to python3.7
Method 2: Use a new method to read the model, and replace the body of the main function with the following code snippets:
# 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])
Method 3: For analysis reason 2, remove an output. The model can normally summarize the
final output of the summary modified model:
Output:
----------------------------------------------------------------
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