MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DropoutLayer.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{
21 public class DropoutLayer<T> : NeuronLayer<T>
22 {
26 Blob<T> m_blobRand;
30 double m_dfThreshold;
34 double m_dfScale;
35 uint m_uiThreshold;
36
37 long m_hCuda = 0;
38 long m_hBottomDesc = 0;
39 long m_hStates = 0;
40 long m_hReserved = 0;
41 long m_hDropoutDesc = 0;
42 string m_strBottomSize = null;
43 ulong m_ulStates = 0;
44 ulong m_ulReserved = 0;
45
58 : base(cuda, log, p)
59 {
61 m_blobRand = new Blob<T>(cuda, log);
62 m_blobRand.Name = m_param.name + " rand";
63 }
64
66 protected override void dispose()
67 {
68 if (m_blobRand != null)
69 {
70 m_blobRand.Dispose();
71 m_blobRand = null;
72 }
73
74 if (m_hDropoutDesc != 0)
75 {
76 m_cuda.FreeDropoutDesc(m_hDropoutDesc);
77 m_hDropoutDesc = 0;
78 }
79
80 if (m_hStates != 0)
81 {
82 m_cuda.FreeMemory(m_hStates);
83 m_hStates = 0;
84 }
85
86 if (m_hReserved != 0)
87 {
88 m_cuda.FreeMemory(m_hReserved);
89 m_hReserved = 0;
90 }
91
92 if (m_hBottomDesc != 0)
93 {
94 m_cuda.FreeTensorDesc(m_hBottomDesc);
95 m_hBottomDesc = 0;
96 }
97
98 if (m_hCuda != 0)
99 {
100 m_cuda.FreeCuDNN(m_hCuda);
101 m_hCuda = 0;
102 }
103
104 base.dispose();
105 }
106
111 protected override void setup_internal_blobs(BlobCollection<T> col)
112 {
113 col.Add(m_blobRand);
114 }
115
121 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
122 {
123 base.LayerSetUp(colBottom, colTop);
124
125 m_dfThreshold = m_param.dropout_param.dropout_ratio;
126 m_log.CHECK(m_dfThreshold > 0.0, "Threshold should be > 0");
127 m_log.CHECK(m_dfThreshold < 1.0, "Threshold should be < 1");
128 m_dfScale = 1.0 / (1.0 - m_dfThreshold);
129 m_uiThreshold = (uint)(uint.MaxValue * m_dfThreshold);
130
131 shareLayerBlob(m_blobRand, colBottom[0].shape());
132
134 return;
135
136 m_hCuda = m_cuda.CreateCuDNN();
137 m_hBottomDesc = m_cuda.CreateTensorDesc();
138 m_hDropoutDesc = m_cuda.CreateDropoutDesc();
139 }
140
146 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
147 {
148 base.Reshape(colBottom, colTop);
149
150 // Setup the cache for random number generation
151 m_blobRand.ReshapeLike(colBottom[0]);
152
154 return;
155
156 string strBottomSize = colBottom[0].ToSizeString();
157 m_log.CHECK(strBottomSize == colTop[0].ToSizeString(), "The bottom[0] and top[0] must have the same size!");
158
159 if (strBottomSize != m_strBottomSize)
160 {
161 ulong ulStates;
162 ulong ulReserved;
163
164 m_cuda.SetTensorDesc(m_hBottomDesc, colBottom[0].num, colBottom[0].channels, colBottom[0].height, colBottom[0].width);
165 m_cuda.GetDropoutInfo(m_hCuda, m_hBottomDesc, out ulStates, out ulReserved);
166
167 if (ulStates > m_ulStates)
168 {
169 if (m_hStates != 0)
170 m_cuda.FreeMemory(m_hStates);
171
172 m_hStates = m_cuda.AllocMemory((long)ulStates);
173 m_ulStates = ulStates;
174 }
175
176 if (ulReserved > m_ulReserved)
177 {
178 if (m_hReserved != 0)
179 m_cuda.FreeMemory(m_hReserved);
180
181 m_hReserved = m_cuda.AllocMemory((long)ulReserved);
182 m_ulReserved = ulReserved;
183 }
184
185 long lSeed = m_param.dropout_param.seed;
186
187 if (lSeed == 0)
188 lSeed = DateTime.Now.Ticks;
189
190 m_cuda.SetDropoutDesc(m_hCuda, m_hDropoutDesc, m_dfThreshold, m_hStates, lSeed);
191 m_strBottomSize = strBottomSize;
192 }
193 }
194
195
209 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
210 {
212 forward_caffe(colBottom, colTop);
213 else
214 forward_cudnn(colBottom, colTop);
215 }
216
223 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
224 {
226 backward_caffe(colTop, rgbPropagateDown, colBottom);
227 else
228 backward_cudnn(colTop, rgbPropagateDown, colBottom);
229 }
230
254 protected void forward_caffe(BlobCollection<T> colBottom, BlobCollection<T> colTop)
255 {
256 long hBottomData = colBottom[0].gpu_data;
257 long hTopData = colTop[0].mutable_gpu_data;
258 int nCount = colBottom[0].count();
259
260 if (m_phase == Phase.TRAIN && m_param.dropout_param.active)
261 {
262 long hMask = m_blobRand.mutable_gpu_data;
263
264 m_cuda.rng_uniform(nCount, m_tZero, convert(uint.MaxValue), hMask);
265 // set thresholds
266 m_cuda.dropout_fwd(nCount, hBottomData, hMask, m_uiThreshold, convert(m_dfScale), hTopData);
267 }
268 else
269 {
270 m_cuda.copy(nCount, hBottomData, hTopData);
271 }
272 }
273
280 protected void backward_caffe(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
281 {
282 if (!rgbPropagateDown[0])
283 return;
284
285 long hTopDiff = colTop[0].gpu_diff;
286 long hBottomDiff = colBottom[0].mutable_gpu_diff;
287
288 if (m_phase == Phase.TRAIN && m_param.dropout_param.active)
289 {
290 long hMask = m_blobRand.gpu_data;
291 int nCount = colBottom[0].count();
292
293 m_cuda.dropout_bwd(nCount, hTopDiff, hMask, m_uiThreshold, convert(m_dfScale), hBottomDiff);
294 }
295 else
296 {
297 m_cuda.copy(colTop[0].count(), hTopDiff, hBottomDiff);
298 }
299 }
300
306 protected void forward_cudnn(BlobCollection<T> colBottom, BlobCollection<T> colTop)
307 {
308 long hBottomData = colBottom[0].gpu_data;
309 long hTopData = colTop[0].mutable_gpu_data;
310
311 if (m_phase == Phase.TRAIN)
312 m_cuda.DropoutForward(m_hCuda, m_hDropoutDesc, m_hBottomDesc, hBottomData, m_hBottomDesc, hTopData, m_hReserved);
313 else
314 m_cuda.copy(colBottom[0].count(), hBottomData, hTopData);
315 }
316
323 protected void backward_cudnn(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
324 {
325 if (!rgbPropagateDown[0])
326 return;
327
328 long hTopDiff = colTop[0].gpu_diff;
329 long hBottomDiff = colBottom[0].mutable_gpu_diff;
330
331 if (m_phase == Phase.TRAIN)
332 m_cuda.DropoutBackward(m_hCuda, m_hDropoutDesc, m_hBottomDesc, hTopDiff, m_hBottomDesc, hBottomDiff, m_hReserved);
333 else
334 m_cuda.copy(colTop[0].count(), hTopDiff, hBottomDiff);
335 }
336 }
337}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK(bool b, string str)
Test a flag for true.
Definition: Log.cs:227
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
During training only, sets a random portion of to 0, adjusting the rest of the vector magnitude acco...
Definition: DropoutLayer.cs:22
void backward_cudnn(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Run the Backward computation using the Engine CUDNN mode as specified in the LayerParameter.
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: DropoutLayer.cs:66
override void setup_internal_blobs(BlobCollection< T > col)
Add all internal blobs.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation using either the Engine.CAFFE or Engine.CUDNN mode as specified in the La...
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer for use with both Engine.CAFFE and Engine.CUDNN modes.
DropoutLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The DeconvolutionLayer constructor.
Definition: DropoutLayer.cs:57
void forward_cudnn(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation using the Engine CUDNN mode as specified in the LayerParameter.
void forward_caffe(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation using the Engine CAFFE mode as specified in the LayerParameter.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Run the Backward computation using either the Engine.CAFFE or Engine.CUDNN mode as specified in the L...
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
void backward_caffe(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Run the Backward computation using the Engine CAFFE mode as specified in the LayerParameter.
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
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
bool shareLayerBlob(Blob< T > b, List< int > rgMinShape)
Attempts to share a Layer Blob if another parameter Blob with the same name and acceptable size is fo...
Definition: Layer.cs:1170
T m_tZero
Specifies a generic type equal to 0.0.
Definition: Layer.cs:76
Phase m_phase
Specifies the Phase under which the Layer is run.
Definition: Layer.cs:51
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
The NeuronLayer is an interface for layers that take one blob as input (x) and produce only equally-s...
Definition: NeuronLayer.cs:22
double dropout_ratio
Specifies the dropout ratio. (e.g. the probability that values will be dropped out and set to zero....
long seed
Specifies the seed used by cuDnn for random number generation.
bool active
Specifies whether or not the dropout is active or not. When inactive and training,...
bool useCudnn()
Queries whether or not to use NVIDIA's cuDnn.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
LayerType
Specifies the layer type.
DropoutParameter dropout_param
Returns the parameter set when initialized with LayerType.DROPOUT
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
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