private void Worker()
 {
     while (!CancelThreads.Token.IsCancellationRequested && PathImages.TryDequeue(out string image))
     {
         ResultClassification result = Model.PredictModel(image);
         Result.Enqueue(result);
         ImageRecognitionCompleted(result);
     }
 }
Beispiel #2
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);
        }