lllyasviel/style2paints · error · ValueError

Axis cannot be zero

Error message

Axis cannot be zero

What it means

InstanceNorm.build() validates the layer's axis configuration before constructing weights. Axis 0 is rejected because axis 0 in Keras is the batch/sample dimension; normalizing over it would mix statistics across samples in the batch, which is meaningless for instance normalization. The library throws eagerly at build time so the misconfiguration is caught before any weights are created.

Source

Thrown at V4/s2p_v4_server/InstanceNorm.py:74

                 gamma_constraint=None,
                 **kwargs):
        super(InstanceNormalization, self).__init__(**kwargs)
        self.supports_masking = True
        self.axis = axis
        self.epsilon = epsilon
        self.center = center
        self.scale = scale
        self.beta_initializer = initializers.get(beta_initializer)
        self.gamma_initializer = initializers.get(gamma_initializer)
        self.beta_regularizer = regularizers.get(beta_regularizer)
        self.gamma_regularizer = regularizers.get(gamma_regularizer)
        self.beta_constraint = constraints.get(beta_constraint)
        self.gamma_constraint = constraints.get(gamma_constraint)

    def build(self, input_shape):
        ndim = len(input_shape)
        if self.axis == 0:
            raise ValueError('Axis cannot be zero')

        if (self.axis is not None) and (ndim == 2):
            raise ValueError('Cannot specify axis for rank 1 tensor')

        self.input_spec = InputSpec(ndim=ndim)

        if self.axis is None:
            shape = (1,)
        else:
            shape = (input_shape[self.axis],)

        if self.scale:
            self.gamma = self.add_weight(shape=shape,
                                         name='gamma',
                                         initializer=self.gamma_initializer,
                                         regularizer=self.gamma_regularizer,
                                         constraint=self.gamma_constraint)
        else:

View on GitHub (pinned to a0d164d6a8)

Solutions

  1. Set axis to the channel dimension (typically 1 for channels-first or -1/last for channels-last), never 0
  2. If axis was passed programmatically, use 1-based or negative indexing so it cannot be 0
  3. If axis=None is acceptable, omit the axis argument entirely so normalization applies to the whole input

Example fix

# before
norm = InstanceNorm(axis=0)

# after
norm = InstanceNorm(axis=1)  # or axis=-1 for channels-last
Defensive patterns

Strategy: validation

Validate before calling

def check_axis(axis, ndim):
    if axis == 0:
        raise ValueError('InstanceNorm axis cannot be 0 (batch dimension)')
    if axis is not None and ndim == 2:
        raise ValueError('axis must be None for rank-1/2D inputs')

check_axis(axis, len(input_shape))

Type guard

def is_valid_norm_input(x, axis):
    return axis != 0 and (axis is None or len(x.shape) != 2)

Prevention

When it happens

Trigger: Calling the layer (or building a model containing it) with InstanceNorm(axis=0, ...), or relying on a default/derived axis value that resolves to 0 — e.g. passing axis as an index computed from a 0-based loop or config dict.

Common situations: Porting models from other frameworks where the channel axis convention differs (channels-first vs channels-last) and the axis index is off by one; hand-editing layer configs; programmatically generating axis values that start at 0.

Related errors


AI-assisted analysis of lllyasviel/style2paints@a0d164d6a8 (2026-09-02). Data as JSON: /api/errors/5e52d28ca5000bd5. Report an issue: GitHub.