Exemple #1
0
        /// <summary>
        /// Conv2s the forward frame.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="output">The output.</param>
        /// <param name="weight">The weight.</param>
        /// <param name="bias">The bias.</param>
        /// <param name="finput">The finput.</param>
        /// <param name="kW">The k w.</param>
        /// <param name="kH">The k h.</param>
        /// <param name="dW">The d w.</param>
        /// <param name="dH">The d h.</param>
        /// <param name="padW">The pad w.</param>
        /// <param name="padH">The pad h.</param>
        /// <param name="nInputPlane">The n input plane.</param>
        /// <param name="inputWidth">Width of the input.</param>
        /// <param name="inputHeight">Height of the input.</param>
        /// <param name="nOutputPlane">The n output plane.</param>
        /// <param name="outputWidth">Width of the output.</param>
        /// <param name="outputHeight">Height of the output.</param>
        private static void Conv2ForwardFrame(NDArray input, NDArray output, NDArray weight, NDArray bias, NDArray finput,
                                              int kW,
                                              int kH,
                                              int dW,
                                              int dH,
                                              int padW,
                                              int padH,
                                              long nInputPlane,
                                              long inputWidth,
                                              long inputHeight,
                                              long nOutputPlane,
                                              long outputWidth,
                                              long outputHeight)
        {
            var inputRef  = NativeWrapper.AllocTensorRef(input);
            var finputRef = NativeWrapper.AllocTensorRef(finput);

            var inputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));

            Marshal.StructureToPtr(inputRef, inputPtr, false);
            var finputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));

            Marshal.StructureToPtr(finputRef, finputPtr, false);

            try
            {
                CpuOpsNative.TS_Unfolded_Copy(finputPtr, inputPtr, kW, kH, dW, dH, padW, padH, (int)nInputPlane, (int)inputWidth, (int)inputHeight, (int)outputWidth, (int)outputHeight);

                using (var output2d = output.View(nOutputPlane, outputHeight * outputWidth))
                {
                    if (bias != null)
                    {
                        using (var biasExp = bias.Expand(nOutputPlane, output2d.Shape[1]))
                        {
                            Ops.Copy(output2d, biasExp);
                        }
                    }
                    else
                    {
                        Ops.Fill(output, 0);
                    }

                    Ops.Addmm(output2d, 1, output2d, 1, weight, finput);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(inputPtr);
                Marshal.FreeHGlobal(finputPtr);
                NativeWrapper.FreeTensorRef(inputRef);
                NativeWrapper.FreeTensorRef(finputRef);
            }
        }
Exemple #2
0
        // allArgs should start with a null placeholder for the RNG object
        /// <summary>
        /// Invokes the with RNG.
        /// </summary>
        /// <param name="seed">The seed.</param>
        /// <param name="method">The method.</param>
        /// <param name="allArgs">All arguments.</param>
        private static void InvokeWithRng(int?seed, MethodInfo method, params object[] allArgs)
        {
            if (!seed.HasValue)
            {
                seed = seedGen.Next();
            }

            IntPtr rng;

            NativeWrapper.CheckResult(CpuOpsNative.TS_NewRNG(out rng));
            NativeWrapper.CheckResult(CpuOpsNative.TS_SetRNGSeed(rng, seed.Value));
            allArgs[0] = rng;
            NativeWrapper.InvokeTypeMatch(method, allArgs);
            NativeWrapper.CheckResult(CpuOpsNative.TS_DeleteRNG(rng));
        }
Exemple #3
0
        /// <summary>
        /// Conv2s the backward input frame.
        /// </summary>
        /// <param name="gradOutput">The grad output.</param>
        /// <param name="gradInput">The grad input.</param>
        /// <param name="weight">The weight.</param>
        /// <param name="fgradInput">The fgrad input.</param>
        /// <param name="cd">The cd.</param>
        private static void Conv2BackwardInputFrame(NDArray gradOutput, NDArray gradInput, NDArray weight, NDArray fgradInput, ConvolutionDesc2d cd)
        {
            using (var gradOutput2d = gradOutput.View(gradOutput.Shape[0], gradOutput.Shape[1] * gradOutput.Shape[2]))
            {
                Ops.Addmm(fgradInput, 0, fgradInput, 1, weight, gradOutput2d);
            }

            Ops.Fill(gradInput, 0);

            IntPtr fgradInputPtr, gradInputPtr;

            using (NativeWrapper.BuildTensorRefPtr(fgradInput, out fgradInputPtr))
                using (NativeWrapper.BuildTensorRefPtr(gradInput, out gradInputPtr))
                {
                    CpuOpsNative.TS_Unfolded_Acc(fgradInputPtr, gradInputPtr, cd.kW, cd.kH, cd.dW, cd.dH, cd.padW, cd.padH,
                                                 (int)gradInput.Shape[0], (int)gradInput.Shape[2], (int)gradInput.Shape[1],
                                                 (int)gradOutput.Shape[2], (int)gradOutput.Shape[1]);
                }
        }
