RuntimeError: Given groups=1, weight of size [128, 55, 11, 11], expected input[64, 57, 28, 28] to have 55 channels, but got 57 channels instead

huangapple go评论104阅读模式
英文:

RuntimeError: Given groups=1, weight of size [128, 55, 11, 11], expected input[64, 57, 28, 28] to have 55 channels, but got 57 channels instead

问题

以下是您提供的代码的翻译部分:

PyTorch Lightning 模型

  1. class CPM(pl.LightningModule):
  2. def __init__(self, k, verbose=False):
  3. # 模型的初始化和结构定义
  4. # ...
  5. # 各个阶段的前向传播函数定义
  6. # ...
  7. def forward(self, image, center_map):
  8. # 模型的前向传播
  9. # ...
  10. def training_step(self, batch, batch_idx):
  11. # 训练过程中的步骤
  12. # ...
  13. def validation_step(self, batch, batch_idx):
  14. # 验证过程中的步骤
  15. # ...
  16. def test_step(self, batch, batch_idx):
  17. # 测试过程中的步骤
  18. # ...
  19. def configure_optimizers(self):
  20. # 优化器配置
  21. # ...
  22. def train_dataloader(self):
  23. # 训练数据加载器
  24. # ...

数据集

对于数据集,我使用了 Kaggle 上的 freihand 数据集。然后,我使用了 Torch DataLoader 来加载数据集,该数据集返回原始图像、关键点和中心图,其中中心图是具有本地化关键点的图像。

错误

  1. /tmp/ipykernel_27/995635534.py in _stage2(self, pool3_stage2_map, conv7_stage1_map, pool_center_map)
  2. 89 x = F.relu(self.conv4_stage2(pool3_stage2_map))
  3. 90 x = torch.cat([x, conv7_stage1_map, pool_center_map], dim=1)
  4. ---> 91 x = F.relu(self.Mconv1_stage2(x))
  5. 92 x = F.relu(self.Mconv2_stage2(x))
  6. 93 x = F.relu(self.Mconv3_stage2(x))
  7. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
  8. 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
  9. 1109 or _global_forward_hooks or _global_forward_pre_hooks):
  10. -> 1110 return forward_call(*input, **kwargs)
  11. 1111 # Do not call functions when jit is used
  12. 1112 full_backward_hooks, non_full_backward_hooks = [], []
  13. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
  14. 445
  15. 446 def forward(self, input: Tensor) -> Tensor:
  16. --> 447 return self._conv_forward(input, self.weight, self.bias)
  17. 448
  18. 449 class Conv3d(_ConvNd):
  19. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
  20. 442 _pair(0), self.dilation, self.groups)
  21. 443 return F.conv2d(input, weight, bias, self.stride,
  22. --> 444 self.padding, self.dilation, self.groups)
  23. 445
  24. 446 def forward(self, input: Tensor) -> Tensor:
  25. RuntimeError: 给定 groups=1weight 的大小为 [128, 55, 11, 11]预期输入[64, 57, 28, 28]应具有 55 个通道但实际有 57 个通道

希望这有助于您理解代码和问题。如果您需要任何进一步的帮助,请随时提问。

英文:

I'm trying to train a Convolutional-Pose-Model (CPM) using Pytorch and in the below you can see the torch-lightning model along with the error I encountered. How do I solve this issue ?

