MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BlobCollection.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using System.IO;
7using System.Globalization;
8
9namespace MyCaffe.common
10{
15 public class BlobCollection<T> : IEnumerable<Blob<T>>, IDisposable
16 {
17 List<Blob<T>> m_rgBlobs = new List<Blob<T>>();
18
23 {
24 }
25
29 public int Count
30 {
31 get { return m_rgBlobs.Count; }
32 }
33
39 public Blob<T> this[int nIdx]
40 {
41 get { return m_rgBlobs[nIdx]; }
42 set { m_rgBlobs[nIdx] = value; }
43 }
44
50 public Blob<T> this[string strName]
51 {
52 get
53 {
54 Blob<T> blob = FindBlob(strName);
55 if (blob != null)
56 return blob;
57
58 throw new Exception("Could not find the blob named '" + strName + "' in the collection.");
59 }
60 }
61
67 public Blob<T> FindBlob(string strName)
68 {
69 for (int i = 0; i < m_rgBlobs.Count; i++)
70 {
71 if (m_rgBlobs[i] != null && m_rgBlobs[i].Name == strName)
72 return m_rgBlobs[i];
73 }
74
75 return null;
76 }
77
83 public void Insert(int nIdx, Blob<T> b)
84 {
85 m_rgBlobs.Insert(nIdx, b);
86 }
87
92 public void Add(Blob<T> b)
93 {
94 m_rgBlobs.Add(b);
95 }
96
101 public void Add(BlobCollection<T> rg)
102 {
103 if (rg == null)
104 return;
105
106 foreach (Blob<T> b in rg)
107 {
108 m_rgBlobs.Add(b);
109 }
110 }
111
117 public bool Remove(Blob<T> b)
118 {
119 return m_rgBlobs.Remove(b);
120 }
121
126 public void RemoveAt(int nIdx)
127 {
128 m_rgBlobs.RemoveAt(nIdx);
129 }
130
135 public void Clear(bool bDispose = false)
136 {
137 if (bDispose)
138 {
139 foreach (Blob<T> b in m_rgBlobs)
140 {
141 b.Dispose();
142 }
143 }
144
145 m_rgBlobs.Clear();
146 }
147
153 public bool Contains(Blob<T> blob)
154 {
155 return m_rgBlobs.Contains(blob);
156 }
157
164 {
166
167 foreach (Blob<T> b1 in m_rgBlobs)
168 {
169 if (b1 != null && b1.Name.Contains(b.Name))
170 rg.Add(b1);
171 }
172
173 return rg;
174 }
175
180 public IEnumerator<Blob<T>> GetEnumerator()
181 {
182 return m_rgBlobs.GetEnumerator();
183 }
184
189 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
190 {
191 return m_rgBlobs.GetEnumerator();
192 }
193
199 {
201
202 foreach (Blob<T> b in m_rgBlobs)
203 {
204 col.Add(b.Clone());
205 }
206
207 return col;
208 }
209
215 {
216 for (int i = 0; i < m_rgBlobs.Count; i++)
217 {
218 m_rgBlobs[i].ReshapeLike(src[i]);
219 }
220 }
221
226 public void ReshapeLike(Blob<T> blob)
227 {
228 for (int i = 0; i < m_rgBlobs.Count; i++)
229 {
230 m_rgBlobs[i].ReshapeLike(blob);
231 }
232 }
233
238 public void Reshape(int[] rgShape)
239 {
240 for (int i = 0; i < m_rgBlobs.Count; i++)
241 {
242 m_rgBlobs[i].Reshape(rgShape);
243 }
244 }
245
253 public void Reshape(int nN, int nC, int nH, int nW)
254 {
255 for (int i = 0; i < m_rgBlobs.Count; i++)
256 {
257 m_rgBlobs[i].Reshape(nN, nC, nH, nW);
258 }
259 }
260
266 public void CopyFrom(BlobCollection<T> bSrc, bool bCopyDiff = false)
267 {
268 if (Count != bSrc.Count)
269 throw new Exception("The source and destination should have the same count.");
270
271 for (int i = 0; i < bSrc.Count; i++)
272 {
273 m_rgBlobs[i].CopyFrom(bSrc[i], bCopyDiff, true);
274 }
275 }
276
283 public void Accumulate(CudaDnn<T> cuda, BlobCollection<T> src, bool bAccumulateDiff)
284 {
285 for (int i = 0; i < src.Count; i++)
286 {
287 Blob<T> bSrc = src[i];
288 Blob<T> bDst = m_rgBlobs[i];
289 int nSrcCount = bSrc.count();
290 int nDstCount = bDst.count();
291
292 if (nSrcCount != nDstCount)
293 throw new Exception("The src and dst blobs at index #" + i.ToString() + " have different sizes!");
294
295 if (bAccumulateDiff)
296 {
297 if (bSrc.DiffExists && bDst.DiffExists)
298 cuda.add(nSrcCount, bSrc.gpu_diff, bDst.gpu_diff, bDst.mutable_gpu_diff);
299 }
300 else
301 {
302 cuda.add(nSrcCount, bSrc.gpu_data, bDst.gpu_data, bDst.mutable_gpu_data);
303 }
304 }
305 }
306
311 public void SetDiff(double df)
312 {
313 for (int i = 0; i < m_rgBlobs.Count; i++)
314 {
315 m_rgBlobs[i].SetDiff(df);
316 }
317 }
318
323 public void SetData(double df)
324 {
325 for (int i = 0; i < m_rgBlobs.Count; i++)
326 {
327 m_rgBlobs[i].SetData(df);
328 }
329 }
330
340 public BlobCollection<T> MathSub(BlobCollection<T> col, bool bSkipFirstItem)
341 {
342 if (col.Count != m_rgBlobs.Count)
343 throw new Exception("The input blob collection must have the same count at this blob collection!");
344
346
347 for (int i = 0; i < m_rgBlobs.Count; i++)
348 {
349 if (i > 0 || !bSkipFirstItem)
350 colOut.Add(m_rgBlobs[i].MathSub(col[i]));
351 }
352
353 return colOut;
354 }
355
366 public BlobCollection<T> MathAdd(BlobCollection<T> colA, double dfScale, bool bSkipFirstItem)
367 {
368 if (colA.Count != m_rgBlobs.Count)
369 throw new Exception("The input blob collection must have the same count at this blob collection!");
370
372 T fScale = (T)Convert.ChangeType(dfScale, typeof(T));
373
374 for (int i = 0; i < m_rgBlobs.Count; i++)
375 {
376 if (i > 0 || !bSkipFirstItem)
377 colOut.Add(m_rgBlobs[i].MathAdd(colA[i], fScale));
378 }
379
380 return colOut;
381 }
382
389 public BlobCollection<T> MathDiv(double dfVal, bool bSkipFirstItem)
390 {
392 T fVal = (T)Convert.ChangeType(dfVal, typeof(T));
393
394 for (int i = 0; i < m_rgBlobs.Count; i++)
395 {
396 if (i > 0 || !bSkipFirstItem)
397 colOut.Add(m_rgBlobs[i].MathDiv(fVal));
398 }
399
400 return colOut;
401 }
402
409 public void Save(BinaryWriter bw, bool bData, bool bDiff)
410 {
411 bw.Write(m_rgBlobs.Count);
412
413 foreach (Blob<T> b in m_rgBlobs)
414 {
415 b.Save(bw, bData, bDiff);
416 }
417 }
418
428 public static BlobCollection<T> Load(CudaDnn<T> cuda, Log log, BinaryReader br, bool bData, bool bDiff)
429 {
431 int nCount = br.ReadInt32();
432
433 for (int i = 0; i < nCount; i++)
434 {
435 col.Add(Blob<T>.Load(cuda, log, br, bData, bDiff));
436 }
437
438 return col;
439 }
440
449 public bool Share(Blob<T> b, List<int> rgMinShape, bool bThrowExceptions, bool bAllowEndsWith = false)
450 {
451 int nCount = 0;
452
453 if (rgMinShape != null && rgMinShape.Count > 0)
454 {
455 nCount = rgMinShape[0];
456 for (int i = 1; i < rgMinShape.Count; i++)
457 {
458 nCount *= rgMinShape[i];
459 }
460 }
461
462 foreach (Blob<T> blobShare in m_rgBlobs)
463 {
464 if (blobShare.Name == b.Name || (bAllowEndsWith && blobShare.Name.EndsWith(b.Name)))
465 {
466 if (nCount > 0)
467 {
468 if (blobShare.count() < nCount)
469 {
470 if (bThrowExceptions)
471 throw new Exception("The blob to be shared is smaller that the expected minimum count!");
472
473 return false;
474 }
475 }
476
477 b.Share(blobShare);
478 return true;
479 }
480 }
481
482 return false;
483 }
484
489 public bool SnapshotRequested(bool bReset)
490 {
491 bool bRequested = false;
492
493 foreach (Blob<T> blob in m_rgBlobs)
494 {
495 if (blob.snapshot_requested)
496 {
497 bRequested = true;
498
499 if (bReset)
500 blob.snapshot_requested = false;
501 }
502 }
503
504 return bRequested;
505 }
506
513 public Dictionary<string, Tuple<double, double, double, double>> CollectMinMax(Blob<T> blobWork, bool bDiff = false)
514 {
515 Dictionary<string, Tuple<double, double, double, double>> rgMinMax = new Dictionary<string, Tuple<double, double, double, double>>();
516 Dictionary<string, int> rgDuplicates = new Dictionary<string, int>();
517
518 foreach (Blob<T> b in m_rgBlobs)
519 {
520 Tuple<double, double, double, double> minmax = bDiff ? b.minmax_diff(blobWork, true) : b.minmax_data(blobWork, true);
521 string strName = b.Name;
522
523 if (rgMinMax.ContainsKey(strName))
524 {
525 if (!rgDuplicates.ContainsKey(strName))
526 rgDuplicates.Add(strName, 1);
527 else
528 rgDuplicates[strName]++;
529
530 strName = b.Name + "_DUPLICATE_NAME_" + rgDuplicates[strName].ToString();
531 }
532
533 rgMinMax.Add(strName, minmax);
534 }
535
536 return rgMinMax;
537 }
538
542 public void Dispose()
543 {
544 foreach (Blob<T> b in m_rgBlobs)
545 {
546 b.Dispose();
547 }
548
549 m_rgBlobs.Clear();
550 }
551 }
552}
The Log class provides general output in text form.
Definition: Log.cs:13
The BlobCollection contains a list of Blobs.
BlobCollection()
The BlobCollection constructor.
void Dispose()
Release all resource used by the collection and its Blobs.
BlobCollection< T > MathSub(BlobCollection< T > col, bool bSkipFirstItem)
Create a new collection of cloned Blobs created by calling MathSub to subtract the Blobs in this coll...
void Add(Blob< T > b)
Add a new Blob to the collection.
void Accumulate(CudaDnn< T > cuda, BlobCollection< T > src, bool bAccumulateDiff)
Accumulate the diffs from one BlobCollection into another.
void SetData(double df)
Set all blob data to the value specified.
void SetDiff(double df)
Set all blob diff to the value specified.
bool Contains(Blob< T > blob)
Returns whether or not the collection contains a given blob.
BlobCollection< T > FindRelatedBlobs(Blob< T > b)
Find all Blobs in the collection that contain (in part or in whole) the name of a given Blob.
Dictionary< string, Tuple< double, double, double, double > > CollectMinMax(Blob< T > blobWork, bool bDiff=false)
Collect all min/max, inf and nan values from all Blobs in the collection.
BlobCollection< T > Clone()
Copy the collection and return it as a new collection.
bool SnapshotRequested(bool bReset)
Returns whether or not any blobs have a snapshot request set to true.
int Count
Returns the number of items in the collection.
void Clear(bool bDispose=false)
Remove all items from the collection.
bool Remove(Blob< T > b)
If it exists, remove a Blob from the collection.
void Insert(int nIdx, Blob< T > b)
Insert a blob at the given index.
Blob< T > FindBlob(string strName)
Finds a given blob in the collection based on its name.
bool Share(Blob< T > b, List< int > rgMinShape, bool bThrowExceptions, bool bAllowEndsWith=false)
Share the first Blob found with the same name as the given Blob.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
void Save(BinaryWriter bw, bool bData, bool bDiff)
Save the collection to a binary writer.
void ReshapeLike(Blob< T > blob)
Reshapes all blobs in the collection to the sizes of the source.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
BlobCollection< T > MathAdd(BlobCollection< T > colA, double dfScale, bool bSkipFirstItem)
Create a new collection of cloned Blobs created by calling MathAdd to add the Blobs in this collectio...
BlobCollection< T > MathDiv(double dfVal, bool bSkipFirstItem)
Create a new collection of cloned Blobs created by calling MathDif to divide the Blobs in this collec...
IEnumerator< Blob< T > > GetEnumerator()
Get the enumerator for the collection.
void RemoveAt(int nIdx)
Remove a Blob at a given index in the collection.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
void Reshape(int nN, int nC, int nH, int nW)
Reshapes all blobs in the collection to the given shape.
static BlobCollection< T > Load(CudaDnn< T > cuda, Log log, BinaryReader br, bool bData, bool bDiff)
Loads a new collection from a binary reader.
void Add(BlobCollection< T > rg)
Add another BlobCollection to this one.
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
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1555
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1487
void Share(Blob< T > b)
Share another Blob with this one, by setting the data and diff to the same data and diff of the other...
Definition: Blob.cs:1850
Blob< T > Clone()
Copies the Blob, including its data and diff.
Definition: Blob.cs:2202
bool DiffExists
Returns whether or not the Diff portion exists.
Definition: Blob.cs:676
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
void Save(BinaryWriter bw, bool bData, bool bDiff, bool bIncludeName=true)
Saves this Blob to a binary stream.
Definition: Blob.cs:2298
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1541
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
bool snapshot_requested
Get/set the snapshot request.
Definition: Blob.cs:1883
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
void add(int n, long hA, long hB, long hC, long hY)
Adds A, B and C and places the result in Y.
Definition: CudaDnn.cs:7209
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12