Ejemplo n.º 1
0
Archivo: xnor.cs Proyecto: fel88/Xnor
        /*public static void memcpy(float[] a1, long dst, float[] a2, long dst2, long size)
         * {
         *  for (int i = 0; i < size / 4; i++)
         *  {
         *      a1[i + dst] = a2[i + dst2];
         *  }
         * }*/
        /*
         *  THFloatTensor *input,
         *                                      THFloatTensor *output,
         *                                      THIntTensor *weight,
         *                                      THFloatTensor *bias,
         *                                      THFloatTensor *columns,
         *                                      THFloatTensor *alphas,
         *                                      int kH, int kW,
         *                                      int dH, int dW,
         *                                      int padH, int padW)*/
        public static void THNN_Bin_SpatialConvolutionMM_updateOutput(
            IntPtr input,
            IntPtr output,
            IntPtr weight,
            IntPtr bias,
            IntPtr columns,
            IntPtr alphas,
            int kH, int kW,
            int dH, int dW,
            int padH, int padW
            , bool quantOutput = false)
        {
            int ndim = THWrapper.THFloatTensor_nDimension(input);
            int dimf = 0;
            int dimh = 1;
            int dimw = 2;

            if (ndim == 4)
            {
                dimf++;
                dimh++;
                dimw++;
            }
            var nInputPlane = THWrapper.THFloatTensor_size(input, dimf);
            var inputHeight = THWrapper.THFloatTensor_size(input, dimh);
            var inputWidth  = THWrapper.THFloatTensor_size(input, dimw);

            var nOutputPlane = THWrapper.THFloatTensor_size(weight, 0);
            var outputHeight = (inputHeight + 2 * padH - kH) / dH + 1;
            var outputWidth  = (inputWidth + 2 * padW - kW) / dW + 1;

            //InternalArray ones = new InternalArray(new int[] { 1 });
            IntPtr ones = THWrapper.THFloatTensor_new();

            if (bias != null && THWrapper.THFloatTensor_nDimension(bias) == 1)
            {
                THWrapper.THFloatTensor_resize2d(bias, THWrapper.THFloatTensor_size(bias, 0), 1);
                //THFloatTensor_resize2d(bias, bias.Shape[0], 1);
            }
            THWrapper.THFloatTensor_resize2d(ones, 1, outputHeight * outputWidth);
            //THFloatTensor_resize2d(ones, 1, outputHeight * outputWidth);
            THWrapper.THFloatTensor_fill(ones, 1);
            //THFloatTensor_fill(ones, 1);

            var T = THWrapper.THFloatTensor_size(input, 0);
            //InternalArray bin_col = new InternalArray(new int[] { 1 });
            var bin_col = THWrapper.THIntTensor_new();

            THWrapper.THFloatTensor_resize4d(output, T, (int)nOutputPlane, outputHeight, outputWidth);
            //THFloatTensor_resize4d(output, T, (int)nOutputPlane, outputHeight, outputWidth);
            THWrapper.THFloatTensor_resize3d(columns, T, kW * kH * nInputPlane, outputHeight * outputWidth);
            //THFloatTensor_resize3d(columns, T, kW * kH * nInputPlane, outputHeight * outputWidth);
            THWrapper.THIntTensor_resize3d(bin_col, T, (int)nOutputPlane, outputHeight * outputWidth);
            //THIntTensor_resize3d(bin_col, T, (int)nOutputPlane, outputHeight * outputWidth);

            for (int t = 0; t < T; t++)
            {
                /*var input_t = input.Get2DImageFrom4DArray(0, t);
                 * var columns_t = columns.Get2DImageFrom4DArray(0, t);
                 * var bin_col_t = bin_col.Get2DImageFrom4DArray(0, t);*/


                //var _bin_col = bin_col.ToTHTensor();
                var input_t   = THWrapper.THFloatTensor_newSelect(input, 0, t);
                var columns_t = THWrapper.THFloatTensor_newSelect(columns, 0, t);
                var bin_col_t = THWrapper.THIntTensor_newSelect(bin_col, 0, t);

                /* var bbb = InternalArray.FromTHFloatTensor(columns_t);
                 * for (int i = 0; i < bbb.Data.Length; i++)
                 * {
                 *   var res = bbb.Data[i];
                 *   if (res != 0)
                 *   {
                 *
                 *   }
                 *
                 * }*/
                THNN_unfolded_copy(
                    columns_t, input_t, kW, kH, dW, dH, padW, padH,
                    (int)nInputPlane, (int)inputWidth, (int)inputHeight, (int)outputWidth, (int)outputHeight
                    );
                //  bbb = InternalArray.FromTHFloatTensor(columns_t);

                //Debug.Assert(bbb.Data[1800] == -0.053401);
                //Debug.Assert(bbb.Data[4200] == -0.852360);

                /* for (int i = 0; i < bbb.Data.Length; i++)
                 * {
                 *   var res = bbb.Data[i];
                 *   if (res != 0)
                 *   {
                 *
                 *   }
                 * }*/
                encode_cols_cpu(columns_t, bin_col_t);
                //  var bb = InternalArray.FromTHIntTensor(bin_col_t);

                /* for (int i = 0; i < bb.IntData.Length; i++)
                 * {
                 *   var res = bb.IntData[i];
                 *   if (res != 0)
                 *   {
                 *
                 *   }
                 * }*/
            }
            for (int t = 0; t < T; t++)
            {
                /* THFloatTensor* output_t = THFloatTensor_newSelect(output, 0, t);
                 * THIntTensor* bin_col_t = THIntTensor_newSelect(bin_col, 0, t);
                 */
                /*var output_t = output.Get2DImageFrom4DArray(0, t);
                 * var bin_col_t = bin_col.Get2DImageFrom4DArray(0, t);*/
                //var _output = output.ToTHTensor();
                //var _bin_col = bin_col.ToTHTensor();
                var output_t  = THWrapper.THFloatTensor_newSelect(output, 0, t);
                var bin_col_t = THWrapper.THIntTensor_newSelect(bin_col, 0, t);
                THNN_Bin_SpatialConvolutionMM_updateOutput_frame(
                    output_t, weight, bias, ones, bin_col_t, alphas, kW, kH, dW, dH, padW, padH,
                    nInputPlane, inputWidth, inputHeight, nOutputPlane, outputWidth, outputHeight, quantOutput
                    );
            }
        }