public void Detect(byte[] image) { var mappedByteBuffer = GetModelAsMappedByteBuffer(); var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer); var tensor = interpreter.GetInputTensor(0); var tensor2 = interpreter.GetOutputTensor(0); var shape1 = tensor2.Shape(); var shape = tensor.Shape(); var width = shape[1]; var height = shape[2]; var byteBuffer = GetPhotoAsByteBuffer(image, width, height); var streamReader = new StreamReader(Android.App.Application.Context.Assets.Open("labels.txt")); var labels = streamReader.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList(); //Convert our two-dimensional array into a Java.Lang.Object, the required input for Xamarin.TensorFlow.List.Interpreter var outputLocations = new float[1, 13, 13, 50]; var output = ByteBuffer.AllocateDirect(33800); try { interpreter.Run(byteBuffer, output); } catch (Exception ex) { var x = ex.Message; } }
public async Task Classify(List <List <float> > dataframe) { var mappedByteBuffer = GetModelAsMappedByteBuffer(); var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer); var tensor = interpreter.GetInputTensor(0); var shape = tensor.Shape(); var width = shape[1]; var height = shape[2]; var sr = new StreamReader(Application.Context.Assets.Open("labels.txt")); var labels = sr.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList(); var outputLocations = new float[1][] { new float[labels.Count] }; var outputs = Java.Lang.Object.FromArray(outputLocations); //interpreter.Run(dataframe, outputs); var classificationResult = outputs.ToArray <float[]>(); }
public async Task Classify(byte[] bytes) { var modelByteBuffer = LoadModel("dog-identification.tflite"); var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(modelByteBuffer); var inputTensor = interpreter.GetInputTensor(0); var shape = inputTensor.Shape(); var width = shape[1]; var height = shape[2]; List <String> labels = new List <String>(); var labelsStream = Application.Context.Assets.Open("labels.txt"); using (StreamReader sr = new StreamReader(labelsStream)) { string line; while ((line = sr.ReadLine()) != null) { labels.Add(line); } } var input = GetPhotoAsByteBuffer(bytes, width, height); var outputLocations = new float[1][] { new float[labels.Count] }; var outputs = Java.Lang.Object.FromArray(outputLocations); interpreter.Run(input, outputs); var classificationResults = outputs.ToArray <float[]>(); System.Console.WriteLine("I got results: "); for (int i = 0; i < classificationResults[0].Length; i++) { System.Console.WriteLine($"{labels[i]} : {classificationResults[0][i]}"); } var results = new List <Classification>(); for (var i = 0; i < labels.Count; i++) { var label = labels[i]; results.Add(new Classification(label, classificationResults[0][i])); } ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(results)); }
public async Task Classify(byte[] bytes) { var mappedByteBuffer = GetModelAsMappedByteBuffer(); // The following line with fail with compilation error // Error CS0234 The type or namespace name 'GPU' does not exist in the namespace 'Xamarin.TensorFlow.Lite' (are you missing an assembly reference?) var gpuDelegate = new Xamarin.TensorFlow.Lite.GPU.GpuDelegate(); Xamarin.TensorFlow.Lite.Interpreter.Options options = new Xamarin.TensorFlow.Lite.Interpreter.Options().AddDelegate(gpuDelegate); var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer, options); // If we comment 3 lines above and uncomment 1 line below everything works fine. //var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer); var tensor = interpreter.GetInputTensor(0); var shape = tensor.Shape(); var width = shape[1]; var height = shape[2]; var byteBuffer = GetPhotoAsByteBuffer(bytes, width, height); var sr = new StreamReader(Application.Context.Assets.Open("labels.txt")); var labels = sr.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList(); var outputLocations = new float[1][] { new float[labels.Count] }; var outputs = Java.Lang.Object.FromArray(outputLocations); interpreter.Run(byteBuffer, outputs); var classificationResult = outputs.ToArray <float[]>(); var result = new List <Classification>(); for (var i = 0; i < labels.Count; i++) { var label = labels[i]; result.Add(new Classification(label, classificationResult[0][i])); } ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result)); }
public List <ImageClassificationModel> Classify(byte[] image) { MappedByteBuffer mappedByteBuffer = GetModelAsMappedByteBuffer(); // TODO: Find a solution that is not being deprecated. Xamarin.TensorFlow.Lite.Interpreter interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer); //To resize the image, we first need to get its required width and height Xamarin.TensorFlow.Lite.Tensor tensor = interpreter.GetInputTensor(0); int[] shape = tensor.Shape(); int width = shape[1]; int height = shape[2]; ByteBuffer byteBuffer = GetPhotoAsByteBuffer(image, width, height); //use StreamReader to import the labels from labels.txt StreamReader streamReader = new StreamReader(Application.Context.Assets.Open("labels.txt")); //Transform labels.txt into List<string> List <string> labels = streamReader.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList(); //Convert our two-dimensional array into a Java.Lang.Object, the required input for Xamarin.TensorFlow.List.Interpreter float[][] outputLocations = new float[1][] { new float[labels.Count] }; Java.Lang.Object outputs = Java.Lang.Object.FromArray(outputLocations); interpreter.Run(byteBuffer, outputs); float[][] classificationResult = outputs.ToArray <float[]>(); //Map the classificationResult to the labels and sort the result to find which label has the highest probability List <ImageClassificationModel> classificationModelList = new List <ImageClassificationModel>(); for (int i = 0; i < labels.Count; i++) { string label = labels[i]; classificationModelList.Add(new ImageClassificationModel(label, classificationResult[0][i])); } return(classificationModelList); }
public void Classify(byte[] bytes) { var mappedByteBuffer = GetModelAsMappedByteBuffer(); var interpreter = new Xamarin.TensorFlow.Lite.Interpreter(mappedByteBuffer); var tensor = interpreter.GetInputTensor(0); var shape = tensor.Shape(); var width = shape[1]; var height = shape[2]; var sr = new StreamReader(Application.Context.Assets.Open("hotdog-or-not-labels.txt")); var labels = sr.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList(); var outputLocations = new float[1][] { new float[labels.Count] }; var outputs = Java.Lang.Object.FromArray(outputLocations); var byteBuffer = GetPhotoAsByteBuffer(bytes, width, height); interpreter.Run(byteBuffer, outputs); var predictionResult = outputs.ToArray <float[]>(); var result = new Dictionary <string, float>(); for (var i = 0; i < labels.Count; i++) { var label = labels[i]; result.Add(label, predictionResult[0][i]); } ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result)); }