{"record":{"id":"5e0b424d838aee09","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"backbone-is-none","errorCode":null,"errorMessage":"backbone is None","messagePattern":"backbone is None","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/ssd/src/ssd_model.py","lineNumber":36,"sourceCode":"        self.feature_extractor = nn.Sequential(*list(net.children())[:7])\n\n        conv4_block1 = self.feature_extractor[-1][0]\n\n        # 修改conv4_block1的步距，从2->1\n        conv4_block1.conv1.stride = (1, 1)\n        conv4_block1.conv2.stride = (1, 1)\n        conv4_block1.downsample[0].stride = (1, 1)\n\n    def forward(self, x):\n        x = self.feature_extractor(x)\n        return x\n\n\nclass SSD300(nn.Module):\n    def __init__(self, backbone=None, num_classes=21):\n        super(SSD300, self).__init__()\n        if backbone is None:\n            raise Exception(\"backbone is None\")\n        if not hasattr(backbone, \"out_channels\"):\n            raise Exception(\"the backbone not has attribute: out_channel\")\n        self.feature_extractor = backbone\n\n        self.num_classes = num_classes\n        # out_channels = [1024, 512, 512, 256, 256, 256] for resnet50\n        self._build_additional_features(self.feature_extractor.out_channels)\n        self.num_defaults = [4, 6, 6, 6, 4, 4]\n        location_extractors = []\n        confidence_extractors = []\n\n        # out_channels = [1024, 512, 512, 256, 256, 256] for resnet50\n        for nd, oc in zip(self.num_defaults, self.feature_extractor.out_channels):\n            # nd is number_default_boxes, oc is output_channel\n            location_extractors.append(nn.Conv2d(oc, nd * 4, kernel_size=3, padding=1))\n            confidence_extractors.append(nn.Conv2d(oc, nd * self.num_classes, kernel_size=3, padding=1))\n\n        self.loc = nn.ModuleList(location_extractors)","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/ssd/src/ssd_model.py#L18-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a backbone: SSD300(backbone=resnet50_fpn_backbone(), num_classes=...)\nCheck torchvision version for resnet50_fpn_backbone availability (moved to torchvision.models.detection.backbone_utils in newer versions)\nAdd an assertion before construction that the backbone factory returned non-None"],"exampleFix":"// before\nmodel = SSD300(num_classes=21)\n// after\nbackbone = resnet50_fpn_backbone(pretrain_path='resnet50.pth')\nmodel = SSD300(backbone=backbone, num_classes=21)","handlingStrategy":"validation","validationCode":"backbone = resnet50_fpn_backbone()\nassert backbone is not None, 'backbone factory returned None'\nmodel = SSD300(backbone=backbone, num_classes=21)","typeGuard":"def build_ssd(backbone, num_classes):\n    assert backbone is not None, 'SSD300 requires a backbone'\n    from pytorch_object_detection.ssd.src.ssd_model import SSD300\n    return SSD300(backbone=backbone, num_classes=num_classes)","tryCatchPattern":"try:\n    model = SSD300(backbone=backbone, num_classes=21)\nexcept Exception as e:\n    print(f'Model construction failed: {e}; ensure backbone passed')","preventionTips":["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"],"tags":["pytorch","ssd","backbone","constructor"],"backgroundTag":"missing-backbone-argument","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}