dotnet/machinelearning · error · InvalidOperationException

Invalid image resizing mode value

Error message

Invalid image resizing mode value

What it means

ImageResizer maps the estimator's ResizingKind enum (IsoCrop with anchors, or Fill) onto native resize modes. The switch covers ResizingKind.IsoCrop and ResizingKind.Fill; any other/unrecognized value hits the discard arm and throws InvalidOperationException. This normally indicates a value outside the defined enum range was used.

Source

Thrown at src/Microsoft.ML.ImageAnalytics/ImageResizer.cs:323

                        }

                        dst = src.CloneWithResizing(
                                info.ImageWidth,
                                info.ImageHeight,
                                info.Resizing switch
                                {
                                    ImageResizingEstimator.ResizingKind.IsoPad => ImageResizeMode.Pad,
                                    ImageResizingEstimator.ResizingKind.IsoCrop =>
                                        info.Anchor switch
                                        {
                                            ImageResizingEstimator.Anchor.Top => ImageResizeMode.CropAnchorTop,
                                            ImageResizingEstimator.Anchor.Bottom => ImageResizeMode.CropAnchorBottom,
                                            ImageResizingEstimator.Anchor.Left => ImageResizeMode.CropAnchorLeft,
                                            ImageResizingEstimator.Anchor.Right => ImageResizeMode.CropAnchorRight,
                                            _ => ImageResizeMode.CropAnchorCentral
                                        },
                                    ImageResizingEstimator.ResizingKind.Fill => ImageResizeMode.Fill,
                                    _ => throw new InvalidOperationException($"Invalid image resizing mode value")
                                });

                        dst.Tag = src.Tag;
                        Contracts.Assert(dst.Width == info.ImageWidth && dst.Height == info.ImageHeight);
                    };

                return del;
            }
        }
    }

    /// <summary>
    /// <see cref="IEstimator{TTransformer}"/> for the <see cref="ImageResizingTransformer"/>.
    /// </summary>
    /// <remarks>
    /// <format type="text/markdown"><![CDATA[
    ///
    /// ###  Estimator Characteristics

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Use only ImageResizingEstimator.ResizingKind.IsoCrop or .Fill
  2. Validate/deserialize config into the enum with Enum.TryParse before constructing the estimator
  3. Align Microsoft.ML package versions so enum definitions match at compile and run time

Example fix

// before
var est = new ImageResizingEstimator(env, columns, (ResizingKind)7, ...);
// after
if (!Enum.TryParse<ResizingKind>(resizeKindText, out var kind) ||
    (kind != ResizingKind.IsoCrop && kind != ResizingKind.Fill))
    throw new ArgumentException($"Unsupported resizing kind: {resizeKindText}");
var est = new ImageResizingEstimator(env, columns, kind, ...);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Enum.IsDefined(typeof(ImageResizingEstimator.ResizingKind), resizingKind))
    throw new ArgumentException($"Unknown resizing kind: {resizingKind}");

Type guard

bool IsValidResizingKind(object v) => v is ImageResizingEstimator.ResizingKind k && (k == ImageResizingEstimator.ResizingKind.IsoCrop || k == ImageResizingEstimator.ResizingKind.Fill);

Try / catch

try
{
    BuildResizer(options);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("resizing mode"))
{
    logger.LogError(ex, "Bad ResizingKind {Kind} in config", options.ResizingKind);
}

Prevention

When it happens

Trigger: Passing an out-of-range or undefined ImageResizingEstimator.ResizingKind value (e.g. a cast int, a value from a newer/older assembly version) into the ImageResizingEstimator/ImageResizingTransformer options so MakeGetter's switch reaches its default arm.

Common situations: Deserializing resize options from JSON/config where the enum value was stored as a raw integer or renamed between ML.NET versions; assembly version mismatches between Microsoft.ML packages.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/4828921b4c6489d3. Report an issue: GitHub.