MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DataParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
6using MyCaffe.basecode;
7
8namespace MyCaffe.param
9{
17 [Serializable]
18 [TypeConverter(typeof(ExpandableObjectConverter))]
20 {
24 public enum DB
25 {
29 NONE = 0,
33 IMAGEDB = 1
34 }
35
36 string m_strSource = null;
37 uint m_nBatchSize;
38 DB m_backend = DB.IMAGEDB;
39 uint m_nPrefetch = 4;
40 bool? m_bEnableRandomSelection = null;
41 bool? m_bEnablePairSelection = null;
42 bool m_bDisplayTiming = false;
43 LABEL_TYPE m_labelType = LABEL_TYPE.SINGLE;
44 bool m_bPrimaryData = true;
45 string m_strSynchronizeWith = null;
46 bool m_bSyncTarget = false;
47 int m_nImagesPerBlob = 1;
48 bool m_bOutputAllLabels = false;
49 bool m_bBalanceMatches = true;
50 bool m_bOutputImageInfo = false;
51 int m_nForcedPrimaryLabel = -1;
52 bool m_bEnableNoiseForNonMatch = false;
53 DataNoiseParameter m_dataNoiseParam = new DataNoiseParameter();
54 bool m_bEnableDebugOutput = false;
55 DataDebugParameter m_dataDebugParam = new DataDebugParameter();
56 int m_nOneHotLabelEncodingSize = 0; // Note when using OneHotLabelEncoding, m_labelType must = LABEL_TYPE.MULTIPLE
57
61 public event EventHandler<VerifyBatchSizeArgs> OnVerifyBatchSize;
62
65 {
66 }
67
71 [Description("When used with the DATA parameter, specifies the data 'source' within the database. Some sources are used for training whereas others are used for testing. When used with the IMAGE_DATA parameter, the 'source' specifies the data 'source' file containing the list of image file names. Each dataset has both a training and testing data source.")]
72 public string source
73 {
74 get { return m_strSource; }
75 set { m_strSource = value; }
76 }
77
81 [Description("Specifies the batch size of images to collect and train on each iteration of the network. NOTE: Setting the training netorks batch size >= to the testing net batch size will conserve memory by allowing the training net to share its gpu memory with the testing net.")]
82 public virtual uint batch_size
83 {
84 get { return m_nBatchSize; }
85 set
86 {
87 if (OnVerifyBatchSize != null)
88 {
90 OnVerifyBatchSize(this, args);
91 if (args.Error != null)
92 throw args.Error;
93 }
94
95 m_nBatchSize = value;
96 }
97 }
98
106 [Description("Specifies the backend database type. Currently only the IMAGEDB database type is supported. However protofiles specifying the 'LMDB' backend are converted into the 'IMAGEDB' type.")]
107 public DB backend
108 {
109 get { return m_backend; }
110 set { m_backend = value; }
111 }
112
117 [Description("Specifies the number of batches to prefetch to host memory. Increase this value if data access bandwidth varies.")]
118 public uint prefetch
119 {
120 get { return m_nPrefetch; }
121 set { m_nPrefetch = value; }
122 }
123
127 [Category("Data Selection"), Description("Specifies whether or not to randomly query images from the data source. When false, images are queried in sequence which can often have poorer training results.")]
129 {
130 get { return m_bEnableRandomSelection; }
131 set { m_bEnableRandomSelection = value; }
132 }
133
137 [Category("Data Selection"), Description("Specifies whether or not to select images in a pair sequence. When enabled, the first image queried is queried using the 'random' selection property, and then the second image queried is the image just after the first image queried (even if queried randomly).")]
139 {
140 get { return m_bEnablePairSelection; }
141 set { m_bEnablePairSelection = value; }
142 }
143
147 [Category("Debugging"), Description("Specifies whether or not to display the timing of each image read.")]
148 public bool display_timing
149 {
150 get { return m_bDisplayTiming; }
151 set { m_bDisplayTiming = value; }
152 }
153
157 [Category("Labels"), Description("Specifies the label type: SINGLE - the default which uses the 'Label' field, or MULTIPLE - which uses the 'DataCriteria' field.")]
159 {
160 get { return m_labelType; }
161 set { m_labelType = value; }
162 }
163
167 [Category("Data Selection"), Description("Specifies whether or not this data is the primary dataset as opposed to the target dataset. By default, this is set to 'true'.")]
168 public bool primary_data
169 {
170 get { return m_bPrimaryData; }
171 set { m_bPrimaryData = value; }
172 }
173
177 [Category("Synchronization"), Description("Specifies whether or not this is to be synchronized with another data layer as the target.")]
179 {
180 get { return m_bSyncTarget; }
181 set { m_bSyncTarget = value; }
182 }
183
191 [Category("Synchronization"), Description("Specifies a secondary (target) dataset to synchronize with.")]
192 public string synchronize_with
193 {
194 get { return m_strSynchronizeWith; }
195 set { m_strSynchronizeWith = value; }
196 }
197
206 [Category("Multi-Image"), Description("Optionally, specifies the number of images to load into each blob channel, where each image is stacked on the channel dimension so a 3 channel item then becomes a 6 channel item when two images are stacked (default = 1).")]
208 {
209 get { return m_nImagesPerBlob; }
210 set { m_nImagesPerBlob = value; }
211 }
212
216 [Category("Multi-Image"), Description("Optionally, specifies to output all labels for the stacked images instead of just the comparison. This setting only applies when 'images_per_blob' > 1 (default = false).")]
218 {
219 get { return m_bOutputAllLabels; }
220 set { m_bOutputAllLabels = value; }
221 }
222
226 [Category("Multi-Image"), Description("Optionally, specifies to balance the matches by alternating between matching classes and non matching classes. This setting only applies when 'images_per_blob' > 1 (default = true).")]
227 public bool balance_matches
228 {
229 get { return m_bBalanceMatches; }
230 set { m_bBalanceMatches = value; }
231 }
232
236 [Category("Debugging"), Description("Optionally, specifies to output image information such as index and label which is only intended for debugging as it dramatically slows down processing. (default = false).")]
238 {
239 get { return m_bOutputImageInfo; }
240 set { m_bOutputImageInfo = value; }
241 }
242
246 [Category("Multi-Image"), Description("Optionally, specifies to force the label of the primary image to this value. This setting only applies when 'images_per_blob' > 1 (default = -1 which causes this setting to be ignored).")]
248 {
249 get { return m_nForcedPrimaryLabel; }
250 set { m_nForcedPrimaryLabel = value; }
251 }
252
256 [Category("Multi-Image"), Description("Optionally, specifies to use a noise generated image for the non matching image instead of an image from a different class. This setting only applies when 'images_per_blob' > 1 (default = false).")]
258 {
259 get { return m_bEnableNoiseForNonMatch; }
260 set { m_bEnableNoiseForNonMatch = value; }
261 }
262
266 [Category("Multi-Image"), Description("Optionally, specifies the DataNoiseParameter that defines the noise used when 'enable_noise_for_nonmatch' = True. This setting only applies when 'images_per_blob' > 1.")]
268 {
269 get { return m_dataNoiseParam; }
270 set { m_dataNoiseParam = value; }
271 }
272
276 [Category("Debugging"), Description("Optionally, specifies to output debug information about the data sent out through the top. (default = false).")]
278 {
279 get { return m_bEnableDebugOutput; }
280 set { m_bEnableDebugOutput = value; }
281 }
282
286 [Category("Debugging"), Description("Optionally, specifies the parameters used when 'enable_debug_output' = True.")]
288 {
289 get { return m_dataDebugParam; }
290 set { m_dataDebugParam = value; }
291 }
292
296 [Category("Labels"), Description("When greater than 0 (default = 0), labels are one-hot encoded to a vector of the one-hot label size (e.g., when size = 4: 3 -> 0 1 1 0; 4 -> 1 0 0 0)")]
298 {
299 get { return m_nOneHotLabelEncodingSize; }
300 set { m_nOneHotLabelEncodingSize = value; }
301 }
302
304 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
305 {
306 RawProto proto = RawProto.Parse(br.ReadString());
307 DataParameter p = FromProto(proto);
308
309 if (!bNewInstance)
310 Copy(p);
311
312 return p;
313 }
314
316 public override void Copy(LayerParameterBase src)
317 {
319 m_strSource = p.m_strSource;
320 m_nBatchSize = p.m_nBatchSize;
321 m_backend = p.m_backend;
322 m_nPrefetch = p.m_nPrefetch;
323 m_bEnableRandomSelection = p.m_bEnableRandomSelection;
324 m_bEnablePairSelection = p.m_bEnablePairSelection;
325 m_bDisplayTiming = p.m_bDisplayTiming;
326 m_labelType = p.m_labelType;
327 m_bPrimaryData = p.m_bPrimaryData;
328 m_strSynchronizeWith = p.m_strSynchronizeWith;
329 m_bSyncTarget = p.m_bSyncTarget;
330 m_nImagesPerBlob = p.m_nImagesPerBlob;
331 m_bOutputAllLabels = p.m_bOutputAllLabels;
332 m_bBalanceMatches = p.m_bBalanceMatches;
333 m_bOutputImageInfo = p.m_bOutputImageInfo;
334 m_bEnableNoiseForNonMatch = p.m_bEnableNoiseForNonMatch;
335 m_dataNoiseParam.Copy(p.m_dataNoiseParam);
336 m_bEnableDebugOutput = p.m_bEnableDebugOutput;
337 m_dataDebugParam.Copy(p.m_dataDebugParam);
338 m_nForcedPrimaryLabel = p.m_nForcedPrimaryLabel;
339 m_nOneHotLabelEncodingSize = p.m_nOneHotLabelEncodingSize;
340 }
341
343 public override LayerParameterBase Clone()
344 {
346 p.Copy(this);
347 return p;
348 }
349
355 public override RawProto ToProto(string strName)
356 {
357 RawProtoCollection rgChildren = new RawProtoCollection();
358
359 rgChildren.Add("source", "\"" + source + "\"");
360 rgChildren.Add("batch_size", batch_size.ToString());
361 rgChildren.Add("backend", backend.ToString());
362
363 if (prefetch != 4)
364 rgChildren.Add("prefetch", prefetch.ToString());
365
366 rgChildren.Add("enable_random_selection", enable_random_selection.GetValueOrDefault(true).ToString());
367
368 if (enable_pair_selection.GetValueOrDefault(false) == true)
369 rgChildren.Add("enable_pair_selection", enable_pair_selection.Value.ToString());
370
371 if (display_timing == true)
372 rgChildren.Add("display_timing", display_timing.ToString());
373
374 if (label_type != LABEL_TYPE.SINGLE)
375 rgChildren.Add("label_type", label_type.ToString());
376
377 if (primary_data == false)
378 rgChildren.Add("primary_data", primary_data.ToString());
379
380 if (synchronize_with != null)
381 rgChildren.Add("synchronize_with", m_strSynchronizeWith);
382
384 rgChildren.Add("synchronize_target", m_bSyncTarget.ToString());
385
386 if (m_nImagesPerBlob > 1)
387 {
388 rgChildren.Add("images_per_blob", m_nImagesPerBlob.ToString());
389 rgChildren.Add("output_all_labels", m_bOutputAllLabels.ToString());
390 rgChildren.Add("balance_matches", m_bBalanceMatches.ToString());
391 }
392
394 rgChildren.Add("output_image_information", m_bOutputImageInfo.ToString());
395
397 {
398 rgChildren.Add("enable_noise_for_nonmatch", m_bEnableNoiseForNonMatch.ToString());
399 rgChildren.Add(m_dataNoiseParam.ToProto("data_noise_param"));
400 }
401
403 {
404 rgChildren.Add("enable_debug_output", m_bEnableDebugOutput.ToString());
405 rgChildren.Add(m_dataDebugParam.ToProto("data_debug_param"));
406 }
407
408 if (m_nForcedPrimaryLabel >= 0)
409 rgChildren.Add("forced_primary_label", m_nForcedPrimaryLabel.ToString());
410
411 if (one_hot_label_size > 0)
412 rgChildren.Add("one_hot_label_size", one_hot_label_size.ToString());
413
414 return new RawProto(strName, "", rgChildren);
415 }
416
423 public static DataParameter FromProto(RawProto rp, DataParameter p = null)
424 {
425 string strVal;
426
427 if (p == null)
428 p = new DataParameter();
429
430 if ((strVal = rp.FindValue("source")) != null)
431 p.source = strVal.Trim('\"');
432
433 if ((strVal = rp.FindValue("batch_size")) != null)
434 p.batch_size = uint.Parse(strVal);
435
436 if ((strVal = rp.FindValue("backend")) != null)
437 {
438 switch (strVal)
439 {
440 case "IMAGEDB":
441 p.backend = DB.IMAGEDB;
442 break;
443
444 case "LMDB":
445 p.backend = DB.IMAGEDB;
446 break;
447
448 case "NONE":
449 p.backend = DB.NONE;
450 break;
451
452 default:
453 throw new Exception("Unknown 'backend' value " + strVal);
454 }
455 }
456
457 if ((strVal = rp.FindValue("prefetch")) != null)
458 p.prefetch = uint.Parse(strVal);
459
460 if ((strVal = rp.FindValue("enable_random_selection")) != null)
461 p.enable_random_selection = bool.Parse(strVal);
462
463 if ((strVal = rp.FindValue("enable_pair_selection")) != null)
464 p.enable_pair_selection = bool.Parse(strVal);
465
466 if ((strVal = rp.FindValue("display_timing")) != null)
467 p.display_timing = bool.Parse(strVal);
468
469 if ((strVal = rp.FindValue("label_type")) != null)
470 {
471 switch (strVal)
472 {
473 case "SINGLE":
474 p.label_type = LABEL_TYPE.SINGLE;
475 break;
476
477 case "MULTIPLE":
478 p.label_type = LABEL_TYPE.MULTIPLE;
479 break;
480
481 default:
482 throw new Exception("Unknown 'label_type' value " + strVal);
483 }
484 }
485
486 if ((strVal = rp.FindValue("primary_data")) != null)
487 p.primary_data = bool.Parse(strVal);
488
489 p.synchronize_with = rp.FindValue("synchronize_with");
490
491 if ((strVal = rp.FindValue("synchronize_target")) != null)
492 p.synchronize_target = bool.Parse(strVal);
493
494 if ((strVal = rp.FindValue("images_per_blob")) != null)
495 p.images_per_blob = int.Parse(strVal);
496
497 if ((strVal = rp.FindValue("output_all_labels")) != null)
498 p.output_all_labels = bool.Parse(strVal);
499
500 if ((strVal = rp.FindValue("balance_matches")) != null)
501 p.balance_matches = bool.Parse(strVal);
502
503 if ((strVal = rp.FindValue("output_image_information")) != null)
504 p.output_image_information = bool.Parse(strVal);
505
506 if ((strVal = rp.FindValue("enable_noise_for_nonmatch")) != null)
507 p.enable_noise_for_nonmatch = bool.Parse(strVal);
508
509 RawProto rpDataNoise = rp.FindChild("data_noise_param");
510 if (rpDataNoise != null)
511 p.data_noise_param = DataNoiseParameter.FromProto(rpDataNoise);
512
513 if ((strVal = rp.FindValue("enable_debug_output")) != null)
514 p.enable_debug_output = bool.Parse(strVal);
515
516 RawProto rpDataDebug = rp.FindChild("data_debug_param");
517 if (rpDataDebug != null)
518 p.data_debug_param = DataDebugParameter.FromProto(rpDataDebug);
519
520 if ((strVal = rp.FindValue("forced_primary_label")) != null)
521 p.forced_primary_label = int.Parse(strVal);
522
523 if ((strVal = rp.FindValue("one_hot_label_size")) != null)
524 p.one_hot_label_size = int.Parse(strVal);
525
526 return p;
527 }
528 }
529
533 public class VerifyBatchSizeArgs : EventArgs
534 {
535 uint m_uiBatchSize;
536 Exception m_err = null;
537
542 public VerifyBatchSizeArgs(uint uiBatchSize)
543 {
544 m_uiBatchSize = uiBatchSize;
545 }
546
551 public Exception Error
552 {
553 get { return m_err; }
554 set { m_err = value; }
555 }
556
561 {
562 get { return m_uiBatchSize; }
563 }
564 }
565}
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
RawProto FindChild(string strName)
Searches for a given node.
Definition: RawProto.cs:231
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
The DataDebugParameter is used by the DataParameter when the 'enable_debugging' = True.
RawProto ToProto(string strName)
Convert the DataDebugParameter into a RawProto.
void Copy(DataDebugParameter src)
Copies the specified source data noise parameter to this one.
static DataDebugParameter FromProto(RawProto rp, DataDebugParameter p=null)
Parses the parameter from a RawProto.
The DataNoiseParameter is used by the DataParameter when the 'enable_noise_for_nonmatch' = True,...
RawProto ToProto(string strName)
Convert the DataNoiseParameter into a RawProto.
static DataNoiseParameter FromProto(RawProto rp, DataNoiseParameter p=null)
Parses the parameter from a RawProto.
void Copy(DataNoiseParameter pSrc)
Copies the specified source data noise parameter to this one.
Specifies the parameter for the data layer.
virtual uint batch_size
Specifies the batch size.
DB
Defines the database type to use.
LABEL_TYPE label_type
(optional, default = SINGLE) Specifies the label type: SINGLE - the default which uses the 'Label' fi...
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
bool synchronize_target
(optional, default = false) Specifies whether or not this is a to be synchronized with another data l...
bool display_timing
(optional, default = false) Specifies whether or not to display the timing of each image read.
bool? enable_random_selection
(optional, default = null) Specifies whether or not to randomly query images from the data source....
int images_per_blob
(optional, default = 1) Specifies the number of images to load into each blob channel....
string synchronize_with
(optional, default = null) Specifies a secondary (target) dataset to syncrhonize with.
bool primary_data
(optional, default = true) Specifies whether or not the data is the primary datset as opposed to a se...
DataNoiseParameter data_noise_param
Specifies the DataNoiseParameter used when 'enable_noise_for_nonmatch' = True.
bool enable_noise_for_nonmatch
(optional, default = false) When true an image consisting of noise initialized with noise filler.
int one_hot_label_size
When greater than 0 (default = 0), labels are one-hot encoded to a vector of the one-hot label size (...
bool? enable_pair_selection
(optional, default = null) Specifies whether or not to select images in a pair sequence....
int forced_primary_label
(optional, default = -1) When >= 0, this label is used as the primary image label when 'images_per_bl...
override void Copy(LayerParameterBase src)
Copy on parameter to another.
DB backend
Specifies the backend database.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
DataDebugParameter data_debug_param
Specifies the DataDebugParameter used when 'enable_debug_output' = True.
string source
When used with the DATA parameter, specifies the data 'source' within the database....
bool output_all_labels
(optional, default = false) When using images_per_blob > 1, 'output_all_labels' specifies to output a...
static DataParameter FromProto(RawProto rp, DataParameter p=null)
Parses the parameter from a RawProto.
bool enable_debug_output
(optional, default = false) When true the data sent out through the top are saved as images into the ...
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
EventHandler< VerifyBatchSizeArgs > OnVerifyBatchSize
This event is, optionally, called to verify the batch size of the DataParameter.
uint prefetch
Prefetch queue (Number of batches to prefetch to host memory, increase if data access bandwidth varie...
bool output_image_information
(optional, default = false) When true image information such as index and label are output....
bool balance_matches
(optional, default = true) When using images_per_blob > 1, 'balance_matches' specifies to query image...
DataParameter()
Constructor for the parameter.
The LayerParameterBase is the base class for all other layer specific parameters.
LABEL_TYPE
Defines the label type.
The VerifyBatchSizeArgs class defines the arguments of the OnVerifyBatchSize event.
uint ProposedBatchSize
Specifies the proposed batch size that the DataLayer would like to use.
VerifyBatchSizeArgs(uint uiBatchSize)
VerifyBatchSizeArgs constructor.
Exception Error
Get/set the error value. For example if the receiver of the event determines that the batch size is i...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ NONE
No training category specified.
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