Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 6
0
        public List <string> PredictNextWord(string word)
        {
            switch (Language)
            {
            case "pt":

                using (Stream ms = context.Assets.Open("Models/idx2word_pt.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        idx2word = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <int, String> >(sr.ReadToEnd());
                    }
                }

                using (Stream ms = context.Assets.Open("Models/word2idx_pt.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        word2idx = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <String, int> >(sr.ReadToEnd());
                    }
                }

                if (!word2idx.ContainsKey(word))
                {
                    return(new List <string>());
                }

                var il = new float[10];

                for (var i = 0; i < 10; i++)
                {
                    il[i] = (float)(word2idx[word]);
                }

                if (model == null)
                {
                    var assets = Application.Context.Assets;
                    AssetFileDescriptor fileDescriptor = assets.OpenFd("Models/predictor_pt.tflite");
                    FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
                    FileChannel         fileChannel    = inputStream.Channel;
                    long startOffset    = fileDescriptor.StartOffset;
                    long declaredLength = fileDescriptor.DeclaredLength;
                    var  asd            = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength);
                    model = new Xamarin.TensorFlow.Lite.Interpreter(asd);
                }

                byte[] ibytes = new byte[il.Length * sizeof(float)];
                Buffer.BlockCopy(il, 0, ibytes, 0, ibytes.Length);

                var bytebuffer = Java.Nio.ByteBuffer.Wrap(ibytes);
                var output     = Java.Nio.ByteBuffer.AllocateDirect(4 * 10 * (idx2word.Count + 1));

                model.Run(bytebuffer, output);

                List <Double> res = new List <double>();

                var buffer = new byte[4 * 10 * (idx2word.Count + 1)];

                Marshal.Copy(output.GetDirectBufferAddress(), buffer, 0, 4 * 10 * (idx2word.Count + 1));

                for (var i = 0; i < idx2word.Count + 1; i++)
                {
                    res.Add(BitConverter.ToSingle(buffer, i * 4));
                }

                return(res.OrderByDescending(x => x).Select(x2 => idx2word[res.IndexOf(x2)]).Take(10).ToList());

            case "es":

                using (Stream ms = context.Assets.Open("Models/idx2word_es.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        idx2word = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <int, String> >(sr.ReadToEnd());
                    }
                }

                using (Stream ms = context.Assets.Open("Models/word2idx_es.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        word2idx = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <String, int> >(sr.ReadToEnd());
                    }
                }

                if (!word2idx.ContainsKey(word))
                {
                    return(new List <string>());
                }

                var il2 = new float[10];

                for (var i = 0; i < 10; i++)
                {
                    il2[i] = (float)(word2idx[word]);
                }

                if (model == null)
                {
                    var assets = Application.Context.Assets;
                    AssetFileDescriptor fileDescriptor = assets.OpenFd("Models/predictor_es.tflite");
                    FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
                    FileChannel         fileChannel    = inputStream.Channel;
                    long startOffset    = fileDescriptor.StartOffset;
                    long declaredLength = fileDescriptor.DeclaredLength;
                    var  asd            = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength);
                    model = new Xamarin.TensorFlow.Lite.Interpreter(asd);
                }

                byte[] ibytes2 = new byte[il2.Length * sizeof(float)];
                Buffer.BlockCopy(il2, 0, ibytes2, 0, ibytes2.Length);

                var bytebuffer2 = Java.Nio.ByteBuffer.Wrap(ibytes2);
                var output2     = Java.Nio.ByteBuffer.AllocateDirect(4 * 10 * (idx2word.Count + 1));

                model.Run(bytebuffer2, output2);

                List <Double> res2 = new List <double>();

                var buffer2 = new byte[4 * 10 * (idx2word.Count + 1)];

                Marshal.Copy(output2.GetDirectBufferAddress(), buffer2, 0, 4 * 10 * (idx2word.Count + 1));

                for (var i = 0; i < idx2word.Count + 1; i++)
                {
                    res2.Add(BitConverter.ToSingle(buffer2, i * 4));
                }

                return(res2.OrderByDescending(x => x).Select(x2 => idx2word[res2.IndexOf(x2)]).Take(10).ToList());

            default:

                using (Stream ms = context.Assets.Open("Models/idx2word_en.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        idx2word = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <int, String> >(sr.ReadToEnd());
                    }
                }

                using (Stream ms = context.Assets.Open("Models/word2idx_en.txt"))
                {
                    using (var sr = new StreamReader(ms))
                    {
                        word2idx = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <String, int> >(sr.ReadToEnd());
                    }
                }

                if (!word2idx.ContainsKey(word))
                {
                    return(new List <string>());
                }

                var il3 = new float[10];

                for (var i = 0; i < 10; i++)
                {
                    il3[i] = (float)(word2idx[word]);
                    System.Diagnostics.Debug.WriteLine(il3[i]);
                }

                if (model == null)
                {
                    var assets = Application.Context.Assets;
                    AssetFileDescriptor fileDescriptor = assets.OpenFd("Models/predictor_en.tflite");
                    FileInputStream     inputStream    = new FileInputStream(fileDescriptor.FileDescriptor);
                    FileChannel         fileChannel    = inputStream.Channel;
                    long startOffset    = fileDescriptor.StartOffset;
                    long declaredLength = fileDescriptor.DeclaredLength;
                    var  asd            = fileChannel.Map(FileChannel.MapMode.ReadOnly, startOffset, declaredLength);
                    model = new Xamarin.TensorFlow.Lite.Interpreter(asd);
                }

                byte[] ibytes3 = new byte[il3.Length * sizeof(float)];
                Buffer.BlockCopy(il3, 0, ibytes3, 0, ibytes3.Length);

                var bytebuffer3 = Java.Nio.ByteBuffer.Wrap(ibytes3);
                var output3     = Java.Nio.ByteBuffer.AllocateDirect(4 * 10 * (idx2word.Count + 1));

                model.Run(bytebuffer3, output3);

                List <Double> res3 = new List <double>();

                var buffer3 = new byte[4 * 10 * (idx2word.Count + 1)];

                Marshal.Copy(output3.GetDirectBufferAddress(), buffer3, 0, 4 * 10 * (idx2word.Count + 1));

                for (var i = 0; i < idx2word.Count + 1; i++)
                {
                    res3.Add(BitConverter.ToSingle(buffer3, i * 4));
                }

                return(res3.OrderByDescending(x => x).Select(x2 => idx2word[res3.IndexOf(x2)]).Take(10).ToList());
            }
        }