2using System.Collections.Generic;
26 List<Tuple<string, int>> m_rgLines =
new List<Tuple<string, int>>();
44 private T[] m_rgTopData =
null;
54 : base(cuda, log, p, null, evtCancel)
87 List<Tuple<string, int>> rgLines =
new List<Tuple<string, int>>();
89 while (m_rgLines.Count > 0)
91 int nIdx = m_random.Next(m_rgLines.Count);
92 rgLines.Add(m_rgLines[nIdx]);
93 m_rgLines.RemoveAt(nIdx);
95 if (m_rgLines.Count == 1)
97 rgLines.Add(m_rgLines[0]);
105 private void loadFileList()
107 if (m_rgLines.Count > 0)
114 using (StreamReader sr =
new StreamReader(strSource))
116 string strLine = sr.ReadLine();
118 while (strLine !=
null)
120 if (!
string.IsNullOrEmpty(strLine))
122 int nPos = strLine.LastIndexOf(
' ');
125 nPos = strLine.LastIndexOf(
',');
127 nPos = strLine.LastIndexOf(
';');
130 m_log.
CHECK_GT(nPos, 0,
"The separator character of ' ' or ',' or ';' could not be found!");
131 string strFile = strLine.Substring(0, nPos);
132 string strLabel = strLine.Substring(nPos + 1);
133 int nLabel =
int.Parse(strLabel);
135 m_rgLines.Add(
new Tuple<string, int>(strFile, nLabel));
138 strLine = sr.ReadLine();
145 private string getRootFolder()
159 string strRootFolder = getRootFolder();
164 if (nH == 0 && nW == 0)
172 m_src =
new basecode.descriptors.SourceDescriptor(0,
"Internal", nW, nH, nC,
false,
false);
188 string strRootFolder = getRootFolder();
190 m_log.
CHECK((nNewHeight == 0 && nNewWidth == 0) || (nNewHeight > 0 && nNewWidth > 0),
"Current implementation requires new_height and new_width to be set at the same time.");
204 m_log.
WriteLine(
"WARNING: Shuffling or skipping recommended for multi-GPU.");
207 m_log.
WriteLine(
"A total of " + m_rgLines.Count.ToString(
"N0") +
" images.");
214 m_log.
WriteLine(
"Skipping first " + nSkip.ToString() +
" data points.");
215 m_log.
CHECK_GT(m_rgLines.Count, nSkip,
"Not enough data points to skip.");
220 Datum datum = loadImage(strRootFolder, m_rgLines[m_nLinesId], bIsColor, nNewHeight, nNewWidth);
225 rgTopShape[0] = nBatchSize;
236 List<int> rgLabelShape =
new List<int>() { nBatchSize };
238 colTop[1].
Reshape(rgLabelShape);
246 private Datum loadImage(
string strRootFolder, Tuple<string, int> item,
bool bIsColor,
int nNewHeight,
int nNewWidth)
248 string strFile = strRootFolder + item.Item1;
249 int nLabel = m_rgLines[m_nLinesId].Item2;
250 m_log.
CHECK(File.Exists(strFile),
"Could not find the file '" + strFile +
"'!");
251 Bitmap bmp =
new Bitmap(strFile);
254 if ((nNewWidth > 0 && nNewHeight > 0) && (bmp.Width != nNewWidth || bmp.Height != nNewHeight))
262 int nChannels = (bIsColor) ? 3 : 1;
264 if (typeof(T) == typeof(
double))
280 m_log.
CHECK(batch.
Data.count() > 0,
"There is no space allocated for data!");
285 string strRootFolder = getRootFolder();
287 T[] rgTopLabel =
null;
288 int nCount = batch.
Label.count();
289 m_log.
CHECK_GT(nCount, 0,
"The label count cannot be zero!");
290 rgTopLabel =
new T[nCount];
302 for (
int i = 0; i < nBatchSize; i++)
307 m_log.
CHECK_GT(m_rgLines.Count, m_nLinesId,
"The lines ID is too small!");
308 int nIdx = m_nLinesId;
310 nIdx = m_random.Next(m_rgLines.Count);
311 datum = loadImage(strRootFolder, m_rgLines[nIdx], bIsColor, nNewHeight, nNewWidth);
327 rgTopShape[0] = nBatchSize;
328 batch.
Data.Reshape(rgTopShape);
331 for (
int k = 1; k < rgTopShape.Count; k++)
333 nDim *= rgTopShape[k];
336 int nTopLen = nDim * nBatchSize;
337 if (m_rgTopData ==
null || m_rgTopData.Length != nTopLen)
338 m_rgTopData =
new T[nTopLen];
342 int nDimCount = nDim;
344 Array.Copy(rgTrans, 0, m_rgTopData, nDim * i, nDimCount);
346 rgTopLabel[i] = (T)Convert.ChangeType(datum.
Label, typeof(T));
354 batch.
Data.SetCPUData(m_rgTopData);
355 batch.
Label.SetCPUData(rgTopLabel);
370 if (m_nLinesId == m_rgLines.Count)
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
The Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
int height
Specifies the height of the data.
int width
Specifies the width of the data.
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Datum GetImageDataF(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataF function converts a Bitmap into a Datum using the float type for real data.
static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataD function converts a Bitmap into a Datum using the double type for real data.
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_GT(double df1, double df2, string str)
Test whether one number is greater than another.
int Label
Return the known label of the data.
The BlobCollection contains a list of Blobs.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
SourceDescriptor m_src
Specifies the SourceDescriptor of the data source.
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...
CancelEvent m_evtCancel
Specifies the cancellation event for the internal thread.
Batch< T >[] m_rgPrefetch
Specifies the pre-fetch cache.
The Batch contains both the data and label Blobs of the batch.
Blob< T > Label
Returns the label Blob of the batch.
Blob< T > Data
Returns the data Blob of the batch.
The ImageDataLayer loads data from the image files located in the root directory specified....
Stopwatch m_swTimerTransaction
Specfies a second timer used to calculate the transaction time.
override int ExactNumTopBlobs
Specifies the exact number of top blobs as 2 for this layer: data, label.
override void dispose()
Releases all GPU and host resources used by the Layer.
override int ExactNumBottomBlobs
No bottom blobs are used by this layer.
virtual void shuffleImages()
Shuffle the images so that they are loaded in a random order.
override bool setupSourceDescriptor()
Allows any derivative classes to pre-initialize the m_src which is used in LayerSetup before the Data...
override void load_batch(Batch< T > batch)
Load a batch of data in the background (this is run on an internal thread within the BasePrefetchingD...
Stopwatch m_swTimerBatch
Specifies a first timer used to calcualte the batch time.
double m_dfTransTime
Specifies the transaction time.
ImageDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel)
The ImageDataLayer constructor.
double m_dfReadTime
Specifies the read time.
override void DataLayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the ImageDataLayer by starting up the pre-fetching.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
LayerParameter.LayerType m_type
Specifies the Layer type.
LayerParameter layer_param
Returns the LayerParameter for this Layer.
The LayerParameterEx class is used when sharing another Net to conserve GPU memory and extends the La...
virtual uint batch_size
Specifies the batch size.
bool display_timing
(optional, default = false) Specifies whether or not to display the timing of each image read.
bool? enable_random_selection
(optional, default = null) Specifies whether or not to randomly query images from the data source....
string source
When used with the DATA parameter, specifies the data 'source' within the database....
string root_folder
Specifies the folder containing the image files.
bool shuffle
Specifies whether or not the ImageLayer should shuffle the list of files at each epoch.
uint new_width
When > 0, specifies the new width of the images fed into the network (default = 0).
uint new_height
When > 0, specifies the new height of the images fed into the network (default = 0).
uint rand_skip
Specifies the amount for the image data layer to skip a few points to avoid all asynchronous sgd clie...
bool is_color
Specififies whether or not the image is color or gray-scale.
Specifies the base parameter for all layers.
int solver_rank
Returns the SolverRank of the Solver using this LayerParameter (if any).
DataParameter data_param
Returns the parameter set when initialized with LayerType.DATA
LayerType
Specifies the layer type.
ImageDataParameter image_data_param
Returns the parameter set when initialized with LayerType.IMAGE_DATA
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
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.fillers namespace contains all fillers including the Filler class.
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-...