2using System.Collections.Generic;
26 int m_nLabelChannels = 0;
27 int m_nLabelHeight = 0;
28 int m_nLabelWidth = 0;
45 public event EventHandler<MemoryDataLayerGetDataArgs>
OnGetData;
50 public event EventHandler<MemoryDataLayerPackDataArgs<T>>
OnDataPack;
67 : base(cuda, log, p, null)
70 m_blobData =
new Blob<T>(cuda, log);
72 m_blobLabel =
new Blob<T>(cuda, log);
93 m_nDataSize = m_nChannels * m_nHeight * m_nWidth;
95 m_log.
CHECK_GT(m_nBatchSize * m_nDataSize, 0,
"batch_size, channels, height, and width must be specified and positive in memory_data_param.");
102 m_nLabelSize = m_nLabelChannels * m_nLabelHeight * m_nLabelWidth;
105 m_log.
CHECK_GT(m_nBatchSize * m_nLabelSize, 0,
"batch_size, label_channels, label_height, and label_width must be specified and positive in memory_data_param when using label_type = MULTIPLE.");
114 if (m_nClipSize1 > 1 && m_nClipSize2 == 0)
117 if (m_nClipSize2 > 1 && m_nClipSize1 == 0)
120 if (m_nClipSize1 > 0 || m_nClipSize2 > 0)
124 m_blobClip.
Reshape(
new List<int>() { m_nClipSize1, m_nClipSize2 });
133 m_log.
CHECK_EQ(m_nLabelChannels, m_blobLabel.
channels,
"The actual label channels (" + m_blobLabel.
channels.ToString() +
") do not match the 'memory_data_param.label_channels' setting of " + m_nLabelChannels.ToString() +
".");
134 m_log.
CHECK_EQ(m_nLabelHeight, m_blobLabel.
height,
"The actual label channels (" + m_blobLabel.
height.ToString() +
") do not match the 'memory_data_param.label_channels' setting of " + m_nLabelHeight.ToString() +
".");
135 m_log.
CHECK_EQ(m_nLabelWidth, m_blobLabel.
width,
"The actual label channels (" + m_blobLabel.
width.ToString() +
") do not match the 'memory_data_param.label_channels' setting of " + m_nLabelWidth.ToString() +
".");
140 m_blobData.
Reshape(m_nBatchSize, m_nChannels, m_nHeight, m_nWidth);
145 m_blobLabel.
Reshape(1, m_nLabelChannels, m_nLabelHeight, m_nLabelWidth);
147 m_blobLabel.
Reshape(m_nBatchSize, m_nLabelChannels, m_nLabelHeight, m_nLabelWidth);
151 colTop[0].
Reshape(m_nBatchSize, m_nChannels, m_nHeight, m_nWidth);
169 nLabelIdx = (nClipIdx == -1) ? 1 : nClipIdx + 1;
171 m_nLabelIdx = nLabelIdx;
176 if (m_blobClip !=
null)
179 throw new Exception(
"Could not find a top named 'clip'!");
181 m_nClipIdx = nClipIdx;
194 base.Reshape(colBottom, colTop);
196 m_blobData.
Reshape(m_nBatchSize, m_nChannels, m_nHeight, m_nWidth);
198 colTop[0].
Reshape(m_nBatchSize, m_nChannels, m_nHeight, m_nWidth);
204 m_blobLabel.
Reshape(1, m_nLabelChannels, m_nLabelHeight, m_nLabelWidth);
206 m_blobLabel.
Reshape(m_nBatchSize, m_nLabelChannels, m_nLabelHeight, m_nLabelWidth);
212 if (m_blobClip !=
null)
214 m_blobClip.
Reshape(
new List<int>() { m_nClipSize1, m_nClipSize2 });
259 public virtual void AddDatum(
SimpleDatum sd,
int nLblAxis = 1,
bool bReset =
false,
bool bResizeBatch =
false)
261 List<Datum> rgData =
new List<Datum>();
262 rgData.Add(
new Datum(sd));
276 public virtual void AddDatumVector(
Datum[] rgData,
Datum[] rgClip =
null,
int nLblAxis = 1,
bool bReset =
false,
bool bResizeBatch =
false)
278 AddDatumVector(rgData.ToList(), (rgClip ==
null) ?
null : rgClip.ToList(), nLblAxis, bReset, bResizeBatch);
291 public virtual void AddDatumVector(List<Datum> rgData, List<Datum> rgClip =
null,
int nLblAxis = 1,
bool bReset =
false,
bool bResizeBatch =
false)
294 m_bHasNewData =
false;
296 m_log.
CHECK(!m_bHasNewData,
"Can't add data until current data has been consumed.");
297 int nNum = rgData.Count;
303 m_nBatchSize = rgData.Count;
305 int nNumAligned = (int)Math.Floor((
double)rgData.Count / (double)m_nBatchSize) * m_nBatchSize;
306 m_log.
CHECK_GT(nNumAligned, 0,
"Three are not enough datum to add.");
308 if (nNumAligned < nNum)
310 m_log.
WriteLine(
"WARNING: Clipping batch to batch aligned count of " + nNumAligned.ToString() +
".");
312 for (
int i = nNumAligned; i < nNum; i++)
314 rgData.RemoveAt(rgData.Count - 1);
319 m_log.
CHECK_EQ(nNum % m_nBatchSize, 0,
"The added data must be a multiple of the batch size.");
321 m_blobData.
Reshape(nNum, m_nChannels, m_nHeight, m_nWidth);
328 List<int> rgLblShape =
new List<int>();
329 int nLabelNum = nNum;
334 rgLblShape.Add(nLabelNum);
342 m_log.
CHECK_GE(nLblAxis, 1,
"The label axis must be greater than or equal to 1.");
343 m_log.
CHECK_LE(nLblAxis, 4,
"The label axis must be less than 4.");
346 rgLblShape[nLblAxis] = rgLbl.Count;
349 m_blobLabel.
Reshape(rgLblShape);
357 for (
int i = 0; i < nNum; i++)
360 for (
int j = 0; j < rgLbl.Count; j++)
362 rgLabels[nIdx] = (T)Convert.ChangeType(rgLbl[j], typeof(T));
375 rgLabels[0] = (T)Convert.ChangeType(rgData[nNum - 1].label, typeof(T));
380 for (
int i = 0; i < nNum; i++)
382 rgLabels[i] = (T)Convert.ChangeType(rgData[i].label, typeof(T));
393 throw new Exception(
"To properly handle data packing, you must connect the OnDataPack event and properly fill out the data and clip blobs with the ordering expected by the recurrent layers.");
399 m_bHasNewData =
true;
411 m_log.
CHECK_EQ(n % m_nBatchSize, 0,
"'n' must be a multiple of batch size.");
443 if (m_blobClip !=
null)
449 m_bHasNewData =
true;
457 get {
return (
int)m_nBatchSize; }
460 m_log.
CHECK(!m_bHasNewData,
"Can't change the batch size until current data has been consumed.");
461 m_nBatchSize = value;
470 get {
return m_nChannels; }
478 get {
return m_nHeight; }
486 get {
return m_nWidth; }
494 get {
return m_nClipSize1; }
502 get {
return m_nClipSize2; }
521 for (
int i=1; i<colTop.
Count; i++)
523 if (blobClip ==
null && (colTop[i].
type ==
BLOB_TYPE.CLIP || colTop[i].Name.ToLower().
Contains(
"clip")))
524 blobClip = colTop[i];
526 blobLabel = colTop[i];
529 blobData.
Reshape(m_nBatchSize, m_nChannels, m_nHeight, m_nWidth);
531 nSrcOffset = m_nPos * m_nDataSize;
537 if (blobLabel ==
null)
538 m_log.
WriteError(
new Exception(
"Could not find the MemoryDataLayer 'label' top!"));
540 int nLabelSize = m_nBatchSize;
544 List<int> rgLabelShape =
Utility.Clone<
int>(m_blobLabel.
shape());
545 if (rgLabelShape.Count == 0)
546 rgLabelShape.Add(nLabelSize);
548 rgLabelShape[0] = nLabelSize;
550 blobLabel.
Reshape(rgLabelShape);
552 nSrcOffset = m_nPos * m_nLabelSize;
556 if (m_blobClip !=
null)
558 if (blobClip ==
null)
559 m_log.
WriteError(
new Exception(
"Could not find the MemoryDataLayer 'clip' top!"));
561 blobClip.
CopyFrom(m_blobClip,
false,
true);
564 m_nPos = (m_nPos + m_nBatchSize) % m_nN;
567 m_bHasNewData =
false;
576 bool m_bInitialization =
true;
584 m_bInitialization = bInit;
592 get {
return m_bInitialization; }
606 List<Datum> m_rgData;
607 List<Datum> m_rgClip;
621 m_blobData = blobData;
622 m_blobClip = blobClip;
623 m_blobLabel = blobLabel;
634 get {
return m_lstmType; }
642 get {
return m_blobData; }
650 get {
return m_blobClip; }
658 get {
return m_blobLabel; }
666 get {
return m_rgData; }
674 get {
return m_rgClip; }
The BinaryData class is used to pack and unpack DataCriteria binary data, optionally stored within ea...
static List< float > UnPackFloatList(byte[] rg, DATA_FORMAT fmtExpected)
Unpack the byte array into a list of float values.
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The Log class provides general output in text form.
void CHECK(bool b, string str)
Test a flag for true.
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.
void WriteError(Exception e)
Write an error as output.
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
void CHECK_LE(double df1, double df2, string str)
Test whether one number is less than or equal to another.
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
The SimpleDatum class holds a data input within host memory.
The Utility class provides general utility funtions.
The BlobCollection contains a list of Blobs.
bool Contains(Blob< T > blob)
Returns whether or not the collection contains a given blob.
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.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The Blob is the main holder of data that moves through the Layers of the Net.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
int height
DEPRECIATED; legacy shape accessor height: use shape(2) instead.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
int width
DEPRECIATED; legacy shape accessor width: use shape(3) instead.
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
T[] update_cpu_data()
Update the CPU data by transferring the GPU data over to the Host.
int count()
Returns the total number of items in the Blob.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
The BaseDataLayer is the base class for data Layers that feed Blobs of data into the Net.
DataTransformer< T > m_transformer
Specifies the DataTransformer used to transform each data item as it loaded.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
LayerParameter.LayerType type
Returns the LayerType of this Layer.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The MemoryDataLayerGetDataArgs class is passed to the OnGetData event.
MemoryDataLayerGetDataArgs(bool bInit)
The constructor.
bool Initialization
Returns whether the event was fired during the DataLayerSetup call or not.
The MemoryDataLayer provides data to the Net from memory. This layer is initialized with the MyCaffe....
int width
Returns the data width.
int height
Returns the data height.
override int MinTopBlobs
Returns the minimum number of top blobs: data
virtual void AddDatum(SimpleDatum sd, int nLblAxis=1, bool bReset=false, bool bResizeBatch=false)
This method is used to add a single Datum to the memory.
override void DataLayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the MemoryDataLayer.
virtual void AddDatumVector(Datum[] rgData, Datum[] rgClip=null, int nLblAxis=1, bool bReset=false, bool bResizeBatch=false)
This method is used to add a list of Datums to the memory.
int channels
Returns the data channels.
void Reset(Blob< T > data, Blob< T > labels, int n)
Resets the data by copying the internal data to the parameters specified.
override int MaxTopBlobs
Returns the maximum number of top blobs: data, clip, label
int clip_size2
Returns the clip 2 size, if any exists.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the internal data and outputs.
override int ExactNumBottomBlobs
No bottom blobs are used by this layer.
int batch_size
Returns the batch size.
MemoryDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The BaseDataLayer constructor.
virtual void AddDatumVector(List< Datum > rgData, List< Datum > rgClip=null, int nLblAxis=1, bool bReset=false, bool bResizeBatch=false)
This method is used to add a list of Datums to the memory.
EventHandler< MemoryDataLayerGetDataArgs > OnGetData
The OnGetData event fires on the DataLayerSetup call and each time the data wraps around (e....
EventHandler< MemoryDataLayerPackDataArgs< T > > OnDataPack
The OnDataPack event fires from within the AddDatumVector method and is used to pack the data into a ...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward computation which loads the data into the top (output) Blobs.
int clip_size1
Returns the clip 1 size, if any exists.
void Copy(MemoryDataLayer< T > src)
Copy the data by copying the src alyer data and label to the parameters specified.
override void dispose()
Releases all GPU and host resources used by the Layer.
The MemoryDataLayerPackDataArgs is passed to the OnDataPack event which fires each time the data rece...
Blob< T > Label
Returns the label data to fill with ordered label information.
Blob< T > Clip
Returns the clip data to fill with ordered data for clipping.
List< Datum > ClipItems
Returns the raw clip items to use to fill.
LayerParameter.LayerType LstmType
Returns the LSTM type.
Blob< T > Data
Returns the blob data to fill with ordered data.
List< Datum > DataItems
Returns the raw data items to use to fill.
MemoryDataLayerPackDataArgs(Blob< T > blobData, Blob< T > blobClip, Blob< T > blobLabel, List< Datum > rgData, List< Datum > rgClip, LayerParameter.LayerType type=LayerParameter.LayerType.LSTM)
The constructor.
The LayerParameterBase is the base class for all other layer specific parameters.
LABEL_TYPE
Defines the label type.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
MemoryDataParameter memory_data_param
Returns the parameter set when initialized with LayerType.MEMORY_DATA
List< string > top
Specifies the active top connections (in the bottom, out the top)
LayerType
Specifies the layer type.
uint batch_size
Batch size.
uint clip_length1
Specifies the clip length 1 (default = 0, which means unused).
uint clip_length2
Specifies the clip length 2 (default = 0, which means unused).
uint width
The width of the data.
LABEL_TYPE label_type
(optional, default = SINGLE) Specifies the label type: SINGLE - the default which uses the 'Label' fi...
uint label_height
The height of the label.
uint label_width
The width of the label.
uint channels
The number of channels in the data.
uint label_channels
The number of channels in the label.
uint height
The height of the data.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
BLOB_TYPE
Defines the tpe of data held by a given Blob.
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
The MyCaffe.db.image namespace contains all image database related classes.
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-...