Beispiel #1
0
        private void ThrowWrongInputType()
        {
            var tuple     = OpenSessionSqueezeNet();
            var session   = tuple.Item1;
            var inputData = tuple.Item2;
            var inputMeta = session.InputMetadata;
            var container = new List <NamedOnnxValue>();

            int[] inputDataInt = inputData.Select(x => (int)x).ToArray();
            var   tensor       = new DenseTensor <int>(inputDataInt, inputMeta["data_0"].Dimensions);

            container.Add(NamedOnnxValue.CreateFromTensor <int>("data_0", tensor));
            var ex = Assert.Throws <OnnxRuntimeException>(() => session.Run(container));

            Assert.Equal("[ErrorCode:InvalidArgument] Unexpected input data type. Actual: (class onnxruntime::NonOnnxType<int>) , expected: (class onnxruntime::NonOnnxType<float>)", ex.Message);
            session.Dispose();
        }
Beispiel #2
0
    private float[] InferenceOnnx(float[] input)
    {
        var inputName   = session.InputMetadata.First().Key;
        var inputDim    = session.InputMetadata.First().Value.Dimensions;
        var inputTensor = new DenseTensor <float>(new System.Memory <float>(input), inputDim);

        // OnnxRuntimeでの入力形式であるNamedOnnxValueを作成する
        var inputOnnxValues = new List <NamedOnnxValue> {
            NamedOnnxValue.CreateFromTensor(inputName, inputTensor)
        };

        // 推論を実行
        var results = session.Run(inputOnnxValues);
        var scores  = results.First().AsTensor <float>().ToArray();

        return(scores);
    }
Beispiel #3
0
        private void CanRunInferenceOnAModel()
        {
            string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "squeezenet.onnx");

            using (var session = new InferenceSession(modelPath))
            {
                var inputMeta = session.InputMetadata;
                var container = new List <NamedOnnxValue>();

                float[] inputData = LoadTensorFromFile(@"bench.in"); // this is the data for only one input tensor for this model

                foreach (var name in inputMeta.Keys)
                {
                    Assert.Equal(typeof(float), inputMeta[name].ElementType);
                    Assert.True(inputMeta[name].IsTensor);
                    var tensor = new DenseTensor <float>(inputData, inputMeta[name].Dimensions);
                    container.Add(NamedOnnxValue.CreateFromTensor <float>(name, tensor));
                }

                // Run the inference
                var results = session.Run(container);  // results is an IReadOnlyList<NamedOnnxValue> container

                Assert.Equal(1, results.Count);

                float[] expectedOutput = LoadTensorFromFile(@"bench.expected_out");
                // validate the results
                foreach (var r in results)
                {
                    Assert.Equal("softmaxout_1", r.Name);

                    var   resultTensor       = r.AsTensor <float>();
                    int[] expectedDimensions = { 1, 1000, 1, 1 };  // hardcoded for now for the test data
                    Assert.Equal(expectedDimensions.Length, resultTensor.Rank);

                    var resultDimensions = resultTensor.Dimensions;
                    for (int i = 0; i < expectedDimensions.Length; i++)
                    {
                        Assert.Equal(expectedDimensions[i], resultDimensions[i]);
                    }

                    var resultArray = r.AsTensor <float>().ToArray();
                    Assert.Equal(expectedOutput.Length, resultArray.Length);
                    Assert.Equal(expectedOutput, resultArray, new floatComparer());
                }
            }
        }
Beispiel #4
0
        private void TestModelInputFLOAT16()
        {
            // model takes 1x5 input of fixed type, echoes back
            string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "test_types_FLOAT16.pb");

            using (var session = new InferenceSession(modelPath))
            {
                var container = new List <NamedOnnxValue>();
                var tensorIn  = new DenseTensor <float>(new float[] { 1.0f, 2.0f, -3.0f, float.MinValue, float.MaxValue }, new int[] { 1, 5 });
                var nov       = NamedOnnxValue.CreateFromTensor("input", tensorIn);
                container.Add(nov);
                using (var res = session.Run(container))
                {
                    var tensorOut = res.First().AsTensor <float>();
                    Assert.True(tensorOut.SequenceEqual(tensorIn));
                }
            }
        }
