예제 #1
0
        public static IList <YoloBoundingBox> ObtainBoundingBoxes(string base64Image)
        {
            var            opencvImage = Image.ConvertFromBase64ToMat(base64Image);
            MatOfByte3     mat3        = new MatOfByte3(opencvImage);
            var            indexer     = mat3.GetIndexer();
            Tensor <float> imageData   = new DenseTensor <float>(new[] { 3, opencvImage.Width, opencvImage.Height });

            for (int y = 0; y < opencvImage.Height; y++)
            {
                for (int x = 0; x < opencvImage.Width; x++)
                {
                    Vec3b color = indexer[y, x];
                    imageData[0, y, x] = (float)color.Item2;
                    imageData[1, y, x] = (float)color.Item1;
                    imageData[2, y, x] = (float)color.Item0;
                }
            }

            var yoloParser      = new YoloParser();
            var resultTransform = imageData.Reshape(new ReadOnlySpan <int>(new[] { 3 * 416 * 416 }));
            var yoloModel       = new YoloModel();
            var results         = yoloModel.Evaluate(new[] { resultTransform });
            var boundingBoxes   = yoloParser.ParseOutputs(results.First().ToArray());

            return(boundingBoxes);
        }
예제 #2
0
        public static IList <BoundingBox> ObtainBoundingBoxes(string base64Image)
        {
            var opencvImage = Image.ConvertFromBase64ToMat(base64Image);
            var tensor      = new DenseTensor <float>(new int[] { channels, opencvImage.Height, opencvImage.Width });

            using (var mat = new MatOfByte3(opencvImage))
            {
                var indexer = mat.GetIndexer();
                for (int y = 0; y < opencvImage.Height; y++)
                {
                    for (int x = 0; x < opencvImage.Width; x++)
                    {
                        Vec3b color = indexer[y, x];
                        tensor[0, y, x] = (float)color.Item2;
                        tensor[1, y, x] = (float)color.Item1;
                        tensor[2, y, x] = (float)color.Item0;
                    }
                }
            }

            var transform  = tensor.Reshape(new ReadOnlySpan <int>(new[] { channels *opencvImage.Height *opencvImage.Width }));
            var yoloParser = new YoloOutputParser();
            var yoloModel  = YoloModel.Instance;
            var results    = yoloModel.Evaluate(new[] { transform });

            return(yoloParser.ParseOutputs(results.First().ToArray()));
        }
예제 #3
0
        static void Main(string[] args)
        {
            string path = System.AppContext.BaseDirectory + "myModel.onnx";

            Console.WriteLine(path);
            Tensor <float> input  = new DenseTensor <float>(new[] { 32, 32 });
            Tensor <float> output = new DenseTensor <float>(new[] { 1, 4, 4 });

            for (int y = 0; y < 32; y++)
            {
                for (int x = 0; x < 32; x++)
                {
                    input[y, x] = (float)Math.E;
                }
            }

            //Console.WriteLine(input.GetArrayString());

            // Setup inputs
            List <NamedOnnxValue> inputs = new List <NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("Input", input.Reshape(new [] { 1, 32, 32 }).ToDenseTensor()),
            };
            // Setup outputs
            List <NamedOnnxValue> outputs = new List <NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("Output", output),
            };

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            // Run inference
            InferenceSession session = new InferenceSession(path);

            session.Run(inputs, outputs);
            output = outputs[0].AsTensor <float>();
            Console.WriteLine(output.Reshape(new[] { 4, 4 }).ToDenseTensor().GetArrayString());

            stopWatch.Stop();

            Console.WriteLine(stopWatch.ElapsedMilliseconds.ToString());
        }
예제 #4
0
파일: Program.cs 프로젝트: volody/onnx-demo
        static Score PredictLocal(InferenceSession session, float[] digit)
        {
            var            now = DateTime.Now;
            Tensor <float> x   = new DenseTensor <float>(digit.Length);

            for (int i = 0; i <= digit.Length - 1; i++)
            {
                x[i] = digit[i] / 255.0f;
            }

            int[] dims = { 1, 1, 28, 28 };  // hardcoded for now for the test data
            x = x.Reshape(dims);

            var input = new List <NamedOnnxValue>()
            {
                NamedOnnxValue.CreateFromTensor("Input3", x)
            };

            try
            {
                var prediction = session.Run(input).First().AsTensor <float>().ToArray();
                return(new Score()
                {
                    Status = $"Local Mode: {session}",
                    Empty = false,
                    Prediction = Array.IndexOf(prediction, prediction.Max()),
                    Scores = prediction.Select(i => System.Convert.ToDouble(i)).ToList(),
                    Time = (DateTime.Now - now).TotalSeconds
                });
            }
            catch (Exception e)
            {
                return(new Score()
                {
                    Status = e.Message
                });
            }
        }
예제 #5
0
        public static Tensor <T> Transpose <T>(this Tensor <T> tensor, ReadOnlySpan <int> axes)
        {
            int inputExtSize  = 4 - tensor.Rank;
            int outputExtSize = 4 - axes.Length;

            Span <int> extendedPerm = stackalloc int[4];

            for (int i = 0; i < outputExtSize; i++)
            {
                extendedPerm[i] = i;
            }
            for (int i = 0; i < axes.Length; i++)
            {
                extendedPerm[i + outputExtSize] = axes[i] + inputExtSize;
            }

            Span <int> outSizes = stackalloc int[4];
            Span <int> oldDims  = stackalloc int[4] {
                1, 1, 1, 1
            };

            for (int i = 4 - tensor.Dimensions.Length; i < 4; i++)
            {
                oldDims[i] = tensor.Dimensions[i - (4 - tensor.Dimensions.Length)];
            }
            for (int i = 0; i < 4; i++)
            {
                outSizes[i] = oldDims[extendedPerm[i]];
            }

            Span <int> outp           = stackalloc int[4];
            Span <int> inp            = stackalloc int[4];
            var        buffer         = new T[tensor.Length];
            var        destDimensions = new int[tensor.Dimensions.Length];

            for (int i = 0; i < axes.Length; i++)
            {
                destDimensions[i] = tensor.Dimensions[axes[i]];
            }
            var output = new DenseTensor <T>(buffer, outSizes);

            tensor = tensor.Reshape(oldDims);

            for (outp[3] = 0; outp[3] < outSizes[3]; outp[3]++)
            {
                inp[extendedPerm[3]] = outp[3];
                for (outp[2] = 0; outp[2] < outSizes[2]; outp[2]++)
                {
                    inp[extendedPerm[2]] = outp[2];
                    for (outp[1] = 0; outp[1] < outSizes[1]; outp[1]++)
                    {
                        inp[extendedPerm[1]] = outp[1];
                        for (outp[0] = 0; outp[0] < outSizes[0]; outp[0]++)
                        {
                            inp[extendedPerm[0]] = outp[0];
                            output[outp]         = tensor[inp];
                        }
                    }
                }
            }

            return(output.Reshape(destDimensions));
        }