internal void Init(string modelName, string labelsFileName, ModelType modelType)
        {
            _modelType = modelType;

            try
            {
                var assets = Android.App.Application.Context.Assets;
                using (var sr = new StreamReader(File.Exists(labelsFileName)? File.OpenRead(labelsFileName) : assets.Open(labelsFileName)))
                {
                    var content = sr.ReadToEnd();
                    _labels = content.Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();
                }

                _inferenceInterface = new TensorFlowInferenceInterface(assets, modelName);
                InputSize           = Convert.ToInt32(_inferenceInterface.GraphOperation(InputName).Output(0).Shape().Size(1));
                var iter = _inferenceInterface.Graph().Operations();
                while (iter.HasNext && !_hasNormalizationLayer)
                {
                    var op = iter.Next() as Operation;
                    if (op.Name().Contains(DataNormLayerPrefix))
                    {
                        _hasNormalizationLayer = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ImageClassifierException("Failed to load the model - check the inner exception for more details", ex);
            }
        }
        public InceptionClassifier(AssetManager assetManager)
        {
            labels.AddRange(ReadLabelsIntoMemory(assetManager, LABEL_FILE));
            inferenceInterface = new TensorFlowInferenceInterface(assetManager, MODEL_FILE);
            var operation  = inferenceInterface.GraphOperation(OUTPUT_NAME);
            var numClasses = operation.Output(0).Shape().Size(1);

            //TODO: Confirm in Android, because my results are 1001 labels, 1008 classes
            System.Console.WriteLine($"Read {labels.Count} labels, output layer size is {numClasses}");
            var bmp     = BitmapCreate(assetManager, "husky.png");
            var results = Recognize(bmp);
        }
示例#3
0
        public ImageClassifier()
        {
            var assets = Application.Context.Assets;

            inferenceInterface = new TensorFlowInferenceInterface(assets, "model.pb");
            _inputSize         = (int)inferenceInterface.GraphOperation(InputName).Output(0).Shape().Size(1);

            using (var sr = new StreamReader(assets.Open("labels.txt")))
            {
                var content = sr.ReadToEnd();
                labels = content.Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToList();
            }
        }
        public async Task InitializeAsync(ModelType modelType, params string[] parameters)
        {
            var assets    = Android.App.Application.Context.Assets;
            var modelFile = $"file:///android_asset/{parameters[0]}";
            var labelFile = parameters[1];

            labels       = new List <string>();
            using var sr = new StreamReader(assets.Open(labelFile));
            var content = await sr.ReadToEndAsync();

            var labelStrings = content.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(e => e.TrimEnd('\r'));

            labels.AddRange(labelStrings);

            inferenceInterface = new TensorFlowInferenceInterface(assets, modelFile);
            inputSize          = (int)inferenceInterface.GraphOperation(INPUT_NAME).Output(0).Shape().Size(1);
        }
        public async void Classification(byte[] bytes)
        {
            var assets             = Application.Context.Assets;
            var inferenceInterface = new TensorFlowInferenceInterface(assets, "model.pb");

            var inputSize = (int)inferenceInterface.GraphOperation("Placeholder").Output(0).Shape().Size(1);

            List <string> labels;

            using (var streamReader = new StreamReader(assets.Open("labels.txt")))
            {
                labels = streamReader.ReadToEnd().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s))
                         .ToList();
            }

            var bitmap = await BitmapFactory.DecodeByteArrayAsync(bytes, 0, bytes.Length);

            var resizedMap = Bitmap.CreateScaledBitmap(bitmap, inputSize, inputSize, false)
                             .Copy(Bitmap.Config.Argb8888, false);

            var floatValues = new float[inputSize * inputSize * 3];
            var intValues   = new int[inputSize * inputSize];

            resizedMap.GetPixels(intValues, 0, inputSize, 0, 0, inputSize, inputSize);

            for (var i = 0; i < intValues.Length; ++i)
            {
                var intValue = intValues[i];
                floatValues[i * 3 + 0] = (intValue & 0xFF) - 105f;
                floatValues[i * 3 + 1] = ((intValue >> 8) & 0xFF) - 117f;
                floatValues[i * 3 + 2] = ((intValue >> 16) & 0xFF) - 124f;
            }

            var operation = inferenceInterface.GraphOperation("loss");

            var outputs = new float[labels.Count];

            inferenceInterface.Feed("Placeholder", floatValues, 1, inputSize, inputSize, 3);
            inferenceInterface.Run(new[] { "loss" }, true);
            inferenceInterface.Fetch("loss", outputs);

            var result = new Dictionary <string, float>();

            for (var i = 0; i < labels.Count; i++)
            {
                var label = labels[i];
                result.Add(label, outputs[i]);
            }

            var maxConf = 0f;

            for (var i = 0; i < outputs.Length; ++i)
            {
                if (outputs[i] > maxConf)
                {
                    maxConf = outputs[i];
                }
            }

            ClassificationCompleted?.Invoke(this, new ClassificationEventArgs(result));
        }
 long OutputSize()
 {
     return(inferenceInterface.GraphOperation(OUTPUT_NAME).Output(0).Shape().Size(1));
 }