MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
NetParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
7using MyCaffe.basecode;
8using MyCaffe.common;
9
10namespace MyCaffe.param
11{
15 [Serializable]
16 [TypeConverter(typeof(ExpandableObjectConverter))]
18 {
19 string m_strName = "";
20 List<string> m_rgstrInput = new List<string>();
21 List<BlobShape> m_rgInputShape = new List<BlobShape>();
22 List<int> m_rgInputDim = new List<int>();
23 bool m_bForceBackward = false;
24 NetState m_state = new NetState();
25 bool m_bDebugInfo = false;
26 List<LayerParameter> m_rgLayers = new List<LayerParameter>();
27 int m_nProjectID = 0;
28 int m_nSolverCount = 1;
29 int m_nSolverRank = 0;
30
32 public NetParameter()
33 : base()
34 {
35 }
36
41 public void Save(BinaryWriter bw)
42 {
43 bw.Write(m_strName);
44 Utility.Save<string>(bw, m_rgstrInput);
45 Utility.Save<BlobShape>(bw, m_rgInputShape);
46 Utility.Save<int>(bw, m_rgInputDim);
47 bw.Write(m_bForceBackward);
48 m_state.Save(bw);
49 bw.Write(m_bDebugInfo);
50 Utility.Save<LayerParameter>(bw, m_rgLayers);
51 }
52
58 public static NetParameter Load(BinaryReader br)
59 {
60 NetParameter p = new NetParameter();
61
62 p.m_strName = br.ReadString();
63 p.m_rgstrInput = Utility.Load<string>(br);
64 p.m_rgInputShape = Utility.Load<BlobShape>(br);
65 p.m_rgInputDim = Utility.Load<int>(br);
66 p.m_bForceBackward = br.ReadBoolean();
67 p.m_state = NetState.Load(br);
68 p.m_bDebugInfo = br.ReadBoolean();
69 p.m_rgLayers = Utility.Load<LayerParameter>(br);
70
71 return p;
72 }
73
77 [ReadOnly(true)]
78 [Description("Specifies the ID of the project that created this net param (if any).")]
79 public int ProjectID
80 {
81 get { return m_nProjectID; }
82 set { m_nProjectID = value; }
83 }
84
88 [Description("The name of the network.")]
89 public string name
90 {
91 get { return m_strName; }
92 set { m_strName = value; }
93 }
94
98 [Browsable(false)]
99 public List<string> input
100 {
101 get { return m_rgstrInput; }
102 set { m_rgstrInput = value; }
103 }
104
108 [Browsable(false)]
109 public List<BlobShape> input_shape
110 {
111 get { return m_rgInputShape; }
112 set { m_rgInputShape = value; }
113 }
114
121 [Browsable(false)]
122 public List<int> input_dim
123 {
124 get { return m_rgInputDim; }
125 set { m_rgInputDim = value; }
126 }
127
133 [Description("Specifies whether or not the network will force every layer to carry out the backward operation.")]
134 public bool force_backward
135 {
136 get { return m_bForceBackward; }
137 set { m_bForceBackward = value; }
138 }
139
145 [Browsable(false)]
147 {
148 get { return m_state; }
149 set { m_state = value; }
150 }
151
156 [Description("Specifies whether or not to output debugging information to the output window.")]
157 public bool debug_info
158 {
159 get { return m_bDebugInfo; }
160 set { m_bDebugInfo = value; }
161 }
162
167 [Browsable(false)]
168 public List<LayerParameter> layer
169 {
170 get { return m_rgLayers; }
171 set { m_rgLayers = value; }
172 }
173
177 [Browsable(false)]
178 public int solver_count
179 {
180 get { return m_nSolverCount; }
181 set { m_nSolverCount = value; }
182 }
183
187 [Browsable(false)]
188 public int solver_rank
189 {
190 get { return m_nSolverRank; }
191 set { m_nSolverRank = value; }
192 }
193
195 public override RawProto ToProto(string strName)
196 {
197 return ToProto(strName, false);
198 }
199
206 public RawProto ToProto(string strName, bool bIncludeState)
207 {
208 RawProtoCollection rgChildren = new RawProtoCollection();
209
210 rgChildren.Add("name", name, RawProto.TYPE.STRING);
211 rgChildren.Add<string>("input", input);
212
213 foreach (BlobShape bs in input_shape)
214 {
215 rgChildren.Add(bs.ToProto("input_shape"));
216 }
217
218 rgChildren.Add<int>("input_dim", input_dim);
219
220 if (force_backward != false)
221 rgChildren.Add("force_backward", force_backward.ToString());
222
223 if (bIncludeState)
224 rgChildren.Add(state.ToProto("state"));
225
226 if (debug_info != false)
227 rgChildren.Add("debug_info", debug_info.ToString());
228
229 foreach (LayerParameter lp in layer)
230 {
231 rgChildren.Add(lp.ToProto("layer"));
232 }
233
234 return new RawProto(strName, "", rgChildren);
235 }
236
243 {
244 string strVal;
245 NetParameter p = new NetParameter();
246
247 if ((strVal = rp.FindValue("name")) != null)
248 p.name = strVal;
249
250 p.input = rp.FindArray<string>("input");
251
252 RawProtoCollection rgp = rp.FindChildren("input_shape");
253 foreach (RawProto rpChild in rgp)
254 {
255 p.input_shape.Add(BlobShape.FromProto(rpChild));
256 }
257
258 p.input_dim = rp.FindArray<int>("input_dim");
259
260 if ((strVal = rp.FindValue("force_backward")) != null)
261 p.force_backward = bool.Parse(strVal);
262
263 RawProto rpState = rp.FindChild("state");
264 if (rpState != null)
265 p.state = NetState.FromProto(rpState);
266
267 if ((strVal = rp.FindValue("debug_info")) != null)
268 p.debug_info = bool.Parse(strVal);
269
270 rgp = rp.FindChildren("layer", "layers");
271 foreach (RawProto rpChild in rgp)
272 {
273 p.layer.Add(LayerParameter.FromProto(rpChild));
274 }
275
276 return p;
277 }
278
284 public static Dictionary<string, BlobShape> InputFromProto(RawProto rp)
285 {
286 List<string> rgstrInput = rp.FindArray<string>("input");
287 List<BlobShape> rgShape = new List<BlobShape>();
288
289 RawProtoCollection rgp = rp.FindChildren("input_shape");
290 foreach (RawProto rpChild in rgp)
291 {
292 rgShape.Add(BlobShape.FromProto(rpChild));
293 }
294
295 if (rgstrInput.Count != rgShape.Count)
296 throw new Exception("The input array and shape array must have the same count!");
297
298 Dictionary<string, BlobShape> rgInput = new Dictionary<string, BlobShape>();
299 for (int i = 0; i < rgstrInput.Count; i++)
300 {
301 rgInput.Add(rgstrInput[i], rgShape[i]);
302 }
303
304 return rgInput;
305 }
306
314 public NetParameter Clone(bool bCloneLayers = true, int? nSolverCount = null, int? nSolverRank = null)
315 {
316 NetParameter p = new NetParameter();
317
318 p.m_nProjectID = m_nProjectID;
319 p.m_strName = m_strName;
320 p.m_rgstrInput = Utility.Clone<string>(m_rgstrInput);
321 p.m_rgInputShape = Utility.Clone<BlobShape>(m_rgInputShape);
322 p.m_rgInputDim = Utility.Clone<int>(m_rgInputDim);
323 p.m_bForceBackward = m_bForceBackward;
324 p.m_state = (m_state != null) ? m_state.Clone() : null;
325 p.m_bDebugInfo = m_bDebugInfo;
326
327 if (bCloneLayers)
328 p.m_rgLayers = Utility.Clone<LayerParameter>(m_rgLayers);
329
330 if (nSolverCount == null)
331 nSolverCount = m_nSolverCount;
332
333 if (nSolverRank == null)
334 nSolverRank = m_nSolverRank;
335
336 p.m_nSolverCount = nSolverCount.Value;
337 p.m_nSolverRank = nSolverRank.Value;
338
339 return p;
340 }
341
346 public string DebugString()
347 {
348 string str = m_strName + " -> layers(" + m_rgLayers.Count.ToString() + ") {";
349
350 foreach (LayerParameter p in m_rgLayers)
351 {
352 str += p.name;
353 str += ", ";
354 }
355
356 str = str.TrimEnd(' ', ',');
357 str += "}";
358
359 return str;
360 }
361
369 {
370 foreach (LayerParameter p in m_rgLayers)
371 {
372 if (p.type == type)
373 {
374 if (p.MeetsPhase(phase))
375 return p;
376 }
377 }
378
379 return null;
380 }
381
387 public int FindLayerIndex(string strName)
388 {
389 for (int i = 0; i < m_rgLayers.Count; i++)
390 {
391 if (m_rgLayers[i].name == strName)
392 return i;
393 }
394
395 return -1;
396 }
397
403 {
404 foreach (LayerParameter layer in m_rgLayers)
405 {
406 if (layer.type == LayerParameter.LayerType.POOLING)
407 layer.pooling_param.reshape_algorithm = alg;
408 }
409 }
410 }
411}
The BaseParameter class is the base class for all other parameter classes.
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
TYPE
Defines the type of a RawProto node.
Definition: RawProto.cs:27
RawProto FindChild(string strName)
Searches for a given node.
Definition: RawProto.cs:231
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
Definition: RawProto.cs:263
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 shape of a Blob.
Definition: BlobShape.cs:15
override RawProto ToProto(string strName)
Converts the BlobShape to a RawProto.
Definition: BlobShape.cs:153
static BlobShape FromProto(RawProto rp)
Parse a new BlobShape from a RawProto.
Definition: BlobShape.cs:167
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
LayerType type
Specifies the type of this LayerParameter.
bool MeetsPhase(Phase phase)
Determines whether or not this LayerParameter meets a given Phase.
static LayerParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
LayerType
Specifies the layer type.
override RawProto ToProto(string strName)
Constructor for the parameter.
Specifies the parameters use to create a Net
Definition: NetParameter.cs:18
static NetParameter FromProto(RawProto rp)
Parse a RawProto into a new instance of the parameter.
NetState state
The current 'state' of the network, including the phase, level and stage. Some layers may be included...
bool debug_info
Print debugging information about results while running Net::Forward, Net::Backward and Net::Update.
List< string > input
The input blobs to the network.
bool force_backward
Whether the network will force every layer to carry out backward operation. If set False,...
int ProjectID
Specifies the ID of the project that created this net param (if any).
Definition: NetParameter.cs:80
int FindLayerIndex(string strName)
Locates the index of a layer based on a given layer name.
string DebugString()
Returns a debug string for the network.
List< int > input_dim
DEPRECIATED - 4D input dimensions - use 'input_shape' instead. If specified, for each input blob ther...
string name
The name of the network.
Definition: NetParameter.cs:90
override RawProto ToProto(string strName)
Constructor for the parameter.
List< LayerParameter > layer
The layers that make up the net. Each of their configurations, including connectivity and behavior,...
LayerParameter FindLayer(LayerParameter.LayerType type, Phase phase=Phase.NONE)
Locates a layer based on a layer type and phase.
int solver_rank
Specifies the rank of the solver using this network.
void SetPoolingReshapeAlgorithm(PoolingParameter.PoolingReshapeAlgorithm alg)
Sets all pooling layers to use the specified reshape algorithm.
NetParameter()
Constructor for the parameter.
Definition: NetParameter.cs:32
void Save(BinaryWriter bw)
Save the parameter to a binary writer.
Definition: NetParameter.cs:41
RawProto ToProto(string strName, bool bIncludeState)
Save the parameter settings to a RawProto.
int solver_count
Specifies the number of solvers used in a multi-gpu training session.
static Dictionary< string, BlobShape > InputFromProto(RawProto rp)
Collect the inputs from the RawProto.
NetParameter Clone(bool bCloneLayers=true, int? nSolverCount=null, int? nSolverRank=null)
Creates a new copy of this instance of the parameter.
List< BlobShape > input_shape
The shape of the input blobs.
static NetParameter Load(BinaryReader br)
Load a new instance of the parameter from a binary reader.
Definition: NetParameter.cs:58
Specifies the NetState which includes the phase, level and stage for which a given Net is to run unde...
Definition: NetState.cs:19
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
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 Save(BinaryWriter bw)
Saves the NetState to a binary writer.
Definition: NetState.cs:35
Specifies the parameters for the PoolingLayer.
PoolingReshapeAlgorithm
Defines the pooling reshape algorithm to use.
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