Beispiel #1
0
 public Cpu3DTensor(int rows, int columns, int depth)
 {
     _rows    = rows;
     _columns = columns;
     _depth   = depth;
     _data    = new CpuMatrix(DenseMatrix.Build.Dense(rows * columns, depth));
 }
Beispiel #2
0
 public Cpu3DTensor(int rows, int columns, int depth)
 {
     RowCount    = rows;
     ColumnCount = columns;
     Depth       = depth;
     _data       = new CpuMatrix(DenseMatrix.Build.Dense(rows * columns, depth));
 }
Beispiel #3
0
 public Cpu4DTensor(int rows, int columns, int depth, int count)
 {
     _rows    = rows;
     _columns = columns;
     _depth   = depth;
     _count   = count;
     _data    = new CpuMatrix(DenseMatrix.Build.Dense(_rows * _count * _depth, _count));
 }
Beispiel #4
0
 public Cpu4DTensor(int rows, int columns, int depth, int count)
 {
     RowCount    = rows;
     ColumnCount = columns;
     Depth       = depth;
     Count       = count;
     _data       = new CpuMatrix(DenseMatrix.Build.Dense(RowCount * Count * Depth, Count));
 }
Beispiel #5
0
        public IMatrix CreateMatrix(IReadOnlyList <FloatArray> data)
        {
            var rowCount    = data.Count;
            var columnCount = data.First().Data.Length;
            var ret         = new CpuMatrix(DenseMatrix.Create(rowCount, columnCount, 0f));

            for (var i = 0; i < rowCount; i++)
            {
                for (var j = 0; j < columnCount; j++)
                {
                    ret[i, j] = data[i].Data[j];
                }
            }
            return(ret);
        }
Beispiel #6
0
        public (I3DTensor Result, IReadOnlyList <(object X, object Y)> Index) MaxPool(
            int filterWidth,
            int filterHeight,
            int stride,
            bool calculateIndex)
        {
            var newColumns = (ColumnCount - filterWidth) / stride + 1;
            var newRows    = (RowCount - filterHeight) / stride + 1;
            var matrixList = new List <CpuMatrix>();
            var indexList  = calculateIndex ? new List <(object X, object Y)>() : null;
            var posList    = ConvolutionHelper.Default(ColumnCount, RowCount, filterWidth, filterHeight, stride);

            for (var k = 0; k < Depth; k++)
            {
                var matrix = _data[k];
                var xIndex = new int[newColumns * newRows];
                var yIndex = new int[newColumns * newRows];
                var layer  = new CpuMatrix(DenseMatrix.Create(newRows, newColumns, 0f));

                foreach (var item in posList)
                {
                    var first   = item.First();
                    var targetX = first.X / stride;
                    var targetY = first.Y / stride;
                    var maxVal  = float.MinValue;
                    var bestX   = -1;
                    var bestY   = -1;
                    foreach (var pos in item)
                    {
                        var val = matrix[pos.Y, pos.X];
                        if (val > maxVal || bestX == -1)
                        {
                            bestX  = pos.X;
                            bestY  = pos.Y;
                            maxVal = val;
                        }
                    }
                    var index = targetX * newRows + targetY;
                    xIndex[index]           = bestX;
                    yIndex[index]           = bestY;
                    layer[targetY, targetX] = maxVal;
                }
                matrixList.Add(layer);
                indexList?.Add((xIndex, yIndex));
            }
            return(new Cpu3DTensor(matrixList), indexList);
        }
Beispiel #7
0
        public I3DTensor MaxPool(int filterWidth, int filterHeight, int stride, List <Dictionary <Tuple <int, int>, Tuple <int, int> > > indexPosList)
        {
            var newColumns = (ColumnCount - filterWidth) / stride + 1;
            var newRows    = (RowCount - filterHeight) / stride + 1;
            var ret        = new List <CpuMatrix>();

            for (var k = 0; k < Depth; k++)
            {
                var matrix   = _data[k];
                var indexPos = new Dictionary <Tuple <int, int>, Tuple <int, int> >();
                var layer    = new CpuMatrix(DenseMatrix.Create(newRows, newColumns, 0f));
                for (var x = 0; x < newColumns; x++)
                {
                    for (var y = 0; y < newRows; y++)
                    {
                        var xOffset = x * stride;
                        var yOffset = y * stride;
                        var maxVal  = float.MinValue;
                        var bestX   = xOffset;
                        var bestY   = yOffset;
                        for (var i = 0; i < filterWidth; i++)
                        {
                            for (var j = 0; j < filterHeight; j++)
                            {
                                var xPos = xOffset + i;
                                var yPos = yOffset + j;
                                var val  = matrix[yPos, xPos];
                                if (val > maxVal)
                                {
                                    bestX  = xPos;
                                    bestY  = yPos;
                                    maxVal = val;
                                }
                            }
                        }
                        indexPos[Tuple.Create(bestX, bestY)] = Tuple.Create(x, y);
                        layer[y, x] = maxVal;
                    }
                }
                if (indexPosList != null)
                {
                    indexPosList.Add(indexPos);
                }
                ret.Add(layer);
            }
            return(new Cpu3DTensor(ret));
        }
Beispiel #8
0
        public Cpu3DTensor(IReadOnlyList <IIndexableMatrix> matrixList)
        {
            var first = matrixList.First();

            Debug.Assert(matrixList.All(m => m.RowCount == first.RowCount && m.ColumnCount == first.ColumnCount));
            _rows    = first.RowCount;
            _columns = first.ColumnCount;
            _depth   = matrixList.Count;

            var offset  = 0;
            var rowSize = _rows * _columns;
            var data    = new float[rowSize * _depth];

            foreach (var matrix in matrixList)
            {
                Array.Copy(matrix.GetInternalArray(), 0, data, offset, rowSize);
                offset += rowSize;
            }

            _data = new CpuMatrix(DenseMatrix.Build.Dense(rowSize, _depth, data));
        }
Beispiel #9
0
        public Cpu4DTensor(IReadOnlyList <IIndexable3DTensor> tensorList)
        {
            var first = tensorList.First();

            Debug.Assert(tensorList.All(m =>
                                        m.RowCount == first.RowCount && m.ColumnCount == first.ColumnCount &&
                                        m.Depth == first.Depth));
            RowCount    = first.RowCount;
            ColumnCount = first.ColumnCount;
            Depth       = first.Depth;
            Count       = tensorList.Count;
            var offset  = 0;
            var rowSize = RowCount * ColumnCount * Depth;
            var data    = new float[rowSize * Count];

            foreach (var matrix in tensorList)
            {
                Array.Copy(matrix.GetInternalArray(), 0, data, offset, rowSize);
                offset += rowSize;
            }

            _data = new CpuMatrix(DenseMatrix.Build.Dense(rowSize, Count, data));
        }