예제 #1
0
파일: LRNLayer.cs 프로젝트: lulzzz/MyCaffe
        /// <summary>
        /// Reshape the bottom (input) and top (output) blobs.
        /// </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 Reshape(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            m_log.CHECK_EQ(4, colBottom[0].num_axes, "Input must have 4 axes, corresponding to (num, channels, height, width)");
            m_nNum      = colBottom[0].num;
            m_nChannels = colBottom[0].channels;
            m_nHeight   = colBottom[0].height;
            m_nWidth    = colBottom[0].width;

            switch (m_param.lrn_param.norm_region)
            {
            case LRNParameter.NormRegion.ACROSS_CHANNELS:
                colTop[0].Reshape(m_nNum, m_nChannels, m_nHeight, m_nWidth);
                if (!m_param.lrn_param.useCudnn())
                {
                    m_blobScale.Reshape(m_nNum, m_nChannels, m_nHeight, m_nWidth);
                }
                break;

            case LRNParameter.NormRegion.WITHIN_CHANNEL:
                m_splitLayer.Reshape(colBottom, m_colSplitTopVec);
                m_squareLayer.Reshape(m_colSquareBottomVec, m_colSquareTopVec);
                m_poolLayer.Reshape(m_colSquareTopVec, m_colPoolTopVec);
                m_powerLayer.Reshape(m_colPoolTopVec, m_colPowerTopVec);
                m_productLayer.Reshape(m_colProductBottomVec, colTop);
                break;
            }

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

            m_cuda.SetTensorDesc(m_hBottomDesc, m_nNum, m_nChannels, m_nHeight, m_nWidth);
            m_cuda.SetTensorDesc(m_hTopDesc, m_nNum, m_nChannels, m_nHeight, m_nWidth);
            m_cuda.SetLRNDesc(m_hNormDesc, (uint)m_nSize, m_dfAlpha, m_dfBeta, m_dfK);

            if (m_param.lrn_param.norm_region == LRNParameter.NormRegion.WITHIN_CHANNEL)
            {
                int nTotalSize = m_nNum * m_nChannels * m_nHeight * m_nWidth;

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

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

                    m_hTempData1    = m_cuda.AllocMemory(nTotalSize);
                    m_hTempData2    = m_cuda.AllocMemory(nTotalSize);
                    m_nTempDataSize = nTotalSize;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Reshape the bottom (input) and top (output) blobs.
        /// </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 Reshape(BlobCollection <T> colBottom, BlobCollection <T> colTop)
        {
            m_log.CHECK_EQ(4, colBottom[0].num_axes, "Input must have 4 axes, corresponding to (num, channels, height, width)");

            // Do nothing if bottom shape is unchanged since last Reshape.
            if (m_nNum == colBottom[0].num &&
                m_nChannels == colBottom[0].channels &&
                m_nBottomH == colBottom[0].height &&
                m_nBottomW == colBottom[0].width &&
                m_bReshapedFirstTime)
            {
                return;
            }

            m_nNum               = colBottom[0].num;
            m_nChannels          = colBottom[0].channels;
            m_nBottomH           = colBottom[0].height;
            m_nBottomW           = colBottom[0].width;
            m_bReshapedFirstTime = true;

            if (m_nPyramidHeight == 1)
            {
                LayerParameter pooling_param = getPoolingParam(0, m_nBottomH, m_nBottomW, m_param.spp_param);

                if (m_rgPoolingLayers[0] != null)
                {
                    m_rgPoolingLayers[0].Dispose();
                }

                m_rgPoolingLayers[0] = new PoolingLayer <T>(m_cuda, m_log, pooling_param);
                m_rgPoolingLayers[0].Setup(colBottom, colTop);
                m_rgPoolingLayers[0].Reshape(colBottom, colTop);
                return;
            }

            m_split_layer.Reshape(colBottom, m_colBlobSplitTopVec);

            for (int i = 0; i < m_nPyramidHeight; i++)
            {
                LayerParameter pooling_param = getPoolingParam(i, m_nBottomH, m_nBottomW, m_param.spp_param);

                if (m_rgPoolingLayers[i] != null)
                {
                    m_rgPoolingLayers[i].Dispose();
                }

                m_rgPoolingLayers[i] = new PoolingLayer <T>(m_cuda, m_log, pooling_param);
                m_rgPoolingLayers[i].Setup(m_rgPoolingBottomVec[i], m_rgPoolingTopVecs[i]);
                m_rgPoolingLayers[i].Reshape(m_rgPoolingBottomVec[i], m_rgPoolingTopVecs[i]);
                m_rgFlattenLayers[i].Reshape(m_rgPoolingTopVecs[i], m_rgFlattenLayerTopVecs[i]);
            }

            m_concat_layer.Reshape(m_colBlobConcatBottomVec, colTop);
        }