MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ModelDataLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.fillers;
9using System.IO;
11using MyCaffe.db.image;
12
13namespace MyCaffe.layers.beta
14{
19 public class ModelDataLayer<T> : Layer<T>
20 {
22 CancelEvent m_evtCancel;
23 DataItem m_currentData = null;
24 Data m_data = null;
25 ulong m_lOffset = 0;
26 float[] m_rgEncInput1;
27 float[] m_rgEncClip;
28 float[] m_rgDecInput;
29 float[] m_rgDecClip;
30 float[] m_rgDecTarget;
31
35 public event EventHandler<OnGetDataArgs> OnGetData;
36
59 : base(cuda, log, p)
60 {
61 if (db.GetVersion() != DB_VERSION.IMG_V2)
62 throw new Exception("The ModelDataLayer requires the ImageDatabase V2 or higher.");
63
64 m_db = db as IXImageDatabase2;
65 if (m_db == null)
66 throw new Exception("The ModelDataLayer requires the ImageDatabase V2 or higher.");
67
68 m_evtCancel = evtCancel;
69 m_type = LayerParameter.LayerType.MODEL_DATA;
70 }
71
75 protected override void dispose()
76 {
77 base.dispose();
78 }
79
84 public override int MinBottomBlobs
85 {
86 get { return (m_phase == Phase.RUN) ? 3 : 0; }
87 }
88
93 public override int MaxBottomBlobs
94 {
95 get { return (m_phase == Phase.RUN) ? 3 : 0; }
96 }
97
101 public override int MinTopBlobs
102 {
103 get { return 6; }
104 }
105
109 public override int MaxTopBlobs
110 {
111 get { return 6; }
112 }
113
118 {
119 get { return (m_currentData == null) ? new IterationInfo(true, true, 0) : m_currentData.IterationInfo; }
120 }
121
126 {
127 get { return (m_data == null) ? 0 : m_data.DecoderVocabCount; }
128 }
129
133 public override bool SupportsPreProcessing
134 {
135 get { return false; }
136 }
137
141 public override bool SupportsPostProcessing
142 {
143 get { return false; }
144 }
145
150 private void PreProcessData(ModelDataParameter p)
151 {
152 List<SimpleResult> rgFullList = new List<SimpleResult>();
153 int nMax = -1;
154
155 if (m_phase == Phase.RUN)
156 nMax = 1;
157
158 foreach (string strSrc in p.source)
159 {
160 m_log.WriteLine("Loading source '" + strSrc + "'...");
161 List<SimpleResult> rgRes = m_db.GetAllResults(strSrc, true, nMax);
162 rgFullList.AddRange(rgRes);
163 }
164
165 rgFullList = rgFullList.OrderBy(p1 => p1.TimeStamp).ThenBy(p1 => p1.Index).ToList();
166 m_data = new Data(rgFullList);
167 }
168
174 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
175 {
176 // Refuse transformation parameters since ModelData is totally generic.
177 if (m_param.transform_param != null)
178 m_log.WriteLine("WARNING: " + m_type.ToString() + " does not transform data.");
179
180 m_log.CHECK_EQ(m_param.model_data_param.batch_size, 1, "Currently, only batch_size = 1 supported.");
181 m_log.CHECK_EQ(colTop.Count, 6, "When normal or reverse encoder output used, there must be 6 tops: dec, dclip, enc | encr, eclip, vocabcount, dectgt (only valid on TEST | TRAIN)");
182
183 // Load the encoder and decoder input data into the Data.
184 PreProcessData(m_param.model_data_param);
185
186 m_rgDecInput = new float[m_param.model_data_param.batch_size];
187 m_rgDecClip = new float[m_param.model_data_param.batch_size];
190
191 if (m_phase != Phase.RUN)
192 m_rgDecTarget = new float[m_param.model_data_param.batch_size];
193
194 reshape(colTop, true);
195 }
196
201 protected bool Skip()
202 {
203 ulong nSize = (ulong)m_param.solver_count;
204 ulong nRank = (ulong)m_param.solver_rank;
205 // In test mode, only rank 0 runs, so avoid skipping.
206 bool bKeep = (m_lOffset % nSize) == nRank || m_param.phase == Phase.TEST;
207
208 return !bKeep;
209 }
210
214 protected void Next()
215 {
216 m_currentData = m_data.GetNextData(m_param.model_data_param.shuffle);
217 }
218
224 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
225 {
226 reshape(colTop, false);
227 }
228
229 private void reshape(BlobCollection<T> colTop, bool bSetup)
230 {
231 int nBatchSize = (int)m_param.model_data_param.batch_size;
232 int nT = (int)m_param.model_data_param.time_steps;
233 int nI = (int)m_param.model_data_param.input_dim;
234 List<int> rgTopShape = new List<int>() { nT, nBatchSize, nI };
235 int nTopIdx = 0;
236
237 // Reshape the decoder input.
238 if (!bSetup)
239 colTop[nTopIdx].Reshape(new List<int>() { 1, nBatchSize, 1 });
240 nTopIdx++;
241
242 // Reshape the decoder clip.
243 if (!bSetup)
244 colTop[nTopIdx].Reshape(new List<int>() { 1, nBatchSize });
245 nTopIdx++;
246
247 // Reshape the encoder data
248 if (!bSetup)
249 colTop[nTopIdx].Reshape(rgTopShape);
250 nTopIdx++;
251
252 // Reshape the encoder clip for attention.
253 if (!bSetup)
254 colTop[nTopIdx].Reshape(new List<int>() { nT, nBatchSize });
255 nTopIdx++;
256
257 // Reshape the vocab count.
258 colTop[nTopIdx].Reshape(new List<int>() { 1 });
259 if (bSetup)
260 colTop[nTopIdx].SetData(m_data.DecoderVocabCount + 2, 0);
261 nTopIdx++;
262
263 // Reshape the decoder target.
264 if (!bSetup)
265 colTop[nTopIdx].Reshape(new List<int>() { 1, nBatchSize, 1 });
266 }
267
276 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
277 {
278 int nBatch = (int)m_param.model_data_param.batch_size;
279 int nT = (int)m_param.model_data_param.time_steps;
280
281 Array.Clear(m_rgDecInput, 0, m_rgDecInput.Length);
282 if (m_phase != Phase.RUN)
283 Array.Clear(m_rgDecTarget, 0, m_rgDecTarget.Length);
284 Array.Clear(m_rgDecClip, 0, m_rgDecClip.Length);
285 Array.Clear(m_rgEncInput1, 0, m_rgEncInput1.Length);
286 Array.Clear(m_rgEncClip, 0, m_rgEncClip.Length);
287
288 int nTopIdx = 0;
289
290 if (m_phase != Phase.RUN)
291 {
292 for (int i = 0; i < nBatch; i++)
293 {
294 while (Skip())
295 Next();
296
297 Next();
298
299 if (OnGetData != null)
301
302 int nIdx = i * nT;
303
304 Array.Copy(m_currentData.EncoderInput, 0, m_rgEncInput1, i * m_currentData.EncoderInput.Length, m_currentData.EncoderInput.Length);
305
306 int nEncInputCount = m_currentData.EncoderInput.Length / (int)m_param.model_data_param.input_dim;
307 for (int j = 0; j < nT && j < nEncInputCount; j++)
308 {
309 m_rgEncClip[nIdx + j] = (j == 0) ? 0 : 1;
310 }
311
312 m_rgDecClip[i] = m_currentData.DecoderClip;
313 m_rgDecInput[i] = m_currentData.DecoderInput;
314 m_rgDecTarget[i] = m_currentData.DecoderTarget;
315 }
316
317 colTop[nTopIdx].mutable_cpu_data = convert(m_rgDecInput);
318 nTopIdx++;
319
320 colTop[nTopIdx].mutable_cpu_data = convert(m_rgDecClip);
321 nTopIdx++;
322
323 colTop[nTopIdx].mutable_cpu_data = convert(m_rgEncInput1);
324 nTopIdx++;
325
326 colTop[nTopIdx].mutable_cpu_data = convert(m_rgEncClip);
327 nTopIdx++;
328
329 nTopIdx++; // vocab count.
330
331 colTop[nTopIdx].mutable_cpu_data = convert(m_rgDecTarget);
332 nTopIdx++;
333 }
334 else
335 {
336 int nBtmIdx = 0;
337 float fDecInput = convertF(colBottom[nBtmIdx].GetData(0));
338 if (fDecInput < 0)
339 fDecInput = 1;
340
341 nBtmIdx++;
342
343 // Decoder input.
344 colTop[nTopIdx].SetData(fDecInput, 0);
345 nTopIdx++;
346
347 // Decoder clip.
348 colTop[nTopIdx].SetData((fDecInput == 1) ? 0 : 1, 0);
349 nTopIdx++;
350
351 colTop[nTopIdx].CopyFrom(colBottom[nBtmIdx]);
352 nTopIdx++;
353 nBtmIdx++;
354
355 // Encoder clip.
356 colTop[nTopIdx].CopyFrom(colBottom[nBtmIdx]);
357 }
358 }
359
361 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
362 {
363 }
364 }
365
366
367 namespace ModelData
368 {
369#pragma warning disable 1591
370
371 class Data
372 {
373 Random m_random = new Random((int)DateTime.Now.Ticks);
374 List<SimpleResult> m_rgData;
375 int m_nDecoderVocabCount = 1;
376 int m_nCurrentSequence = -1;
377 int m_nCurrentOutputIdx = 0;
378 int m_nSequenceIdx = 0;
379 int m_nIxInput = 1;
380 int m_nIterations = 0;
381 int m_nOutputCount = 0;
382
383 public Data(List<SimpleResult> rgData)
384 {
385 m_rgData = rgData;
386 m_nDecoderVocabCount = rgData[0].Target.Length;
387 }
388
389 public int DecoderVocabCount
390 {
391 get { return m_nDecoderVocabCount; }
392 }
393
394 public static DataItem GetInputData(float[] rgfInput, int? nDecInput = null)
395 {
396 int nClip = 1;
397
398 if (!nDecInput.HasValue)
399 {
400 nClip = 0;
401 nDecInput = 1;
402 }
403
404 return new DataItem(rgfInput, nDecInput.Value, -1, nClip, false, true, 0);
405 }
406
407 public DataItem GetNextData(bool bShuffle)
408 {
409 int nDecClip = 1;
410
411 bool bNewSequence = false;
412 bool bNewEpoch = false;
413
414 if (m_nCurrentSequence == -1)
415 {
416 m_nIterations++;
417 bNewSequence = true;
418
419 if (bShuffle)
420 {
421 m_nCurrentSequence = m_random.Next(m_rgData.Count);
422 }
423 else
424 {
425 m_nCurrentSequence = m_nSequenceIdx;
426 m_nSequenceIdx++;
427 if (m_nSequenceIdx == m_rgData.Count)
428 m_nSequenceIdx = 0;
429 }
430
431 m_nOutputCount = m_rgData[m_nCurrentSequence].Target.Length;
432 nDecClip = 0;
433
434 if (m_nIterations == m_rgData.Count)
435 {
436 bNewEpoch = true;
437 m_nIterations = 0;
438 }
439 }
440
441 int nIxTarget = 0;
442
443 if (m_nCurrentOutputIdx < m_rgData[m_nCurrentSequence].Target.Length)
444 nIxTarget = m_rgData[m_nCurrentSequence].Target[m_nCurrentOutputIdx];
445
446 DataItem data = new DataItem(m_rgData[m_nCurrentSequence].Result, m_nIxInput, nIxTarget, nDecClip, bNewEpoch, bNewSequence, m_nOutputCount);
447 m_nIxInput = nIxTarget;
448
449 m_nCurrentOutputIdx++;
450
451 if (m_nCurrentOutputIdx == m_rgData[m_nCurrentSequence].Target.Length)
452 {
453 m_nCurrentSequence = -1;
454 m_nCurrentOutputIdx = 0;
455 m_nIxInput = 1;
456 }
457
458 return data;
459 }
460 }
461
462 class DataItem
463 {
464 IterationInfo m_iter;
465 float[] m_rgInput;
466 int m_nIxInput;
467 int m_nIxTarget;
468 int m_nDecClip;
469
470 public DataItem(float[] rgInput, int nIxInput, int nIxTarget, int nDecClip, bool bNewEpoch, bool bNewSequence, int nOutputCount)
471 {
472 m_rgInput = rgInput;
473 m_nIxInput = nIxInput;
474 m_nIxTarget = nIxTarget;
475 m_nDecClip = nDecClip;
476 m_iter = new IterationInfo(bNewEpoch, bNewSequence, nOutputCount);
477 }
478
479 public float[] EncoderInput
480 {
481 get { return m_rgInput; }
482 }
483
484 public int DecoderInput
485 {
486 get { return m_nIxInput; }
487 }
488
489 public int DecoderTarget
490 {
491 get { return m_nIxTarget; }
492 }
493
494 public int DecoderClip
495 {
496 get { return m_nDecClip; }
497 }
498
500 {
501 get { return m_iter; }
502 }
503 }
504
505#pragma warning restore 1591
506
510 public class IterationInfo
511 {
512 bool m_bNewEpoch;
513 bool m_bNewSequence;
514 int m_nOutputCount;
515
522 public IterationInfo(bool bNewEpoch, bool bNewSequence, int nOutputCount)
523 {
524 m_bNewEpoch = bNewEpoch;
525 m_bNewSequence = bNewSequence;
526 m_nOutputCount = nOutputCount;
527 }
528
532 public bool NewEpoch
533 {
534 get { return m_bNewEpoch; }
535 }
536
540 public bool NewSequence
541 {
542 get { return m_bNewSequence; }
543 }
544
548 public int OutputCount
549 {
550 get { return m_nOutputCount; }
551 }
552 }
553
554
558 public class OnGetDataArgs : EventArgs
559 {
560 IterationInfo m_iter;
561
567 {
568 m_iter = iter;
569 }
570
575 {
576 get { return m_iter; }
577 }
578 }
579 }
580}
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
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The Result class contains a single result.
Definition: Result.cs:14
The BlobCollection contains a list of Blobs.
void SetData(double df)
Set all blob data to the value specified.
int Count
Returns the number of items in the collection.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
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
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
Phase m_phase
Specifies the Phase under which the Layer is run.
Definition: Layer.cs:51
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
The IterationInfo class contains information about each iteration.
bool NewEpoch
Returns whether or not the current iteration is in a new epoch.
IterationInfo(bool bNewEpoch, bool bNewSequence, int nOutputCount)
The constructor.
int OutputCount
Returns the output count of the current sequence.
bool NewSequence
Returns whether or not the current iteration is in a new sequence.
Defines the arguments passed to the OnGetData event.
OnGetDataArgs(IterationInfo iter)
The constructor.
The ModelDataLayer loads data from RawImageResults table for an encoder/decoder type model.
IterationInfo? IterationInfo
Returns information on the current iteration.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override int? MinBottomBlobs
When running in TRAIN or TEST phase, returns 0 for data layers have no bottom (input) Blobs....
override bool SupportsPostProcessing
Should return true when pre postprocessing methods are overriden.
override void dispose()
Release all internal blobs.
EventHandler< OnGetDataArgs > OnGetData
The OnGetTrainingData is called during each forward pass after getting the training data for the pass...
override bool SupportsPreProcessing
Should return true when pre processing methods are overriden.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: dec, dclip, enc, eclip,...
override int MaxTopBlobs
Returns the maximum number of required top (output) Blobs: dec, dclip, enc, eclip,...
ModelDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
The ModelDataLayer constructor.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - data Layers do not perform backward..
int? DecoderVocabularyCount
Returns the decoder vocabulary count.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Data layers have no bottoms, so reshaping is trivial.
void Next()
Proceeds to the next data item. When shuffling, the next item is randomly selected.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation, which fills the data into the top (output) Blobs.
override int? MaxBottomBlobs
When running in TRAIN or TEST phase, returns 0 for data layers have no bottom (input) Blobs....
bool Skip()
Skip to the next data input.
Specifies the base parameter for all layers.
int solver_count
Returns the number of Solvers participating in a multi-GPU session for which the Solver using this La...
int solver_rank
Returns the SolverRank of the Solver using this LayerParameter (if any).
TransformationParameter transform_param
Returns the parameter set when initialized with LayerType.TRANSFORM
Phase phase
Specifies the Phase for which this LayerParameter is run.
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
ModelDataParameter model_data_param
Returns the parameter set when initialized with LayerType.MODEL_DATA
Specifies the parameter for the model data layer.
bool shuffle
Specifies the whether to shuffle the data or now.
uint time_steps
Specifies the maximum length for each encoder input.
virtual uint batch_size
Specifies the batch size.
List< string > source
Specifies the data 'sources' within the database. Each source must already have pre-calculated RawIma...
uint input_dim
Specifies the input dimension for each encoder input.
The IXDatabaseBase interface defines the general interface to the in-memory database.
Definition: Interfaces.cs:444
DB_VERSION GetVersion()
Returns the version of the MyCaffe Image Database being used.
The IXImageDatabase2 interface defines the general interface to the in-memory image database (v2).
Definition: Interfaces.cs:1092
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
DB_VERSION
Defines the image database version to use.
Definition: Interfaces.cs:397
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.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers.beta namespace contains all beta stage layers.
Definition: LayerFactory.cs:9
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