WZMIAOMIAO/deep-learning-for-image-processing · error · TypeError
mask_roi_pool should be of type MultiScaleRoIAlign or None i
Error message
mask_roi_pool should be of type MultiScaleRoIAlign or None instead of {} What it means
MaskRCNN.__init__ type-checks mask_roi_pool: it must be a MultiScaleRoIAlign instance or None (to use the default). Any other object raises this TypeError reporting the actual type received.
Source
Thrown at pytorch_object_detection/mask_rcnn/network_files/mask_rcnn.py:138
box_roi_pool=None,
box_head=None,
box_predictor=None,
box_score_thresh=0.05,
box_nms_thresh=0.5,
box_detections_per_img=100,
box_fg_iou_thresh=0.5,
box_bg_iou_thresh=0.5,
box_batch_size_per_image=512,
box_positive_fraction=0.25,
bbox_reg_weights=None,
# Mask parameters
mask_roi_pool=None,
mask_head=None,
mask_predictor=None,
):
if not isinstance(mask_roi_pool, (MultiScaleRoIAlign, type(None))):
raise TypeError(
f"mask_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(mask_roi_pool)}"
)
if num_classes is not None:
if mask_predictor is not None:
raise ValueError("num_classes should be None when mask_predictor is specified")
out_channels = backbone.out_channels
if mask_roi_pool is None:
mask_roi_pool = MultiScaleRoIAlign(featmap_names=["0", "1", "2", "3"], output_size=14, sampling_ratio=2)
if mask_head is None:
mask_layers = (256, 256, 256, 256)
mask_dilation = 1
mask_head = MaskRCNNHeads(out_channels, mask_layers, mask_dilation)
if mask_predictor is None:View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Use torchvision.ops.MultiScaleRoIAlign(featmap_names=['0','1','2','3'], output_size=14, sampling_ratio=2) for mask_roi_pool
- Pass None to let the model build the default MultiScaleRoIAlign
- If you need custom pooling, subclass or wrap it so the argument is a genuine MultiScaleRoIAlign, or patch the check consciously
Example fix
// before
from torchvision.ops import RoIAlign
model = MaskRCNN(backbone, num_classes=91, mask_roi_pool=RoIAlign((14,14), 2, 0))
// after
from torchvision.ops import MultiScaleRoIAlign
model = MaskRCNN(backbone, num_classes=91,
mask_roi_pool=MultiScaleRoIAlign(featmap_names=['0','1','2','3'], output_size=14, sampling_ratio=2)) Defensive patterns
Strategy: type-guard
Validate before calling
if mask_roi_pool is not None and not isinstance(mask_roi_pool, MultiScaleRoIAlign):
raise TypeError(f'mask_roi_pool must be MultiScaleRoIAlign or None, got {type(mask_roi_pool)}') Type guard
def is_valid_mask_roi_pool(x):
return x is None or isinstance(x, MultiScaleRoIAlign) Try / catch
try:
model = MaskRCNN(backbone, num_classes=num_classes, mask_roi_pool=mask_roi_pool)
except TypeError as e:
if 'mask_roi_pool' in str(e):
model = MaskRCNN(backbone, num_classes=num_classes, mask_roi_pool=None)
else:
raise Prevention
- Import MultiScaleRoIAlign from torchvision.ops (not RoIAlign) for mask_roi_pool arguments
- Pass None to use defaults unless you specifically need custom featmap_names/output_size
- Type-hint parameters as Optional[MultiScaleRoIAlign] in wrapper code
When it happens
Trigger: Passing a custom ROI-pooling module (e.g. RoIAlign from torchvision.ops, an nn.AdaptiveAvgPool2d, or a differently-parameterized pooling layer) as the mask_roi_pool argument of MaskRCNN(...).
Common situations: Confusing torchvision.ops.RoIAlign with MultiScaleRoIAlign; copying a box_roi_pool instance into mask_roi_pool from the wrong class hierarchy; older/newer torchvision versions where the expected class moved modules.
Understand the failure class
Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.
Related errors
- In training mode, targets should be passed
- Expected target boxes to be of type Tensor, got {:}.
- num_classes should be None when box_predictor is specified
- num_classes should not be None when box_predictor is not spe
- 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/1a40f518d8d5887f.
Report an issue: GitHub.