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 17:07:05 浏览: 259
基于pytorch实现的ghostnetv1、v2、v3对瓶子垃圾图像分类源码、数据集
你的报错信息显示 `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` 的问题。
阅读全文