Exemplo n.º 1
0
        private Layer ConvertFullyConnected(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var options = op.BuiltinOptions <tflite.FullyConnectedOptions>().Value;
            var weights = _graph.Tensors(inputs[1]).Value;
            var bias    = _graph.Tensors(inputs[2]).Value;

            if (input.ShapeLength == 4 && (input.Shape(1) != 1 || input.Shape(2) != 1))
            {
                var flatten = new TensorflowFlatten(input.GetShapeArray().ToNCHW());
                var layer   = new FullyConnected(flatten.Output.Dimensions, _model.GetTensor <float>(weights), _model.GetTensor <float>(bias),
                                                 options.FusedActivationFunction.ToActivationFunction());
                layer.Input.SetConnection(flatten.Output);
                _inputs.Add(flatten.Input, inputs[0]);
                _outputs.Add(op.Outputs(0), layer.Output);
                return(layer);
            }
            else
            {
                var layer = new FullyConnected(input.GetShapeArray().ToNCHW(), _model.GetTensor <float>(weights), _model.GetTensor <float>(bias),
                                               options.FusedActivationFunction.ToActivationFunction());
                _inputs.Add(layer.Input, inputs[0]);
                _outputs.Add(op.Outputs(0), layer.Output);
                return(layer);
            }
        }
Exemplo n.º 2
0
        private Layer ConvertDepthwiseConv2d(tflite.Operator op)
        {
            var inputs   = op.GetInputsArray();
            var input    = _graph.Tensors(inputs[0]).Value;
            var options  = op.BuiltinOptions <tflite.DepthwiseConv2DOptions>().Value;
            var weights  = _graph.Tensors(inputs[1]).Value;
            var bias     = _graph.Tensors(inputs[2]).Value;
            var depthMul = options.DepthMultiplier;

            if (input.GetShapeArray().ToNCHW()[1] == 1 && depthMul != 1)
            {
                var layer = new Conv2d(input.GetShapeArray().ToNCHW(), _model.GetTensor <float>(weights).ToOIHW().Transpose(new[] { 1, 0, 2, 3 }), _model.GetTensor <float>(bias),
                                       options.Padding.ToPadding(), options.StrideW, options.StrideH, options.FusedActivationFunction.ToActivationFunction());
                _inputs.Add(layer.Input, inputs[0]);
                _outputs.Add(op.Outputs(0), layer.Output);
                return(layer);
            }
            else if (depthMul != 1)
            {
                throw new LayerNotSupportedException("DEPTHWISE_CONV_2D", "depth_multiplier must be 1");
            }

            {
                var layer = new DepthwiseConv2d(input.GetShapeArray().ToNCHW(), _model.GetTensor <float>(weights).ToOIHW(), _model.GetTensor <float>(bias),
                                                options.Padding.ToPadding(), options.StrideW, options.StrideH, options.FusedActivationFunction.ToActivationFunction());
                _inputs.Add(layer.Input, inputs[0]);
                _outputs.Add(op.Outputs(0), layer.Output);
                return(layer);
            }
        }
Exemplo n.º 3
0
        private Layer ConvertLeakyRelu(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var options = op.BuiltinOptions <tflite.LeakyReluOptions>().Value;

            var layer = new LeakyRelu(input.GetShapeArray().ToNCHW(), options.Alpha);

            _inputs.Add(layer.Input, inputs[0]);
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }
Exemplo n.º 4
0
        private Layer ConvertReshape(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var options = op.BuiltinOptions <tflite.ReshapeOptions>().Value;

            var layer = new TensorflowReshape(input.GetShapeArray().ToNCHW(), options.GetNewShapeArray().ToNCHW());

            _inputs.Add(layer.Input, inputs[0]);
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }
Exemplo n.º 5
0
        private Layer ConvertMaxPool2d(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var options = op.BuiltinOptions <tflite.Pool2DOptions>().Value;

            var layer = new MaxPool2d(input.GetShapeArray().ToNCHW(), options.Padding.ToPadding(), options.FilterWidth, options.FilterHeight, options.StrideW,
                                      options.StrideH, options.FusedActivationFunction.ToActivationFunction());

            _inputs.Add(layer.Input, inputs[0]);
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }
Exemplo n.º 6
0
        private Layer ConvertResizeNearestNeighbor(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var newSize = _model.GetTensor <int>(_graph.Tensors(inputs[1]).Value);
            var options = op.BuiltinOptions <tflite.ResizeNearestNeighborOptions>().Value;

            var blockShape = _graph.Tensors(inputs[1]).Value;
            var layer      = new ResizeNearestNeighbor(input.GetShapeArray().ToNCHW(), newSize[1], newSize[0], options.AlignCorners);

            _inputs.Add(layer.Input, inputs[0]);
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }
Exemplo n.º 7
0
        private Layer ConvertConv2d(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var input   = _graph.Tensors(inputs[0]).Value;
            var options = op.BuiltinOptions <tflite.Conv2DOptions>().Value;
            var weights = _graph.Tensors(inputs[1]).Value;
            var bias    = _graph.Tensors(inputs[2]).Value;

            var layer = new Conv2d(input.GetShapeArray().ToNCHW(), _model.GetTensor <float>(weights).ToOIHW(), _model.GetTensor <float>(bias),
                                   options.Padding.ToPadding(), options.StrideW, options.StrideH, options.FusedActivationFunction.ToActivationFunction());

            _inputs.Add(layer.Input, inputs[0]);
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }
Exemplo n.º 8
0
        private Layer ConvertConcatenation(tflite.Operator op)
        {
            var inputs  = op.GetInputsArray();
            var options = op.BuiltinOptions <tflite.ConcatenationOptions>().Value;

            if (options.Axis != 3)
            {
                throw new NotSupportedException("Axis of concatenation must be 3.");
            }
            var layer = new Concatenation(inputs.Select(x => new ReadOnlyMemory <int>(_graph.Tensors(x).Value.GetShapeArray().ToNCHW())));

            for (int i = 0; i < inputs.Length; i++)
            {
                _inputs.Add(layer.Inputs[i], inputs[i]);
            }
            _outputs.Add(op.Outputs(0), layer.Output);
            return(layer);
        }