Beispiel #5
0
        public static List <NamedOnnxValue> StringToEmbeddedTensor(string sentence)
        {
            var     container = new List <NamedOnnxValue>();
            NDArray features  = np.zeros((256, 128));

            for (int i = 0; i < sentence.Length; i++)
            {
                int idx = Char2Index(sentence[i]);
                features[string.Format(":,{0}", i)] = idx != -1 ? char_embedding[string.Format("{0}", idx)] : np.random.stardard_normal(256);
            }
            features = features.astype(np.float32);
            features = features.flatten();
            var tensor    = new DenseTensor <float>(features.ToArray <float>(), new int[] { 1, 256, 128 });
            var onnxvalue = NamedOnnxValue.CreateFromTensor <float>("input", tensor);

            container.Add(onnxvalue);
            return(container);
        }
        public JsonResult predictCNN([FromBody] List <byte> imageBytes)
        {
            float[]          floatArray       = imageBytes.Select(i => Convert.ToSingle(i / 255.0)).ToArray();
            var              matrix           = floatArray.ToTensor().Reshape(new[] { 28, 28 });
            InferenceSession inferenceSession = _inferenceSessions["cnn"];
            var              tensor           = new DenseTensor <float>(floatArray, inferenceSession.InputMetadata["Input3"].Dimensions);
            var              results          = inferenceSession.Run(new List <NamedOnnxValue> {
                NamedOnnxValue.CreateFromTensor("Input3", tensor)
            }).ToArray();
            var weights = results[0].AsTensor <float>().ToList();
            var probs   = weights.Select(x => x + Math.Abs(weights.Min()));

            probs = probs.Select(x => x / probs.Sum()).ToArray();
            var pred          = probs.Select((n, i) => (Number: n, Index: i)).Max().Index;
            var WrappedReturn = new { prediction = pred, probabilities = probs };

            return(Json(WrappedReturn));
        }
Beispiel #7
0
        protected override void OnPlanning(GraphPlanContext context)
        {
            var graph = context.TFGraph;

            var dummyBoxes = new DenseTensor <float>(Boxes.Dimensions);
            var variances  = new DenseTensor <float>(VariancesOutput.Dimensions);

            for (int i = 0; i < variances.Length / 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    variances[i * 4 + j] = Variances[j];
                }
            }

            context.TFOutputs[Boxes]           = graph.Const(dummyBoxes.ToTFTensor());
            context.TFOutputs[VariancesOutput] = graph.Const(variances.ToTFTensor());
        }
Beispiel #8
0
        private void TestModelInputSTRING()
        {
            // model takes 1x5 input of fixed type, echoes back
            string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "test_types_STRING.onnx");

            using (var session = new InferenceSession(modelPath))
            {
                var container = new List <NamedOnnxValue>();
                var tensorIn  = new DenseTensor <string>(new string[] { "a", "c", "d", "z", "f" }, new int[] { 1, 5 });
                var nov       = NamedOnnxValue.CreateFromTensor("input", tensorIn);
                container.Add(nov);
                using (var res = session.Run(container))
                {
                    var tensorOut = res.First().AsTensor <string>();
                    Assert.True(tensorOut.SequenceEqual(tensorIn));
                }
            }
        }
Beispiel #9
0
        public string ProcessImage(string path)
        {
            Image image = Image.FromStream(new MemoryStream(Convert.FromBase64String(path)));

            const int TargetWidth  = 224;
            const int TargetHeight = 224;

            var bitmap = ResizeImage(image, TargetWidth, TargetHeight);

            // Перевод пикселов в тензор и нормализация
            //var input = new Tensor<float>();
            var input  = new DenseTensor <float>(new[] { 1, 3, TargetHeight, TargetWidth });
            var mean   = new[] { 0.485f, 0.456f, 0.406f };
            var stddev = new[] { 0.229f, 0.224f, 0.225f };

            for (int y = 0; y < TargetHeight; y++)
            {
                for (int x = 0; x < TargetWidth; x++)
                {
                    var color = bitmap.GetPixel(x, y);
                    input[0, 0, y, x] = ((color.R / 255f) - mean[0]) / stddev[0];
                    input[0, 1, y, x] = ((color.G / 255f) - mean[1]) / stddev[1];
                    input[0, 2, y, x] = ((color.B / 255f) - mean[2]) / stddev[2];
                }
            }

            // Подготавливаем входные данные нейросети. Имя input задано в файле модели
            var inputs = new List <NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("data", input)
            };

            //Console.WriteLine("Predicting contents of image...");
            using IDisposableReadOnlyCollection <DisposableNamedOnnxValue> results = session.Run(inputs);

            // Получаем 1000 выходов и считаем для них softmax
            var output  = results.First().AsEnumerable <float>().ToArray();
            var sum     = output.Sum(x => (float)Math.Exp(x));
            var softmax = output.Select(x => (float)Math.Exp(x) / sum);

            return(softmax
                   .Select((x, i) => new { Label = classLabels[i], Confidence = x })
                   .OrderByDescending(x => x.Confidence).FirstOrDefault().Label);
        }
