Пример #1
0
        /** @copydoc Layer::dispose */
        protected override void dispose()
        {
            foreach (PoolingLayer <T> layer in m_rgPoolingLayers)
            {
                layer.Dispose();
            }

            m_rgPoolingLayers.Clear();

            if (m_colBlobSplitTopVec != null)
            {
                m_colBlobSplitTopVec.Dispose();
                m_colBlobSplitTopVec = null;
            }

            if (m_split_layer != null)
            {
                m_split_layer.Dispose();
                m_split_layer = null;
            }

            if (m_colBlobPoolingOutputs != null)
            {
                m_colBlobPoolingOutputs.Dispose();
                m_colBlobPoolingOutputs = null;
            }

            if (m_colBlobFlattenOutputs != null)
            {
                m_colBlobFlattenOutputs.Dispose();
                m_colBlobFlattenOutputs = null;
            }

            foreach (FlattenLayer <T> layer in m_rgFlattenLayers)
            {
                layer.Dispose();
            }

            m_rgFlattenLayers.Clear();

            m_rgPoolingBottomVec.Clear();
            m_rgPoolingTopVecs.Clear();
            m_rgFlattenLayerTopVecs.Clear();

            base.dispose();
        }
Пример #2
0
        /// <summary>
        /// Setup the layer for both Engine.CUDNN and Engine.CAFFE modes.
        /// </summary>
        /// <param name="colBottom">Specifies the collection of bottom (input) Blobs.</param>
        /// <param name="colTop">Specifies the collection of top (output) Blobs.</param>
        public override void LayerSetUp(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            m_nSize = (int)m_param.lrn_param.local_size;
            m_log.CHECK_EQ(m_nSize % 2, 1, "LRN only supports odd values for local_size.");
            m_nPrePad = (m_nSize - 1) / 2;
            m_dfAlpha = m_param.lrn_param.alpha;
            m_dfBeta  = m_param.lrn_param.beta;
            m_dfK     = m_param.lrn_param.k;

            if (m_param.lrn_param.norm_region == LRNParameter.NormRegion.WITHIN_CHANNEL)
            {
                // Set up split_layer to use in the numerator and denominator.
                m_colSplitTopVec = new BlobCollection <T>();
                m_colSplitTopVec.Add(m_blobProductInput);
                m_colSplitTopVec.Add(m_blobSquareInput);
                LayerParameter split_param = new LayerParameter(LayerParameter.LayerType.SPLIT, "split");
                m_splitLayer = new SplitLayer <T>(m_cuda, m_log, split_param);
                m_splitLayer.Setup(colBottom, m_colSplitTopVec);

                // Set up square_layer to square teh inputs.
                m_colSquareBottomVec = new BlobCollection <T>();
                m_colSquareTopVec    = new BlobCollection <T>();
                m_colSquareBottomVec.Add(m_blobSquareInput);
                m_colSquareTopVec.Add(m_blobSquareOutput);
                LayerParameter square_param = new LayerParameter(LayerParameter.LayerType.POWER, "square");
                square_param.power_param.power = 2.0;
                m_squareLayer = new PowerLayer <T>(m_cuda, m_log, square_param);
                m_squareLayer.Setup(m_colSquareBottomVec, m_colSquareTopVec);

                // Set up pool_layer to sum over square neighborhoods of the input.
                m_colPoolTopVec = new BlobCollection <T>();
                m_colPoolTopVec.Add(m_blobPoolOutput);
                LayerParameter pool_param = new LayerParameter(LayerParameter.LayerType.POOLING, "pool");
                pool_param.pooling_param.pool = PoolingParameter.PoolingMethod.AVE;
                pool_param.pooling_param.pad.Add((uint)m_nPrePad);
                pool_param.pooling_param.kernel_size.Add((uint)m_nSize);
                m_poolLayer = new PoolingLayer <T>(m_cuda, m_log, pool_param);
                m_poolLayer.Setup(m_colSquareTopVec, m_colPoolTopVec);

                // Set up power_layer to compute (1 + alpha/N^2 s)^-beta, where s is
                // the sum of the squared neighborhood (the output of pool_layer)
                m_colPowerTopVec = new BlobCollection <T>();
                m_colPowerTopVec.Add(m_blobPowerOutput);
                LayerParameter power_param = new LayerParameter(LayerParameter.LayerType.POWER, "power");
                power_param.power_param.power = -m_dfBeta;
                power_param.power_param.scale = m_dfAlpha;
                power_param.power_param.shift = 1.0;
                m_powerLayer = new PowerLayer <T>(m_cuda, m_log, power_param);
                m_powerLayer.Setup(m_colPoolTopVec, m_colPowerTopVec);

                // Set up a product_layer to compute outputs by multiplying inputs by the
                // inverse denominator computed by the power layer.
                m_colProductBottomVec = new BlobCollection <T>();
                m_colProductBottomVec.Add(m_blobProductInput);
                m_colProductBottomVec.Add(m_blobPowerOutput);
                LayerParameter product_param = new LayerParameter(LayerParameter.LayerType.ELTWISE, "product");
                product_param.eltwise_param.operation = EltwiseParameter.EltwiseOp.PROD;
                m_productLayer = new EltwiseLayer <T>(m_cuda, m_log, product_param);
                m_productLayer.Setup(m_colProductBottomVec, colTop);
            }

            if (!m_param.lrn_param.useCudnn())
            {
                return;
            }

            m_hCuDnn      = m_cuda.CreateCuDNN();
            m_hNormDesc   = m_cuda.CreateLRNDesc();
            m_hBottomDesc = m_cuda.CreateTensorDesc();
            m_hTopDesc    = m_cuda.CreateTensorDesc();
        }
