为什么在ML模型初始化期间出现TypeError错误?

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

Why am I getting a TypeError during ML Model initialisation?

问题

错误信息中显示了一个问题:mlp2 参数被设置为 None,但你不知道如何解决它。根据你提供的代码,这个参数确实被设置为 None,但这似乎是在代码中有意为之的。

在你的 PointNetSetAbstraction 类中,有这个条件:

  1. for out_channel in mlp2:
  2. self.mlp2_convs.append(nn.Sequential(nn.Conv1d(last_channel, out_channel, 1, bias=False),
  3. nn.BatchNorm1d(out_channel)))
  4. last_channel = out_channel

这段代码只有在 mlp2 不为 None 时才会执行,否则它不会添加任何层。因此,将 mlp2 参数设置为 None 是合理的,只要在使用这个类时确保你不传递任何 mlp2 参数即可。

如果你想为 mlp2 参数传递一个非空的列表,你需要在创建 PointNetSetAbstraction 的实例时将其传递给该类的构造函数。如果你需要进一步的帮助或有其他问题,请提出。

英文:

I am running a deep learning project at https://github.com/hyangwinter/flownet3d_pytorch. There are some problems during operation, and I don't know how to solve them.

The error is as follows:

  1. Namespace(batch_size=64, cycle=False, dataset='SceneflowDataset', dataset_path='data/data_processed_maxcut_35_20k_2k_8192/', dropout=0.5, emb_dims=512, epochs=250, eval=False, exp_name='flownet3d', gaussian_noise=False, lr=0.001, model='flownet', model_path='', momentum=0.9, no_cuda=False, num_points=2048, seed=1234, test_batch_size=32, unseen=False, use_sgd=False)
  2. train : 20006
  3. test : 2007
  4. Traceback (most recent call last):
  5. File "main.py", line 282, in <module>
  6. main()
  7. File "main.py", line 254, in main
  8. net = FlowNet3D(args).cuda()
  9. File "/home/ubuntu/project/flownet3d_pytorch/model.py", line 13, in __init__
  10. self.sa1 = PointNetSetAbstraction(npoint=1024, radius=0.5, nsample=16, in_channel=3, mlp=[32,32,64], group_all=False)
  11. File "/home/ubuntu/project/flownet3d_pytorch/util.py", line 225, in __init__
  12. for out_channel in mlp2:
  13. TypeError: 'NoneType' object is not iterable`

My code:

  1. class FlowNet3D(nn.Module):
  2. def __init__(self,args):
  3. super(FlowNet3D,self).__init__()
  4. self.sa1 = PointNetSetAbstraction(npoint=1024, radius=0.5, nsample=16, in_channel=3, mlp=[32,32,64], group_all=False)
  5. self.sa2 = PointNetSetAbstraction(npoint=256, radius=1.0, nsample=16, in_channel=64, mlp=[64, 64, 128], group_all=False)
  6. self.sa3 = PointNetSetAbstraction(npoint=64, radius=2.0, nsample=8, in_channel=128, mlp=[128, 128, 256], group_all=False)
  7. self.sa4 = PointNetSetAbstraction(npoint=16, radius=4.0, nsample=8, in_channel=256, mlp=[256,256,512], group_all=False)
  8. self.fe_layer = FlowEmbedding(radius=10.0, nsample=64, in_channel = 128, mlp=[128, 128, 128], pooling='max', corr_func='concat')
  9. self.su1 = PointNetSetUpConv(nsample=8, radius=2.4, f1_channel = 256, f2_channel = 512, mlp=[], mlp2=[256, 256])
  10. self.su2 = PointNetSetUpConv(nsample=8, radius=1.2, f1_channel = 128+128, f2_channel = 256, mlp=[128, 128, 256], mlp2=[256])
  11. self.su3 = PointNetSetUpConv(nsample=8, radius=0.6, f1_channel = 64, f2_channel = 256, mlp=[128, 128, 256], mlp2=[256])
  12. self.fp = PointNetFeaturePropogation(in_channel = 256+3, mlp = [256, 256])
  13. self.conv1 = nn.Conv1d(256, 128, kernel_size=1, bias=False)
  14. self.bn1 = nn.BatchNorm1d(128)
  15. self.conv2=nn.Conv1d(128, 3, kernel_size=1, bias=True)
  16. def forward(self, pc1, pc2, feature1, feature2):
  17. l1_pc1, l1_feature1 = self.sa1(pc1, feature1)
  18. l2_pc1, l2_feature1 = self.sa2(l1_pc1, l1_feature1)
  19. l1_pc2, l1_feature2 = self.sa1(pc2, feature2)
  20. l2_pc2, l2_feature2 = self.sa2(l1_pc2, l1_feature2)
  21. _, l2_feature1_new = self.fe_layer(l2_pc1, l2_pc2, l2_feature1, l2_feature2)
  22. l3_pc1, l3_feature1 = self.sa3(l2_pc1, l2_feature1_new)
  23. l4_pc1, l4_feature1 = self.sa4(l3_pc1, l3_feature1)
  24. l3_fnew1 = self.su1(l3_pc1, l4_pc1, l3_feature1, l4_feature1)
  25. l2_fnew1 = self.su2(l2_pc1, l3_pc1, torch.cat([l2_feature1, l2_feature1_new], dim=1), l3_fnew1)
  26. l1_fnew1 = self.su3(l1_pc1, l2_pc1, l1_feature1, l2_fnew1)
  27. l0_fnew1 = self.fp(pc1, l1_pc1, feature1, l1_fnew1)
  28. x = F.relu(self.bn1(self.conv1(l0_fnew1)))
  29. sf = self.conv2(x)
  30. return sf
  31. class PointNetSetAbstraction(nn.Module):
  32. def __init__(self, npoint, radius, nsample, in_channel, mlp, mlp2 = None, group_all = False):
  33. super(PointNetSetAbstraction, self).__init__()
  34. self.npoint = npoint
  35. self.radius = radius
  36. self.nsample = nsample
  37. self.group_all = group_all
  38. self.mlp_convs = nn.ModuleList()
  39. self.mlp_bns = nn.ModuleList()
  40. self.mlp2_convs = nn.ModuleList()
  41. last_channel = in_channel+3
  42. for out_channel in mlp:
  43. self.mlp_convs.append(nn.Conv2d(last_channel, out_channel, 1, bias = False))
  44. self.mlp_bns.append(nn.BatchNorm2d(out_channel))
  45. last_channel = out_channel
  46. for out_channel in mlp2:
  47. self.mlp2_convs.append(nn.Sequential(nn.Conv1d(last_channel, out_channel, 1, bias=False),
  48. nn.BatchNorm1d(out_channel)))
  49. last_channel = out_channel
  50. if group_all:
  51. self.queryandgroup = pointutils.GroupAll()
  52. else:
  53. self.queryandgroup = pointutils.QueryAndGroup(radius, nsample)
  54. def forward(self, xyz, points):
  55. """
  56. Input:
  57. xyz: input points position data, [B, C, N]
  58. points: input points data, [B, D, N]
  59. Return:
  60. new_xyz: sampled points position data, [B, S, C]
  61. new_points_concat: sample points feature data, [B, S, D']
  62. """
  63. device = xyz.device
  64. B, C, N = xyz.shape
  65. xyz_t = xyz.permute(0, 2, 1).contiguous()
  66. # if points is not None:
  67. # points = points.permute(0, 2, 1).contiguous()
  68. if self.group_all == False:
  69. fps_idx = pointutils.furthest_point_sample(xyz_t, self.npoint) # [B, N]
  70. new_xyz = pointutils.gather_operation(xyz, fps_idx) # [B, C, N]
  71. else:
  72. new_xyz = xyz
  73. new_points = self.queryandgroup(xyz_t, new_xyz.transpose(2, 1).contiguous(), points) # [B, 3+C, N, S]
  74. # new_xyz: sampled points position data, [B, C, npoint]
  75. # new_points: sampled points data, [B, C+D, npoint, nsample]
  76. for i, conv in enumerate(self.mlp_convs):
  77. bn = self.mlp_bns[i]
  78. new_points = F.relu(bn(conv(new_points)))
  79. new_points = torch.max(new_points, -1)[0]
  80. for i, conv in enumerate(self.mlp2_convs):
  81. new_points = F.relu(conv(new_points))
  82. return new_xyz, new_points`

