0

good day!

i have a situation while trying to process several images in a batch using c#, NumSharp and an onnx model

See an extract code below:

var modelPath = @"Resources/model_20221026.onnx";
                var outputColumnNames = new[] { "dense_4" };
                var inputColumnNames = new[] { "input_5" };
                var mlContext = new MLContext();
                var pipeline = mlContext.Transforms.ApplyOnnxModel(outputColumnNames, inputColumnNames, modelPath);
                
                var img0 = Bitmap.FromFile(path, true);
                
                var content = ResizeImage(img0, 512, 512);
                img0.Dispose();

                var data0 = content.ToNDArray(flat: false, copy: false);
                var dataNP = np.array(data0).astype(NPTypeCode.Single).flatten().ToArray<float>();
                content.Dispose();

                var input = new Input { Data = dataNP };

                var dataView = mlContext.Data.LoadFromEnumerable(new[] { input });
                var transformedValues = pipeline.Fit(dataView).Transform(dataView);
                var output = mlContext.Data.CreateEnumerable<Output>(transformedValues, reuseRowObject: false);

                float[] probabilities = output.Single().Data;

                float defect = (float)Math.Round(100 * probabilities[0], 2);
                float nodefect = (float)Math.Round(100 * probabilities[1], 2);

                string[] NameImg = path.Split('\\');
                Logger.Info("------------ID: "
                            + " " + NameImg[NameImg.Length - 1]
                            + " - Defect: " + defect + "%"
                            + " - NoDefect: " + nodefect + "%");

It will do it once, sometimes twice, but then, it will crash. The application will crash suddently, and won't write in logs anything if i try to catch any argument error.

If i try in debug mode i get this error:

enter image description here

enter image description here

Any idea is very welcome, thanks!

I tried in: Debug mode Disposing contents Instead of opening a file with Bitmap.FromFile, i tried to stream the content I try catching arguments exceptions and exceptions in general

chacoff
  • 44
  • 3
  • @NucS, i think i found the answer to my problem in your post here: https://stackoverflow.com/questions/57396469/convert-in-memory-bitmap-image-to-4-dimensional-array-like-numpy I used GetBitmapBytes(Bitmap image) instead of ToNDArray and worked – chacoff Oct 27 '22 at 19:35

0 Answers0