Beispiel #10
0
        private Tensor <float> GetTensorInputFromImg(Mat srcMat)
        {
            float DIVIDE_VAL = 127.5f;

            double scale    = srcMat.Height * 1.0 / 32;
            int    dstWidth = (int)(srcMat.Width / scale);

            Mat imgResized = new Mat(32, dstWidth, DepthType.Cv32F, 3);

            CvInvoke.Resize(srcMat, imgResized, new Size(dstWidth, 32));

            Tensor <float> inputs = new DenseTensor <float>(new[] { 1, 3, imgResized.Height, imgResized.Width });

            Image <Rgb, byte> outImg = imgResized.ToImage <Rgb, byte>();
            int rows = outImg.Rows;
            int cols = outImg.Cols;

            byte[,,] data = outImg.Data;

            try
            {
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < cols; j++)
                    {
                        float rr = (float)((data[i, j, 0] - DIVIDE_VAL) / DIVIDE_VAL);
                        float gg = (float)((data[i, j, 1] - DIVIDE_VAL) / DIVIDE_VAL);
                        float bb = (float)((data[i, j, 2] - DIVIDE_VAL) / DIVIDE_VAL);

                        inputs[0, 0, i, j] = rr;
                        inputs[0, 1, i, j] = gg;
                        inputs[0, 2, i, j] = bb;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write("GetTensorInputFromImg, " + ex.Message);
            }

            outImg.Dispose();

            return(inputs);
        }
Beispiel #11
0
        private static Prediction OneImgRecognition(string path)
        {
            using var image = Image.Load <Rgb24>(path);
            const int TargetWidth  = 28;
            const int TargetHeight = 28;

            image.Mutate(x =>
            {
                x.Resize(new ResizeOptions
                {
                    Size = new Size(TargetWidth, TargetHeight),
                    Mode = ResizeMode.Crop,
                });
            });

            var input  = new DenseTensor <float>(new[] { 1, 1, TargetHeight, TargetWidth });
            var mean   = new[] { 0.485f, 0.456f, 0.406f };
            var stddev = new[] { 0.229f, 0.224f, 0.225f };

            for (int y = 0; y < TargetHeight; y++)
            {
                Span <Rgb24> pixelSpan = image.GetPixelRowSpan(y);
                for (int x = 0; x < TargetWidth; x++)
                {
                    input[0, 0, y, x] = ((pixelSpan[x].R / 255f) - mean[0]) / stddev[0];
                }
            }

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

            using IDisposableReadOnlyCollection <DisposableNamedOnnxValue> results = session.Run(inputs);

            var output  = results.First().AsEnumerable <float>().ToArray();
            var sum     = output.Sum(x => (float)Math.Exp(x));
            var softmax = output.Select(x => (float)Math.Exp(x) / sum);

            float confidence = softmax.Max();
            int   label      = softmax.ToList().IndexOf(confidence);

            return(new Prediction(path, label, confidence));
        }
        /// <summary>
        /// Solves the system of linear equations AX = B for X, where A, B, and X are general matrices.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static DenseTensor <double> Solve(DenseTensor <double> a, DenseTensor <double> b)
        {
            if (a.Rank != 2)
            {
                throw new ArgumentException("a must be a square matrix", nameof(a));
            }
            if (a.Dimensions[0] != a.Dimensions[1])
            {
                throw new ArgumentException("a must be a square matrix", nameof(a));
            }
            if (b.Rank != 2)
            {
                throw new ArgumentException("b must be a matrix", nameof(b));
            }
            if (a.Dimensions[0] != b.Dimensions[0])
            {
                throw new ArgumentException("The number of rows in b must match the number of rows in a", nameof(b));
            }

            // need to clone the inputs because LAPack will mutate the values
            var aClone = (DenseTensor <double>)a.Clone();
            var bClone = (DenseTensor <double>)b.Clone();

            unsafe
            {
                Span <int> pivotIntegers = stackalloc int[a.Dimensions[1]];
                fixed(double *aPtr = &aClone.Buffer.Span.DangerousGetPinnableReference())
                fixed(double *bPtr = &bClone.Buffer.Span.DangerousGetPinnableReference())
                fixed(int *ipiv    = &pivotIntegers.DangerousGetPinnableReference())
                {
                    LAPACKE_dgesv(
                        a.IsReversedStride ? LAPACK_COL_MAJOR : LAPACK_ROW_MAJOR,
                        a.Dimensions[0],
                        b.Dimensions[1],
                        aPtr,
                        a.Dimensions[1],
                        ipiv,
                        bPtr,
                        b.Dimensions[1]);
                }
            }

            return(bClone);
        }
