コード例 #1
0
        /// <inheritdoc/>
        public override Tensor DepthwiseConv2D(Tensor X, Tensor K, Tensor B, int[] stride, int[] pad, Layer.FusedActivation fusedActivation)
        {
            if (K.kernelDepth != 1)
            {
                return(base.DepthwiseConv2D(X, K, B, stride, pad, fusedActivation));
            }

            Assert.IsTrue(X.shape.Is4D());
            Assert.AreEqual(K.kernelDepth, 1);
            Assert.AreEqual(K.kernelCount, X.channels);
            Assert.AreEqual(K.kernelCount, B.flatWidth);
            Assert.AreEqual(B.flatWidth, B.length);
            Assert.AreEqual(stride.Length, 2);
            Assert.AreEqual(pad.Length, 4);

            var      Oshape   = X.shape.ApplyKernel(K.shape, stride, pad);
            Material material = new Material(PixelShaderSingleton.Instance.FindShader("Barracuda/DepthwiseConv2D"));

            SetTensor(material, "X", X);
            SetTensor(material, "K", K);
            SetTensor(material, "B", B);

            material.SetVector("_Stride", new Vector4(stride[0], stride[1], 0, 0));
            material.SetVector("_Pad", new Vector4(pad[0], pad[1], pad[2], pad[3]));
            material.SetInt("_ActivationMode", (int)(fusedActivation));

            var O = Dispatch(material, Oshape);

            if (!IsFusedActivationSupported(fusedActivation))
            {
                O = Activation(m_StringCache.Lookup("Barracuda/", fusedActivation.ToString()), O);
            }

            return(O);
        }
コード例 #2
0
        /// <inheritdoc/>
        public override Tensor Normalization(Tensor X, Tensor S, Tensor B, int pool, int axis, float epsilon, Layer.FusedActivation fusedActivation)
        {
            if (!X.shape.Is4D())
            {
                throw new NotImplementedException();
            }

            if (axis != TensorShape.C && axis != -1)
            {
                return(base.Normalization(X, S, B, pool, axis, epsilon, fusedActivation));
            }

            if (pool == 1 && X.batch != 1)
            {
                return(base.Normalization(X, S, B, pool, axis, epsilon, fusedActivation)); // @TODO: Instance Normalization with batch > 1
            }
            if (pool <= 0)
            {
                pool = X.batch;
            }

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

            material.SetFloat("_Epsilon", epsilon);
            material.SetInt("_ActivationMode", (int)fusedActivation);

            SetTensor(material, "X", X);
            SetTensor(material, "W", S);
            SetTensor(material, "B", B);

            var O = Dispatch(material, X.shape);

            if (!IsFusedActivationSupported(fusedActivation))
            {
                O = Activation(m_StringCache.Lookup("Barracuda/", fusedActivation.ToString()), O);
            }

            return(O);
        }
コード例 #3
0
        /// <inheritdoc/>
        public override Tensor Dense(Tensor X, Tensor W, Tensor B, Layer.FusedActivation fusedActivation)
        {
            Assert.IsTrue(W.dimensions <= 2);
            Assert.AreEqual(B.flatWidth, B.length);
            Assert.AreEqual(X.flatWidth, W.flatHeight);

            var Oshape = new TensorShape(X.flatHeight, W.flatWidth);

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

            SetTensor(material, "X", X);
            SetTensor(material, "W", W);
            SetTensor(material, "B", B);
            material.SetInt("_ActivationMode", (int)fusedActivation);

            var O = Dispatch(material, Oshape);

            if (!IsFusedActivationSupported(fusedActivation))
            {
                O = Activation(m_StringCache.Lookup("Barracuda/", fusedActivation.ToString()), O);
            }

            return(O);
        }
コード例 #4
0
        /// <inheritdoc/>
        public override Tensor Conv2DTrans(Tensor X, Tensor K, Tensor B, int[] stride, int[] pad, int[] outputAdjustment, Layer.FusedActivation fusedActivation)
        {
            Assert.IsTrue(X.shape.Is4D());
            Assert.AreEqual(X.channels, K.kernelDepth);
            Assert.AreEqual(K.kernelCount, B.flatWidth);
            Assert.AreEqual(B.flatWidth, B.length);
            Assert.AreEqual(stride.Length, 2);
            Assert.AreEqual(pad.Length, 4);

            var      Oshape   = X.shape.ApplyKernelInverse(K.shape, stride, pad, outputAdjustment);
            Material material = new Material(PixelShaderSingleton.Instance.FindShader("Barracuda/Conv2DTrans"));

            // one pass version
            pad = new int[]
            {
                K.kernelWidth - pad[0] - 1, K.kernelHeight - pad[1] - 1,
                K.kernelWidth - pad[2] - 1, K.kernelHeight - pad[3] - 1
            };

            SetTensor(material, "X", X);
            SetTensor(material, "K", K);
            SetTensor(material, "B", B);

            material.SetVector("_Stride", new Vector4(stride[0], stride[1], 0, 0));
            material.SetVector("_Pad", new Vector4(pad[0], pad[1], 0, 0));
            material.SetInt("_ActivationMode", (int)(fusedActivation));

            var O = Dispatch(material, Oshape);

            if (!IsFusedActivationSupported(fusedActivation))
            {
                O = Activation(m_StringCache.Lookup("Barracuda/", fusedActivation.ToString()), O);
            }

            return(O);
        }