Example #1
0
        /// <summary>
        /// Get output as `Tensor`
        /// </summary>
        /// <param name="idx">output index</param>
        /// <param name="batchCount">max batch count</param>
        /// <param name="fromBatch">start from batch</param>
        /// <returns>`Tensor`</returns>
        /// <exception cref="Exception">thrown if called on raw test set (only JSON test set is supported)</exception>
        public Tensor GetOutputAsTensor(int idx = 0, int batchCount = -1, int fromBatch = 0)
        {
            if (rawTestSet != null)
            {
                throw new Exception("GetOutputAsTensor is not supported for RAW test suites");
            }

            TensorShape shape = GetOutputShape(idx);

            Assert.IsTrue(shape.sequenceLength == 1 && shape.numberOfDirections == 1);
            var barracudaArray = new BarracudaArrayFromManagedArray(GetOutputData(idx));

            var maxBatchCount = barracudaArray.Length / shape.flatWidth;

            fromBatch = Math.Min(fromBatch, maxBatchCount - 1);
            if (batchCount < 0)
            {
                batchCount = maxBatchCount - fromBatch;
            }
            batchCount = Math.Min(batchCount, maxBatchCount - fromBatch);

            var shapeArray = shape.ToArray();

            shapeArray[TensorShape.DataBatch] = batchCount;
            var tensorShape = new TensorShape(shapeArray);

            var offset = fromBatch * tensorShape.flatWidth;
            var res    = new Tensor(tensorShape, new SharedArrayTensorData(barracudaArray, tensorShape, offset));

            res.name = GetOutputName(idx);
            res.name = res.name.EndsWith(":0") ? res.name.Remove(res.name.Length - 2) : res.name;

            return(res);
        }
        /// <summary>
        /// Add an input to the model
        /// </summary>
        public Model.Input Input(string name, TensorShape shape)
        {
            m_Model.inputs.Add(new Model.Input {
                name = name, shape = shape.ToArray()
            });

            return(m_Model.inputs.Last());
        }
Example #3
0
        static public TensorShape Gather(TensorShape[] shapes, int axis)
        {
            TensorShape X       = shapes[0];
            TensorShape indices = shapes[1];

            int[] shape = X.ToArray();
            shape[axis] = indices.length;

            return(new TensorShape(shape));
        }
        /// <summary>
        /// Generates a Tensor with random values drawn from a uniform distribution.
        /// The shape of the tensor is specified by shape
        /// The uniform distribution scale is specified by min and max range
        /// </summary>
        public Layer RandomUniform(string name, TensorShape shape, float min, float max, float seed)
        {
            Layer layer = new Layer(name, Layer.Type.RandomUniform);

            layer.alpha = (max - min);
            layer.beta  = min;
            layer.pad   = new int[1] {
                (int)seed
            };
            layer.pool = shape.ToArray();
            m_Model.layers.Add(layer);

            return(layer);
        }
        /// <summary>
        /// Generates a Tensor with random values drawn from a normal distribution.
        /// The shape of the tensor is specified by scale
        /// The normal distribution is specified by mean and scale
        /// </summary>
        public Layer RandomNormal(string name, TensorShape shape, float mean, float scale, float seed)
        {
            Layer layer = new Layer(name, Layer.Type.RandomNormal);

            layer.alpha = scale;
            layer.beta  = mean;
            layer.pad   = new int[1] {
                (int)seed
            };
            layer.pool = shape.ToArray();
            m_Model.layers.Add(layer);

            return(layer);
        }
Example #6
0
        /// <summary>
        /// Get input as `Tensor`
        /// </summary>
        /// <param name="idx">input index</param>
        /// <param name="batchCount">max batch count</param>
        /// <param name="fromBatch">start from batch</param>
        /// <returns>`Tensor`</returns>
        /// <exception cref="Exception">thrown if called on raw test set (only JSON test set is supported)</exception>
        public Tensor GetInputAsTensor(int idx = 0, int batchCount = -1, int fromBatch = 0)
        {
            if (rawTestSet != null)
            {
                throw new Exception("GetInputAsTensor is not supported for RAW test suites");
            }

            TensorShape shape = GetInputShape(idx);

            Assert.IsTrue(shape.sequenceLength == 1 && shape.numberOfDirections == 1);
            var array         = GetInputData(idx);
            var maxBatchCount = array.Length / shape.flatWidth;

            fromBatch = Math.Min(fromBatch, maxBatchCount - 1);
            if (batchCount < 0)
            {
                batchCount = maxBatchCount - fromBatch;
            }

            // pad data with 0s, if test-set doesn't have enough batches
            var shapeArray = shape.ToArray();

            shapeArray[TensorShape.DataBatch] = batchCount;
            var tensorShape             = new TensorShape(shapeArray);
            var managedBufferStartIndex = fromBatch * tensorShape.flatWidth;
            var count = Math.Min(batchCount, maxBatchCount - fromBatch) * tensorShape.flatWidth;

            float[] dataToUpload = new float[tensorShape.length];
            Array.Copy(array, managedBufferStartIndex, dataToUpload, 0, count);

            var data = new ArrayTensorData(tensorShape.length);

            data.Upload(dataToUpload, tensorShape, 0);

            var res = new Tensor(tensorShape, data);

            res.name = GetInputName(idx);
            res.name = res.name.EndsWith(":0") ? res.name.Remove(res.name.Length - 2) : res.name;

            return(res);
        }
Example #7
0
        static public TensorShape Reshape(this TensorShape shape, int[] size)
        {
            Assert.AreEqual(size.Length, 4);
            var newShapeArray = shape.ToArray();

            // From: https://github.com/onnx/onnx/blob/master/docs/Operators.md#Reshape
            //
            // At most one dimension of the new shape can be -1.
            // In this case, the value is inferred from the size of the tensor and the remaining dimensions.
            //
            // A dimension could also be 0,
            // in which case the actual dimension value is unchanged (i.e. taken from the input tensor).

            var multipleOf   = 1;
            var unknownIndex = -1;

            for (int q = 0; q < size.Length; ++q)
            {
                if (size[q] > 0)
                {
                    multipleOf      *= size[q];
                    newShapeArray[q] = size[q];
                }
                else if (size[q] == 0)
                {
                    multipleOf *= newShapeArray[q];
                }
                else if (unknownIndex == -1)
                {
                    unknownIndex = q;
                }
                else
                {
                    throw new ArgumentException("Can only specify one unknown dimension");
                }
            }

            if (unknownIndex == -1)
            {
                // all dimensions are given
                var newShape = new TensorShape(newShapeArray);
                if (shape.length != newShape.length)
                {
                    throw new ArgumentException("Cannot reshape array of size " + shape.length +
                                                " into shape " + newShape);
                }
                return(newShape);
            }

            var  solveForIndex = shape.length / multipleOf;
            bool remainderLeft = shape.length % multipleOf != 0;

            if (remainderLeft)
            {
                throw new ArgumentException("Cannot reshape array of size " + shape.length +
                                            " into shape with multiple of " + multipleOf + " elements");
            }

            newShapeArray[unknownIndex] = solveForIndex;
            return(new TensorShape(newShapeArray));
        }
 /// <summary>
 /// Apply shape to the input tensor. Number of elements in the shape must match number of elements in input tensor.
 /// </summary>
 public Layer Reshape(string name, object input, TensorShape shape)
 {
     return(Reshape(name, input, shape.ToArray()));
 }