Beispiel #13
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());
        }
Beispiel #14
0
        public static (DenseTensor <double> X, DenseTensor <double> Y) ToExamples(this IEnumerable <IEnumerable <double> > matrix)
        {
            // materialize
            double[][] x = (from v in matrix select v.ToArray()).ToArray();

            // determine matrix
            // size and type
            var(n, d) = FindDimensions(x, true);  // clip last col
            var m = new DenseTensor <double>(new[] { n, d });

            // fill 'er up!
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < d; j++)
                {
                    if (j >= x[i].Length)  // over bound limits
                    {
                        m[i, j] = 0;       // pad overflow to 0
                    }
                    else
                    {
                        m[i, j] = x[i][j];
                    }
                }
            }

            // fill up vector
            DenseTensor <double> y = new DenseTensor <double>(n);

            for (int i = 0; i < n; i++)
            {
                if (d >= x[i].Length)
                {
                    y[i] = 0;              // pad overflow to 0
                }
                else
                {
                    y[i] = x[i][d];
                }
            }

            return(m, y);
        }
Beispiel #15
0
        private void ThrowWrongInputType()
        {
            var tuple     = OpenSessionSqueezeNet();
            var session   = tuple.Item1;
            var inputData = tuple.Item2;
            var inputMeta = session.InputMetadata;
            var container = new List <NamedOnnxValue>();

            int[] inputDataInt = inputData.Select(x => (int)x).ToArray();
            var   tensor       = new DenseTensor <int>(inputDataInt, inputMeta["data_0"].Dimensions);

            container.Add(NamedOnnxValue.CreateFromTensor <int>("data_0", tensor));
            var ex  = Assert.Throws <OnnxRuntimeException>(() => session.Run(container));
            var msg = ex.ToString().Substring(0, 101);

            // TODO: message is diff in LInux. Use substring match
            Assert.Equal("Microsoft.ML.OnnxRuntime.OnnxRuntimeException: [ErrorCode:InvalidArgument] Unexpected input data type", msg);
            session.Dispose();
        }
Beispiel #16
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string review = req.Query["review"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            review = review ?? data?.review;

            var models = new Dictionary <string, string>();

            models.Add("points", GetFileAndPathFromStorage(context, log, "model327", "pipeline_points_range.onnx"));
            models.Add("price", GetFileAndPathFromStorage(context, log, "model327", "pipeline_price_range.onnx"));
            models.Add("variety", GetFileAndPathFromStorage(context, log, "model327", "pipeline_variety.onnx"));

            var inputTensor = new DenseTensor <string>(new string[] { review }, new int[] { 1, 1 });
            //create input data for session.
            var input = new List <NamedOnnxValue> {
                NamedOnnxValue.CreateFromTensor <string>("input", inputTensor)
            };

            //create now object points: result
            var inferenceResults = new Dictionary <string, IDictionary <string, float> >();

            foreach (var model in models)
            {
                log.LogInformation($"Start inference session for {model.Key}");
                var session         = new InferenceSession(model.Value);
                var output          = session.Run(input).ToList().Last().AsEnumerable <NamedOnnxValue>();
                var inferenceResult = output.First().AsDictionary <string, float>();
                var topThreeResult  = inferenceResult.OrderByDescending(dict => dict.Value).Take(3)
                                      .ToDictionary(pair => pair.Key, pair => pair.Value);
                log.LogInformation($"Top five results for {model.Key} {topThreeResult}");
                inferenceResults.Add(model.Key, topThreeResult);
                Console.Write(inferenceResult);
            }

            return(new JsonResult(inferenceResults));
        }
