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

The inverted_residual_setting should not be empty.

Error message

The inverted_residual_setting should not be empty.

What it means

MobileNetV3.__init__ raises ValueError when inverted_residual_setting is falsy (None or an empty list). The block-configuration list defines the entire feature extractor, so constructing the model without it is treated as a programmer error rather than an allowed default.

Source

Thrown at pytorch_classification/Test6_mobilenet/model_v3.py:152

    def forward(self, x: Tensor) -> Tensor:
        result = self.block(x)
        if self.use_res_connect:
            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,

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Use the provided helpers MobileNetV3Large(num_classes=...) or MobileNetV3Small(num_classes=...) instead of constructing MobileNetV3 directly.
  2. If constructing directly, pass a valid non-empty List[InvertedResidualConfig].
  3. Check that the variable holding your config list isn't accidentally None due to an earlier failed assignment.

Example fix

// before
model = MobileNetV3(inverted_residual_setting=None, num_classes=5)
// after
model = MobileNetV3Large(num_classes=5)
Defensive patterns

Strategy: validation

Validate before calling

if not inverted_residual_setting:
    raise ValueError("inverted_residual_setting must be a non-empty list")
model = MobileNetV3(inverted_residual_setting=inverted_residual_setting, num_classes=num_classes)

Type guard

def is_valid_setting(s) -> bool:
    return isinstance(s, list) and len(s) > 0 and all(isinstance(x, InvertedResidualConfig) for x in s)

Try / catch

try:
    model = MobileNetV3(inverted_residual_setting=setting, num_classes=n)
except ValueError as e:
    if "should not be empty" in str(e):
        model = MobileNetV3Large(num_classes=n)
    else:
        raise

Prevention

When it happens

Trigger: Calling MobileNetV3() with inverted_residual_setting=None or [] — e.g. forgetting to pass the bneck config list, or passing a variable that failed to populate.

Common situations: Instantiating MobileNetV3 directly instead of via the _mobilenet_v3_conf/.MobileNetV3Large or MobileNetV3Small factory helpers that build the standard configs; refactoring code and dropping the argument.

Related errors


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