pytorch中ConvNeXt v2模型加入CBAM模块后报错:Traceback (most recent call last): File "/home/adminis/hpy/ConvNextV2_Demo/train+.py", line 234, in <module> model_ft = convnextv2_base(pretrained=True) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 201, in convnextv2_base model = ConvNeXtV2(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024], **kwargs) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 114, in init self.apply(self.init_weights) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) [Previous line repeated 4 more times] File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 617, in apply fn(self) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 121, in init_weights nn.init.constant(m.bias, 0) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/init.py", line 186, in constant return no_grad_fill(tensor, val) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/init.py", line 59, in no_grad_fill return tensor.fill_(val) AttributeError: 'NoneType' object has no attribute 'fill_' 部分代码如下:for i in range(4): stage = nn.Sequential( *[Block(dim=dims[i], drop_path=dp_rates[cur + j]) for j in range(depths[i])], CBAM(gate_channels=dims[i]) ) self.stages.append(stage) cur += depths def _init_weights(self, m): if isinstance(m, (nn.Conv2d, nn.Linear)): trunc_normal_(m.weight, std=.02) nn.init.constant_(m.bias, 0)
时间: 2023-08-15 21:07:05 浏览: 272
你的报错信息显示 `AttributeError: 'NoneType' object has no attribute 'fill_'`,这个错误通常出现在你试图对 `None` 进行操作的时候,而在你的代码中这个错误出现在 `nn.init.constant_(m.bias, 0)` 这一行,因此很可能是你的 `bias` 为 `None` 导致的。你可以添加一个判断语句来避免这个问题:
```
def _init_weights(self, m):
if isinstance(m, (nn.Conv2d, nn.Linear)):
trunc_normal_(m.weight, std=.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
```
这样当 `bias` 为 `None` 时就不会执行初始化,避免了出现 `AttributeError` 的问题。
相关问题
pytorch中ConvNeXt v2模型加入CBAM模块后报错:Traceback (most recent call last): File "/home/adminis/hpy/ConvNextV2_Demo/train+.py", line 234, in <module> model_ft = convnextv2_base(pretrained=True) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 201, in convnextv2_base model = ConvNeXtV2(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024], **kwargs) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 114, in init self.apply(self.init_weights) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 616, in apply module.apply(fn) [Previous line repeated 4 more times] File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/modules/module.py", line 617, in apply fn(self) File "/home/adminis/hpy/ConvNextV2_Demo/models/convnext_v2.py", line 121, in init_weights nn.init.constant(m.bias, 0) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/init.py", line 186, in constant return no_grad_fill(tensor, val) File "/home/adminis/anaconda3/envs/wln/lib/python3.9/site-packages/torch/nn/init.py", line 59, in no_grad_fill return tensor.fill_(val) AttributeError: 'NoneType' object has no attribute 'fill_' 部分代码如下:for i in range(4): stage = nn.Sequential( *[Block(dim=dims[i], drop_path=dp_rates[cur + j]) for j in range(depths[i])], CBAM(gate_channels=dims[i]) ) self.stages.append(stage) cur += depths[i]
这个错误的原因是 `nn.init.constant(m.bias, 0)` 中的 `m.bias` 是 `None`。这可能是因为您的 `CBAM` 模块没有可训练的参数(例如,`bias=False`),因此在应用权重初始化时,`m.bias` 为 `None`。
要解决此问题,您可以在 `init_weights` 方法中检查 `m.bias` 是否为 `None`,然后再进行初始化,如下所示:
```python
def init_weights(self, m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Sequential):
for layer in m:
init_weights(layer)
else:
if m.bias is not None:
nn.init.constant_(m.bias, 0)
```
您可以将上面的代码添加到您的 `ConvNeXtV2` 类中,并使用修改后的 `init_weights` 方法进行初始化。这样,即使 `CBAM` 模块没有可训练的偏置参数,它也不会导致上述错误。
阅读全文