Beispiel #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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Conv2s the forward.
        /// </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="cd">The cd.</param>
        public void Conv2Forward(NDArray input, NDArray output, NDArray weight, NDArray bias, NDArray finput, ConvolutionDesc2d cd)
        {
            var batchSize    = input.Shape[0];
            var nInputPlane  = input.Shape[1];
            var inputWidth   = input.Shape[3];
            var inputHeight  = input.Shape[2];
            var nOutputPlane = weight.Shape[0];

            var outputWidth  = (inputWidth + 2 * cd.padW - cd.kW) / cd.dW + 1;
            var outputHeight = (inputHeight + 2 * cd.padH - cd.kH) / cd.dH + 1;


            for (long i = 0; i < batchSize; ++i)
            {
                using (var input_i = input.Select(0, i))
                    using (var output_i = output.Select(0, i))
                    {
                        using (var output2d = output_i.View(nOutputPlane, outputHeight * outputWidth))
                        {
                            if (bias != null)
                            {
                                using (var biasExp = bias.Expand(nOutputPlane, output2d.Shape[1]))
                                {
                                    Ops.Copy(output2d, biasExp);
                                }
                            }
                            else
                            {
                                Ops.Fill(output_i, 0);
                            }

                            im2colKernels.Im2Col(input_i, finput, (int)nInputPlane, (int)inputHeight, (int)inputWidth,
                                                 cd.kH, cd.kW, cd.padH, cd.padW, cd.dH, cd.dW, 1, 1);

                            Ops.Addmm(output2d, 1, output2d, 1, weight, finput);
                        }
                    }
            }
        }