Beispiel #17
0
        /// <summary>
        /// Parses net output (detect) to predictions.
        /// </summary>
        private List <YoloPrediction> ParseDetect(DenseTensor <float> output, Image image)
        {
            var result = new List <YoloPrediction>();

            var(xGain, yGain) = (_model.Width / (float)image.Width, _model.Height / (float)image.Height);

            for (int i = 0; i < output.Length / _model.Dimensions; i++) // iterate tensor
            {
                if (output[0, i, 4] <= _model.Confidence)
                {
                    continue;
                }

                for (int j = 5; j < _model.Dimensions; j++)              // compute mul conf
                {
                    output[0, i, j] = output[0, i, j] * output[0, i, 4]; // conf = obj_conf * cls_conf
                }

                for (int k = 5; k < _model.Dimensions; k++)
                {
                    if (output[0, i, k] <= _model.MulConfidence)
                    {
                        continue;
                    }

                    var xMin = (output[0, i, 0] - output[0, i, 2] / 2) / xGain; // top left x
                    var yMin = (output[0, i, 1] - output[0, i, 3] / 2) / yGain; // top left y
                    var xMax = (output[0, i, 0] + output[0, i, 2] / 2) / xGain; // bottom right x
                    var yMax = (output[0, i, 1] + output[0, i, 3] / 2) / yGain; // bottom right y

                    YoloLabel label = _model.Labels[k - 5];

                    var prediction = new YoloPrediction(label, output[0, i, k])
                    {
                        Rectangle = new RectangleF(xMin, yMin, xMax - xMin, yMax - yMin)
                    };

                    result.Add(prediction);
                }
            }

            return(result);
        }
Beispiel #18
0
        private void ThrowWrongDimensions()
        {
            var tuple     = OpenSessionSqueezeNet();
            var session   = tuple.Item1;
            var inputMeta = session.InputMetadata;
            var container = new List <NamedOnnxValue>();
            var inputData = new float[] { 0.1f, 0.2f, 0.3f };
            var tensor    = new DenseTensor <float>(inputData, new int[] { 1, 3 });

            container.Add(NamedOnnxValue.CreateFromTensor <float>("data_0", tensor));
            var ex = Assert.Throws <OnnxRuntimeException>(() => session.Run(container));

            Assert.True(
                !string.IsNullOrEmpty(ex.Message) &&
                ex.Message.StartsWith("[ErrorCode:Fail]") &&
                ex.Message.Contains("X num_dims does not match W num_dims. X: {1,3} W: {64,3,3,3}")
                );
            session.Dispose();
        }
