MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
NetState.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using MyCaffe.basecode;
7using MyCaffe.common;
9
10namespace MyCaffe.param
11{
16 [Serializable]
17 [TypeConverter(typeof(ExpandableObjectConverter))]
18 public class NetState : BaseParameter, ICloneable, IComparable
19 {
20 Phase m_phase = Phase.TEST;
21 int m_nLevel = 0;
22 List<string> m_rgStage = new List<string>();
23
27 public NetState()
28 {
29 }
30
35 public void Save(BinaryWriter bw)
36 {
37 bw.Write((int)m_phase);
38 bw.Write(m_nLevel);
39 Utility.Save<string>(bw, m_rgStage);
40 }
41
47 public static NetState Load(BinaryReader br)
48 {
49 NetState ns = new NetState();
50
51 ns.m_phase = (Phase)br.ReadInt32();
52 ns.m_nLevel = br.ReadInt32();
53 ns.m_rgStage = Utility.Load<string>(br);
54
55 return ns;
56 }
57
61 [Description("Specifies the Phase of the NetState.")]
62 public Phase phase
63 {
64 get { return m_phase; }
65 set { m_phase = value; }
66 }
67
71 [Description("Specifies the level of the NetState.")]
72 public int level
73 {
74 get { return m_nLevel; }
75 set { m_nLevel = value; }
76 }
77
81 [Description("Specifies the stages of the NetState.")]
82 public List<string> stage
83 {
84 get { return m_rgStage; }
85 set
86 {
87 if (value == null)
88 m_rgStage = new List<string>();
89 else
90 m_rgStage = value;
91 }
92 }
93
98 public void MergeFrom(NetState ns)
99 {
100 if (ns == null)
101 return;
102
103 if (m_phase == Phase.NONE)
104 m_phase = ns.phase;
105
106 m_nLevel = ns.level;
107
108 foreach (string strStage in ns.m_rgStage)
109 {
110 if (!m_rgStage.Contains(strStage))
111 m_rgStage.Add(strStage);
112 }
113 }
114
120 {
121 NetState ns = new NetState();
122
123 ns.m_phase = m_phase;
124 ns.m_nLevel = m_nLevel;
125 ns.m_rgStage = Utility.Clone<string>(m_rgStage);
126
127 return ns;
128 }
129
134 object ICloneable.Clone()
135 {
136 return Clone();
137 }
138
144 public int CompareTo(object obj)
145 {
146 NetState ns = obj as NetState;
147
148 if (obj == null)
149 return 1;
150
151 if (!Compare(ns))
152 return 1;
153
154 return 0;
155 }
156
162 public override RawProto ToProto(string strName)
163 {
164 RawProtoCollection rgChildren = new RawProtoCollection();
165
166 rgChildren.Add("phase", phase.ToString());
167 rgChildren.Add("level", level.ToString());
168 rgChildren.Add<string>("stage", stage);
169
170 return new RawProto(strName, "", rgChildren);
171 }
172
178 public static NetState FromProto(RawProto rp)
179 {
180 string strVal;
181 NetState p = new NetState();
182
183 if ((strVal = rp.FindValue("phase")) != null)
184 {
185 switch (strVal)
186 {
187 case "TEST":
188 p.phase = Phase.TEST;
189 break;
190
191 case "TRAIN":
192 p.phase = Phase.TRAIN;
193 break;
194
195 case "NONE":
196 p.phase = Phase.NONE;
197 break;
198
199 default:
200 throw new Exception("Unknown 'phase' value: " + strVal);
201 }
202 }
203
204 if ((strVal = rp.FindValue("level")) != null)
205 p.level = int.Parse(strVal);
206
207 p.stage = rp.FindArray<string>("stage");
208
209 return p;
210 }
211 }
212}
The BaseParameter class is the base class for all other parameter classes.
virtual bool Compare(BaseParameter p)
Compare this parameter to another parameter.
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
Definition: RawProto.cs:17
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static void Save(BinaryWriter bw, List< double > rg)
Save a list of double to a binary writer.
Definition: Utility.cs:337
Specifies the NetState which includes the phase, level and stage for which a given Net is to run unde...
Definition: NetState.cs:19
int CompareTo(object obj)
Compares this NetState to another one.
Definition: NetState.cs:144
NetState()
The NetState constructor.
Definition: NetState.cs:27
static NetState FromProto(RawProto rp)
Parses a RawProto representing a NetState into a NetState instance.
Definition: NetState.cs:178
override RawProto ToProto(string strName)
Converts this NetState to a RawProto.
Definition: NetState.cs:162
int level
Specifies the level of the NetState.
Definition: NetState.cs:73
Phase phase
Specifies the Phase of the NetState.
Definition: NetState.cs:63
NetState Clone()
Creates a new copy of this NetState instance.
Definition: NetState.cs:119
static NetState Load(BinaryReader br)
Loads a new NetState instance from a binary reader.
Definition: NetState.cs:47
void MergeFrom(NetState ns)
Merges another NetState with this instance.
Definition: NetState.cs:98
void Save(BinaryWriter bw)
Saves the NetState to a binary writer.
Definition: NetState.cs:35
List< string > stage
Specifies the stages of the NetState.
Definition: NetState.cs:83
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12