Пример #1
0
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation,
                                                                   string imagesFolder,
                                                                   string labelsLocation,
                                                                   PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classificate images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
                imageData.ConsoleWrite();
                yield return(imageData);
            }
        }
Пример #2
0
        protected IEnumerable <ImageNetData> PredictDataUsingModel(List <ImageNetData> imageData,
                                                                   string labelsLocation,
                                                                   PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classify images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            foreach (var sample in imageData)
            {
                var probs  = model.Predict(sample).PredictedLabels;
                var result = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                };
                (result.PredictedLabel, result.Probability) = GetBestLabel(labels, probs);

                if (result.Probability > 0.4f)
                {
                    result.ConsoleWrite();
                    yield return(result);
                }
            }
        }
        public static void ConsoleWrite(this ImageNetDataProbability self)
        {
            var defaultForeground = Console.ForegroundColor;
            var labelColor        = ConsoleColor.Magenta;
            var probColor         = ConsoleColor.Blue;
            var exactLabel        = ConsoleColor.Green;
            var failLabel         = ConsoleColor.Red;

            Console.Write("ImagePath: ");
            Console.ForegroundColor = labelColor;
            Console.Write($"{Path.GetFileName(self.ImagePath)}");
            Console.ForegroundColor = defaultForeground;
            Console.Write(" labeled as ");
            Console.ForegroundColor = labelColor;
            Console.Write(self.Label);
            Console.ForegroundColor = defaultForeground;
            Console.Write(" predicted as ");
            if (self.Label.Equals(self.PredictedLabel))
            {
                Console.ForegroundColor = exactLabel;
                Console.Write($"{self.PredictedLabel}");
            }
            else
            {
                Console.ForegroundColor = failLabel;
                Console.Write($"{self.PredictedLabel}");
            }
            Console.ForegroundColor = defaultForeground;
            Console.Write(" with probability ");
            Console.ForegroundColor = probColor;
            Console.Write(self.Probability);
            Console.ForegroundColor = defaultForeground;
            Console.WriteLine("");
        }
Пример #4
0
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation, string imagesFolder, PredictionModel <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classificate images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine(" ");

            model.TryGetScoreLabelNames(out string[] labels);
            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder).ToList();

            // add an extra image to "really" test the model
            testData = testData.Concat(new[] { new ImageNetData()
                                               {
                                                   ImagePath = Path.Combine(imagesFolder, "teddy5.jpg")
                                               } }).ToList();

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                };
                (imageData.Label, imageData.Probability) = GetLabel(labels, probs);
                imageData.ConsoleWriteLine();
                yield return(imageData);
            }
        }
Пример #5
0
        protected string PredictDataUsingModel(string testLocation,
                                               string imagesFolder,
                                               string labelsLocation,
                                               PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);

            List <ImageNetDataProbability> result = new List <ImageNetDataProbability>();

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample);
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = ModelHelpers.GetBestLabel(labels, probs.PredictedLabels);
                result.Add(imageData);
            }
            return(result.OrderByDescending(x => x.Probability).FirstOrDefault().PredictedLabel);
        }
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation,
                                                                   string imagesFolder,
                                                                   string labelsLocation,
                                                                   PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classificate images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            double acc = 0;
            double tp  = 0;
            double fp  = 0;
            double fn  = 0;

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);
            var count    = 0;

            Console.ForegroundColor = ConsoleColor.Red;

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);

                //imageData.ConsoleWrite();

                acc += imageData.Label == imageData.PredictedLabel ? 1 : 0;
                tp  += imageData.PredictedLabel == imageData.Label && imageData.Label == labels[1] ? 1 : 0;
                fp  += imageData.Label == labels[0] && imageData.PredictedLabel == labels[1] ? 1 : 0;
                fn  += imageData.Label == labels[1] && imageData.PredictedLabel == labels[0] ? 1 : 0;

                count++;
                if (count % 100 == 0)
                {
                    Console.WriteLine($"{count} images have been processed");
                }

                yield return(imageData);
            }

            acc = (acc / testData.ToList().Count()) * 100;
            double prec = tp / (tp + fp);
            double rec  = tp / (tp + fn);
            double f1   = 2 * (prec * rec) / (prec + rec);


            Console.WriteLine($"Accuracy: {acc}%, precision: {prec}, recall: {rec}, f1 score: {f1}");
        }
Пример #7
0
        protected ImageNetData PredictDataUsingModel(ImageNetData testData,
                                                     string labelsLocation,
                                                     PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = testData.Label
            };

            (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
            return(imageData);
        }
Пример #8
0
        public static void ConsoleWrite(this ImageNetDataProbability self)
        {
            var defaultForeground = Console.ForegroundColor;
            var labelColor        = ConsoleColor.Magenta;
            var probColor         = ConsoleColor.Blue;

            Console.Write("ImagePath: ");
            Console.ForegroundColor = labelColor;
            Console.Write($"{Path.GetFileName(self.ImagePath)}");
            Console.ForegroundColor = defaultForeground;
            Console.Write(" identified as as ");
            Console.Write($"{self.PredictedLabel}");
            Console.ForegroundColor = defaultForeground;
            Console.Write(" with probability ");
            Console.ForegroundColor = probColor;
            Console.Write(self.Probability);
            Console.ForegroundColor = defaultForeground;
            Console.WriteLine("");
        }
        protected IEnumerable <ImageNetDataProbability> PredictImageUsingModel(string testLocation,
                                                                               string imageFile,
                                                                               string labelsLocation,
                                                                               PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = new ImageNetData {
                ImagePath = imageFile
            };

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = testData.Label
            };

            (imageData.PredictedLabel, imageData.Probability) = ModelHelpers.GetBestLabel(labels, probs);
            yield return(imageData);
        }
        public ImageNetDataProbability PredictSingleImageDataUsingModel(
            string imagePath,
            string labelsLocation,
            PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            Debug.WriteLine($"Images folder: {imagePath}");
            Debug.WriteLine($"Labels folder: {labelsLocation}");
            var labels   = ReadLabels(labelsLocation);
            var testData = new ImageNetData {
                ImagePath = imagePath
            };

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = string.Empty
            };

            (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
            return(imageData);
        }