Beispiel #19
0
        public void TestSetRowArgbArray()
        {
            using (var bitmap = SurfaceExtensionsTest.LoadResource("style.png"))
            {
                var line     = new Rectangle(13, 27, 64, 1);
                var data     = bitmap.LockBits(line, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                var expected = new byte[line.Width * 4];
                _ = data.GetRowArgb(expected, 0, 0);
                bitmap.UnlockBits(data);

                var tensor = new DenseTensor <float>(new Rectangle(Point.Empty, bitmap.Size).ToNHWC());
                tensor.SetRowArgb(expected, line.X, line.Y);

                var actual = new byte[expected.Length];
                _ = tensor.GetRowArgb(actual, line.X, line.Y);

                CollectionAssert.AreEqual(expected, actual);
            }
        }
Beispiel #20
0
        public int LoadAndPredict(Image <Rgb24> image)
        {
            // using var image =  Image.Load<Rgb24>(img_name);

            const int TargetWidth  = 28;
            const int TargetHeight = 28;

            image.Mutate(x =>
            {
                x.Resize(new ResizeOptions {
                    Size = new Size(TargetWidth, TargetHeight),
                    Mode = ResizeMode.Crop
                }).Grayscale();
            });

            var input = new DenseTensor <float>(new[] { 1, 1, TargetHeight, TargetWidth });

            for (int y = 0; y < TargetHeight; y++)
            {
                Span <Rgb24> pixelSpan = image.GetPixelRowSpan(y);

                for (int x = 0; x < TargetWidth; x++)
                {
                    input[0, 0, y, x] = pixelSpan[x].R / 255.0f;
                }
            }

            using var session = new InferenceSession(model_name);
            string input_name = session.InputMetadata.Keys.First();
            var    inputs     = new List <NamedOnnxValue> {
                NamedOnnxValue.CreateFromTensor(input_name, input)
            };

            using IDisposableReadOnlyCollection <DisposableNamedOnnxValue> results = session.Run(inputs);

            var output  = results.First().AsEnumerable <float>().ToArray();
            var sum     = output.Sum(x => (float)Math.Exp(x));
            var softmax = output.Select(x => (float)Math.Exp(x) / sum);
            var query   = softmax.Select((x, i) => new { Label = classLabels[i], Confidence = x })
                          .OrderByDescending(x => x.Confidence);

            return(Int32.Parse(query.First().Label));
        }
Beispiel #21
0
        static Tuple <InferenceSession, float[], DenseTensor <float>, float[]> OpenSessionSqueezeNet(int?cudaDeviceId = null)
        {
            string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "squeezenet.onnx");
            var    option    = new SessionOptions();

#if USE_CUDA
            if (cudaDeviceId.HasValue)
            {
                option = SessionOptions.MakeSessionOptionWithCudaProvider(cudaDeviceId.Value);
            }
#endif
            var session = (cudaDeviceId.HasValue)
                ? new InferenceSession(modelPath, option)
                : new InferenceSession(modelPath);
            float[] inputData      = LoadTensorFromFile(@"bench.in");
            float[] expectedOutput = LoadTensorFromFile(@"bench.expected_out");
            var     inputMeta      = session.InputMetadata;
            var     tensor         = new DenseTensor <float>(inputData, inputMeta["data_0"].Dimensions);
            return(new Tuple <InferenceSession, float[], DenseTensor <float>, float[]>(session, inputData, tensor, expectedOutput));
        }
Beispiel #22
0
        private PredictionResult Predict(DenseTensor <float> input, string single_image_path)
        {
            var inputs = new List <NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor("data", input)
            };

            using IDisposableReadOnlyCollection <DisposableNamedOnnxValue> results = session.Run(inputs);

            // Получаем 1000 выходов и считаем для них softmax
            var output  = results.First().AsEnumerable <float>().ToArray();
            var sum     = output.Sum(x => (float)Math.Exp(x));
            var softmax = output.Select(x => (float)Math.Exp(x) / sum);

            var confidence = softmax.Max();
            var class_idx  = softmax.ToList().IndexOf(confidence);


            return(new PredictionResult(single_image_path, LabelMap.classLabels[class_idx], confidence));
        }
        public static Tensor <float> ConvertBitmapToFloatTensor(Bitmap bitmap)
        {
            // Create a tensor with the appropiate dimensions
            Tensor <float> tensor = new DenseTensor <float>(new[] { 1, 3, bitmap.Height, bitmap.Width });

            // Iterate over the bitmap and copy each pixel to the tensor
            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    Color color = bitmap.GetPixel(x, y);

                    tensor[0, 0, y, x] = color.R / (float)255.0;
                    tensor[0, 1, y, x] = color.G / (float)255.0;
                    tensor[0, 2, y, x] = color.B / (float)255.0;
                }
            }

            return(tensor);
        }
