Git Product home page Git Product logo

Comments (3)

MrBean2016 avatar MrBean2016 commented on September 26, 2024

Change

  class ImageInputData
    {
        [ImageType(224, 224)]
        public Bitmap Image { get; set; }
    }

to

  class ImageInputData
    {
        [ImageType(224, 224)]
        public MLImage Image { get; set; }
    }

from bitmaponnxprediction.

karlhm76 avatar karlhm76 commented on September 26, 2024

Thank you,

I have since reimplemented my code only using Microsoft.ML.OnnxRuntime and using Bitmaps with basic GDI functions for preprocessing, but it will be good to see how it should work with ML.NET.

With my changes, my code has become:

            Microsoft.ML.OnnxRuntime.Tensors.Tensor<float> input = new Microsoft.ML.OnnxRuntime.Tensors.DenseTensor<float>(new[] { 1, 3, 720, 576 });

            BitmapData bitmapData = originalImage.LockBits(new System.Drawing.Rectangle(0, 0, originalImage.Width, originalImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            int stride = bitmapData.Stride;
            IntPtr scan0 = bitmapData.Scan0;

            unsafe
            {
                byte* ptr = (byte*)scan0;
                for (int y = 0; y < originalImage.Height; y++)
                {
                    for (int x = 0; x < originalImage.Width; x++)
                    {
                        int offset = y * stride + x * 3;
                        input[0, 0, y, x] = ptr[offset + 2] / 255.0f; // Red channel
                        input[0, 1, y, x] = ptr[offset + 1] / 255.0f; // Green channel
                        input[0, 2, y, x] = ptr[offset] / 255.0f;     // Blue channel
                    }
                }
            }


            originalImage.UnlockBits(bitmapData);


            var inputs = new List<Microsoft.ML.OnnxRuntime.NamedOnnxValue>
            {
                Microsoft.ML.OnnxRuntime.NamedOnnxValue.CreateFromTensor("images", input)
            };

            var session = new Microsoft.ML.OnnxRuntime.InferenceSession(this.txtONNXFile.Text, sessionOptions);
            Microsoft.ML.OnnxRuntime.IDisposableReadOnlyCollection<Microsoft.ML.OnnxRuntime.DisposableNamedOnnxValue> results = session.Run(inputs);

and it has worked very well so far, without any ML.NET problems, but I will try your solution and see whether it can solve the original issue.

from bitmaponnxprediction.

eNeRGy164 avatar eNeRGy164 commented on September 26, 2024

Updated the code. Thanks @MrBean2016

from bitmaponnxprediction.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.