Beispiel #1
0
        /// <summary>
        /// Load the parameters from the string.
        /// <remarks>This should get called from load().
        /// The format is binary and not necessarily fit for human consumption.</remarks>
        /// </summary>
        public void PLoad(BinaryReader reader)
        {
            string key, value;
            bool   ok = false;
            string s;

            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                //s = reader.ReadLine();   // to check me!
                BinIO.string_read(reader, out s);
                string[] parts = s.Trim().Split('=');
                if (parts.Length != 2)
                {
                    break;
                }
                key   = parts[0];
                value = parts[1];
                if (String.Equals(key, "END_OF_PARAMETERS", StringComparison.InvariantCulture))
                {
                    ok = true;
                    break;
                }
                _params[key] = value;
            }
            if (!ok)
            {
                throw new Exception("PLoad: parameters not properly terminated in save file");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Load network from new format
        /// </summary>
        /// <param name="filepath">full file path<</param>
        public override void Load(string filepath)
        {
            FileStream   stream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);

            try
            {
                string s;
                BinIO.string_read(reader, out s);
                if (s == "<object>")
                {
                    BinIO.string_read(reader, out s);
                    if (s != this.Name && s != this.GetType().Name)
                    {
                        throw new Exception("LenetClassifier: incorrect file format");
                    }
                    this.Load(reader);
                    BinIO.string_read(reader, out s);
                    if (s != "</object>")
                    {
                        throw new Exception("Expected string: </object>");
                    }
                }
                else
                {
                    throw new Exception("Expected string: <object>");
                }
            }
            finally
            {
                reader.Close();
                stream.Close();
            }
        }
Beispiel #3
0
 /// <summary>
 /// Save the parameters to the string.
 /// <remarks>This should get called from save().
 /// The format is binary and not necessarily fit for human consumption.</remarks>
 /// </summary>
 public void PSave(BinaryWriter writer)
 {
     foreach (string key in _params.Keys)
     {
         string s = String.Format("{0}={1}", key, _params[key]);
         BinIO.string_write(writer, s);
         //writer.Write(String.Format("{0}={1}\n", key, _params[key]));
     }
     BinIO.string_write(writer, "END_OF_PARAMETERS=HERE");
     //writer.Write("END_OF_PARAMETERS=HERE\n");
 }
Beispiel #4
0
        public virtual void Load(BinaryReader reader)
        {
            // before doing anything else, clear all the
            // persistent variables to their default state
            foreach (string wname in _wrappers_dict.Keys)
            {
                _wrappers_dict[wname].Clear();
            }
            BinIO.magic_read(reader, Name);
            this.PLoad(reader);
            string s;

            BinIO.string_read(reader, out s);
            if (s != "<component>")
            {
                throw new Exception("Expected string: <component>");
            }
            while (true)
            {
                // read tag
                BinIO.string_read(reader, out s);
                if (s == "</component>")
                {
                    break;
                }
                if (s != "<item>")
                {
                    throw new Exception("Expected string: <item>");
                }
                // read wrapper name
                BinIO.string_read(reader, out s);
                if (!_wrappers_dict.ContainsKey(s))
                {
                    throw new Exception(String.Format("Wrapper name '{0}' is not persisted", s));
                }
                // read wrapper data
                if (ComponentIO.level == 2)
                {
                    Global.Debugf("info", "{0," + (ComponentIO.level) + "}loading {1}", "", s);
                }
                _wrappers_dict[s].Load(reader);
                // read tag
                BinIO.string_read(reader, out s);
                if (s != "</item>")
                {
                    throw new Exception("Expected string: </item>");
                }
            }
            this.ReImport();
        }
Beispiel #5
0
 // saving and loading
 public virtual void Save(BinaryWriter writer)
 {
     BinIO.magic_write(writer, Name);
     this.PSave(writer);
     BinIO.string_write(writer, "<component>");
     foreach (string wname in _wrappers_dict.Keys)
     {
         BinIO.string_write(writer, "<item>");
         Global.Debugf("iodetail", "writing {0} {1}", Name, wname);
         BinIO.string_write(writer, wname);
         _wrappers_dict[wname].Save(writer);
         BinIO.string_write(writer, "</item>");
     }
     BinIO.string_write(writer, "</component>");
 }
