Length of memory (691200) must match product of dimensions (3).
System information
- OS version/distro: Windows 10
- .NET Version (eg., dotnet --info): netcoreapp3.1
Issue
- What did you do? I loaded an ONNX model to ML.NET and tried to run it.
- What happened? I am getting this exception when trying to do a prediction: "Length of memory (691200) must match product of dimensions (3)"
- What did you expect? Prediction
Source code / logs
Full error message:
System.ArgumentException: 'Length of memory (691200) must match product of dimensions (3).'
Pipeline that I am using:
var pipeline = _mlContext.Transforms
.ResizeImages(
outputColumnName: "resized_image",
imageWidth: 640,
imageHeight: 360,
inputColumnName: "image")
.Append(_mlContext.Transforms
.ExtractPixels(
outputColumnName: ModelConfigParameters.InputColumns.First(),
outputAsFloatArray: false,
colorsToExtract: ImagePixelExtractingEstimator.ColorBits.Rgb,
orderOfExtraction: ImagePixelExtractingEstimator.ColorsOrder.ARGB,
interleavePixelColors: false,
inputColumnName: "resized_image"))
.Append(_mlContext.Transforms
.ApplyOnnxModel(
modelFile: onnxModelPath,
outputColumnNames: ModelConfigParameters.OutputColumns,
inputColumnNames: ModelConfigParameters.InputColumns));
Input object to the pipeline:
public class InputImage
{
[ImageType(width: 640, height: 360)]
[ColumnName("image")]
public Bitmap Image { get; set; }
}
Output object (predictions):
public class OutputPredictions
{
[ColumnName("detection_scores:0")]
public float[] DetectionScores;
[ColumnName("detection_boxes:0")]
public float[] DetectionBoxes;
[ColumnName("detection_classes:0")]
public float[] DetectionClasses;
[ColumnName("num_detections:0")]
public float[] NumDetections;
}
The code that fails on the last line:
var imgPath = @"some/path/to/image.png";
var img = (Bitmap)Image.FromFile(imgPath);
var inputData = new InputImage() { Image = img };
var emptyData = _mlContext.Data.LoadFromEnumerable(new List<InputImage>());
var model = pipeline.Fit(emptyData);
var prediction = _mlContext.Model.CreatePredictionEngine<InputImage, OutputPredictions>(model).Predict(inputData);
When I inspect pipeline using the var preview = pipeline.Preview(emptyData); I get this scheme:

and the input and output of the ONNX model is following:

The error you are getting is because after you are loading in the image, the dimensions are not right. The 691200 is coming from your image dimensions * 3 channels (640 * 360 * 3). Could you attach your image you are using so we can take a closer look at it?
@michaelgsharp Here is the link to image that I am using. But I tried to change [ImageType(width: 640, height: 360)] attribute to the size of the input image but nothing has changed.
I also don't understand the point of ImageType argument. Why do I have to specify image size? What if I want to load images with various sizes and resize them in my pipeline?
@horczech, I dont have your onnx model so I can't verify the whole pipeline, but other than running that step I was able to resolve your issue. There were 2 things I changed slightly.
Your InputImage class needs to have dimensions equal to the picture dimensions before you do any resizing. For the picture you linked, that would be this.
public class InputImage
{
[ImageType(width: 1280, height: 724)]
[ColumnName("image")]
public Bitmap Image { get; set; }
}
The only other thing I changed was your call to fit. Right now you call it with an empty dataset:
var model = pipeline.Fit(emptyData);
This is the cause for the specific error you are seeing. ML.Net is not happy with an empty dataset being passed in. To resolve this, I just made the dataset you passed in have the input image you created and use that with fit.
var inputData = new InputImage() { Image = img };
var data = mlContext.Data.LoadFromEnumerable(new List<InputImage>() { inputData });
var model = pipeline.Fit(data);
After that you can create your PredictionEngine exactly as you are doing it now.
I believe you have to have all of your input images the same size. I am trying to verify that still and will update this issue with the answer when I hear back about it, but you should assume for now that all the input images need the same size. I believe this is because you define the InputImage size, so all images must have that size.
I am going to close this issue for now. If the 2 changes I suggested in my prior comment don't fix your issue, please reopen this issue so we can take another look at it. I will still post another comment here when I hear back about the image size situation regardless of whether this issue gets opened again or not.
@horczech, I was just informed that no, your input images don't need to be the same size. The example here, https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.imageestimatorscatalog.resizeimages?view=ml-dotnet, shows how to do images of differing input sizes and then resizing them to the size you want.