public void Test1() { var onnxPath = @"../../../../TinyYolo/Data/TinyYolo2_model.onnx"; var testDataPath = @"../../../Data"; var ml = new MLContext(); var train = new Train(ml); var yolo = new TinyYoloModel(CommonHelper.GetAbsolutionPath(onnxPath, typeof(TinyYoloModel))); var transformer = train.LoadOnnx(yolo); var testImage = Path.Combine(testDataPath, "Image1.jpg" /** Image file here */); testImage = CommonHelper.GetAbsolutionPath(testImage, typeof(UnitTest1)); var image = Image.FromFile(testImage); var bitmap = (Bitmap)image; var testImageData = new ImageNetData { Image = bitmap }; var objectDetection = ml.Model.CreatePredictionEngine <ImageNetData, TinyYoloPrediction>(transformer); TinyYoloPrediction result = new TinyYoloPrediction(); objectDetection.Predict(testImageData, ref result); OnnxOutputParser parser = new OnnxOutputParser(yolo); var boxes = parser.ParseOutputs(result.PredictedLabels); boxes = parser.FilterBoundingBoxes(boxes, 3, .5f); if (boxes.Any()) { var saveLocation = CommonHelper.GetAbsolutionPath(testDataPath + "/result", typeof(UnitTest1)); CommonHelper.DrawRect(testDataPath, saveLocation, "Image1.jpg", boxes); } else { Assert.False(true); } }
private void LoadModel() { // Check for an Onnx model exported from Custom Vision var customVisionExport = Directory.GetFiles(modelsDirectory, "*.zip").FirstOrDefault(); // If there is one, use it. if (customVisionExport != null) { var customVisionModel = new CustomVisionModel(customVisionExport); var modelConfigurator = new OnnxModelConfigurator(customVisionModel); outputParser = new OnnxOutputParser(customVisionModel); customVisionPredictionEngine = modelConfigurator.GetMlNetPredictionEngine <CustomVisionPrediction>(); } else // Otherwise default to Tiny Yolo Onnx model { var tinyYoloModel = new TinyYoloModel(Path.Combine(modelsDirectory, "TinyYolo2_model.onnx")); var modelConfigurator = new OnnxModelConfigurator(tinyYoloModel); outputParser = new OnnxOutputParser(tinyYoloModel); tinyYoloPredictionEngine = modelConfigurator.GetMlNetPredictionEngine <TinyYoloPrediction>(); } }
private void InitializeModel() { var modelsDirectory = Path.Combine(Environment.CurrentDirectory, @"ML\OnnxModels"); var customVisionExport = Directory.GetFiles(modelsDirectory, "*.zip").FirstOrDefault(); // custom vision model if (customVisionExport != null) { var customVisionModel = new CustomVisionModel(customVisionExport); var modelConfigurator = new OnnxModelConfigurator(customVisionModel); OutputParser = new OnnxOutputParser(customVisionModel); CustomVisionPredictionEngine = modelConfigurator.GetMlNetPredictionEngine <CustomVisionPrediction>(); } else // default model { var tinyYoloModel = new TinyYoloModel(Path.Combine(modelsDirectory, "TinyYolo2_model.onnx")); var modelConfigurator = new OnnxModelConfigurator(tinyYoloModel); OutputParser = new OnnxOutputParser(tinyYoloModel); TinyYoloPredictionEngine = modelConfigurator.GetMlNetPredictionEngine <TinyYoloPrediction>(); } }