Пример #3
0
        /** @copydoc Layer::dispose */
        protected override void dispose()
        {
            m_blobScale.Dispose();
            m_blobSquareInput.Dispose();
            m_blobSquareOutput.Dispose();
            m_blobPoolOutput.Dispose();
            m_blobPowerOutput.Dispose();
            m_blobProductInput.Dispose();

            if (m_splitLayer != null)
            {
                m_splitLayer.Dispose();
                m_splitLayer = null;
            }

            if (m_squareLayer != null)
            {
                m_squareLayer.Dispose();
                m_squareLayer = null;
            }

            if (m_poolLayer != null)
            {
                m_poolLayer.Dispose();
                m_poolLayer = null;
            }

            if (m_powerLayer != null)
            {
                m_powerLayer.Dispose();
                m_powerLayer = null;
            }

            if (m_powerLayer != null)
            {
                m_productLayer.Dispose();
                m_powerLayer = null;
            }

            if (m_hNormDesc != 0)
            {
                m_cuda.FreeLRNDesc(m_hNormDesc);
                m_hNormDesc = 0;
            }

            if (m_hBottomDesc != 0)
            {
                m_cuda.FreeTensorDesc(m_hBottomDesc);
                m_hBottomDesc = 0;
            }

            if (m_hTopDesc != 0)
            {
                m_cuda.FreeTensorDesc(m_hTopDesc);
                m_hTopDesc = 0;
            }

            if (m_hCuDnn != 0)
            {
                m_cuda.FreeCuDNN(m_hCuDnn);
                m_hCuDnn = 0;
            }

            if (m_hTempData1 != 0)
            {
                m_cuda.FreeMemory(m_hTempData1);
                m_hTempData1 = 0;
            }

            if (m_hTempData2 != 0)
            {
                m_cuda.FreeMemory(m_hTempData2);
                m_hTempData2 = 0;
            }

            base.dispose();
        }
