WZMIAOMIAO/deep-learning-for-image-processing · error · Exception
backbone is None
Error message
backbone is None
What it means
SSD300.__init__ raises Exception('backbone is None') when instantiated without a backbone argument. SSD300 extracts features from a pretrained backbone (e.g. resnet50-fpn-style) and has no default one; the constructor validates it first.
Source
Thrown at pytorch_object_detection/ssd/src/ssd_model.py:36
self.feature_extractor = nn.Sequential(*list(net.children())[:7])
conv4_block1 = self.feature_extractor[-1][0]
# 修改conv4_block1的步距,从2->1
conv4_block1.conv1.stride = (1, 1)
conv4_block1.conv2.stride = (1, 1)
conv4_block1.downsample[0].stride = (1, 1)
def forward(self, x):
x = self.feature_extractor(x)
return x
class SSD300(nn.Module):
def __init__(self, backbone=None, num_classes=21):
super(SSD300, self).__init__()
if backbone is None:
raise Exception("backbone is None")
if not hasattr(backbone, "out_channels"):
raise Exception("the backbone not has attribute: out_channel")
self.feature_extractor = backbone
self.num_classes = num_classes
# out_channels = [1024, 512, 512, 256, 256, 256] for resnet50
self._build_additional_features(self.feature_extractor.out_channels)
self.num_defaults = [4, 6, 6, 6, 4, 4]
location_extractors = []
confidence_extractors = []
# out_channels = [1024, 512, 512, 256, 256, 256] for resnet50
for nd, oc in zip(self.num_defaults, self.feature_extractor.out_channels):
# nd is number_default_boxes, oc is output_channel
location_extractors.append(nn.Conv2d(oc, nd * 4, kernel_size=3, padding=1))
confidence_extractors.append(nn.Conv2d(oc, nd * self.num_classes, kernel_size=3, padding=1))
self.loc = nn.ModuleList(location_extractors)View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Pass a backbone: SSD300(backbone=resnet50_fpn_backbone(), num_classes=...) Check torchvision version for resnet50_fpn_backbone availability (moved to torchvision.models.detection.backbone_utils in newer versions) Add an assertion before construction that the backbone factory returned non-None
Example fix
// before model = SSD300(num_classes=21) // after backbone = resnet50_fpn_backbone(pretrain_path='resnet50.pth') model = SSD300(backbone=backbone, num_classes=21)
Defensive patterns
Strategy: validation
Validate before calling
backbone = resnet50_fpn_backbone() assert backbone is not None, 'backbone factory returned None' model = SSD300(backbone=backbone, num_classes=21)
Type guard
def build_ssd(backbone, num_classes):
assert backbone is not None, 'SSD300 requires a backbone'
from pytorch_object_detection.ssd.src.ssd_model import SSD300
return SSD300(backbone=backbone, num_classes=num_classes) Try / catch
try:
model = SSD300(backbone=backbone, num_classes=21)
except Exception as e:
print(f'Model construction failed: {e}; ensure backbone passed') Prevention
- Always pass a backbone kwarg to SSD300
- Verify backbone factory imports work for your torchvision version
- Wrap construction in a helper that asserts non-None backbone
When it happens
Trigger: Calling SSD300() or SSD300(num_classes=21) with no backbone kwarg; a factory function returning None backbone due to a failed pretrained-weight load path.
Common situations: Following a tutorial snippet that shows SSD300(num_classes=...) only; torchvision backbone builder behind a version-dependent import failing silently and returning None; forgetting to pass create_backbone() result.
Related errors
- the backbone not has attribute: out_channel
- return_layers are not present in model
- backbone should contain an attribute out_channelsspecifying
- backbone should contain an attribute out_channels specifying
- In training mode, targets should be passed
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/5e0b424d838aee09.
Report an issue: GitHub.