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's __init__ requires a non-empty inverted_residual_setting list describing the block stack. An empty (falsy) value would produce a model with no feature blocks, so it raises ValueError immediately rather than building a broken network.

Source

Thrown at pytorch_segmentation/deeplab_v3/src/mobilenet_backbone.py:159

    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. Pass a real config list, e.g. use the provided mobilev3_large_150(num_classes=...) or mobilev3_small_100(...) factory functions.
  2. Populate inverted_residual_setting with valid InvertedResidualConfig entries.
  3. Check upstream code that builds the settings list for filtering bugs that empty it.

Example fix

// before
model = MobileNetV3(inverted_residual_setting=[], num_classes=20)
// after
from src.mobilenet_backbone import mobilev3_large_150
model = mobilev3_large_150(num_classes=20)
Defensive patterns

Strategy: validation

Validate before calling

assert inverted_residual_setting, "inverted_residual_setting must be non-empty"

Type guard

def is_nonempty_config_list(settings) -> bool:
    return bool(settings)

Try / catch

try:
    model = MobileNetV3(inverted_residual_setting=settings, num_classes=n)
except ValueError as e:
    logging.error(f"{e}; len(settings)={len(settings) if settings else 0}"); raise

Prevention

When it happens

Trigger: Calling MobileNetV3(inverted_residual_setting=[]) or omitting/None-ing the setting without passing a preset name, e.g. building the model with an empty custom config.

Common situations: Constructing the model programmatically with a config list that ended up empty (filtered out all blocks); forgetting to import/use the provided mobilev3_large_150/mobilev3_small_100 settings builders.

Related errors


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