MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MultiBoxLossParameter.cs
1using System;
2using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using MyCaffe.basecode;
9
10namespace MyCaffe.param.ssd
11{
20 [Serializable]
21 [TypeConverter(typeof(ExpandableObjectConverter))]
23 {
24 LocLossType m_locLossType = LocLossType.SMOOTH_L1;
25 ConfLossType m_confLossType = ConfLossType.SOFTMAX;
26 float m_fLocWeight = 1.0f;
27 uint m_nNumClasses;
28 bool m_bShareLocation = true;
29 MatchType m_matchType = MatchType.PER_PREDICTION;
30 float m_fOverlapThreshold = 0.5f;
31 uint m_nBackgroundLabelId = 0;
32 bool m_bUseDifficultGt = true;
33 bool? m_bDoNegMining = null;
34 float m_fNegPosRatio = 3.0f;
35 float m_fNegOverlap = 0.5f;
37 bool m_bEncodeVarianceInTarget = false;
38 bool m_bMapObjectToAgnostic = false;
39 bool m_bIgnoreCrossBoundaryBbox = false;
40 bool m_bBpInside = false;
41 MiningType m_miningType = MiningType.MAX_NEGATIVE;
43 int m_nSampleSize = 64;
44 bool m_bUsePriorForNms = false;
45 bool m_bUsePriorForMatching = true;
46 bool m_bUseGpu = false;
47
51 public enum LocLossType
52 {
56 L2,
60 SMOOTH_L1
61 }
62
68 public static LocLossType LocLossTypeFromString(string str)
69 {
70 switch (str)
71 {
72 case "L2":
73 return LocLossType.L2;
74
75 case "SMOOTH_L1":
76 return LocLossType.SMOOTH_L1;
77
78 default:
79 throw new Exception("Unknown LocLossType '" + str + "'!");
80 }
81 }
82
86 public enum ConfLossType
87 {
91 SOFTMAX,
95 LOGISTIC
96 }
97
103 public static ConfLossType ConfLossTypeFromString(string str)
104 {
105 switch (str)
106 {
107 case "SOFTMAX":
108 return ConfLossType.SOFTMAX;
109
110 case "LOGISTIC":
111 return ConfLossType.LOGISTIC;
112
113 default:
114 throw new Exception("Unknown ConfLossType '" + str + "'!");
115 }
116 }
117
121 public enum MatchType
122 {
126 BIPARTITE,
130 PER_PREDICTION
131 }
132
138 public static MatchType MatchTypeFromString(string str)
139 {
140 switch (str)
141 {
142 case "BIPARTITE":
143 return MatchType.BIPARTITE;
144
145 case "PER_PREDICTION":
146 return MatchType.PER_PREDICTION;
147
148 default:
149 throw new Exception("Unknown MatchType '" + str + "'!");
150 }
151 }
152
153
157 public enum MiningType
158 {
162 NONE,
166 MAX_NEGATIVE,
173 HARD_EXAMPLE
174 }
175
181 public static MiningType MiningTypeFromString(string str)
182 {
183 switch (str)
184 {
185 case "NONE":
186 return MiningType.NONE;
187
188 case "MAX_NEGATIVE":
189 return MiningType.MAX_NEGATIVE;
190
191 case "HARD_EXAMPLE":
192 return MiningType.HARD_EXAMPLE;
193
194 default:
195 throw new Exception("Unknown MiningType '" + str + "'!");
196 }
197 }
198
199
204 {
205 }
206
210 [Description("Get/set the localization loss type (default = SMOOTH_L1).")]
212 {
213 get { return m_locLossType; }
214 set { m_locLossType = value; }
215 }
216
220 [Description("Get/set the confidence loss type (default = SOFTMAX).")]
222 {
223 get { return m_confLossType; }
224 set { m_confLossType = value; }
225 }
226
230 [Description("Get/set the weight for the localization loss (default = 1.0).")]
231 public float loc_weight
232 {
233 get { return m_fLocWeight; }
234 set { m_fLocWeight = value; }
235 }
236
240 [Description("Get/set the number of classes to be predicted - required")]
241 public uint num_classes
242 {
243 get { return m_nNumClasses; }
244 set { m_nNumClasses = value; }
245 }
246
250 [Description("Get/sets whether or not the bounding box is shared among different classes (default = true).")]
251 public bool share_location
252 {
253 get { return m_bShareLocation; }
254 set { m_bShareLocation = value; }
255 }
256
260 [Description("Get/set the matching method used during training (default = PER_PREDICTION).")]
262 {
263 get { return m_matchType; }
264 set { m_matchType = value; }
265 }
266
270 [Description("Get/set the overlap threshold (default = 0.5).")]
271 public float overlap_threshold
272 {
273 get { return m_fOverlapThreshold; }
274 set { m_fOverlapThreshold = value; }
275 }
276
280 [Description("Get/set the background label id.")]
282 {
283 get { return m_nBackgroundLabelId; }
284 set { m_nBackgroundLabelId = value; }
285 }
286
290 [Description("Get/set whether or not to consider the difficult ground truth (defalt = true).")]
292 {
293 get { return m_bUseDifficultGt; }
294 set { m_bUseDifficultGt = value; }
295 }
296
303 [Description("DEPRECIATED: Get/set whether or not to perform negative mining (default = false).")]
304 public bool? do_neg_mining
305 {
306 get { return m_bDoNegMining; }
307 set { m_bDoNegMining = value; }
308 }
309
313 [Description("Get/set the negative/positive ratio (default = 3.0).")]
314 public float neg_pos_ratio
315 {
316 get { return m_fNegPosRatio; }
317 set { m_fNegPosRatio = value; }
318 }
319
323 [Description("Get/set the negative overlap upperbound for the unmatched predictions (default = 0.5).")]
324 public float neg_overlap
325 {
326 get { return m_fNegOverlap; }
327 set { m_fNegOverlap = value; }
328 }
329
333 [Description("Get/set the coding method for the bounding box.")]
335 {
336 get { return m_codeType; }
337 set { m_codeType = value; }
338 }
339
343 [Description("Get/set whether or not to encode the variance of the prior box in the loc loss target instead of in the bbox (default = false).")]
345 {
346 get { return m_bEncodeVarianceInTarget; }
347 set { m_bEncodeVarianceInTarget = value; }
348 }
349
353 [Description("Get/set whether or not to map all object classes to an agnostic class (default = false). This is useful when learning objectness detector.")]
355 {
356 get { return m_bMapObjectToAgnostic; }
357 set { m_bMapObjectToAgnostic = value; }
358 }
359
364 [Description("Get/set whether or not to ignore cross boundary bbox during matching (default = false). The cross boundary bbox is a bbox who is outside of the image region.")]
366 {
367 get { return m_bIgnoreCrossBoundaryBbox; }
368 set { m_bIgnoreCrossBoundaryBbox = value; }
369 }
370
374 [Description("Get/set whether or not to only backpropagate on corners which are inside of the image region when encode type is CORNER or CORNER_SIZE (default = false).")]
375 public bool bp_inside
376 {
377 get { return m_bBpInside; }
378 set { m_bBpInside = value; }
379 }
380
384 [Description("Get/set the mining type used during training (default = MAX_NEGATIVE).")]
386 {
387 get { return m_miningType; }
388 set { m_miningType = value; }
389 }
390
394 [Description("Get/set the parameters used for the non maximum suppression during hard example training.")]
396 {
397 get { return m_nmsParam; }
398 set { m_nmsParam = value; }
399 }
400
404 [Description("Get/set the number of samples (default = 64).")]
405 public int sample_size
406 {
407 get { return m_nSampleSize; }
408 set { m_nSampleSize = value; }
409 }
410
414 [Description("Get/set whether or not to use the prior bbox for nms.")]
416 {
417 get { return m_bUsePriorForNms; }
418 set { m_bUsePriorForNms = value; }
419 }
420
424 [Description("Get/set whether or not to use prior for matching.")]
426 {
427 get { return m_bUsePriorForMatching; }
428 set { m_bUsePriorForMatching = value; }
429 }
430
434 [Description("Use the GPU version of the algorithm.")]
435 public bool use_gpu
436 {
437 get { return m_bUseGpu; }
438 set { m_bUseGpu = value; }
439 }
440
447 public override object Load(BinaryReader br, bool bNewInstance = true)
448 {
449 RawProto proto = RawProto.Parse(br.ReadString());
451
452 if (!bNewInstance)
453 Copy(p);
454
455 return p;
456 }
457
462 public override void Copy(LayerParameterBase src)
463 {
465
466 m_locLossType = p.loc_loss_type;
467 m_confLossType = p.conf_loss_type;
468 m_fLocWeight = p.loc_weight;
469 m_nNumClasses = p.num_classes;
470 m_bShareLocation = p.share_location;
471 m_matchType = p.match_type;
472 m_fOverlapThreshold = p.overlap_threshold;
473 m_nBackgroundLabelId = p.background_label_id;
474 m_bUseDifficultGt = p.use_difficult_gt;
475 m_bDoNegMining = p.do_neg_mining;
476 m_fNegPosRatio = p.neg_pos_ratio;
477 m_fNegOverlap = p.neg_overlap;
478 m_codeType = p.code_type;
479 m_bEncodeVarianceInTarget = p.encode_variance_in_target;
480 m_bMapObjectToAgnostic = p.map_object_to_agnostic;
481 m_bIgnoreCrossBoundaryBbox = p.ignore_cross_boundary_bbox;
482 m_bBpInside = p.bp_inside;
483 m_miningType = p.mining_type;
484 m_nmsParam = p.nms_param.Clone();
485 m_nSampleSize = p.sample_size;
486 m_bUsePriorForNms = p.use_prior_for_nms;
487 m_bUsePriorForMatching = p.use_prior_for_matching;
488 m_bUseGpu = p.use_gpu;
489 }
490
495 public override LayerParameterBase Clone()
496 {
498 p.Copy(this);
499 return p;
500 }
501
507 public override RawProto ToProto(string strName)
508 {
509 RawProtoCollection rgChildren = new RawProtoCollection();
510
511 rgChildren.Add(new RawProto("loc_loss_type", loc_loss_type.ToString()));
512 rgChildren.Add(new RawProto("conf_loss_type", conf_loss_type.ToString()));
513 rgChildren.Add(new RawProto("loc_weight", loc_weight.ToString()));
514 rgChildren.Add(new RawProto("num_classes", num_classes.ToString()));
515 rgChildren.Add(new RawProto("share_location", share_location.ToString()));
516 rgChildren.Add(new RawProto("match_type", match_type.ToString()));
517 rgChildren.Add(new RawProto("overlap_threshold", overlap_threshold.ToString()));
518 rgChildren.Add(new RawProto("background_label_id", background_label_id.ToString()));
519 rgChildren.Add(new RawProto("use_difficult_gt", use_difficult_gt.ToString()));
520 rgChildren.Add(new RawProto("do_neg_mining", do_neg_mining.ToString()));
521 rgChildren.Add(new RawProto("neg_pos_ratio", neg_pos_ratio.ToString()));
522 rgChildren.Add(new RawProto("neg_overlap", neg_overlap.ToString()));
523 rgChildren.Add(new RawProto("code_type", code_type.ToString()));
524 rgChildren.Add(new RawProto("encode_variance_in_target", encode_variance_in_target.ToString()));
525 rgChildren.Add(new RawProto("map_object_to_agnostic", map_object_to_agnostic.ToString()));
526 rgChildren.Add(new RawProto("ignore_cross_boundary_bbox", ignore_cross_boundary_bbox.ToString()));
527 rgChildren.Add(new RawProto("bp_inside", bp_inside.ToString()));
528 rgChildren.Add(new RawProto("mining_type", mining_type.ToString()));
529 rgChildren.Add(nms_param.ToProto("nms_param"));
530 rgChildren.Add(new RawProto("sample_size", sample_size.ToString()));
531 rgChildren.Add(new RawProto("use_prior_for_nms", use_prior_for_nms.ToString()));
532 rgChildren.Add(new RawProto("use_prior_for_matching", use_prior_for_matching.ToString()));
533 rgChildren.Add(new RawProto("use_gpu", use_gpu.ToString()));
534
535 return new RawProto(strName, "", rgChildren);
536 }
537
544 {
546 string strVal;
547
548 if ((strVal = rp.FindValue("loc_loss_type")) != null)
550
551 if ((strVal = rp.FindValue("conf_loss_type")) != null)
553
554 if ((strVal = rp.FindValue("loc_weight")) != null)
555 p.loc_weight = ParseFloat(strVal);
556
557 if ((strVal = rp.FindValue("num_classes")) != null)
558 p.num_classes = uint.Parse(strVal);
559
560 if ((strVal = rp.FindValue("share_location")) != null)
561 p.share_location = bool.Parse(strVal);
562
563 if ((strVal = rp.FindValue("match_type")) != null)
565
566 if ((strVal = rp.FindValue("overlap_threshold")) != null)
567 p.overlap_threshold = ParseFloat(strVal);
568
569 if ((strVal = rp.FindValue("background_label_id")) != null)
570 p.background_label_id = uint.Parse(strVal);
571
572 if ((strVal = rp.FindValue("use_difficult_gt")) != null)
573 p.use_difficult_gt = bool.Parse(strVal);
574
575 if ((strVal = rp.FindValue("do_neg_mining")) != null)
576 p.do_neg_mining = bool.Parse(strVal);
577
578 if ((strVal = rp.FindValue("neg_pos_ratio")) != null)
579 p.neg_pos_ratio = ParseFloat(strVal);
580
581 if ((strVal = rp.FindValue("neg_overlap")) != null)
582 p.neg_overlap = ParseFloat(strVal);
583
584 if ((strVal = rp.FindValue("code_type")) != null)
586
587 if ((strVal = rp.FindValue("encode_variance_in_target")) != null)
588 p.encode_variance_in_target = bool.Parse(strVal);
589
590 if ((strVal = rp.FindValue("map_object_to_agnostic")) != null)
591 p.map_object_to_agnostic = bool.Parse(strVal);
592
593 if ((strVal = rp.FindValue("ignore_corss_boundary_bbox")) != null)
594 p.ignore_cross_boundary_bbox = bool.Parse(strVal);
595
596 if ((strVal = rp.FindValue("bp_inside")) != null)
597 p.bp_inside = bool.Parse(strVal);
598
599 if ((strVal = rp.FindValue("mining_type")) != null)
601
602 RawProto rpNms = rp.FindChild("nms_param");
603 if (rpNms != null)
605
606 if ((strVal = rp.FindValue("sample_size")) != null)
607 p.sample_size = int.Parse(strVal);
608
609 if ((strVal = rp.FindValue("use_prior_for_nms")) != null)
610 p.use_prior_for_nms = bool.Parse(strVal);
611
612 if ((strVal = rp.FindValue("use_prior_for_matching")) != null)
613 p.use_prior_for_matching = bool.Parse(strVal);
614
615 if ((strVal = rp.FindValue("use_gpu")) != null)
616 p.use_gpu = bool.Parse(strVal);
617
618 return p;
619 }
620 }
621}
static float ParseFloat(string strVal)
Parse float values using the US culture if the decimal separator = '.', then using the native culture...
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 LayerParameterBase is the base class for all other layer specific parameters.
Specifies the parameters for the MultiBoxLossParameter.
float loc_weight
Get/set the weight for the localization loss (default = 1.0).
float overlap_threshold
Get/set the overlap threshold (default = 0.5).
bool use_difficult_gt
Get/set whether or not to consider the difficult ground truth (defalt = true).
override object Load(BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
bool use_gpu
Use the GPU version of the algorithm.
MatchType
Defines the matching method used during training.
PriorBoxParameter.CodeType code_type
Get/set the coding method for the bounding box.
static MiningType MiningTypeFromString(string str)
Convert a string into a MiningType.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
float neg_overlap
Get/set the negative overlap upperbound for the unmatched predictions (default = 0....
LocLossType
Defines the localization loss types.
int sample_size
Get/set the number of samples (default = 64).
float neg_pos_ratio
Get/set the negative/positive ratio (default = 3.0).
bool share_location
Get/sets whether or not the bounding box is shared among different classes (default = true).
MiningType
Defines the mining type used during training.
static MultiBoxLossParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
NonMaximumSuppressionParameter nms_param
Get/set the parameters used for the non maximum suppression during hard example training.
LocLossType loc_loss_type
Get/set the localization loss type (default = SMOOTH_L1).
static MatchType MatchTypeFromString(string str)
Convert a string into a MatchType.
uint background_label_id
Get/set the background label id.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
ConfLossType
Defines the confidence loss types.
bool encode_variance_in_target
Get/set whether or not to encode the variance of the prior box in the loc loss target instead of in t...
bool ignore_cross_boundary_bbox
Get/set whether or not to ignore cross boundary bbox during matching (default = false)....
static ConfLossType ConfLossTypeFromString(string str)
Convert a string into a ConfLossType.
bool map_object_to_agnostic
Get/set whether or not to map all object classes to an agnostic class (default = false)....
bool? do_neg_mining
DEPRECIATED: Get/set whether or not to perform negative mining (default = false).
bool bp_inside
Get/set whether or not to only backpropagate on corners which are inside of the image region when enc...
static LocLossType LocLossTypeFromString(string str)
Convert a string into a LocLossType.
ConfLossType conf_loss_type
Get/set the confidence loss type (default = SOFTMAX).
bool use_prior_for_matching
Get/set whether or not to use prior for matching.
uint num_classes
Get/set the number of classes to be predicted - required!
MiningType mining_type
Get/set the mining type used during training (default = MAX_NEGATIVE).
bool use_prior_for_nms
Get/set whether or not to use the prior bbox for nms.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
MatchType match_type
Get/set the matching method used during training (default = PER_PREDICTION).
Specifies the parameters for the NonMaximumSuppressionParameter used with SSD.
override RawProto ToProto(string strName)
Convert this object to a raw proto.
NonMaximumSuppressionParameter Clone()
Return a clone of the object.
static new NonMaximumSuppressionParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
Specifies the parameters for the PriorBoxParameter.
static CodeType CodeTypeFromString(string str)
Convert a string into a CodeType.
CodeType
Defines the encode/decode type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ NONE
No training category specified.
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12