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

Besides being non-empty, inverted_residual_setting must be a List whose every element is an InvertedResidualConfig. Otherwise the model cannot safely read each block's fields, so __init__ raises TypeError naming the expected type.

Source

Thrown at pytorch_segmentation/deeplab_v3/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 entry to InvertedResidualConfig with the right field order/values.
  2. Use the repo's factory functions (mobilev3_large_150 / mobilev3_small_100) instead of hand-built lists.
  3. If you have dicts, map them: [InvertedResidualConfig(**d) for d in settings] after verifying fields.

Example fix

// before
settings = [{"input_c": 16, "kernel": 3, "expanded_c": 64, ...}]
model = MobileNetV3(inverted_residual_setting=settings)
// after
settings = [InvertedResidualConfig(16, 3, 16, 16, False, "RE", 1, 1, 1)]
model = MobileNetV3(inverted_residual_setting=settings)
Defensive patterns

Strategy: type-guard

Validate before calling

from src.mobilenet_backbone import InvertedResidualConfig
assert isinstance(settings, list) and all(isinstance(s, InvertedResidualConfig) for s in settings)

Type guard

from src.mobilenet_backbone import InvertedResidualConfig
def is_valid_settings(settings) -> bool:
    return isinstance(settings, list) and all(isinstance(s, InvertedResidualConfig) for s in settings)

Try / catch

try:
    model = MobileNetV3(inverted_residual_setting=settings, num_classes=n)
except TypeError as e:
    logging.error(f"{e}; types={[type(s).__name__ for s in settings]}"); raise

Prevention

When it happens

Trigger: Passing inverted_residual_setting as a tuple, dict, or a list of plain dicts/dataclass-like objects that are not InvertedResidualConfig instances.

Common situations: Building block configs as raw dicts instead of InvertedResidualConfig; mixing configs copied from torchvision's MobileNetV3 (different dataclass) with this repo's implementation; JSON-loaded configs.

Related errors


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