I noticed that the mlp2 parameter is set to NONE but I don't know how to solve it. In the source code implementation, this parameter is also NONE

答案1

得分: 1

由于您的 self.sa1 没有名为 mlp2 的参数,它将使用默认值,即您在以下代码中定义的 None

  1. class PointNetSetAbstraction(nn.Module):
  2. def __init__(self, npoint, radius, nsample, in_channel, mlp, mlp2=None, group_all=False):

当然,None 不能迭代,为了解决这个问题,您可以在 for 循环之前添加一个 if 条件。另外,您也可以像您已经为 self.su1 做的那样传递 mlp2=[]

英文:

Since your self.sa1 do not have parameters named mlp2, it will use the default one, None as you defined in

  1. class PointNetSetAbstraction(nn.Module):
  2. def init(self, npoint, radius, nsample, in_channel, mlp, mlp2 = None, group_all = False):

And of cause None is not iterable, to solve this, you can just add a if-condition before the for-loop. Otherwise, you can also just pass mlp2=[] as you already done for self.su1.

答案2

得分: 0

如您在问题中指出的那样,在函数调用中mlp2被设置为None。尝试迭代mlp2时:

  1. for out_channel in mlp2:

代码正确地引发了TypeError

很难理解您试图做什么,特别是因为mlp2不是一个好的变量名(它应该是什么?)。根据您的代码上下文,我会假设mlp2是一个包含您层大小的列表。您可以尝试类似以下的内容:

  1. mlp2 = [16, 16, 16, 16]

这将初始化模型以具有大小为16的层(如果代码的其余部分正常工作的话)。

英文:

As you pointed out in your question, mlp2 is set to None in the function call. When trying to iterate over mlp2 with

  1. for out_channel in mlp2:

the code corectly throws a TypeError.

It is hard to understand what you are trying to do, especially because mlp2 is not a good variable name (what even should that be?). From the context of your code I would assume that mpl2 is a list that holds the sizes of your layers. You might try something like:

  1. mlp2 = [16,16,16,16]

This will initialize the Model to have layers of size 16 (if the rest of the code is working).

huangapple
  • 本文由 发表于 2023年6月8日 14:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429186.html
匿名

发表评论

匿名网友

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

确定