コード例 #1
0
 static internal unsafe void Get8DParametersNoAlloc(this TensorShape shape, int[] parameters, int *parameters8D, int defaultValue)
 {
     if (parameters.Length == TensorShape.MaxRank)
     {
         for (int i = 0; i < TensorShape.MaxRank; ++i)
         {
             parameters8D[i] = parameters[i];
         }
     }
     else
     {
         Assert.AreEqual(4, parameters.Length);
         if (!shape.Is4D())
         {
             Assert.IsTrue(false, $"4D Parameters {parameters} can't be used with a tensor of shape {shape} as it contains other dimensions, please use 8D parameters for this shape.");
         }
         parameters8D[0] = defaultValue;
         parameters8D[1] = defaultValue;
         parameters8D[2] = parameters[0];
         parameters8D[3] = defaultValue;
         parameters8D[4] = defaultValue;
         parameters8D[5] = parameters[1];
         parameters8D[6] = parameters[2];
         parameters8D[7] = parameters[3];
     }
 }
コード例 #2
0
        public virtual float[] Download(TensorShape shape)
        {
            Assert.IsTrue(shape.Is4D());

            var count = shape.length;

            Profiler.BeginSample("Barracuda.DownloadDataFromGPU");
            Assert.IsTrue(maxCapacity >= count);
            count = Math.Min(maxCapacity, count);

            m_AsyncDownloadSchedulingFrame = -1;

            int w = Mathf.Min(shape.length, MaxTextureSize);
            int h = Mathf.Max(1, ComputeHelper.IDivC(shape.length, w));

            Texture2D     texture   = new Texture2D(w, h, TextureFormat.RFloat, false);
            RenderTexture rttexture = new RenderTexture(w, h, 0, RenderTextureFormat.RFloat);


            Material material = new Material(PixelShaderSingleton.Instance.FindShader("Barracuda/TensorToBuffer"));


            material.SetVector("XdeclShape", new Vector4(shape.batch, shape.height, shape.width, shape.channels));
            material.SetTexture("Xdata", bufferAsTexture);
            material.SetInt("_OutputWidth", w);
            material.SetInt("_OutputHeight", h);

            Graphics.Blit(null, rttexture, material);


            var previousActiveRT = RenderTexture.active;

            RenderTexture.active = rttexture;
            Rect rectReadPicture = new Rect(0, 0, w, h);

            texture.ReadPixels(rectReadPicture, 0, 0);
            texture.Apply();

            var data = new float[count];

            Buffer.BlockCopy(texture.GetRawTextureData(), 0, data, 0, count * sizeof(float));

            RenderTexture.active = previousActiveRT;

            return(data);
        }
コード例 #3
0
        static internal int[] Get8DPermutationsForNCHWPermutationsAndShape(this TensorShape shape, int[] permutations)
        {
            if (permutations.Length == TensorShape.MaxRank)
            {
                return(permutations);
            }

            Assert.AreEqual(4, permutations.Length);
            if (!shape.Is4D())
            {
                Assert.IsTrue(false, $"4D Permutation {permutations} can't be used with a tensor of shape {shape} as it contains other dimensions, please use an 8D permutation for this shape.");
            }
            int batchOldAxis    = Convert4DTo8DAxis(permutations[0]);
            int channelOldIndex = Convert4DTo8DAxis(permutations[1]);
            int heightOldIndex  = Convert4DTo8DAxis(permutations[2]);
            int widthOldIndex   = Convert4DTo8DAxis(permutations[3]);

            return(new int[] { 0, 1, batchOldAxis, 3, 4, channelOldIndex, heightOldIndex, widthOldIndex });
        }
コード例 #4
0
        static internal NativeArray <int> Get8DPermutationsForNCHWPermutationsAndShape(this TensorShape shape, NativeArray <int> inPermutations)
        {
            if (!shape.hasNamedDimensions)
            {
                shape = shape.AsNamed();
            }

            if (inPermutations.Length == TensorShape.MaxRank)
            {
                return(inPermutations);
            }

            Assert.AreEqual(4, inPermutations.Length);
            if (!shape.Is4D())
            {
                Assert.IsTrue(false, $"4D Permutation {inPermutations.ToString()} can't be used with a tensor of shape {shape} as it contains other dimensions, please use an 8D permutation for this shape.");
            }
            int batchOldAxis    = Convert4DTo8DAxis(inPermutations[0]);
            int channelOldIndex = Convert4DTo8DAxis(inPermutations[1]);
            int heightOldIndex  = Convert4DTo8DAxis(inPermutations[2]);
            int widthOldIndex   = Convert4DTo8DAxis(inPermutations[3]);

            // Valid only for single frame
            NativeArray <int> outPermutations = new NativeArray <int>(8, Allocator.Temp);

            outPermutations[0] = 0;
            outPermutations[1] = 1;
            outPermutations[2] = batchOldAxis;
            outPermutations[3] = 3;
            outPermutations[4] = 4;
            outPermutations[5] = channelOldIndex;
            outPermutations[6] = heightOldIndex;
            outPermutations[7] = widthOldIndex;

            return(outPermutations);
        }