Exemple #4
0
        /// <summary>
        /// Spatials the maximum pooling backward.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="gradOutput">The grad output.</param>
        /// <param name="gradInput">The grad input.</param>
        /// <param name="indices">The indices.</param>
        /// <param name="cd">The cd.</param>
        /// <param name="ceilMode">if set to <c>true</c> [ceil mode].</param>
        public static void SpatialMaxPoolingBackward(NDArray input, NDArray gradOutput, NDArray gradInput, NDArray indices, ConvolutionDesc2d cd, bool ceilMode)
        {
            var dimw = 3;
            var dimh = 2;
            var dimc = 1;

            var nbatch  = input.Shape[0];
            var nslices = input.Shape[dimc];
            var iheight = input.Shape[dimh];
            var iwidth  = input.Shape[dimw];
            var owidth  = gradOutput.Shape[dimw];
            var oheight = gradOutput.Shape[dimh];

            Ops.Fill(gradInput, 0);


            using (var gradOutputContig = Ops.AsContiguous(gradOutput))
            {
                for (int i = 0; i < nbatch; ++i)
                {
                    using (var gradInput_i = gradInput.Select(0, i))
                        using (var gradOutput_i = gradOutputContig.Select(0, i))
                            using (var indices_i = indices.Select(0, i))
                            {
                                IntPtr gradInput_iPtr, gradOutput_iPtr, indices_iPtr;
                                using (NativeWrapper.BuildTensorRefPtr(gradInput_i, out gradInput_iPtr))
                                    using (NativeWrapper.BuildTensorRefPtr(gradOutput_i, out gradOutput_iPtr))
                                        using (NativeWrapper.BuildTensorRefPtr(indices_i, out indices_iPtr))
                                        {
                                            CpuOpsNative.TS_SpatialMaxPooling_updateGradInput_frame(gradInput_iPtr, gradOutput_iPtr, indices_iPtr,
                                                                                                    nslices, iwidth, iheight,
                                                                                                    owidth, oheight,
                                                                                                    cd.dW, cd.dH);
                                        }
                            }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Spatials the maximum pooling forward.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="output">The output.</param>
        /// <param name="indices">The indices.</param>
        /// <param name="cd">The cd.</param>
        /// <param name="ceilMode">if set to <c>true</c> [ceil mode].</param>
        /// <exception cref="ArgumentException">input must be a 4D tensor</exception>
        /// <exception cref="InvalidOperationException">
        /// input image is smaller than kernel size
        /// or
        /// pad should be smaller than half of the kernel size
        /// </exception>
        public static void SpatialMaxPoolingForward(NDArray input, NDArray output, NDArray indices, ConvolutionDesc2d cd, bool ceilMode)
        {
            if (input.DimensionCount != 4)
            {
                throw new ArgumentException("input must be a 4D tensor");
            }

            var dimw = 3;
            var dimh = 2;
            var dimc = 1;

            if (input.Shape[dimw] < cd.kW - cd.padW || input.Shape[dimh] < cd.kH - cd.padH)
            {
                throw new InvalidOperationException("input image is smaller than kernel size");
            }

            if (cd.padW > cd.kW / 2 || cd.padH > cd.kH / 2)
            {
                throw new InvalidOperationException("pad should be smaller than half of the kernel size");
            }

            var nbatch  = input.Shape[0];
            var nslices = input.Shape[dimc];
            var iheight = input.Shape[dimh];
            var iwidth  = input.Shape[dimw];

            long owidth;
            long oheight;

            if (ceilMode)
            {
                oheight = (long)(Math.Ceiling((float)(iheight - cd.kH + 2 * cd.padH) / cd.dH)) + 1;
                owidth  = (long)(Math.Ceiling((float)(iwidth - cd.kW + 2 * cd.padW) / cd.dW)) + 1;
            }
            else
            {
                oheight = (long)(Math.Floor((float)(iheight - cd.kH + 2 * cd.padH) / cd.dH)) + 1;
                owidth  = (long)(Math.Floor((float)(iwidth - cd.kW + 2 * cd.padW) / cd.dW)) + 1;
            }

            if (cd.padW != 0 || cd.padH != 0)
            {
                // ensure that the last pooling starts inside the image
                if ((oheight - 1) * cd.dH >= iheight + cd.padH)
                {
                    --oheight;
                }
                if ((owidth - 1) * cd.dW >= iwidth + cd.padW)
                {
                    --owidth;
                }
            }

            using (var inputContig = Ops.AsContiguous(input))
            {
                for (int i = 0; i < nbatch; ++i)
                {
                    using (var input_i = inputContig.Select(0, i))
                        using (var output_i = output.Select(0, i))
                            using (var indices_i = indices.Select(0, i))
                            {
                                IntPtr input_iPtr, output_iPtr, indices_iPtr;
                                using (NativeWrapper.BuildTensorRefPtr(input_i, out input_iPtr))
                                    using (NativeWrapper.BuildTensorRefPtr(output_i, out output_iPtr))
                                        using (NativeWrapper.BuildTensorRefPtr(indices_i, out indices_iPtr))
                                        {
                                            CpuOpsNative.TS_SpatialMaxPooling_updateOutput_frame(input_iPtr, output_iPtr, indices_iPtr,
                                                                                                 nslices, iwidth, iheight,
                                                                                                 owidth, oheight,
                                                                                                 cd.kW, cd.kH, cd.dW, cd.dH, cd.padW, cd.padH);
                                        }
                            }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Gets the last error.
        /// </summary>
        /// <returns>System.String.</returns>
        private static string GetLastError()
        {
            var strPtr = CpuOpsNative.TS_GetLastError();

            return(Marshal.PtrToStringAnsi(strPtr));
        }