Пример #4
0
        /// <summary>
        /// Setup the layer.
        /// </summary>
        /// <param name="colBottom">Specifies the collection of bottom (input) Blobs.</param>
        /// <param name="colTop">Specifies the collection of top (output) Blobs.</param>
        public override void LayerSetUp(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            m_nNum               = colBottom[0].num;
            m_nChannels          = colBottom[0].channels;
            m_nBottomH           = colBottom[0].height;
            m_nBottomW           = colBottom[0].width;
            m_bReshapedFirstTime = false;

            m_log.CHECK_GT(m_nBottomH, 0, "Input dimensions cannot be zero.");
            m_log.CHECK_GT(m_nBottomW, 0, "Input dimensions cannot be zero.");

            m_nPyramidHeight = (int)m_param.spp_param.pyramid_height;

            m_colBlobSplitTopVec     = new BlobCollection <T>();
            m_rgPoolingBottomVec     = new List <BlobCollection <T> >();
            m_rgPoolingLayers        = new List <PoolingLayer <T> >();
            m_rgPoolingTopVecs       = new List <BlobCollection <T> >();
            m_colBlobPoolingOutputs  = new BlobCollection <T>();
            m_rgFlattenLayers        = new List <FlattenLayer <T> >();
            m_rgFlattenLayerTopVecs  = new List <BlobCollection <T> >();
            m_colBlobFlattenOutputs  = new BlobCollection <T>();
            m_colBlobConcatBottomVec = new BlobCollection <T>();

            if (m_nPyramidHeight == 1)
            {
                // pooling layer setup
                LayerParameter pp = getPoolingParam(0, m_nBottomH, m_nBottomW, m_param.spp_param);
                m_rgPoolingLayers.Add(new PoolingLayer <T>(m_cuda, m_log, pp));
                m_rgPoolingLayers[0].Setup(colBottom, colTop);
                return;
            }

            // split layer output holders setup
            for (int i = 0; i < m_nPyramidHeight; i++)
            {
                m_colBlobSplitTopVec.Add(new Blob <T>(m_cuda, m_log));
            }

            // split layer setup
            LayerParameter split_param = new param.LayerParameter(LayerParameter.LayerType.SPLIT);

            m_split_layer = new SplitLayer <T>(m_cuda, m_log, split_param);
            m_split_layer.Setup(colBottom, m_colBlobSplitTopVec);

            for (int i = 0; i < m_nPyramidHeight; i++)
            {
                // pooling layer input holders setup
                m_rgPoolingBottomVec.Add(new BlobCollection <T>());
                m_rgPoolingBottomVec[i].Add(m_colBlobSplitTopVec[i]);

                // pooling layer output holders setup
                m_colBlobPoolingOutputs.Add(new Blob <T>(m_cuda, m_log));
                m_rgPoolingTopVecs.Add(new BlobCollection <T>());
                m_rgPoolingTopVecs[i].Add(m_colBlobPoolingOutputs[i]);

                // pooling layer setup
                LayerParameter pooling_param = getPoolingParam(i, m_nBottomH, m_nBottomW, m_param.spp_param);
                m_rgPoolingLayers.Add(new PoolingLayer <T>(m_cuda, m_log, pooling_param));
                m_rgPoolingLayers[i].Setup(m_rgPoolingBottomVec[i], m_rgPoolingTopVecs[i]);

                // flatten layer output holders setup
                m_colBlobFlattenOutputs.Add(new Blob <T>(m_cuda, m_log));
                m_rgFlattenLayerTopVecs.Add(new BlobCollection <T>());
                m_rgFlattenLayerTopVecs[i].Add(m_colBlobFlattenOutputs[i]);

                // flatten layer setup
                LayerParameter flatten_param = new LayerParameter(LayerParameter.LayerType.FLATTEN);
                m_rgFlattenLayers.Add(new FlattenLayer <T>(m_cuda, m_log, flatten_param));
                m_rgFlattenLayers[i].Setup(m_rgPoolingTopVecs[i], m_rgFlattenLayerTopVecs[i]);

                // concat layer input holders setup
                m_colBlobConcatBottomVec.Add(m_colBlobFlattenOutputs[i]);
            }

            // concat layer setup
            LayerParameter concat_param = new LayerParameter(LayerParameter.LayerType.CONCAT);

            m_concat_layer = new ConcatLayer <T>(m_cuda, m_log, concat_param);
            m_concat_layer.Setup(m_colBlobConcatBottomVec, colTop);
        }