MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DebugLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
9namespace MyCaffe.layers
10{
20 public class DebugLayer<T> : Layer<T>, IXDebugData<T>
21 {
22 BlobCollection<T> m_rgBatchData = new BlobCollection<T>();
23 BlobCollection<T> m_rgBatchLabels = new BlobCollection<T>();
24 int m_nCurrentBatchIdx = 0;
25 int m_nLastBatchIdx = 0;
26 int m_nMaxBatches;
27 bool m_bBufferFull = false;
28
38 : base(cuda, log, p)
39 {
41 m_nMaxBatches = p.debug_param.max_stored_batches;
42 }
43
45 protected override void dispose()
46 {
47 m_rgBatchData.Dispose();
48 m_rgBatchLabels.Dispose();
49 base.dispose();
50 }
51
56 {
57 get { return m_rgBatchData; }
58 }
59
64 {
65 get { return m_rgBatchLabels; }
66 }
67
71 public string name
72 {
73 get { return m_param.name; }
74 }
75
79 public long kernel_handle
80 {
81 get { return m_cuda.KernelHandle; }
82 }
83
87 public int load_count
88 {
89 get { return (m_bBufferFull) ? m_rgBatchData.Count : m_nCurrentBatchIdx; }
90 }
91
95 public override int MinBottomBlobs
96 {
97 get { return 2; } // data (embeddings), label
98 }
99
103 public override int MinTopBlobs
104 {
105 get { return 1; } // debug layer passes through the data, but not the label.
106 }
107
113 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
114 {
115 m_log.CHECK_GE(colBottom.Count, 2, "There should be at least two bottom items: data (embeddings) and labels.");
116 m_log.CHECK_EQ(colTop.Count, colBottom.Count - 1, "The top count should equal the bottom count - 1");
117
118 // Allocate the temp batch storage.
119 for (int i = 0; i < m_nMaxBatches; i++)
120 {
122 data.ReshapeLike(colBottom[0]);
123 m_rgBatchData.Add(data);
124
125 Blob<T> label = new common.Blob<T>(m_cuda, m_log, false);
126 label.ReshapeLike(colBottom[1]);
127 m_rgBatchLabels.Add(label);
128 }
129 }
130
136 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
137 {
138 if (!reshapeNeeded(colBottom, colTop))
139 return;
140
141 // Reshape the temp batch storage.
142 for (int i = 0; i < m_nMaxBatches; i++)
143 {
144 m_rgBatchData[i].ReshapeLike(colBottom[0]);
145 m_rgBatchLabels[i].ReshapeLike(colBottom[1]);
146 }
147
148 colTop[0].ReshapeLike(colBottom[0]);
149 }
150
154 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
155 {
156 if (m_nCurrentBatchIdx == m_nMaxBatches)
157 {
158 m_nCurrentBatchIdx = 0;
159 m_bBufferFull = true;
160 }
161
162 // Copy the data into the batch storage.
163 m_cuda.copy(colBottom[0].count(), colBottom[0].gpu_data, m_rgBatchData[m_nCurrentBatchIdx].mutable_gpu_data);
164 m_cuda.copy(colBottom[1].count(), colBottom[1].gpu_data, m_rgBatchLabels[m_nCurrentBatchIdx].mutable_gpu_data);
165
166 m_nLastBatchIdx = m_nCurrentBatchIdx;
167 m_nCurrentBatchIdx++;
168
169 m_cuda.copy(colBottom[0].count(), colBottom[0].gpu_data, colTop[0].mutable_gpu_data);
170 }
171
178 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
179 {
180 // Copy the diff into the batch storage.
181 m_cuda.copy(colTop[0].count(), colTop[0].gpu_diff, m_rgBatchData[m_nLastBatchIdx].mutable_gpu_diff);
182
183 m_cuda.copy(colTop[0].count(), colTop[0].gpu_diff, colBottom[0].mutable_gpu_diff);
184 }
185 }
186}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
The BlobCollection contains a list of Blobs.
void Dispose()
Release all resource used by the collection and its Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
Definition: Blob.cs:64
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
Definition: Blob.cs:648
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The DebugLayer merely stores, up to max_stored_batches, batches of input which are then optionally us...
Definition: DebugLayer.cs:21
BlobCollection< T > data
Returns a collection of Blobs containing the data stored by the DebugLayer.
Definition: DebugLayer.cs:56
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: DebugLayer.cs:113
BlobCollection< T > labels
Returns a collection of Blobs containing the labels stored by the DebugLayer.
Definition: DebugLayer.cs:64
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: DebugLayer.cs:45
override int MinTopBlobs
Returns the minimum number of top (output) Blobs: data (passthrough)
Definition: DebugLayer.cs:104
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward cache and pass through
Definition: DebugLayer.cs:154
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the top (output) to match the bottom (input), and reshape internal buffers.
Definition: DebugLayer.cs:136
string name
Returns the name of the DebugLayer.
Definition: DebugLayer.cs:72
DebugLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The DebugLayer constructor.
Definition: DebugLayer.cs:37
override int MinBottomBlobs
Returns the minimum number of required bottom (input) Blobs: data, label
Definition: DebugLayer.cs:96
int? load_count
Returns the number of batches actually loaded into the DebugLayer.
Definition: DebugLayer.cs:88
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Backward passthrough
Definition: DebugLayer.cs:178
long kernel_handle
Returns the handle to the CudaDnn kernel where the debug GPU memory resides.
Definition: DebugLayer.cs:80
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
virtual bool reshapeNeeded(BlobCollection< T > colBottom, BlobCollection< T > colTop, bool bReset=true)
Tests the shapes of both the bottom and top blobs and if they are the same as the previous sizing,...
Definition: Layer.cs:622
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
int max_stored_batches
Specifies the maximum number of batches to store and search for neighbors. Each batch input is stored...
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
DebugParameter debug_param
Returns the parameter set when initialized with LayerType.DEBUG
LayerType
Specifies the layer type.
The IXDebugData interface is implemented by the DebugLayer to give access to the debug information ma...
Definition: Interfaces.cs:159
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.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
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