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

MobileNetV3.__init__ raises TypeError when inverted_residual_setting is non-empty but is not a List whose elements are all InvertedResidualConfig instances. This guards against passing dicts, tuples, or plain ints/strings as the block specification.

Source

Thrown at pytorch_classification/Test6_mobilenet/model_v3.py:155

            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. Build each entry with InvertedResidualConfig(...) instead of plain dicts and pass the list as-is.
  2. Wrap a single InvertedResidualConfig in a list: [cfg].
  3. If configs come from JSON, deserialize each item into InvertedResidualConfig(**d).
  4. Prefer the MobileNetV3Large/MobileNetV3Small helpers, which construct valid configs internally.

Example fix

// before
model = MobileNetV3(inverted_residual_setting=[{"input_c": 16, "kernel": 3, ...}], num_classes=5)
// after
cfg = InvertedResidualConfig(16, 3, 16, 16, True, "RE", 1, 1, 1)
model = MobileNetV3(inverted_residual_setting=[cfg], num_classes=5)
Defensive patterns

Strategy: type-guard

Validate before calling

ok = isinstance(inverted_residual_setting, list) and all(isinstance(s, InvertedResidualConfig) for s in inverted_residual_setting)
assert ok, "inverted_residual_setting must be List[InvertedResidualConfig]"

Type guard

def is_block_config_list(x) -> bool:
    return isinstance(x, list) and all(isinstance(i, InvertedResidualConfig) for i in x)

Try / catch

try:
    model = MobileNetV3(inverted_residual_setting=setting, num_classes=n)
except TypeError as e:
    if "List[InvertedResidualConfig]" in str(e):
        setting = [InvertedResidualConfig(**d) for d in setting]
        model = MobileNetV3(inverted_residual_setting=setting, num_classes=n)
    else:
        raise

Prevention

When it happens

Trigger: Passing a list of dicts (e.g. JSON-loaded architecture), a tuple of InvertedResidualConfig, a single InvertedResidualConfig (not wrapped in a list), or a list containing mixed types to MobileNetV3(inverted_residual_setting=...).

Common situations: Serializing architecture configs to JSON for config-driven experiments and passing them back in (dicts don't survive as InvertedResidualConfig); hand-building configs with raw dicts instead of the dataclass.

Related errors


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