WZMIAOMIAO/deep-learning-for-image-processing · error · TypeError

The inverted_residual_setting should be List[InvertedResidua

Error message

The inverted_residual_setting should be List[InvertedResidualConfig]

What it means

After the emptiness check, MobileNetV3 validates that inverted_residual_setting is a List whose every element is an InvertedResidualConfig instance. Violating either condition raises this TypeError. This prevents subtle runtime crashes deep in block construction.

Source

Thrown at pytorch_segmentation/lraspp/src/mobilenet_backbone.py:162

            result += x

        return result


class MobileNetV3(nn.Module):
    def __init__(self,
                 inverted_residual_setting: List[InvertedResidualConfig],
                 last_channel: int,
                 num_classes: int = 1000,
                 block: Optional[Callable[..., nn.Module]] = None,
                 norm_layer: Optional[Callable[..., nn.Module]] = None):
        super(MobileNetV3, self).__init__()

        if not inverted_residual_setting:
            raise ValueError("The inverted_residual_setting should not be empty.")
        elif not (isinstance(inverted_residual_setting, List) and
                  all([isinstance(s, InvertedResidualConfig) for s in inverted_residual_setting])):
            raise TypeError("The inverted_residual_setting should be List[InvertedResidualConfig]")

        if block is None:
            block = InvertedResidual

        if norm_layer is None:
            norm_layer = partial(nn.BatchNorm2d, eps=0.001, momentum=0.01)

        layers: List[nn.Module] = []

        # building first layer
        firstconv_output_c = inverted_residual_setting[0].input_c
        layers.append(ConvBNActivation(3,
                                       firstconv_output_c,
                                       kernel_size=3,
                                       stride=2,
                                       norm_layer=norm_layer,
                                       activation_layer=nn.Hardswish))
        # building inverted residual blocks

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Convert each spec dict into `InvertedResidualConfig(**spec)` and collect into a plain `list`
  2. Ensure you use this repo's InvertedResidualConfig class, not torchvision's, so isinstance passes
  3. Pass a `list`, not tuple/dict, and pass the whole list not a single element

Example fix

// before
model = MobileNetV3(inverted_residual_setting=[{...cfg dict...}], num_classes=21)  # TypeError
// after
cfgs = [InvertedResidualConfig(**d) for d in cfg_dicts]
model = MobileNetV3(inverted_residual_setting=cfgs, num_classes=21)
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(cfgs, list) and all(isinstance(c, InvertedResidualConfig) for c in cfgs), \
    "need List[InvertedResidualConfig] (this repo's class)"

Type guard

def is_valid_setting(cfgs):
    from typing import List
    return isinstance(cfgs, list) and all(isinstance(c, InvertedResidualConfig) for c in cfgs)

Try / catch

try:
    model = MobileNetV3(inverted_residual_setting=cfgs, num_classes=nc)
except TypeError:
    cfgs = [InvertedResidualConfig(**c) if isinstance(c, dict) else c for c in cfgs]
    model = MobileNetV3(inverted_residual_setting=list(cfgs), num_classes=nc)

Prevention

When it happens

Trigger: Passing a dict/tuple instead of a list (e.g. an OrderedDict of configs); passing plain dicts parsed from YAML/JSON without converting to InvertedResidualConfig; passing a single config object instead of a list; numpy/py3.8 typing quirks where typing.List check fails — code uses `List` from typing via isinstance which requires bare list.

Common situations: Config-driven model factories reading architecture specs from files; developers passing tuples copied from older code; hand-written presets using dicts with matching keys but no InvertedResidualConfig class; mixing torchvision's MobileNetV3 (which uses its own internal config class) with this repo's class.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/76de692ec8db22ca. Report an issue: GitHub.