Beispiel #6
0
        public override void Load(BinaryReader reader)
        {
            string s;

            BinIO.string_read(reader, out s);
            if (s == "<object>")
            {
                // load lenet arguments
                int nclasses;
                BinIO.scalar_read(reader, out nclasses);
                lenetWrap.Classes = new int[nclasses];
                for (int i = 0; i < nclasses; i++)
                {
                    BinIO.scalar_read(reader, out lenetWrap.Classes[i]);
                }
                byte boolval;
                BinIO.scalar_read(reader, out boolval);
                lenetWrap.TanhSigmoid = Convert.ToBoolean(boolval);
                BinIO.scalar_read(reader, out boolval);
                lenetWrap.NetNorm = Convert.ToBoolean(boolval);
                BinIO.scalar_read(reader, out boolval);
                lenetWrap.AsciiTarget = Convert.ToBoolean(boolval);

                // load Narray from stream
                BinIO.narray_read(reader, lenetparam);
                double[] dbuffer = lenetparam.To1DArray();

                Global.Debugf("info", "loading " + Name + "..");

                // create lenet
                if (lenetWrap.IsEmpty)
                {
                    lenetWrap.CreateLenet(lenetWrap.Classes.Length, lenetWrap.Classes, lenetWrap.TanhSigmoid, lenetWrap.NetNorm, lenetWrap.AsciiTarget);
                }

                // send loaded buffer to created lenet
                lenetWrap.LoadNetworkFromBuffer(dbuffer, dbuffer.Length);
                BinIO.string_read(reader, out s);
                if (s != "</object>")
                {
                    throw new Exception("Expected string: </object>");
                }
            }
        }
Beispiel #7
0
        public static IComponent load_component(BinaryReader reader)
        {
            IComponent result = null;
            string     s;

            BinIO.string_read(reader, out s);
            Global.Debugf("iodetail", "{0}[got {1}]", level, s);
            if (s == "<object>")
            {
                level++;
                BinIO.string_read(reader, out s);
                if (level <= 2)
                {
                    Global.Debugf("info", "{0," + (level - 1) + "}loading component {1}", "", s);
                }

                Global.Debugf("iodetail", "{0}[constructing {1}]", level, s);
                result = ComponentCreator.MakeComponent(s);
                result.Load(reader);
                BinIO.string_read(reader, out s);
                if (s != "</object>")
                {
                    throw new Exception("Expected string: </object>");
                }
                level--;
            }
            else if (s.StartsWith("OBJ:"))
            {
                s = s.Substring(4);
                level++;
                Global.Debugf("iodetail", "{0}[constructing {1}]", level, s);
                result = ComponentCreator.MakeComponent(s);
                result.Load(reader);
                BinIO.string_read(reader, out s);
                if (s != "OBJ:END")
                {
                    throw new Exception("Expected string: </object>");
                }
                level--;
            }
            Global.Debugf("iodetail", "{0}[done]", level);
            return(result);
        }
Beispiel #8
0
 public static void save_component(BinaryWriter writer, IComponent comp)
 {
     if (comp == null)
     {
         Global.Debugf("iodetail", "{0}[writing OBJ:NULL]", level);
         BinIO.string_write(writer, "<null/>");
     }
     else
     {
         Global.Debugf("iodetail", "{0}[writing OBJ:{1}]", level, comp.Name);
         level++;
         BinIO.string_write(writer, "<object>");
         BinIO.string_write(writer, comp.Name);
         comp.Save(writer);
         BinIO.string_write(writer, "</object>");
         level--;
         Global.Debugf("iodetail", "{0}[done]", level);
     }
 }
Beispiel #9
0
        public void LoadOldFormat(BinaryReader reader)
        {
            string magic = BinIO.magic_get(reader, "linerec".Length);

            CHECK_ARG(magic == "linerec" || magic == "linerc2", "magic=='linerec' || magic=='linerc2'");
            PLoad(reader);
            IComponent comp   = ComponentIO.load_component(reader);
            IModel     cmodel = comp as IModel;

            classifier.Object = cmodel;
            counts.Clear();
            if (magic == "linerec")
            {
                PSet("minsize_factor", 0.0);
            }
            else if (magic == "linerc2")
            {
                Narray <int> intcount = counts;
                BinIO.narray_read(reader, intcount);
            }
        }
Beispiel #10
0
        public override void Save(BinaryWriter writer)
        {
            if (lenetWrap.IsEmpty)
            {
                BinIO.string_write(writer, "<null/>");
                return;
            }

            double[] dbuffer;
            int      size;

            // receive buffer from wrapped lenet
            lenetWrap.SaveNetworkToBuffer(out size, out dbuffer);
            lenetparam.Resize(size);
            for (int i = 0; i < size; i++)
            {
                lenetparam.UnsafePut1d(i, dbuffer[i]);
            }

            BinIO.string_write(writer, "<object>");
            //BinIO.string_write(writer, comp.Name);

            // write lenet arguments
            BinIO.scalar_write(writer, lenetWrap.Classes.Length);
            for (int i = 0; i < lenetWrap.Classes.Length; i++)
            {
                BinIO.scalar_write(writer, lenetWrap.Classes[i]);
            }
            BinIO.scalar_write(writer, Convert.ToByte(lenetWrap.TanhSigmoid));
            BinIO.scalar_write(writer, Convert.ToByte(lenetWrap.NetNorm));
            BinIO.scalar_write(writer, Convert.ToByte(lenetWrap.AsciiTarget));

            // save Narray to stream
            BinIO.narray_write(writer, lenetparam);

            BinIO.string_write(writer, "</object>");
        }