Beispiel #24
0
        public void TestSetRowArgbArraySegment()
        {
            using (var bitmap = SurfaceExtensionsTest.LoadResource("style.png"))
            {
                var line     = new Rectangle(13, 27, 64, 1);
                var data     = bitmap.LockBits(line, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                var expected = new byte[data.Stride + 32];
                var memory   = new ArraySegment <byte>(expected, 8, line.Width * 4);
                _ = data.GetRowArgb(memory, 0, 0);
                bitmap.UnlockBits(data);

                var tensor = new DenseTensor <float>(new Rectangle(Point.Empty, bitmap.Size).ToNHWC());
                tensor.SetRowArgb(memory, line.X, line.Y);

                var actual = new byte[memory.Count];
                _ = tensor.GetRowArgb(actual, line.X, line.Y);

                CollectionAssert.AreEqual(expected.Skip(memory.Offset).Take(memory.Count).ToArray(), actual);
            }
        }
Beispiel #25
0
        private void TestPreTrainedModelsOpset7And8()
        {
            var opsets = new[] { "opset7", "opset8" };

            foreach (var opset in opsets)
            {
                var modelRoot = new DirectoryInfo(opset);
                foreach (var model in modelRoot.EnumerateDirectories())
                {
                    // TODO: dims contains 'None'. Session throws error.
                    if (model.ToString() == "test_tiny_yolov2")
                    {
                        continue;
                    }
                    try
                    {
                        //TODO: sometimes, the file name is not 'model.onnx'
                        var session    = new InferenceSession($"{opset}\\{model}\\model.onnx");
                        var inMeta     = session.InputMetadata;
                        var innodepair = inMeta.First();
                        var innodename = innodepair.Key;
                        var innodedims = innodepair.Value.Dimensions;
                        var dataIn     = LoadTensorFromFilePb($"{opset}\\{model}\\test_data_set_0\\input_0.pb");
                        var dataOut    = LoadTensorFromFilePb($"{opset}\\{model}\\test_data_set_0\\output_0.pb");
                        var tensorIn   = new DenseTensor <float>(dataIn, innodedims);
                        var nov        = new List <NamedOnnxValue>();
                        nov.Add(NamedOnnxValue.CreateFromTensor <float>(innodename, tensorIn));
                        var resnov = session.Run(nov);
                        var res    = resnov.ToArray()[0].AsTensor <float>().ToArray <float>();
                        Assert.Equal(res, dataOut, new floatComparer());
                        session.Dispose();
                    }
                    catch (Exception ex)
                    {
                        var msg = $"Opset {opset}: Model {model}: error = {ex.Message}";
                        continue; //TODO: fix it
                        //throw new Exception(msg);
                    }
                } //model
            }     //opset
        }
Beispiel #26
0
            IAsyncEnumerable<Tensor<float>> GetBatchesAsync()
        {
            var dimensions = new[] { _batchSize }.Concat(_dimensions).ToArray();
            var oneSize = Dimensions.GetSize();

            async Task<byte[]> ReadAllBytesAsync(string path)
            {
                using (var fs = File.OpenRead(path))
                {
                    var buffer = new byte[(int)fs.Length];
                    await fs.ReadAsync(buffer, 0, buffer.Length);
                    return buffer;
                }
            }

#if NET471
            return new AsyncEnumerable<Tensor<float>>(async yield =>
            {
#endif
            for (int i = 0; i < _fileNames.Count / _batchSize; i++)
            {
                var tensor = new DenseTensor<float>(dimensions);
                var sources = await Task.WhenAll(from j in Enumerable.Range(i * _batchSize, _batchSize)
                                                 select ReadAllBytesAsync(_fileNames[j]));
                Parallel.For(0, _batchSize, j =>
                {
                    var buffer = tensor.Buffer.Slice(j * oneSize);
                    Process(sources[j], buffer.Span);
                    Postprocess(buffer.Span, PostprocessMethod);
                });

#if NET471
                    await yield.ReturnAsync(tensor);
#else
                yield return tensor;
#endif
            }
#if NET471
            });
#endif
        }
Beispiel #27
0
        static void createSession(int imageIndex)
        {
            string modelPath = Directory.GetCurrentDirectory() + @"/pytorch_mnist.onnx";

            // Optional : Create session options and set the graph optimization level for the session
            //SessionOptions options = new SessionOptions();
            //options.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_EXTENDED;
            //using (var session = new InferenceSession(modelPath, options))

            using (var session = new InferenceSession(modelPath))
            {
                //float[] inputData = LoadTensorFromFile(@"bench.in"); // this is the data for only one input tensor for this model
                Utilities.LoadTensorData();
                float[] inputData = Utilities.ImageData[imageIndex];
                string  label     = Utilities.ImageLabels[imageIndex];
                Console.WriteLine("Selected image is the number: " + label);

                var inputMeta = session.InputMetadata;
                var container = new List <NamedOnnxValue>();
                //PrintInputMetadata(inputMeta);

                foreach (var name in inputMeta.Keys)
                {
                    var tensor = new DenseTensor <float>(inputData, inputMeta[name].Dimensions);
                    container.Add(NamedOnnxValue.CreateFromTensor <float>(name, tensor));
                }

                // Run the inference
                using (var results = session.Run(container))  // results is an IDisposableReadOnlyCollection<DisposableNamedOnnxValue> container
                {
                    // Get the results
                    foreach (var r in results)
                    {
                        Console.WriteLine("Output Name: {0}", r.Name);
                        int prediction = MaxProbability(r.AsTensor <float>());
                        Console.WriteLine("Prediction: " + prediction.ToString());
                        //Console.WriteLine(r.AsTensor<float>().GetArrayString());
                    }
                }
            }
        }
Beispiel #28
0
        public ResultClassification PredictModel(string imageFilePath)
        {
            DenseTensor <float> TensorImage = OnnxClassifier.PreprocImage(imageFilePath);

            var inputs = new List <NamedOnnxValue>
            {
                NamedOnnxValue.CreateFromTensor(session.InputMetadata.Keys.First(), TensorImage)
            };

            using IDisposableReadOnlyCollection <DisposableNamedOnnxValue> results = session.Run(inputs);

            var   output = results.First().AsEnumerable <float>().ToArray();
            float sum    = output.Sum(x => (float)Math.Exp(x));

            var softmax = output.Select(x => (float)Math.Exp(x) / sum).ToList();

            string cl = LabelMap.Labels[softmax.IndexOf(softmax.Max())];
            ResultClassification result = new ResultClassification(imageFilePath, cl, softmax.Max());

            return(result);
        }
Beispiel #29
0
        private List <NamedOnnxValue> BuildInputContainers(ICollection <float[]> floatTensors)
        {
            var data = floatTensors.ToArray();

            if (data.Length != InputShapes.Keys.Count)
            {
                throw new Exception($"Expecting data for {InputShapes.Keys.Count} shapes, received {data.Length}.");
            }

            var container  = new List <NamedOnnxValue>();
            var inputIndex = 0;

            foreach (var input in InputShapes.Keys)
            {
                var tensor = new DenseTensor <float>(data[inputIndex], InputShapes[input]);
                container.Add(NamedOnnxValue.CreateFromTensor <float>(input, tensor));
                inputIndex++;
            }

            return(container);
        }
Beispiel #30
0
        public static void RunSample()
        {
            Console.WriteLine($"*** {nameof(WrapNativeMemory)} ***");

            Console.WriteLine($"Allocated as part of the operation");
            // in this case we pretend that we cannot know the size of the tensor up front and require the native library to allocate it and tell us the size.
            var multTable = GetMultiplicationTable(5);

            Console.WriteLine("Multiplication table:");
            Console.WriteLine(multTable.GetArrayString());

            Console.WriteLine("Sums:");
            for (int row = 0; row < multTable.Dimensions[0]; row++)
            {
                Console.WriteLine(GetRowSum(multTable, row));
            }

            // the memory will be freed when the GC decides to collect the Tensor, we can *try* to force it but no garuntee.
            multTable = null;
            Console.WriteLine("Forcing GC.");
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Console.WriteLine($"Allocated prior to the operation");
            // in this case we'll assume we can know the size of the buffer up front and manage the lifetime explicitly
            using (var nativeMemory = NativeMemory <double> .Allocate(5 * 5))
            {
                Span <int> dimensions = stackalloc int[2];
                dimensions[0] = dimensions[1] = 5;
                var tensor = new DenseTensor <double>(nativeMemory.Memory, dimensions);

                GetMultiplicationTablePreallocated(5, tensor);

                Console.WriteLine("Sums:");
                for (int row = 0; row < tensor.Dimensions[0]; row++)
                {
                    Console.WriteLine(GetRowSum(tensor, row));
                }
            }
        }