MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DebugInformation.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using MyCaffe.layers;
7
8namespace MyCaffe.common
9{
15 public class DebugInformation<T> : IDisposable
16 {
17 string m_strName;
18 double m_dfExtraInfo = 0;
19 Blob<T> m_blobWork;
20 bool m_bDetectNans = false;
21 List<LayerDebugInformation<T>> m_rgLayerInfo = new List<LayerDebugInformation<T>>();
22
29 public DebugInformation(string strName, Blob<T> work, bool bDetectNans)
30 {
31 m_strName = strName;
32 m_blobWork = work;
33 m_bDetectNans = bDetectNans;
34 }
35
39 public void Dispose()
40 {
41 if (m_blobWork != null)
42 {
43 m_blobWork.Dispose();
44 m_blobWork = null;
45 }
46 }
47
53 public LayerDebugInformation<T> FindLayer(string strLayer)
54 {
55 foreach (LayerDebugInformation<T> layer in m_rgLayerInfo)
56 {
57 if (layer.Name == strLayer)
58 return layer;
59 }
60
61 return null;
62 }
63
70 public void Add(Layer<T> layer, BlobCollection<T> colBottomBlobs, BlobCollection<T> colTopBlobs)
71 {
72 m_rgLayerInfo.Add(new LayerDebugInformation<T>(layer, colBottomBlobs, colTopBlobs, m_blobWork, m_bDetectNans));
73 }
74
78 public string Name
79 {
80 get { return m_strName; }
81 }
82
86 public double ExtraInfo
87 {
88 get { return m_dfExtraInfo; }
89 set { m_dfExtraInfo = value; }
90 }
91
95 public List<LayerDebugInformation<T>> LayerInfoList
96 {
97 get { return m_rgLayerInfo; }
98 }
99
106 {
107 if (dbg.Name != m_strName)
108 return false;
109
110 if (dbg.m_rgLayerInfo.Count != m_rgLayerInfo.Count)
111 return false;
112
113 for (int i = 0; i < m_rgLayerInfo.Count; i++)
114 {
115 if (!dbg.m_rgLayerInfo[i].Compare(m_rgLayerInfo[i]))
116 return false;
117 }
118
119 return true;
120 }
121
127 public string DetectFirstNaN(out string strType)
128 {
129 for (int i = 0; i< m_rgLayerInfo.Count; i++)
130 {
131 LayerDebugInformation<T> dbg = m_rgLayerInfo[i];
132 string strBlobNan = dbg.DetectFirstNaN(out strType);
133
134 if (strBlobNan != null)
135 return strBlobNan;
136 }
137
138 strType = null;
139 return null;
140 }
141
147 public string DetectLastNaN(out string strType)
148 {
149 for (int i = m_rgLayerInfo.Count - 1; i >= 0; i--)
150 {
151 LayerDebugInformation<T> dbg = m_rgLayerInfo[i];
152 string strBlobNan = dbg.DetectLastNaN(out strType);
153
154 if (strBlobNan != null)
155 return strBlobNan;
156 }
157
158 strType = null;
159 return null;
160 }
161
166 public override string ToString()
167 {
168 return m_strName;
169 }
170 }
171
176 public class LayerDebugInformation<T>
177 {
178 string m_strName;
179 string m_strType;
180 double m_dfForwardTiming = 0;
181 double m_dfForwardTimingAve = 0;
182 double m_dfBackwardTiming = 0;
183 double m_dfBackwardTimingAve = 0;
184 List<BlobDebugInformation<T>> m_rgBlobInfo = new List<BlobDebugInformation<T>>();
185
194 public LayerDebugInformation(Layer<T> layer, BlobCollection<T> colBottom, BlobCollection<T> colTop, Blob<T> work, bool bDetectNans)
195 {
196 m_strName = layer.layer_param.name;
197 m_strType = layer.type.ToString();
198 m_dfForwardTiming = layer.forward_timing;
199 m_dfForwardTimingAve = layer.forward_timing_average;
200 m_dfBackwardTiming = layer.backward_timing;
201 m_dfBackwardTimingAve = layer.backward_timing_average;
202
203 foreach (Blob<T> b in colBottom)
204 {
205 m_rgBlobInfo.Add(new BlobDebugInformation<T>(b, work, BlobDebugInformation<T>.BLOBTYPE.DATA, BlobDebugInformation<T>.LOCATION.BOTTOM, bDetectNans));
206 }
207
208 foreach (Blob<T> b in colTop)
209 {
210 m_rgBlobInfo.Add(new BlobDebugInformation<T>(b, work, BlobDebugInformation<T>.BLOBTYPE.DATA, BlobDebugInformation<T>.LOCATION.TOP, bDetectNans));
211 }
212
213 foreach (Blob<T> b in layer.blobs)
214 {
215 m_rgBlobInfo.Add(new BlobDebugInformation<T>(b, work, BlobDebugInformation<T>.BLOBTYPE.PARAM, BlobDebugInformation<T>.LOCATION.NONE, bDetectNans));
216 }
217
218 foreach (Blob<T> b in layer.internal_blobs)
219 {
220 m_rgBlobInfo.Add(new BlobDebugInformation<T>(b, work, BlobDebugInformation<T>.BLOBTYPE.INTERNAL, BlobDebugInformation<T>.LOCATION.NONE, bDetectNans));
221 }
222 }
223
227 public string Name
228 {
229 get { return m_strName; }
230 }
231
235 public string Type
236 {
237 get { return m_strType; }
238 }
239
243 public double ForwardTiming
244 {
245 get { return m_dfForwardTiming; }
246 }
247
252 {
253 get { return m_dfForwardTimingAve; }
254 }
255
259 public double BackwardTiming
260 {
261 get { return m_dfBackwardTiming; }
262 }
263
268 {
269 get { return m_dfBackwardTimingAve; }
270 }
271
275 public List<BlobDebugInformation<T>> BlobInfoList
276 {
277 get { return m_rgBlobInfo; }
278 }
279
285 public BlobDebugInformation<T> FindBlob(string strBlob)
286 {
287 foreach (BlobDebugInformation<T> blob in m_rgBlobInfo)
288 {
289 if (blob.Name == strBlob)
290 return blob;
291 }
292
293 return null;
294 }
295
302 {
303 if (dbg.Name != m_strName)
304 return false;
305
306 if (dbg.Type != m_strType)
307 return false;
308
309 if (dbg.m_rgBlobInfo.Count != m_rgBlobInfo.Count)
310 return false;
311
312 for (int i = 0; i < m_rgBlobInfo.Count; i++)
313 {
314 if (!dbg.m_rgBlobInfo[i].Compare(m_rgBlobInfo[i]))
315 return false;
316 }
317
318 return true;
319 }
320
326 public string DetectFirstNaN(out string strType)
327 {
328 for (int i = 0; i < m_rgBlobInfo.Count; i++)
329 {
330 BlobDebugInformation<T> dbg = m_rgBlobInfo[i];
331 string strBlobNan = dbg.DetectFirstNaN(out strType);
332
333 if (strBlobNan != null)
334 return strBlobNan;
335 }
336
337 strType = null;
338
339 return null;
340 }
341
347 public string DetectLastNaN(out string strType)
348 {
349 for (int i=m_rgBlobInfo.Count-1; i>=0; i--)
350 {
351 BlobDebugInformation<T> dbg = m_rgBlobInfo[i];
352 string strBlobNan = dbg.DetectLastNaN(out strType);
353
354 if (strBlobNan != null)
355 return strBlobNan;
356 }
357
358 strType = null;
359
360 return null;
361 }
362
367 public override string ToString()
368 {
369 return m_strName + " (" + m_strType + ")";
370 }
371 }
372
377 public class BlobDebugInformation<T>
378 {
379 BLOBTYPE m_type;
380 LOCATION m_location;
381 string m_strName;
382 string m_strSize;
383 double m_dfDataMin;
384 double m_dfDataMax;
385 double m_dfDiffMin;
386 double m_dfDiffMax;
387 double m_dfDataNanCount = 0;
388 double m_dfDataInfCount = 0;
389 double m_dfDiffNanCount = 0;
390 double m_dfDiffInfCount = 0;
391
395 public enum BLOBTYPE
396 {
400 DATA,
404 PARAM,
409 }
410
414 public enum LOCATION
415 {
419 NONE,
423 BOTTOM,
427 TOP
428 }
429
438 public BlobDebugInformation(Blob<T> b, Blob<T> work, BLOBTYPE type, LOCATION loc = LOCATION.NONE, bool bDetectNans = false)
439 {
440 m_type = type;
441 m_location = loc;
442 m_strName = b.Name;
443 m_strSize = b.ToSizeString();
444
445 Tuple<double, double, double, double> data = b.minmax_data(work, bDetectNans);
446 Tuple<double, double, double, double> diff = b.minmax_diff(work, bDetectNans);
447
448 m_dfDataMin = data.Item1;
449 m_dfDataMax = data.Item2;
450 m_dfDataNanCount = data.Item3;
451 m_dfDataInfCount = data.Item4;
452 m_dfDiffMin = diff.Item1;
453 m_dfDiffMax = diff.Item2;
454 m_dfDiffNanCount = diff.Item3;
455 m_dfDiffInfCount = diff.Item4;
456 }
457
461 public string Name
462 {
463 get { return m_strName; }
464 }
465
469 public string Size
470 {
471 get { return m_strSize; }
472 }
473
477 public double DataMinValue
478 {
479 get
480 {
481 if (DiffNanCount > 0)
482 return double.NaN;
483
484 if (DiffInfCount > 0)
485 return double.PositiveInfinity;
486
487 return m_dfDataMin;
488 }
489 }
490
494 public double DataMaxValue
495 {
496 get
497 {
498 if (DiffNanCount > 0)
499 return double.NaN;
500
501 if (DiffInfCount > 0)
502 return double.PositiveInfinity;
503
504 return m_dfDataMax;
505 }
506 }
507
511 public double DataNanCount
512 {
513 get { return m_dfDataNanCount; }
514 }
515
519 public double DataInfCount
520 {
521 get { return m_dfDataInfCount; }
522 }
523
527 public double DiffMinValue
528 {
529 get
530 {
531 if (DiffNanCount > 0)
532 return double.NaN;
533
534 if (DiffInfCount > 0)
535 return double.PositiveInfinity;
536
537 return m_dfDiffMin;
538 }
539 }
540
544 public double DiffMaxValue
545 {
546 get
547 {
548 if (DiffNanCount > 0)
549 return double.NaN;
550
551 if (DiffInfCount > 0)
552 return double.PositiveInfinity;
553
554 return m_dfDiffMax;
555 }
556 }
557
561 public double DiffNanCount
562 {
563 get { return m_dfDiffNanCount; }
564 }
565
569 public double DiffInfCount
570 {
571 get { return m_dfDiffInfCount; }
572 }
573
577 public BLOBTYPE BlobType
578 {
579 get { return m_type; }
580 }
581
585 public LOCATION BlobLocation
586 {
587 get { return m_location; }
588 }
589
596 {
597 if (dbg.Name != m_strName)
598 return false;
599
600 if (dbg.Size != m_strSize)
601 return false;
602
603 if (dbg.BlobLocation != m_location)
604 return false;
605
606 if (dbg.BlobType != m_type)
607 return false;
608
609 if (dbg.DataMinValue != m_dfDataMin)
610 return false;
611
612 if (dbg.DataMaxValue != m_dfDataMax)
613 return false;
614
615 if (dbg.DataNanCount != m_dfDataNanCount)
616 return false;
617
618 if (dbg.DataInfCount != m_dfDiffInfCount)
619 return false;
620
621 if (dbg.DiffMinValue != m_dfDiffMin)
622 return false;
623
624 if (dbg.DiffMaxValue != m_dfDiffMax)
625 return false;
626
627 if (dbg.DiffNanCount != m_dfDiffNanCount)
628 return false;
629
630 if (dbg.DiffInfCount != m_dfDiffInfCount)
631 return false;
632
633 return true;
634 }
635
641 public string DetectFirstNaN(out string strType)
642 {
643 if (!isValid(m_dfDataMin))
644 {
645 strType = "data min";
646 return m_strName;
647 }
648
649 if (!isValid(m_dfDataMax))
650 {
651 strType = "data max";
652 return m_strName;
653 }
654
655 if (m_dfDataNanCount > 0)
656 {
657 strType = "data nan";
658 return m_strName;
659 }
660
661 if (m_dfDataInfCount > 0)
662 {
663 strType = "data inf";
664 return m_strName;
665 }
666
667 if (!isValid(m_dfDiffMin))
668 {
669 strType = "diff min";
670 return m_strName;
671 }
672
673 if (!isValid(m_dfDiffMax))
674 {
675 strType = "diff max";
676 return m_strName;
677 }
678
679 if (m_dfDiffNanCount > 0)
680 {
681 strType = "diff nan";
682 return m_strName;
683 }
684
685 if (m_dfDiffInfCount > 0)
686 {
687 strType = "diff inf";
688 return m_strName;
689 }
690
691 strType = null;
692 return null;
693 }
694
700 public string DetectLastNaN(out string strType)
701 {
702 if (!isValid(m_dfDiffMin))
703 {
704 strType = "diff min";
705 return m_strName;
706 }
707
708 if (!isValid(m_dfDiffMax))
709 {
710 strType = "diff max";
711 return m_strName;
712 }
713
714 if (m_dfDiffNanCount > 0)
715 {
716 strType = "diff nan";
717 return m_strName;
718 }
719
720 if (m_dfDiffInfCount > 0)
721 {
722 strType = "diff inf";
723 return m_strName;
724 }
725
726 if (!isValid(m_dfDataMin))
727 {
728 strType = "data min";
729 return m_strName;
730 }
731
732 if (!isValid(m_dfDataMax))
733 {
734 strType = "data max";
735 return m_strName;
736 }
737
738 if (m_dfDataNanCount > 0)
739 {
740 strType = "data nan";
741 return m_strName;
742 }
743
744 if (m_dfDataInfCount > 0)
745 {
746 strType = "data inf";
747 return m_strName;
748 }
749
750 strType = null;
751 return null;
752 }
753
754 private bool isValid(double df)
755 {
756 if (double.IsNaN(df))
757 return false;
758
759 if (double.IsInfinity(df))
760 return false;
761
762 return true;
763 }
764
769 public override string ToString()
770 {
771 return m_strName + " (" + m_strSize + ")";
772 }
773 }
774}
The BlobCollection contains a list of Blobs.
The BlobDebugInformation describes debug information relating to a given Blob in a given Layer.
double DiffInfCount
Returns the number of infinity values detected in the Blob diff.
bool Compare(BlobDebugInformation< T > dbg)
Compares this BlobDebugInformation to another one.
double DiffMaxValue
Returns the maximum value of the Blob diff.
override string ToString()
Returns a string representation of the BlobDebugInformation.
string DetectFirstNaN(out string strType)
Search for the first NaN in the Blob.
double DiffNanCount
Returns the number of nans detected in the Blob diff.
double DiffMinValue
Returns the minimum value of the Blob diff.
string Name
Returns the name of the Blob.
double DataInfCount
Returns the number of infinity values detected in the Blob data.
string Size
Returns the size of the Blob.
LOCATION BlobLocation
Returns the Blob location.
double DataMinValue
Returns the minimum value of the Blob data.
BlobDebugInformation(Blob< T > b, Blob< T > work, BLOBTYPE type, LOCATION loc=LOCATION.NONE, bool bDetectNans=false)
The BlobDebugInformation constructor.
BLOBTYPE BlobType
Returns the Blob type.
double DataNanCount
Returns the number of nans detected in the Blob data.
double DataMaxValue
Returns the maximum value of the Blob data.
string DetectLastNaN(out string strType)
Search for the last NaN in the Blob.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
Tuple< double, double, double, double > minmax_data(Blob< T > work, bool bDetectNans=false, bool bUseChunks=false)
Returns the minimum and maximum values in the data of the Blob.
Definition: Blob.cs:2624
Tuple< double, double, double, double > minmax_diff(Blob< T > work, bool bDetectNans=false, bool bUseChunks=false)
Returns the minimum and maximum values in the diff of the Blob.
Definition: Blob.cs:2694
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
string ToSizeString()
Returns a string describing the 4D shape of the Blob.
Definition: Blob.cs:2157
The DebugInformation contains information used to help debug the Layers of a Net while it is training...
void Add(Layer< T > layer, BlobCollection< T > colBottomBlobs, BlobCollection< T > colTopBlobs)
Adds a Layer and its Bottom and Top blob collections.
string Name
Returns the name of the DebugInformation.
LayerDebugInformation< T > FindLayer(string strLayer)
Searches for the named layer.
double ExtraInfo
Get/set extra information about the DebugInformation.
bool Compare(DebugInformation< T > dbg)
Compares this DebugInformation to another.
string DetectFirstNaN(out string strType)
Searches for the first NaN within any of the Layers.
override string ToString()
Returns a string representation of the DebugInformation.
DebugInformation(string strName, Blob< T > work, bool bDetectNans)
The DebugInformation constructor.
void Dispose()
Releases the memory (GPU and Host) used by the DebugInformation including the Workspace.
List< LayerDebugInformation< T > > LayerInfoList
Returns an array of LayerDebugInformation corresponding to each Layer added.
string DetectLastNaN(out string strType)
Searches for the last NaN within any of the Layers.
The LayerDebugInformation describes debug information relating to a given Layer in the Net.
string DetectFirstNaN(out string strType)
Searches for the first NaN within the Layer.
double ForwardTiming
Returns the timing of the last forward pass made by the Layer.
double BackwardTiming
Returns the timing of the last backward pass made by the Layer.
List< BlobDebugInformation< T > > BlobInfoList
Returns the collection of BlobDebugInformation for the Layer.
bool Compare(LayerDebugInformation< T > dbg)
Compare this LayerDebugInformation to another.
LayerDebugInformation(Layer< T > layer, BlobCollection< T > colBottom, BlobCollection< T > colTop, Blob< T > work, bool bDetectNans)
The LayerDebugInformation constructor.
BlobDebugInformation< T > FindBlob(string strBlob)
Searches for the named blob.
string Name
Returns the name of the Layer managed by the LayerDebugInformation.
override string ToString()
Returns the string representation of the LayerDebugInformation.
string DetectLastNaN(out string strType)
Searches for the last NaN within the Layer.
string Type
Returns the type of the Layer managed by the LayerDebugInformation.
double ForwardTimingAverage
Returns the average timing of the forward passes made by the Layer.
double BackwardTimingAverage
Returns the average timing of the backward passes made by the Layer.
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
double backward_timing
Returns the timing of the last backward pass in milliseconds.
Definition: Layer.cs:1257
double backward_timing_average
Returns the average timing of the backward passes in milliseconds.
Definition: Layer.cs:1265
double forward_timing_average
Returns the average timing of the forward passes in milliseconds.
Definition: Layer.cs:1249
LayerParameter.LayerType type
Returns the LayerType of this Layer.
Definition: Layer.cs:927
double forward_timing
Returns the timing of the last forward pass in milliseconds.
Definition: Layer.cs:1241
LayerParameter layer_param
Returns the LayerParameter for this Layer.
Definition: Layer.cs:899
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
@ INTERNAL
The blob is an internal blob used within the layer.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12