MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BasePrefetchingDataLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.db.image;
7using MyCaffe.common;
8using MyCaffe.param;
9using System.Threading;
10using System.Diagnostics;
11
12namespace MyCaffe.layers
13{
18 public abstract class BasePrefetchingDataLayer<T> : BaseDataLayer<T>
19 {
28 BlockingQueue<Batch<T>> m_rgPrefetchFree;
29 BlockingQueue<Batch<T>> m_rgPrefetchFull;
30 Batch<T> m_prefetch_current = null;
31 InternalThread<T> m_internalThread;
32 Exception m_err = null;
33
43 : base(cuda, log, p, db)
44 {
45 m_evtCancel = evtCancel;
46
47 m_internalThread = new InternalThread<T>();
48 m_internalThread.DoWork += new EventHandler<ActionStateArgs<T>>(m_internalThread_DoWork);
49 m_internalThread.OnPreStop += internalThread_OnPreStop;
50 m_internalThread.OnPreStart += internalThread_OnPreStart;
51
53 m_rgPrefetchFree = new BlockingQueue<Batch<T>>(m_evtCancel);
54 m_rgPrefetchFull = new BlockingQueue<Batch<T>>(m_evtCancel);
55
56 for (int i = 0; i < m_rgPrefetch.Length; i++)
57 {
58 m_rgPrefetch[i] = new Batch<T>(cuda, log);
59 m_rgPrefetchFree.Push(m_rgPrefetch[i]);
60 }
61 }
62
63 private void internalThread_OnPreStart(object sender, EventArgs e)
64 {
65 m_rgPrefetchFree.Reset();
66 m_rgPrefetchFull.Reset();
67 }
68
69 private void internalThread_OnPreStop(object sender, EventArgs e)
70 {
71 if (m_rgPrefetchFree != null)
72 m_rgPrefetchFree.Abort();
73
74 if (m_rgPrefetchFull != null)
75 m_rgPrefetchFull.Abort();
76
77 preStop();
78 }
79
84 protected virtual void preStop()
85 {
86 }
87
89 protected override void dispose()
90 {
91 m_internalThread.StopInternalThread();
92
93 if (m_rgPrefetchFull != null)
94 {
95 m_rgPrefetchFull.Dispose();
96 m_rgPrefetchFull = null;
97 }
98
99 if (m_rgPrefetchFree != null)
100 {
101 m_rgPrefetchFree.Dispose();
102 m_rgPrefetchFree = null;
103 }
104
105 base.dispose();
106 }
107
117 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
118 {
119 base.LayerSetUp(colBottom, colTop);
120
121 for (int i = 0; i < m_rgPrefetch.Length; i++)
122 {
123 m_rgPrefetch[i].Data.update_cpu_data();
124
125 if (m_bOutputLabels)
126 m_rgPrefetch[i].Label.update_cpu_data();
127 }
128
129 m_transformer.InitRand();
130
131 if (!delayPrefetch)
132 {
133 m_log.WriteLine("Initializing prefetch for '" + m_param.name + "'...");
135 }
136 else
137 {
138 m_log.WriteLine("Delaying prefetch for '" + m_param.name + "'...");
139 }
140 }
141
145 protected virtual bool delayPrefetch
146 {
147 get { return false; }
148 }
149
153 protected void statupPrefetch()
154 {
155 m_err = null;
156 m_internalThread.StartInternalThread(m_cuda, m_log, m_cuda.GetDeviceID());
157 m_log.WriteLine("Prefetch initialized for '" + m_param.name + "'.");
158 }
159
160 void m_internalThread_DoWork(object sender, ActionStateArgs<T> e)
161 {
162 CudaDnn<T> cuda = m_cuda;
163 Log log = m_log;
164 long hStream = 0; // cuda.CreateStream(false);
165
166 try
167 {
168 Batch<T> batch = new Batch<T>(cuda, log);
169
170 while (!m_internalThread.CancellationPending)
171 {
172 if (m_rgPrefetchFree.Pop(ref batch))
173 {
174 load_batch(batch);
175
176 batch.Data.AsyncGpuPush(hStream);
177 if (hStream != 0)
178 m_cuda.SynchronizeStream(hStream);
179
180 if (m_bOutputLabels)
181 {
182 batch.Label.AsyncGpuPush(hStream);
183 if (hStream != 0)
184 m_cuda.SynchronizeStream(hStream);
185 }
186
187 m_transformer.DistortImage(batch.Data);
188
189 m_rgPrefetchFull.Push(batch);
190 }
191 }
192 }
193 catch (Exception excpt)
194 {
195 m_err = excpt;
196 m_rgPrefetchFull.Abort();
197 m_rgPrefetchFree.Abort();
198 throw excpt;
199 }
200 finally
201 {
202 if (hStream != 0)
203 cuda.FreeStream(hStream);
204 }
205 }
206
211 protected virtual void final_process(Blob<T> blobTop)
212 {
213 return;
214 }
215
222 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
223 {
224 if (m_prefetch_current != null)
225 m_rgPrefetchFree.Push(m_prefetch_current);
226
227 if (m_rgPrefetchFull.Pop(ref m_prefetch_current))
228 {
229 // Reshape to loaded data.
230 colTop[0].ReshapeLike(m_prefetch_current.Data);
231
232 // Copy the data.
233 m_cuda.copy(m_prefetch_current.Data.count(), m_prefetch_current.Data.gpu_data, colTop[0].mutable_gpu_data);
234 final_process(colTop[0]);
235
236 //-----------------------------------------
237 // If the blob has a fixed range set, set
238 // the range in the top data.
239 //-----------------------------------------
240 m_transformer.SetRange(colTop[0]);
241
242 if (m_bOutputLabels)
243 {
244 // Reshape to loaded labels.
245 colTop[1].ReshapeLike(m_prefetch_current.Label);
246
247 // Copy the labels.
248 m_cuda.copy(m_prefetch_current.Label.count(), m_prefetch_current.Label.gpu_data, colTop[1].mutable_gpu_data);
249 }
250 }
251 else if (m_err != null)
252 {
253 throw m_err;
254 }
255 }
256
262 protected abstract void load_batch(Batch<T> batch);
263 }
264
269 public class Batch<T> : IDisposable
270 {
271 Blob<T> m_data;
272 Blob<T> m_label;
273
279 public Batch(CudaDnn<T> cuda, Log log)
280 {
281 m_data = new Blob<T>(cuda, log);
282 m_label = new Blob<T>(cuda, log);
283 }
284
288 public void Dispose()
289 {
290 }
291
296 {
297 get { return m_data; }
298 }
299
304 {
305 get { return m_label; }
306 }
307 }
308}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
The ActionStateArgs are sent to the DoWork event when fired from the InternalThreadEntry.
The BlobCollection contains a list of Blobs.
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
The BlockingQueue is used for synchronized Queue operations.
bool Pop(ref T t)
Remove an item from the front of the queue.
virtual void Dispose(bool bDisposing)
Release all resources used by the queue.
void Reset()
Reset the abort event.
void Abort()
Cancel the blocking queue operations.
void Push(T t)
Add an item to the back of the queue.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
void FreeStream(long h)
Free a stream.
Definition: CudaDnn.cs:3227
The InternalThread manages an internal thread used for Parallel and data collection operations.
void StartInternalThread(CudaDnn< T > cuda, Log log, int nDeviceID=0, object arg=null, int nInitialDelay=0)
Starts running the internal thread function which then calls the DoWork event.
EventHandler OnPreStop
The OnPreStop event fires just after signalling the thread to stop.
bool CancellationPending
Returns whether or not a cancellation is pending.
EventHandler< ActionStateArgs< T > > DoWork
The DoWork event is the working thread function.
void StopInternalThread()
Stops the internal thread.
EventHandler OnPreStart
The OnPreStart event fires just before starting the thread.
The BaseDataLayer is the base class for data Layers that feed Blobs of data into the Net.
bool m_bOutputLabels
Specifies whether or not the Layer should output labels.
DataTransformer< T > m_transformer
Specifies the DataTransformer used to transform each data item as it loaded.
The BasePrefetchingDataLayer is the base class for data Layers that pre-fetch data before feeding the...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward override implements the functionality to load pre-fetch data and feed it into the top (ou...
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
LayerSetUp implements common data layer setup functonality, and calls DatLayerSetUp to do special dat...
override void dispose()
Releases all GPU and host resources used by the Layer.
virtual bool delayPrefetch
Specifies whether or not to delay the prefetch.
CancelEvent m_evtCancel
Specifies the cancellation event for the internal thread.
virtual void final_process(Blob< T > blobTop)
Provides a final processing step that may be utilized by derivative classes.
void statupPrefetch()
Starts the prefetch thread.
abstract void load_batch(Batch< T > batch)
The load_batch abstract function should be overriden by each derivative data Layer to load a batch of...
Batch< T >[] m_rgPrefetch
Specifies the pre-fetch cache.
BasePrefetchingDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
The BaseDataLayer constructor.
virtual void preStop()
The preStop method is called just before stopping the internal thread. Overriding this method gives d...
The Batch contains both the data and label Blobs of the batch.
void Dispose()
Release all GPU and host resources used (if any).
Blob< T > Label
Returns the label Blob of the batch.
Batch(CudaDnn< T > cuda, Log log)
The Batch constructor.
Blob< T > Data
Returns the data Blob of the batch.
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
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
uint prefetch
Prefetch queue (Number of batches to prefetch to host memory, increase if data access bandwidth varie...
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
DataParameter data_param
Returns the parameter set when initialized with LayerType.DATA
The IXDatabaseBase interface defines the general interface to the in-memory database.
Definition: Interfaces.cs:444
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.db.image namespace contains all image database related classes.
Definition: Database.cs:18
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