2using System.Collections.Generic;
23 DataItem m_currentData =
null;
26 float[] m_rgEncInput1;
30 float[] m_rgDecTarget;
62 throw new Exception(
"The ModelDataLayer requires the ImageDatabase V2 or higher.");
66 throw new Exception(
"The ModelDataLayer requires the ImageDatabase V2 or higher.");
68 m_evtCancel = evtCancel;
119 get {
return (m_currentData ==
null) ?
new IterationInfo(
true,
true, 0) : m_currentData.IterationInfo; }
127 get {
return (m_data ==
null) ? 0 : m_data.DecoderVocabCount; }
135 get {
return false; }
143 get {
return false; }
152 List<SimpleResult> rgFullList =
new List<SimpleResult>();
158 foreach (
string strSrc
in p.
source)
161 List<SimpleResult> rgRes = m_db.GetAllResults(strSrc,
true, nMax);
162 rgFullList.AddRange(rgRes);
165 rgFullList = rgFullList.OrderBy(p1 => p1.TimeStamp).ThenBy(p1 => p1.Index).ToList();
166 m_data =
new Data(rgFullList);
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)");
194 reshape(colTop,
true);
226 reshape(colTop,
false);
234 List<int> rgTopShape =
new List<int>() { nT, nBatchSize, nI };
239 colTop[nTopIdx].
Reshape(
new List<int>() { 1, nBatchSize, 1 });
244 colTop[nTopIdx].
Reshape(
new List<int>() { 1, nBatchSize });
249 colTop[nTopIdx].
Reshape(rgTopShape);
254 colTop[nTopIdx].
Reshape(
new List<int>() { nT, nBatchSize });
258 colTop[nTopIdx].
Reshape(
new List<int>() { 1 });
260 colTop[nTopIdx].
SetData(m_data.DecoderVocabCount + 2, 0);
265 colTop[nTopIdx].
Reshape(
new List<int>() { 1, nBatchSize, 1 });
281 Array.Clear(m_rgDecInput, 0, m_rgDecInput.Length);
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);
292 for (
int i = 0; i < nBatch; i++)
304 Array.Copy(m_currentData.EncoderInput, 0, m_rgEncInput1, i * m_currentData.EncoderInput.Length, m_currentData.EncoderInput.Length);
307 for (
int j = 0; j < nT && j < nEncInputCount; j++)
309 m_rgEncClip[nIdx + j] = (j == 0) ? 0 : 1;
312 m_rgDecClip[i] = m_currentData.DecoderClip;
313 m_rgDecInput[i] = m_currentData.DecoderInput;
314 m_rgDecTarget[i] = m_currentData.DecoderTarget;
317 colTop[nTopIdx].mutable_cpu_data =
convert(m_rgDecInput);
320 colTop[nTopIdx].mutable_cpu_data =
convert(m_rgDecClip);
323 colTop[nTopIdx].mutable_cpu_data =
convert(m_rgEncInput1);
326 colTop[nTopIdx].mutable_cpu_data =
convert(m_rgEncClip);
331 colTop[nTopIdx].mutable_cpu_data =
convert(m_rgDecTarget);
337 float fDecInput =
convertF(colBottom[nBtmIdx].GetData(0));
344 colTop[nTopIdx].
SetData(fDecInput, 0);
348 colTop[nTopIdx].
SetData((fDecInput == 1) ? 0 : 1, 0);
351 colTop[nTopIdx].
CopyFrom(colBottom[nBtmIdx]);
356 colTop[nTopIdx].
CopyFrom(colBottom[nBtmIdx]);
369#pragma warning disable 1591
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;
380 int m_nIterations = 0;
381 int m_nOutputCount = 0;
383 public Data(List<SimpleResult> rgData)
386 m_nDecoderVocabCount = rgData[0].Target.Length;
389 public int DecoderVocabCount
391 get {
return m_nDecoderVocabCount; }
394 public static DataItem GetInputData(
float[] rgfInput,
int? nDecInput =
null)
398 if (!nDecInput.HasValue)
404 return new DataItem(rgfInput, nDecInput.Value, -1, nClip,
false,
true, 0);
407 public DataItem GetNextData(
bool bShuffle)
411 bool bNewSequence =
false;
412 bool bNewEpoch =
false;
414 if (m_nCurrentSequence == -1)
421 m_nCurrentSequence = m_random.Next(m_rgData.Count);
425 m_nCurrentSequence = m_nSequenceIdx;
427 if (m_nSequenceIdx == m_rgData.Count)
431 m_nOutputCount = m_rgData[m_nCurrentSequence].Target.Length;
434 if (m_nIterations == m_rgData.Count)
443 if (m_nCurrentOutputIdx < m_rgData[m_nCurrentSequence].Target.Length)
444 nIxTarget = m_rgData[m_nCurrentSequence].Target[m_nCurrentOutputIdx];
446 DataItem data =
new DataItem(m_rgData[m_nCurrentSequence].
Result, m_nIxInput, nIxTarget, nDecClip, bNewEpoch, bNewSequence, m_nOutputCount);
447 m_nIxInput = nIxTarget;
449 m_nCurrentOutputIdx++;
451 if (m_nCurrentOutputIdx == m_rgData[m_nCurrentSequence].Target.Length)
453 m_nCurrentSequence = -1;
454 m_nCurrentOutputIdx = 0;
470 public DataItem(
float[] rgInput,
int nIxInput,
int nIxTarget,
int nDecClip,
bool bNewEpoch,
bool bNewSequence,
int nOutputCount)
473 m_nIxInput = nIxInput;
474 m_nIxTarget = nIxTarget;
475 m_nDecClip = nDecClip;
476 m_iter =
new IterationInfo(bNewEpoch, bNewSequence, nOutputCount);
479 public float[] EncoderInput
481 get {
return m_rgInput; }
484 public int DecoderInput
486 get {
return m_nIxInput; }
489 public int DecoderTarget
491 get {
return m_nIxTarget; }
494 public int DecoderClip
496 get {
return m_nDecClip; }
501 get {
return m_iter; }
505#pragma warning restore 1591
524 m_bNewEpoch = bNewEpoch;
525 m_bNewSequence = bNewSequence;
526 m_nOutputCount = nOutputCount;
534 get {
return m_bNewEpoch; }
542 get {
return m_bNewSequence; }
550 get {
return m_nOutputCount; }
576 get {
return m_iter; }
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
The Result class contains a single result.
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.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
float convertF(T df)
Converts a generic to a float value.
Phase m_phase
Specifies the Phase under which the Layer is run.
LayerParameter.LayerType m_type
Specifies the Layer type.
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.
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).
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Phase
Defines the Phase under which to run a Net.
DB_VERSION
Defines the image database version to use.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.db.image namespace contains all image database related classes.
The MyCaffe.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers.beta namespace contains all beta stage layers.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
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-...