示例#1
0
        static Function SetupScale(ScaleParameter param, List <BlobProto> blobs, List <string> bottoms, string name, string[] inputNames, string[] outputNames)
        {
            //Caffe及びChainerは暗黙的に1次元目をBacthとして利用しているため補正を行う
            int  axis     = param.Axis - 1;
            bool biasTerm = param.BiasTerm;

            if (bottoms.Count == 1)
            {
                //Scaleを作成
                int[] wShape = new int[blobs[0].Shape.Dims.Length];

                for (int i = 0; i < wShape.Length; i++)
                {
                    wShape[i] = (int)blobs[0].Shape.Dims[i];
                }

                return(new MultiplyScale(axis, wShape, biasTerm, blobs[0].Datas, blobs[1].Datas, name, inputNames, outputNames));
            }
            else
            {
                //Biasを作成
                int[] shape = new int[blobs[0].Shape.Dims.Length];

                for (int i = 0; i < shape.Length; i++)
                {
                    shape[i] = (int)blobs[0].Shape.Dims[i];
                }

                return(new AddBias(axis, shape, blobs[0].Datas, name));
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Sets up the scale. </summary>
        ///
        /// <param name="param">        The parameter. </param>
        /// <param name="blobs">        The blobs. </param>
        /// <param name="bottoms">      The bottoms. </param>
        /// <param name="name">         The name. </param>
        /// <param name="inputNames">   List of names of the inputs. </param>
        /// <param name="outputNames">  List of names of the outputs. </param>
        ///
        /// <returns>   A Function. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        static Function SetupScale(ScaleParameter param, List <BlobProto> blobs, List <string> bottoms, string name, string[] inputNames, string[] outputNames)
        {
            // Caffe and Chainer implicitly use the first dimension as Bacth, so make correction
            int  axis     = param.Axis - 1;
            bool biasTerm = param.BiasTerm;

            if (bottoms.Count == 1)
            {
                // Create Scale
                int[] wShape = new int[blobs[0].Shape.Dims.Length];

                for (int i = 0; i < wShape.Length; i++)
                {
                    wShape[i] = (int)blobs[0].Shape.Dims[i];
                }

                return(new MultiplyScale(axis, wShape, biasTerm, blobs[0].Datas, blobs[1].Datas, name, inputNames, outputNames));
            }
            else
            {
                // Create Bias
                int[] shape = new int[blobs[0].Shape.Dims.Length];

                for (int i = 0; i < shape.Length; i++)
                {
                    shape[i] = (int)blobs[0].Shape.Dims[i];
                }

                return(new AddBias(axis, shape, blobs[0].Datas, name));
            }
        }
示例#3
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)
        {
            ScaleParameter p         = m_param.scale_param;
            Blob <T>       blobScale = (colBottom.Count > 1) ? colBottom[1] : m_colBlobs[0];

            // Always set axis == 0 in special case where bias is a scalar
            // (num_axes == 0.  Mathematically eqiuvalent for any choice of axis, os the
            // actual setting can be safely ignored; and computation is most efficient
            // with axis == 0 and (therefore) outer_dim == 1. (Setting m_nAxis to
            // bottom[0].num_axes - 1, giving inner_dim_ == 1, would be equally
            // performant.)
            m_nAxis = (blobScale.num_axes == 0) ? 0 : colBottom[0].CanonicalAxisIndex(p.axis);

            m_log.CHECK_GE(colBottom[0].num_axes, m_nAxis + blobScale.num_axes, "scale blob's shape extends past bottom[0]'s shape when applied starting with bottom[0] axis = " + m_nAxis.ToString());

            for (int i = 0; i < blobScale.num_axes; i++)
            {
                m_log.CHECK_EQ(colBottom[0].shape(m_nAxis + i), blobScale.shape(i), "dimension mismatch between bottom[0]->shape(" + (m_nAxis + i).ToString() + ") and scale->shape(" + i.ToString() + ")");
            }

            m_nOuterDim = colBottom[0].count(0, m_nAxis);
            m_nScaleDim = blobScale.count();
            m_nInnerDim = colBottom[0].count(m_nAxis + blobScale.num_axes);

            if (colBottom[0] == colTop[0])  // in-place computation
            {
                m_blobTemp.ReshapeLike(colBottom[0]);
            }
            else
            {
                colTop[0].ReshapeLike(colBottom[0]);
            }

            m_blobSumResult.Reshape(new List <int>()
            {
                m_nOuterDim *m_nScaleDim
            });
            int nSumMultSize = Math.Max(m_nOuterDim, m_nInnerDim);

            m_blobSumMultiplier.Reshape(new List <int>()
            {
                nSumMultSize
            });
            m_blobSumMultiplier.SetData(1.0);

            if (m_biasLayer != null)
            {
                m_colBiasVec[0] = colTop[0];
                m_biasLayer.Reshape(m_colBiasVec, colTop);
            }
        }
示例#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)
        {
            ScaleParameter p = m_param.scale_param;

            if (colBottom.Count == 1 && blobs.Count > 0)
            {
                m_log.WriteLine("Skipping parameter initialization.");
            }
            else if (colBottom.Count == 1)
            {
                // scale is a learned parameter; initialize it.
                m_nAxis = colBottom[0].CanonicalAxisIndex(p.axis);
                int nNumAxes = p.num_axes;
                m_log.CHECK_GE(nNumAxes, -1, "num_axes must be non-negative, or -1 to extend to the end of bottom[0].");

                if (nNumAxes >= 0)
                {
                    m_log.CHECK_GE(colBottom[0].num_axes, m_nAxis + nNumAxes, "scale blob's shape extends past bottom[0]'s shape when applied starting with bottom[0] axis = " + m_nAxis.ToString());
                }

                m_colBlobs = new BlobCollection <T>();

                List <int> rgShape = new List <int>();
                int        nStart  = m_nAxis;
                int        nEnd    = (nNumAxes == -1) ? colBottom[0].shape().Count : nStart + nNumAxes;

                for (int i = nStart; i < nEnd; i++)
                {
                    rgShape.Add(colBottom[0].shape(i));
                }

                Blob <T> blobScale = new Blob <T>(m_cuda, m_log, rgShape);
                blobScale.Name = "scale";
                FillerParameter fp = p.filler;

                // Default to unit (1) filler for identity operation.
                if (fp == null)
                {
                    fp = new FillerParameter("constant", 1.0);
                }

                Filler <T> filler = Filler <T> .Create(m_cuda, m_log, fp);

                filler.Fill(blobScale);

                m_colBlobs.Add(blobScale);
            }

            if (p.bias_term)
            {
                LayerParameter pb = new LayerParameter(LayerParameter.LayerType.BIAS);
                pb.bias_param.axis     = p.axis;
                pb.bias_param.num_axes = (colBottom.Count > 1) ? colBottom[1].num_axes : p.num_axes;
                pb.bias_param.filler   = p.bias_filler;

                m_colBiasBottomVec = new BlobCollection <T>();
                m_colBiasBottomVec.Add(colBottom[0]);

                m_biasLayer = new BiasLayer <T>(m_cuda, m_log, pb);
                m_biasLayer.Setup(m_colBiasBottomVec, colTop);
                m_nBiasParamId = m_colBlobs.Count;
                m_colBlobs.Add(m_biasLayer.blobs[0]);
                m_rgbBiasPropagateDown = Utility.Create <bool>(1, false);
            }

            m_rgbParamPropagateDown = new DictionaryMap <bool>(m_colBlobs.Count(), true);
        }