pytorch lightning model

  1. class CPM(pl.LightningModule):
  2. def __init__(self, k, verbose = False):
  3. super(CPM, self).__init__()
  4. self.k = k
  5. self.verbose = verbose
  6. self.criterion = nn.MSELoss().cuda()
  7. self.pool_center = nn.AvgPool2d(kernel_size=9, stride=8, padding=1)
  8. self.conv1_stage1 = nn.Conv2d(3, 128, kernel_size=9, padding=4)
  9. self.pool1_stage1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  10. self.conv2_stage1 = nn.Conv2d(128, 128, kernel_size=9, padding=4)
  11. self.pool2_stage1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  12. self.conv3_stage1 = nn.Conv2d(128, 128, kernel_size=9, padding=4)
  13. self.pool3_stage1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  14. self.conv4_stage1 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  15. self.conv5_stage1 = nn.Conv2d(32, 512, kernel_size=9, padding=4)
  16. self.conv6_stage1 = nn.Conv2d(512, 512, kernel_size=1)
  17. self.conv7_stage1 = nn.Conv2d(512, self.k + 1, kernel_size=1)
  18. self.conv1_stage2 = nn.Conv2d(3, 128, kernel_size=9, padding=4)
  19. self.pool1_stage2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  20. self.conv2_stage2 = nn.Conv2d(128, 128, kernel_size=9, padding=4)
  21. self.pool2_stage2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  22. self.conv3_stage2 = nn.Conv2d(128, 128, kernel_size=9, padding=4)
  23. self.pool3_stage2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  24. self.conv4_stage2 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  25. self.Mconv1_stage2 = nn.Conv2d(32 + self.k + 2, 128, kernel_size=11, padding=5)
  26. self.Mconv2_stage2 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  27. self.Mconv3_stage2 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  28. self.Mconv4_stage2 = nn.Conv2d(128, 128, kernel_size=1, padding=0)
  29. self.Mconv5_stage2 = nn.Conv2d(128, self.k + 1, kernel_size=1, padding=0)
  30. self.conv1_stage3 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  31. self.Mconv1_stage3 = nn.Conv2d(32 + self.k + 2, 128, kernel_size=11, padding=5)
  32. self.Mconv2_stage3 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  33. self.Mconv3_stage3 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  34. self.Mconv4_stage3 = nn.Conv2d(128, 128, kernel_size=1, padding=0)
  35. self.Mconv5_stage3 = nn.Conv2d(128, self.k + 1, kernel_size=1, padding=0)
  36. self.conv1_stage4 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  37. self.Mconv1_stage4 = nn.Conv2d(32 + self.k + 2, 128, kernel_size=11, padding=5)
  38. self.Mconv2_stage4 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  39. self.Mconv3_stage4 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  40. self.Mconv4_stage4 = nn.Conv2d(128, 128, kernel_size=1, padding=0)
  41. self.Mconv5_stage4 = nn.Conv2d(128, self.k + 1, kernel_size=1, padding=0)
  42. self.conv1_stage5 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  43. self.Mconv1_stage5 = nn.Conv2d(32 + self.k + 2, 128, kernel_size=11, padding=5)
  44. self.Mconv2_stage5 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  45. self.Mconv3_stage5 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  46. self.Mconv4_stage5 = nn.Conv2d(128, 128, kernel_size=1, padding=0)
  47. self.Mconv5_stage5 = nn.Conv2d(128, self.k + 1, kernel_size=1, padding=0)
  48. self.conv1_stage6 = nn.Conv2d(128, 32, kernel_size=5, padding=2)
  49. self.Mconv1_stage6 = nn.Conv2d(32 + self.k + 2, 128, kernel_size=11, padding=5)
  50. self.Mconv2_stage6 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  51. self.Mconv3_stage6 = nn.Conv2d(128, 128, kernel_size=11, padding=5)
  52. self.Mconv4_stage6 = nn.Conv2d(128, 128, kernel_size=1, padding=0)
  53. self.Mconv5_stage6 = nn.Conv2d(128, self.k + 1, kernel_size=1, padding=0)
  54. def _stage1(self, image):
  55. x = self.pool1_stage1(F.relu(self.conv1_stage1(image)))
  56. x = self.pool2_stage1(F.relu(self.conv2_stage1(x)))
  57. x = self.pool3_stage1(F.relu(self.conv3_stage1(x)))
  58. x = F.relu(self.conv4_stage1(x))
  59. x = F.relu(self.conv5_stage1(x))
  60. x = F.relu(self.conv6_stage1(x))
  61. x = self.conv7_stage1(x)
  62. return x
  63. def _middle(self, image):
  64. x = self.pool1_stage2(F.relu(self.conv1_stage2(image)))
  65. x = self.pool2_stage2(F.relu(self.conv2_stage2(x)))
  66. x = self.pool3_stage2(F.relu(self.conv3_stage2(x)))
  67. return x
  68. def _stage2(self, pool3_stage2_map, conv7_stage1_map, pool_center_map):
  69. x = F.relu(self.conv4_stage2(pool3_stage2_map))
  70. x = torch.cat([x, conv7_stage1_map, pool_center_map], dim=1)
  71. x = F.relu(self.Mconv1_stage2(x))
  72. x = F.relu(self.Mconv2_stage2(x))
  73. x = F.relu(self.Mconv3_stage2(x))
  74. x = F.relu(self.Mconv4_stage2(x))
  75. x = self.Mconv5_stage2(x)
  76. return x
  77. def _stage3(self, pool3_stage2_map, Mconv5_stage2_map, pool_center_map):
  78. x = F.relu(self.conv1_stage3(pool3_stage2_map))
  79. x = torch.cat([x, Mconv5_stage2_map, pool_center_map], dim=1)
  80. x = F.relu(self.Mconv1_stage3(x))
  81. x = F.relu(self.Mconv2_stage3(x))
  82. x = F.relu(self.Mconv3_stage3(x))
  83. x = F.relu(self.Mconv4_stage3(x))
  84. x = self.Mconv5_stage3(x)
  85. return x
  86. def _stage4(self, pool3_stage2_map, Mconv5_stage3_map, pool_center_map):
  87. x = F.relu(self.conv1_stage4(pool3_stage2_map))
  88. x = torch.cat([x, Mconv5_stage3_map, pool_center_map], dim=1)
  89. x = F.relu(self.Mconv1_stage4(x))
  90. x = F.relu(self.Mconv2_stage4(x))
  91. x = F.relu(self.Mconv3_stage4(x))
  92. x = F.relu(self.Mconv4_stage4(x))
  93. x = self.Mconv5_stage4(x)
  94. return x
  95. def _stage5(self, pool3_stage2_map, Mconv5_stage4_map, pool_center_map):
  96. x = F.relu(self.conv1_stage5(pool3_stage2_map))
  97. x = torch.cat([x, Mconv5_stage4_map, pool_center_map], dim=1)
  98. x = F.relu(self.Mconv1_stage5(x))
  99. x = F.relu(self.Mconv2_stage5(x))
  100. x = F.relu(self.Mconv3_stage5(x))
  101. x = F.relu(self.Mconv4_stage5(x))
  102. x = self.Mconv5_stage5(x)
  103. return x
  104. def _stage6(self, pool3_stage2_map, Mconv5_stage5_map, pool_center_map):
  105. x = F.relu(self.conv1_stage6(pool3_stage2_map))
  106. x = torch.cat([x, Mconv5_stage5_map, pool_center_map], dim=1)
  107. x = F.relu(self.Mconv1_stage6(x))
  108. x = F.relu(self.Mconv2_stage6(x))
  109. x = F.relu(self.Mconv3_stage6(x))
  110. x = F.relu(self.Mconv4_stage6(x))
  111. x = self.Mconv5_stage6(x)
  112. return x
  113. def forward(self, image, center_map):
  114. pool_center_map = self.pool_center(center_map)
  115. conv7_stage1_map = self._stage1(image)
  116. pool3_stage2_map = self._middle(image)
  117. print(pool3_stage2_map.shape, conv7_stage1_map.shape,pool_center_map.shape)
  118. Mconv5_stage2_map = self._stage2(pool3_stage2_map, conv7_stage1_map,
  119. pool_center_map)
  120. Mconv5_stage3_map = self._stage3(pool3_stage2_map, Mconv5_stage2_map,
  121. pool_center_map)
  122. Mconv5_stage4_map = self._stage4(pool3_stage2_map, Mconv5_stage3_map,
  123. pool_center_map)
  124. Mconv5_stage5_map = self._stage5(pool3_stage2_map, Mconv5_stage4_map,
  125. pool_center_map)
  126. Mconv5_stage6_map = self._stage6(pool3_stage2_map, Mconv5_stage5_map,
  127. pool_center_map)
  128. return conv7_stage1_map, Mconv5_stage2_map, Mconv5_stage3_map, Mconv5_stage4_map, Mconv5_stage5_map, Mconv5_stage6_map
  129. def training_step(self, batch, batch_idx):
  130. imgs, kpts, centermaps = batch
  131. heats = self.forward(imgs, centermaps)
  132. losses = torch.tensor([self.criterion(heat, kpts) for heat in heats])
  133. loss = torch.sum(losses)
  134. self.log('train_loss', loss)
  135. return loss
  136. def validation_step(self, batch, batch_idx):
  137. imgs, kpts, centermaps = batch
  138. heats = self.forward(imgs, centermaps)
  139. losses = torch.tensor([self.criterion(heat, kpts) for heat in heats])
  140. loss = torch.sum(losses)
  141. self.log('val_loss', loss)
  142. return loss
  143. def test_step(self, batch, batch_idx):
  144. imgs, kpts, centermaps = batch
  145. heat = self.forward(imgs, centermaps)[-1]
  146. return self.get_kpts(heat)
  147. def configure_optimizers(self):
  148. return torch.optim.Adam(self.parameters(), lr = 0.001)
  149. def train_dataloader(self):
  150. IMG_SIZE = 244
  151. HP = HandPose(df, transform = transform, testSize = 0, imgSize = IMG_SIZE, subset = 'train')
  152. Data = data.DataLoader(HP, shuffle = True, batch_size = 64, num_workers = 4)
  153. return Data

