Esempio n. 1
0
        /// <summary>
        /// Collect the inputs from the RawProto.
        /// </summary>
        /// <param name="rp">Specifies the raw proto.</param>
        /// <returns>A dictionary of the inputs and their shapes is returned.</returns>
        public static Dictionary <string, BlobShape> InputFromProto(RawProto rp)
        {
            List <string>    rgstrInput = rp.FindArray <string>("input");
            List <BlobShape> rgShape    = new List <BlobShape>();

            RawProtoCollection rgp = rp.FindChildren("input_shape");

            foreach (RawProto rpChild in rgp)
            {
                rgShape.Add(BlobShape.FromProto(rpChild));
            }

            if (rgstrInput.Count != rgShape.Count)
            {
                throw new Exception("The input array and shape array must have the same count!");
            }

            Dictionary <string, BlobShape> rgInput = new Dictionary <string, BlobShape>();

            for (int i = 0; i < rgstrInput.Count; i++)
            {
                rgInput.Add(rgstrInput[i], rgShape[i]);
            }

            return(rgInput);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <param name="p">Optionally, specifies an instance to load.  If <i>null</i>, a new instance is created and loaded.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static HDF5DataParameter FromProto(RawProto rp, HDF5DataParameter p = null)
        {
            string strVal;

            if (p == null)
                p = new HDF5DataParameter();

            if ((strVal = rp.FindValue("source")) != null)
                p.source = strVal.Trim('\"');

            if ((strVal = rp.FindValue("batch_size")) != null)
                p.batch_size = uint.Parse(strVal);

            if ((strVal = rp.FindValue("shuffle")) != null)
                p.shuffle = bool.Parse(strVal);

            p.m_rgExpectedTopShapes = new List<BlobShape>();
            RawProtoCollection colExpectedTopShapes = rp.FindChildren("expected_top_shape");
            foreach (RawProto rp1 in colExpectedTopShapes)
            {
                p.m_rgExpectedTopShapes.Add(BlobShape.FromProto(rp1));
            }

            return p;
        }
        /// <summary>
        /// Calculate the Copy Axes, inferred axis and constant count.
        /// </summary>
        /// <param name="p">Specifies the layer parameters.</param>
        /// <param name="nInferredAxis">Returns the inferred axis.</param>
        /// <param name="nConstantCount">Returns the constant count.</param>
        /// <param name="log">Optionally, specifies the output log (default = null, which ignores this parameter).</param>
        /// <returns>The copy axes are returned.</returns>
        public static List <int> CalculateCopyAxes(LayerParameter p, out int nInferredAxis, out int nConstantCount, Log log = null)
        {
            List <int> rgCopyAxes = new List <int>();

            nInferredAxis  = -1;
            nConstantCount = 1;

            BlobShape top_blob_shape = p.reshape_param.shape;
            int       top_num_axes   = top_blob_shape.dim.Count();

            for (int i = 0; i < top_num_axes; i++)
            {
                int top_dim = top_blob_shape.dim[i];

                if (top_dim == 0)
                {
                    rgCopyAxes.Add(i);
                }
                else if (top_dim == -1)
                {
                    if (log != null)
                    {
                        log.CHECK_EQ(nInferredAxis, -1, "new shape contains multiple -1 dims; at most a single (1) value of -1 may be specified.");
                    }

                    nInferredAxis = i;
                }
                else
                {
                    nConstantCount *= top_dim;
                }
            }

            return(rgCopyAxes);
        }
        /// <summary>
        /// Copy on parameter to another.
        /// </summary>
        /// <param name="src">Specifies the parameter to copy.</param>
        public override void Copy(LayerParameterBase src)
        {
            ConstantParameter p = (ConstantParameter)src;

            m_outputShape       = p.output_shape.Clone();
            m_rgF               = new List <float>(p.m_rgF);
            m_strBinaryDataFile = p.binary_data_file;
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a copy of the BlobShape.
        /// </summary>
        /// <returns>A new instance of the BlobShape is returned.</returns>
        public BlobShape Clone()
        {
            BlobShape bs = new BlobShape();

            bs.m_rgDim = Utility.Clone <int>(m_rgDim);

            return(bs);
        }
Esempio n. 6
0
        /** @copydoc LayerParameterBase::Copy */
        public override void Copy(LayerParameterBase src)
        {
            ReshapeParameter p = (ReshapeParameter)src;

            if (p.m_shape != null)
            {
                m_shape = p.m_shape.Clone();
            }

            m_nAxis    = p.m_nAxis;
            m_nNumAxes = p.m_nNumAxes;
        }
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static ParameterParameter FromProto(RawProto rp)
        {
            ParameterParameter p = new ParameterParameter();

            RawProto rp1 = rp.FindChild("shape");

            if (rp1 != null)
            {
                p.shape = BlobShape.FromProto(rp1);
            }

            return(p);
        }
Esempio n. 8
0
        /// <summary>
        /// Load the BlobShape from a binary reader.
        /// </summary>
        /// <param name="br">The binary reader to use.</param>
        /// <param name="bNewInstance">When <i>true</i>, a the BlobShape is read into a new instance, otherwise it is read into the current instance.</param>
        /// <returns>The BlobShape instance is returned.</returns>
        public object Load(BinaryReader br, bool bNewInstance)
        {
            BlobShape b = this;

            if (bNewInstance)
            {
                b = new BlobShape();
            }

            b.m_rgDim = Utility.Load <int>(br);

            return(b);
        }
Esempio n. 9
0
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static InputParameter FromProto(RawProto rp)
        {
            InputParameter p = new InputParameter();

            RawProtoCollection col = rp.FindChildren("shape");

            foreach (RawProto rp1 in col)
            {
                BlobShape b = BlobShape.FromProto(rp1);
                p.m_rgShape.Add(b);
            }

            return(p);
        }
Esempio n. 10
0
        /// <summary>
        /// Compares this BlobShape to another.
        /// </summary>
        /// <param name="obj">Specifies the other BlobShape to compare this one to.</param>
        /// <returns>If the two BlobShape's are the same <i>true</i> is returned, otherwise <i>false</i> is returned.</returns>
        public int CompareTo(object obj)
        {
            BlobShape bs = obj as BlobShape;

            if (bs == null)
            {
                return(1);
            }

            if (!Compare(bs))
            {
                return(1);
            }

            return(0);
        }
Esempio n. 11
0
        /// <summary>
        /// Parse a RawProto into a new instance of the parameter.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static NetParameter FromProto(RawProto rp)
        {
            string       strVal;
            NetParameter p = new NetParameter();

            if ((strVal = rp.FindValue("name")) != null)
            {
                p.name = strVal;
            }

            p.input = rp.FindArray <string>("input");

            RawProtoCollection rgp = rp.FindChildren("input_shape");

            foreach (RawProto rpChild in rgp)
            {
                p.input_shape.Add(BlobShape.FromProto(rpChild));
            }

            p.input_dim = rp.FindArray <int>("input_dim");

            if ((strVal = rp.FindValue("force_backward")) != null)
            {
                p.force_backward = bool.Parse(strVal);
            }

            RawProto rpState = rp.FindChild("state");

            if (rpState != null)
            {
                p.state = NetState.FromProto(rpState);
            }

            if ((strVal = rp.FindValue("debug_info")) != null)
            {
                p.debug_info = bool.Parse(strVal);
            }

            rgp = rp.FindChildren("layer", "layers");
            foreach (RawProto rpChild in rgp)
            {
                p.layer.Add(LayerParameter.FromProto(rpChild));
            }

            return(p);
        }
Esempio n. 12
0
        /// <summary>
        /// Loads a BlobProto from a binary reader.
        /// </summary>
        /// <param name="br">Specifies the binary reader.</param>
        /// <param name="bNewInstance">When <i>true</i> a new instance is created, otherwise the data is read into the existing BlobProto.</param>
        /// <returns>The instance of the BlobProto is returned.</returns>
        public object Load(BinaryReader br, bool bNewInstance)
        {
            BlobProto p = this;

            if (bNewInstance)
            {
                p = new BlobProto();
            }

            if (br.ReadBoolean())
            {
                m_rgShape = BlobShape.Load(br);
            }

            m_rgdfData = Utility.Load <double>(br);
            m_rgdfDiff = Utility.Load <double>(br);
            m_rgfData  = Utility.Load <float>(br);
            m_rgfDiff  = Utility.Load <float>(br);

            if (br.ReadBoolean())
            {
                m_nNum = br.ReadInt32();
            }

            if (br.ReadBoolean())
            {
                m_nChannels = br.ReadInt32();
            }

            if (br.ReadBoolean())
            {
                m_nHeight = br.ReadInt32();
            }

            if (br.ReadBoolean())
            {
                m_nWidth = br.ReadInt32();
            }

            return(p);
        }
Esempio n. 13
0
        /// <summary>
        /// Parses a new BlobProto from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the BlobProto is returned.</returns>
        public static BlobProto FromProto(RawProto rp)
        {
            BlobProto p = new BlobProto();

            RawProto rpShape = rp.FindChild("shape");

            if (rpShape != null)
            {
                p.shape = BlobShape.FromProto(rpShape);
            }

            p.num         = (int?)rp.FindValue("num", typeof(int));
            p.channels    = (int?)rp.FindValue("channels", typeof(int));
            p.height      = (int?)rp.FindValue("height", typeof(int));
            p.width       = (int?)rp.FindValue("width", typeof(int));
            p.double_data = rp.FindArray <double>("double_data");
            p.double_diff = rp.FindArray <double>("double_diff");
            p.data        = rp.FindArray <float>("data");
            p.diff        = rp.FindArray <float>("diff");

            return(p);
        }
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static ConstantParameter FromProto(RawProto rp)
        {
            string            strVal;
            ConstantParameter p = new ConstantParameter();

            RawProto shape = rp.FindChild("output_shape");

            if (shape != null)
            {
                p.m_outputShape = BlobShape.FromProto(shape);
            }

            p.m_rgF = rp.FindArray <float>("valuef");

            strVal = rp.FindValue("binary_data_file");
            if (strVal != null)
            {
                p.m_strBinaryDataFile = replace(strVal, '~', ' ');
            }

            return(p);
        }
Esempio n. 15
0
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static DummyDataParameter FromProto(RawProto rp)
        {
            DummyDataParameter p = new DummyDataParameter();
            RawProtoCollection rgp;

            rgp = rp.FindChildren("data_filler");
            foreach (RawProto child in rgp)
            {
                p.data_filler.Add(FillerParameter.FromProto(child));
            }

            rgp = rp.FindChildren("shape");
            foreach (RawProto child in rgp)
            {
                p.shape.Add(BlobShape.FromProto(child));
            }

            p.num      = rp.FindArray <uint>("num");
            p.channels = rp.FindArray <uint>("channels");
            p.height   = rp.FindArray <uint>("height");
            p.width    = rp.FindArray <uint>("width");

            string strVal;

            if ((strVal = rp.FindValue("primary_data")) != null)
            {
                p.primary_data = bool.Parse(strVal);
            }

            if ((strVal = rp.FindValue("force_refill")) != null)
            {
                p.force_refill = bool.Parse(strVal);
            }

            return(p);
        }
Esempio n. 16
0
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static ReshapeParameter FromProto(RawProto rp)
        {
            string           strVal;
            ReshapeParameter p = new ReshapeParameter();

            RawProto rpShape = rp.FindChild("shape");

            if (rpShape != null)
            {
                p.shape = BlobShape.FromProto(rpShape);
            }

            if ((strVal = rp.FindValue("axis")) != null)
            {
                p.axis = int.Parse(strVal);
            }

            if ((strVal = rp.FindValue("num_axes")) != null)
            {
                p.num_axes = int.Parse(strVal);
            }

            return(p);
        }
Esempio n. 17
0
        /// <summary>
        /// Parses the parameter from a RawProto.
        /// </summary>
        /// <param name="rp">Specifies the RawProto to parse.</param>
        /// <returns>A new instance of the parameter is returned.</returns>
        public static DummyDataParameter FromProto(RawProto rp)
        {
            DummyDataParameter p = new DummyDataParameter();
            RawProtoCollection rgp;

            rgp = rp.FindChildren("data_filler");
            foreach (RawProto child in rgp)
            {
                p.data_filler.Add(FillerParameter.FromProto(child));
            }

            rgp = rp.FindChildren("shape");
            foreach (RawProto child in rgp)
            {
                p.shape.Add(BlobShape.FromProto(child));
            }

            p.num      = rp.FindArray <uint>("num");
            p.channels = rp.FindArray <uint>("channels");
            p.height   = rp.FindArray <uint>("height");
            p.width    = rp.FindArray <uint>("width");

            return(p);
        }
Esempio n. 18
0
        /// <summary>
        /// Load the BlobShape from a binary reader.
        /// </summary>
        /// <param name="br">The binary reader to use.</param>
        /// <returns>A new BlobShape instance is returned.</returns>
        public static BlobShape Load(BinaryReader br)
        {
            BlobShape b = new BlobShape();

            return((BlobShape)b.Load(br, true));
        }
Esempio n. 19
0
 /// <summary>
 /// Constructor for the BlobProto
 /// </summary>
 /// <param name="rgShape">Specifies the shape of the blob.</param>
 public BlobProto(List <int> rgShape)
 {
     m_rgShape = new BlobShape(rgShape);
 }
Esempio n. 20
0
 /// <summary>
 /// Compares this BlobShape to another.
 /// </summary>
 /// <param name="bs">Specifies the other BlobShape to compare this one to.</param>
 /// <returns>If the two BlobShape's are the same <i>true</i> is returned, otherwise <i>false</i> is returned.</returns>
 public bool Compare(BlobShape bs)
 {
     return(Utility.Compare <int>(m_rgDim, bs.m_rgDim));
 }
        /** @copydoc LayerParameterBase::Copy */
        public override void Copy(LayerParameterBase src)
        {
            ParameterParameter p = (ParameterParameter)src;

            m_shape = p.m_shape.Clone();
        }
        /// <summary>
        /// Calculates the new shape.
        /// </summary>
        /// <param name="p">Specifies the layer parameters.</param>
        /// <param name="rgShape">Specifies the Bottom[0] shape.</param>
        /// <param name="rgCopyAxes">Specifies the copy axes.</param>
        /// <param name="nInferredAxis">Specifies the inferred axis (if any).</param>
        /// <param name="nConstantCount">Specifies the constant count.</param>
        /// <param name="log">Specifies the output log.</param>
        /// <returns>The new top shape is returned.</returns>
        public static List <int> Reshape(LayerParameter p, List <int> rgShape, List <int> rgCopyAxes, int nInferredAxis, int nConstantCount, Log log = null)
        {
            int num_axes1        = rgShape.Count;
            int input_start_axis = p.reshape_param.axis;
            int start_axis       = (input_start_axis >= 0) ? input_start_axis : num_axes1 + input_start_axis + 1;

            if (log != null)
            {
                log.CHECK_GE(start_axis, 0, "axis " + input_start_axis.ToString() + " out of range");
                log.CHECK_LE(start_axis, num_axes1, "axis " + input_start_axis.ToString() + " out of range for " + num_axes1.ToString() + "-D input blob");
            }

            int num_axes = p.reshape_param.num_axes;

            if (log != null)
            {
                log.CHECK_GE(num_axes, -1, "num_axes must be >= 0, or -1 for all");
            }

            int end_axis = (num_axes == -1) ? num_axes1 : (start_axis + num_axes);

            if (log != null)
            {
                log.CHECK_LE(end_axis, num_axes1, "end_axis = axis + num_axes is out of range");
            }

            int        num_axes_replaced = end_axis - start_axis;
            int        num_axes_retained = num_axes1 - num_axes_replaced;
            BlobShape  top_blob_shape    = p.reshape_param.shape;
            int        num_new_axes      = top_blob_shape.dim.Count;
            List <int> rgTopShape        = new List <int>();
            int        top_shape_index   = 0;

            for (int i = 0; i < start_axis; i++)
            {
                rgTopShape.Add(rgShape[i]);
                top_shape_index++;
            }

            for (int i = 0; i < num_new_axes; i++)
            {
                rgTopShape.Add(top_blob_shape.dim[i]);
                top_shape_index++;
            }

            for (int i = end_axis; i < num_axes1; i++)
            {
                rgTopShape.Add(rgShape[i]);
                top_shape_index++;
            }

            if (log != null)
            {
                log.CHECK_EQ(top_shape_index, rgTopShape.Count, "The top shape count should equal the top_shape_index.");
            }

            for (int i = 0; i < rgCopyAxes.Count; i++)
            {
                int copy_axis_index = rgCopyAxes[i];

                if (log != null)
                {
                    log.CHECK_GT(num_axes1, start_axis + copy_axis_index, "new shape contains a 0, but there was no corresponding bottom axis to copy");
                }

                rgTopShape[start_axis + copy_axis_index] = rgShape[start_axis + copy_axis_index];
            }

            if (nInferredAxis >= 0)
            {
                // A -1 dim was specified; infer the correct dimension by computing the
                // product of the other dimensions.
                int explicit_count = nConstantCount;
                explicit_count *= count(rgShape, 0, start_axis);
                explicit_count *= count(rgShape, end_axis, rgShape.Count);

                for (int i = 0; i < rgCopyAxes.Count; i++)
                {
                    int copy_axis_index = rgCopyAxes[i];
                    explicit_count *= rgTopShape[start_axis + copy_axis_index];
                }

                int nCount = count(rgShape, 0, rgShape.Count);

                if (log != null)
                {
                    log.CHECK_EQ(0, nCount % explicit_count, "bottom count (" + nCount.ToString() + ") must be divisible by the product of the specified dimensions( " + explicit_count.ToString() + ")");
                }

                int inferred_dim = nCount / explicit_count;
                rgTopShape[start_axis + nInferredAxis] = inferred_dim;
            }

            return(rgTopShape);
        }