{"record":{"id":"63728d7ecf709b1d","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"num-classes-should-be-none-when-mask-predictor-is","errorCode":null,"errorMessage":"num_classes should be None when mask_predictor is specified","messagePattern":"num_classes should be None when mask_predictor is specified","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pytorch_object_detection/mask_rcnn/network_files/mask_rcnn.py","lineNumber":144,"sourceCode":"            box_fg_iou_thresh=0.5,\n            box_bg_iou_thresh=0.5,\n            box_batch_size_per_image=512,\n            box_positive_fraction=0.25,\n            bbox_reg_weights=None,\n            # Mask parameters\n            mask_roi_pool=None,\n            mask_head=None,\n            mask_predictor=None,\n    ):\n\n        if not isinstance(mask_roi_pool, (MultiScaleRoIAlign, type(None))):\n            raise TypeError(\n                f\"mask_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(mask_roi_pool)}\"\n            )\n\n        if num_classes is not None:\n            if mask_predictor is not None:\n                raise ValueError(\"num_classes should be None when mask_predictor is specified\")\n\n        out_channels = backbone.out_channels\n\n        if mask_roi_pool is None:\n            mask_roi_pool = MultiScaleRoIAlign(featmap_names=[\"0\", \"1\", \"2\", \"3\"], output_size=14, sampling_ratio=2)\n\n        if mask_head is None:\n            mask_layers = (256, 256, 256, 256)\n            mask_dilation = 1\n            mask_head = MaskRCNNHeads(out_channels, mask_layers, mask_dilation)\n\n        if mask_predictor is None:\n            mask_predictor_in_channels = 256\n            mask_dim_reduced = 256\n            mask_predictor = MaskRCNNPredictor(mask_predictor_in_channels, mask_dim_reduced, num_classes)\n\n        super().__init__(\n            backbone,","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_object_detection/mask_rcnn/network_files/mask_rcnn.py#L126-L162","documentation":"MaskRCNN's constructor lets you either supply a pre-built mask_predictor head or configure the head from num_classes, but not both. When a custom mask_predictor is provided, num_classes must be None because the predictor already defines its output dimensions; passing both is an unresolvable configuration conflict the library detects and raises as ValueError.","triggerScenarios":"Calling MaskRCNN(...) with both a non-None mask_predictor and a non-None num_classes, e.g. MaskRCNN(backbone, num_classes=91, mask_predictor=MyPredictor(...)).","commonSituations":"Swapping in a custom mask head while leaving the tutorial num_classes argument in place; copying torchvision example code and modifying only the predictor; confusion after fine-tuning on a custom dataset.","solutions":["Set num_classes=None when passing a mask_predictor","Ensure the custom mask_predictor itself outputs the desired number of classes (its final conv out_channels)","If you actually want the library to build the head, pass mask_predictor=None and keep num_classes"],"exampleFix":"// before\nmodel = MaskRCNN(backbone, num_classes=91, mask_predictor=my_predictor)\n// after\nmodel = MaskRCNN(backbone, num_classes=None, mask_predictor=my_predictor)","handlingStrategy":"validation","validationCode":"assert not (mask_predictor is not None and num_classes is not None), \"pass either num_classes or mask_predictor, not both\"\nmodel = MaskRCNN(backbone, num_classes=num_classes if mask_predictor is None else None, mask_predictor=mask_predictor)","typeGuard":"def is_maskrcnn_cfg_valid(num_classes, mask_predictor):\n    return mask_predictor is None or num_classes is None","tryCatchPattern":null,"preventionTips":["When providing a custom head, always set the corresponding num_classes argument to None","Document which constructor combinations your team uses","Validate config dicts before instantiation"],"tags":["python","mask-rcnn","configuration"],"backgroundTag":"mutually-exclusive-arguments","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}