static async Task Main(string[] args)
        {
            var rootDir         = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var squeezeNetModel = SqueezeNetModel.CreateFromFilePath(Path.Combine(rootDir, "squeezenet1.0-9.onnx"));

            // Load labels from JSON
            var labels = new List <string>();

            foreach (var kvp in JsonSerializer.Deserialize <Dictionary <string, string> >(File.ReadAllText(Path.Combine(rootDir, "Labels.json"))))
            {
                labels.Add(kvp.Value);
            }

            if (args.Length < 1)
            {
                return;
            }

            var filePath = args[0];

            // Open image file
            SqueezeNetOutput output;

            using (var fileStream = File.OpenRead(filePath))
            {
                // Convert from FileStream to ImageFeatureValue
                var decoder = await BitmapDecoder.CreateAsync(fileStream.AsRandomAccessStream());

                using var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                using var inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
                var imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);

                output = await squeezeNetModel.EvaluateAsync(new SqueezeNetInput
                {
                    data_0 = imageTensor
                });
            }

            // Get result, which is a list of floats with all the probabilities for all 1000 classes of SqueezeNet
            var resultTensor = output.softmaxout_1;
            var resultVector = resultTensor.GetAsVectorView();

            // Order the 1000 results with their indexes to know which class is the highest ranked one
            List <(int index, float p)> results = new List <(int, float)>();

            for (int i = 0; i < resultVector.Count; i++)
            {
                results.Add((index: i, p: resultVector.ElementAt(i)));
            }
            results.Sort((a, b) => a.p switch
            {
                var p when p <b.p => 1,
                              var p when p> b.p => - 1,
                _ => 0
            });
Beispiel #2
0
        public static SqueezeNetModel CreateFromFilePath(string filePath)
        {
            var learningModel = new SqueezeNetModel
            {
                model = LearningModel.LoadFromFilePath(filePath)
            };

            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }