Example #1
0
            public Detector()
            {
                var model = OnnxModel.FromFile("Models\\facial-lmk-detector.onnx");

                var modelOptions = (model as IServiceProvider).GetService(typeof(Microsoft.ML.OnnxRuntime.SessionOptions)) as Microsoft.ML.OnnxRuntime.SessionOptions;

                modelOptions.GraphOptimizationLevel = Microsoft.ML.OnnxRuntime.GraphOptimizationLevel.ORT_DISABLE_ALL;

                _Session = model.CreateSession();
            }
Example #2
0
        public Anime2SketchFilter()
        {
            var model = OnnxModel.FromFile("Models\\anime2sketch_512x512.onnx");

            var modelOptions = (model as IServiceProvider).GetService(typeof(Microsoft.ML.OnnxRuntime.SessionOptions)) as Microsoft.ML.OnnxRuntime.SessionOptions;

            modelOptions.GraphOptimizationLevel = Microsoft.ML.OnnxRuntime.GraphOptimizationLevel.ORT_DISABLE_ALL;

            _Session = model.CreateSession();
        }
Example #3
0
        public void TestResnet50(string imagePath, string expectedResult)
        {
            // https://onnxruntime.ai/docs/tutorials/resnet50_csharp.html

            var srcImage = MemoryBitmap.Load(imagePath);

            var model = OnnxModel.FromFile("Models\\resnet50-v2-7.onnx");

            // image normalization
            // https://github.com/onnx/models/tree/master/vision/classification/resnet#preprocessing
            var modelXform = MultiplyAdd
                             .CreateMul(0.229f, 0.224f, 0.225f)
                             .ConcatAdd(0.485f, 0.456f, 0.406f)
                             // .GetTransposedZYXW()
                             .GetInverse();

            var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>(modelXform);

            using (var session = model.CreateSession())
            {
                var workingTensor = session
                                    .GetInputTensor <float>(0)
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, imagePreprocessor);

                // run

                session.Inference();

                // get results

                var result = session
                             .GetOutputTensor <float>(0)
                             .AsSpanTensor2()
                             .GetSubTensor(0);

                result.ApplySoftMax();

                var scores = result.ToArray();

                var pairs = Labels.resnet50_v2_7_Labels
                            .Zip(scores, (Label, Score) => (Label, Score))
                            .OrderByDescending(item => item.Score)
                            .ToList();

                foreach (var pair in pairs)
                {
                    TestContext.WriteLine($"{pair.Label} = {pair.Score}");
                }

                Assert.AreEqual(expectedResult, pairs[0].Label);
            }
        }
Example #4
0
        public void TestYuNet(string imagePath)
        {
            // https://github.com/opencv/opencv_zoo/tree/master/models/face_detection_yunet
            // https://github.com/ShiqiYu/libfacedetection
            // https://github.com/Kazuhito00/YuNet-ONNX-TFLite-Sample/blob/main/yunet/yunet_onnx.py

            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);

            var pmodel            = OnnxModel.FromFile("Models\\face_detection_yunet_2021sep.onnx");
            var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>();

            using (var session = pmodel.CreateSession())
            {
                var workingTensor = session.UseInputTensor <float>(0, 1, 3, srcImage.Height, srcImage.Width)
                                    .VerifyName(n => n == "input")
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, imagePreprocessor);

                const int COUNT = 15040;

                var loc  = session.UseOutputTensor <float>(0, COUNT, 14).VerifyName(n => n == "loc");
                var conf = session.UseOutputTensor <float>(1, COUNT, 2).VerifyName(n => n == "conf");
                var iou  = session.UseOutputTensor <float>(2, COUNT, 1).VerifyName(n => n == "iou");

                // run

                session.Inference();

                // https://github.com/ShiqiYu/libfacedetection/blob/master/src/facedetectcnn.cpp#L731

                var r_loc  = loc.AsSpanTensor2().UpCast <YuNetEntry>().Span.ToArray();
                var r_conf = conf.AsSpanTensor2().UpCast <System.Numerics.Vector2>().Span.ToArray();
                var r_iou  = iou.AsSpanTensor2().UpCast <float>().Span;

                // softmax1vector2class(mbox_conf);
                // clamp1vector(mbox_iou);

                // SpanTensor.ApplySoftMax(r_conf);

                var sorted = r_conf
                             .Select(item => item.Y)
                             .Zip(r_loc)
                             .OrderByDescending(item => item.First)
                             .ToArray();

                var prior_variance = new [] { 0.1f, 0.1f, 0.2f, 0.2f };
            }
        }
Example #5
0
        public void TestMcnnFace(string imagePath)
        {
            // https://github.com/linxiaohui/mtcnn-opencv/tree/main/mtcnn_cv2

            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);

            var pmodel            = OnnxModel.FromFile("Models\\MCNN\\pnet.onnx");
            var imagePreprocessor = new ImageProcessor <Pixel.RGB96F>();

            using (var session = pmodel.CreateSession())
            {
                var workingTensor = session.UseInputTensor <float>(0, 1, srcImage.Height, srcImage.Width, 3)
                                    .VerifyName(n => n == "input_1")
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, imagePreprocessor);

                var conv2d  = session.UseOutputTensor <float>(0, 1, 251, 251, 4).VerifyName(n => n == "conv2d_4");
                var softmax = session.UseOutputTensor <float>(1, 1, 251, 251, 2).VerifyName(n => n == "softmax");

                // run

                session.Inference();

                // get results:

                var score = softmax
                            .AsSpanTensor4()
                            .GetSubTensor(0)
                            .UpCast <System.Numerics.Vector2>()
                            .Span
                            .ToArray();

                var cv2d = conv2d
                           .AsSpanTensor4()
                           .GetSubTensor(0)
                           .UpCast <System.Numerics.Vector4>()
                           .Span
                           .ToArray();

                var pairs = score
                            .Select(item => item.Y)
                            .Zip(cv2d.Select(xyxy => xyxy * new System.Numerics.Vector4(srcImage.Height, srcImage.Width, srcImage.Height, srcImage.Width)))
                            .OrderByDescending(item => item.First)
                            .ToArray();
            }
        }
Example #6
0
        public void TestArcFace(string imagePath)
        {
            // https://github.com/onnx/models/tree/master/vision/body_analysis/arcface
            // https://github.com/openvinotoolkit/open_model_zoo/tree/2021.4.1/models/public/mtcnn


            var srcImage = MemoryBitmap.Load(imagePath, GDICodec.Default);

            var srcImagePreprocessor = new ImageProcessor <Pixel.RGB96F>();

            var model = OnnxModel.FromFile("Models\\arcfaceresnet100-8.onnx");

            using (var session = model.CreateSession())
            {
                var workingTensor = session.GetInputTensor <float>(0)
                                    .AsSpanTensor4()
                                    .GetSubTensor(0)
                                    .SetImage(srcImage, srcImagePreprocessor);

                // run

                session.Inference();

                // get results

                var result = session
                             .GetOutputTensor <float>(0)
                             .AsSpanTensor2()
                             .GetSubTensor(0)
                             .ToArray();

                var resultv4 = System.Runtime.InteropServices.MemoryMarshal.Cast <float, System.Numerics.Vector4>(result);

                for (int i = 0; i < resultv4.Length; ++i)
                {
                    TestContext.WriteLine(resultv4[i]);
                }
            }
        }