Dataset

As for the dataset, I'm using freihand dataset from kaggle. I then used torch dataloader to load the dataset which returns original image, keypoints and centermap which is an image with its localized keypoints.

  1. /tmp/ipykernel_27/995635534.py in _stage2(self, pool3_stage2_map, conv7_stage1_map, pool_center_map)
  2. 89 x = F.relu(self.conv4_stage2(pool3_stage2_map))
  3. 90 x = torch.cat([x, conv7_stage1_map, pool_center_map], dim=1)
  4. ---> 91 x = F.relu(self.Mconv1_stage2(x))
  5. 92 x = F.relu(self.Mconv2_stage2(x))
  6. 93 x = F.relu(self.Mconv3_stage2(x))
  7. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
  8. 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
  9. 1109 or _global_forward_hooks or _global_forward_pre_hooks):
  10. -> 1110 return forward_call(*input, **kwargs)
  11. 1111 # Do not call functions when jit is used
  12. 1112 full_backward_hooks, non_full_backward_hooks = [], []
  13. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
  14. 445
  15. 446 def forward(self, input: Tensor) -> Tensor:
  16. --> 447 return self._conv_forward(input, self.weight, self.bias)
  17. 448
  18. 449 class Conv3d(_ConvNd):
  19. /opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
  20. 442 _pair(0), self.dilation, self.groups)
  21. 443 return F.conv2d(input, weight, bias, self.stride,
  22. --> 444 self.padding, self.dilation, self.groups)
  23. 445
  24. 446 def forward(self, input: Tensor) -> Tensor:
  25. RuntimeError: Given groups=1, weight of size [128, 55, 11, 11], expected input[64, 57, 28, 28] to have 55 channels, but got 57 channels instead

答案1

得分: 3

错误堆栈跟踪允许您定位错误,看起来是来自您的 Mconv1_stage2 的滤波器数量。

您通过 [x, conv7_stage1_map, pool_center_map] 输入的连接映射与接下来的卷积层期望的滤波器数量 (32 + self.k + 2) 不匹配。如果这是正确的输入,那么您需要调整 Mconv1_stage2in_channels 数量。

英文:

The error stack trace allows you to locate the error, it seems to come from the number of channels your Mconv1_stage2's filters have.

There is a mismatch between the concatenated map you are feeding through [x, conv7_stage1_map, pool_center_map] and the number of filters the following convolutional layer expects (32 + self.k + 2). If this is the correct input, then you need to adjust the number of in_channels for Mconv1_stage2 .

huangapple
  • 本文由 发表于 2023年2月19日 22:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500704.html
匿名

发表评论

匿名网友
#

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定