|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.ML; |
| 5 | + |
| 6 | +namespace ObjectDetection |
| 7 | +{ |
| 8 | + class OnnxModelScorer |
| 9 | + { |
| 10 | + private readonly string imagesLocation; |
| 11 | + private readonly string imagesFolder; |
| 12 | + private readonly string modelLocation; |
| 13 | + private readonly MLContext mlContext; |
| 14 | + |
| 15 | + private IList<YoloBoundingBox> _boxes = new List<YoloBoundingBox>(); |
| 16 | + private readonly YoloWinMlParser _parser = new YoloWinMlParser(); |
| 17 | + |
| 18 | + public OnnxModelScorer(string imagesLocation, string imagesFolder, string modelLocation) |
| 19 | + { |
| 20 | + this.imagesLocation = imagesLocation; |
| 21 | + this.imagesFolder = imagesFolder; |
| 22 | + this.modelLocation = modelLocation; |
| 23 | + mlContext = new MLContext(); |
| 24 | + } |
| 25 | + |
| 26 | + public struct ImageNetSettings |
| 27 | + { |
| 28 | + public const int imageHeight = 416; |
| 29 | + public const int imageWidth = 416; |
| 30 | + } |
| 31 | + |
| 32 | + public struct TinyYoloModelSettings |
| 33 | + { |
| 34 | + // for checking TIny yolo2 Model input and output parameter names, |
| 35 | + //you can use tools like Netron, |
| 36 | + // which is installed by Visual Studio AI Tools |
| 37 | + |
| 38 | + // input tensor name |
| 39 | + public const string ModelInput = "image"; |
| 40 | + |
| 41 | + // output tensor name |
| 42 | + public const string ModelOutput = "grid"; |
| 43 | + } |
| 44 | + |
| 45 | + public void Score() |
| 46 | + { |
| 47 | + var model = LoadModel(imagesFolder, modelLocation); |
| 48 | + |
| 49 | + PredictDataUsingModel(imagesLocation, imagesFolder, model); |
| 50 | + } |
| 51 | + |
| 52 | + private PredictionEngine<ImageNetData, ImageNetPrediction> LoadModel(string imagesFolder, string modelLocation) |
| 53 | + { |
| 54 | + Console.WriteLine("Read model"); |
| 55 | + Console.WriteLine($"Model location: {modelLocation}"); |
| 56 | + Console.WriteLine($"Images folder: {imagesFolder}"); |
| 57 | + Console.WriteLine($"Default parameters: image size=({ImageNetSettings.imageWidth},{ImageNetSettings.imageHeight})"); |
| 58 | + |
| 59 | + var data = mlContext.Data.LoadFromTextFile<ImageNetData>(imagesLocation, hasHeader: true); |
| 60 | + |
| 61 | + var pipeline = mlContext.Transforms.LoadImages(outputColumnName: "image", imageFolder: imagesFolder, inputColumnName: nameof(ImageNetData.ImagePath)) |
| 62 | + .Append(mlContext.Transforms.ResizeImages(outputColumnName: "image", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "image")) |
| 63 | + .Append(mlContext.Transforms.ExtractPixels(outputColumnName: "image")) |
| 64 | + .Append(mlContext.Transforms.ApplyOnnxModel(modelFile: modelLocation, outputColumnNames: new[] { TinyYoloModelSettings.ModelOutput }, inputColumnNames: new[] { TinyYoloModelSettings.ModelInput })); |
| 65 | + |
| 66 | + var model = pipeline.Fit(data); |
| 67 | + |
| 68 | + var predictionEngine = mlContext.Model.CreatePredictionEngine<ImageNetData, ImageNetPrediction>(model); |
| 69 | + |
| 70 | + return predictionEngine; |
| 71 | + } |
| 72 | + |
| 73 | + protected void PredictDataUsingModel(string imagesLocation, |
| 74 | + string imagesFolder, |
| 75 | + PredictionEngine<ImageNetData, ImageNetPrediction> model) |
| 76 | + { |
| 77 | + Console.WriteLine($"Tags file location: {imagesLocation}"); |
| 78 | + Console.WriteLine(""); |
| 79 | + Console.WriteLine("=====Identify the objects in the images====="); |
| 80 | + Console.WriteLine(""); |
| 81 | + |
| 82 | + var testData = ImageNetData.ReadFromCsv(imagesLocation, imagesFolder); |
| 83 | + |
| 84 | + foreach (var sample in testData) |
| 85 | + { |
| 86 | + var probs = model.Predict(sample).PredictedLabels; |
| 87 | + IList<YoloBoundingBox> boundingBoxes = _parser.ParseOutputs(probs); |
| 88 | + var filteredBoxes = _parser.NonMaxSuppress(boundingBoxes, 5, .5F); |
| 89 | + |
| 90 | + Console.WriteLine(".....The objects in the image {0} are detected as below....", sample.Label); |
| 91 | + foreach (var box in filteredBoxes) |
| 92 | + { |
| 93 | + Console.WriteLine(box.Label + " and its Confidence score: " + box.Confidence); |
| 94 | + } |
| 95 | + Console.WriteLine(""); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
0 commit comments