2using System.Collections.Generic;
50 private T[] m_rgTopData =
null;
51 private T[] m_rgTopLabel =
null;
52 private T[] m_rgOneHotLabel =
null;
53 private int[] m_rgTopShape =
null;
54 private bool m_bMatchingCycle =
true;
55 private Datum m_datumNoise =
null;
56 private LabelCollection m_rgBatchLabels =
null;
57 private Blob<T> m_blobMask1 =
null;
58 private Blob<T> m_blobMask =
null;
59 private Blob<T> m_blobDebug1 =
null;
60 private int m_nIteration = 0;
61 private int m_nBatchCount = 0;
78 : base(cuda, log, p, db, evtCancel)
81 throw new Exception(
"Currently only image databases are supported by the DataLayer.");
86 m_rgBatchLabels =
new LabelCollection();
88 Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD> kvSel = db.
GetSelectionMethod();
120 m_blobMask =
new Blob<T>(cuda, log,
false);
121 m_blobMask1 =
new Blob<T>(cuda, log,
false);
125 m_blobDebug1 =
new Blob<T>(cuda, log,
false);
135 if (m_rgBatchLabels !=
null)
136 m_rgBatchLabels.Cancel();
144 if (m_rgBatchLabels !=
null)
146 m_rgBatchLabels.Dispose();
147 m_rgBatchLabels =
null;
150 if (m_blobMask !=
null)
156 if (m_blobMask1 !=
null)
162 if (m_blobDebug1 !=
null)
203 m_rgBatchLabels.Cancel();
208 int nWait = m_rgBatchLabels.WaitProcessing;
212 m_rgBatchLabels.Set(e.
Labels);
223 bool bLoadDataCriteria =
false;
224 m_bMatchingCycle =
true;
229 bLoadDataCriteria =
true;
235 m_rgTopShape =
m_transformer.InferBlobShape(datum, m_rgTopShape);
239 m_datumNoise = createNoisyData(m_rgTopShape, datum);
249 m_rgTopShape[0] = nBatchSize;
250 colTop[0].
Reshape(m_rgTopShape);
261 createMasks(
Utility.Clone<
int>(m_rgTopShape));
265 createDebug(
Utility.Clone<
int>(m_rgTopShape));
270 List<int> rgLabelShape =
new List<int>() { nBatchSize };
288 rgLabelShape.Add(nLen);
296 rgLabelShape.Add(nLen);
306 rgLabelShape.Add(nLen);
307 m_log.
CHECK_EQ(nItemSize, 1,
"Currently only byte sized labels are supported in multi-label scenarios.");
328 rgLabelShape.Add(nChannels);
331 colTop[1].
Reshape(rgLabelShape);
343 private void createMasks(
int[] rgTopShape)
346 m_blobMask.
Reshape(rgTopShape);
348 rgTopShape[1] /= nImgPerBlob;
349 m_blobMask1.
Reshape(rgTopShape);
355 int nDim = m_blobMask1.
count();
357 for (
int n = 0; n < m_blobMask.
num; n++)
359 for (
int j = 0; j < nImgPerBlob; j++)
367 private void createDebug(
int[] rgTopShape)
371 rgTopShape[1] /= nImgPerBlob;
372 m_blobDebug1.
Reshape(rgTopShape);
382 filler.
Fill(blobNoise);
390 m_log.
FAIL(
"No 'mean' image is loaded, yet the MyCaffe Image Database = null, and it is required to get the mean image.");
396 m_log.
FAIL(
"The data source '" +
m_src.Name +
"' does not have a mean image!");
407 if (dfMin < -1.0 || dfMax > 1.0)
408 m_log.
WriteLine(
"WARNING! The noise filler is producing numbers outside of the range [-1,1] which may cause a saturated final noise data image.");
417 if (typeof(T) == typeof(
double))
423 List<double> rgdf1 =
new List<double>(rgdf);
428 List<byte> rgb = rgdf.Select(p => Math.Min((
byte)p, (
byte)255)).ToList();
438 List<float> rgf1 =
new List<float>(rgf);
443 List<byte> rgb = rgf.Select(p => Math.Min((
byte)p, (
byte)255)).ToList();
530 int nCount = blobTop.
count();
531 m_log.
CHECK_EQ(m_blobMask.
count(), nCount,
"The mask must be the same size as the top!");
540 int nChannels = blobTop.
channels / nImgPerBlob;
544 m_log.
WriteLine(
"WARNING! debug output only supported blobs with 4 or more axes.");
549 if (!Directory.Exists(strPath))
550 Directory.CreateDirectory(strPath);
552 strPath = strPath.TrimEnd(
'\\');
553 int nDim = nChannels * blobTop.
height * blobTop.
width;
554 int nCount = m_blobDebug1.
count();
557 m_log.
CHECK_EQ(nDim, nCount,
"The debug data is not sized properly.");
559 for (
int n = 0; n < blobTop.
num; n++)
561 for (
int j = 0; j < nImgPerBlob; j++)
574 string strFile = strPath +
"\\dbgimg_iter_" + m_nIteration.ToString() +
"_num_" + n.ToString() +
"_img_" + j.ToString();
577 if (nChannels == 1 || nChannels == 3)
580 bmp.Save(strFile +
".png");
597 m_log.
CHECK(batch.
Data.count() > 0,
"There is no space allocated for data!");
599 bool bLoadDataCriteria =
false;
602 bLoadDataCriteria =
true;
610 if (m_rgOneHotLabel ==
null || m_rgOneHotLabel.Length != nCount)
611 m_rgOneHotLabel =
new T[nCount];
613 nCount = batch.
Label.count();
615 if (m_rgTopLabel ==
null || m_rgTopLabel.Length != nCount)
616 m_rgTopLabel =
new T[nCount];
620 int nCount = batch.
Label.count();
621 m_log.
CHECK_GT(nCount, 0,
"The label count cannot be zero!");
623 if (m_rgTopLabel ==
null || m_rgTopLabel.Length < nCount)
624 m_rgTopLabel =
new T[nCount];
637 List<int> rgLabels =
null;
638 List<int> rgTargetLabels =
null;
641 rgLabels =
new List<int>();
648 int nWait = m_rgBatchLabels.WaitReady;
652 rgTargetLabels = m_rgBatchLabels.Get();
653 m_log.
CHECK_EQ(nBatchSize, m_rgBatchLabels.Count,
"The batch label count (previously loaded by the primary dataset) does not match the batch size '" +
m_param.
data_param.
batch_size.ToString() +
"' of this layer!");
656 for (
int i = 0; i < nBatchSize; i++)
668 if (rgTargetLabels ==
null)
670 datum =
m_cursor.GetValue(
null, bLoadDataCriteria);
690 if (m_bMatchingCycle)
692 m_rgDatum[j] = getNextPair(
true, datum, bLoadDataCriteria);
697 m_rgDatum[j] = m_datumNoise;
699 m_rgDatum[j] = getNextPair(
false, datum, bLoadDataCriteria);
705 m_rgDatum[j] = m_datumNoise;
707 m_rgDatum[j] =
m_cursor.GetValue(
null, bLoadDataCriteria);
711 m_bMatchingCycle = !m_bMatchingCycle;
716 datum =
m_cursor.GetValue(rgTargetLabels[i], bLoadDataCriteria);
724 if (m_rgDatum !=
null)
726 for (
int n = 0; n < m_rgDatum.Length; n++)
744 m_rgTopShape =
m_transformer.InferBlobShape(datum, m_rgTopShape);
747 if (m_rgDatum !=
null)
748 m_rgTopShape[1] *= (m_rgDatum.Length + 1);
751 m_rgTopShape[0] = nBatchSize;
752 batch.
Data.Reshape(m_rgTopShape);
755 for (
int k = 1; k < m_rgTopShape.Length; k++)
757 nDim *= m_rgTopShape[k];
760 int nTopLen = nDim * nBatchSize;
761 if (m_rgTopData ==
null || m_rgTopData.Length != nTopLen)
762 m_rgTopData =
new T[nTopLen];
766 int nDimCount = nDim;
768 if (m_rgDatum !=
null)
769 nDimCount /= (m_rgDatum.Length + 1);
772 Array.Copy(rgTrans, 0, m_rgTopData, nDim * i, nDimCount);
775 if (m_rgDatum !=
null)
777 for (
int j = 0; j < m_rgDatum.Length; j++)
780 int nOffset = (nDim * i) + (nDimCount * (j + 1));
781 Array.Copy(rgTrans, 0, m_rgTopData, nOffset, nDimCount);
794 for (
int j = 0; j < m_rgOneHotLabel.Length; j++)
796 if ((datum.
Label & nMask) == 0)
799 m_rgOneHotLabel[j] =
m_tOne;
804 Array.Copy(m_rgOneHotLabel, 0, m_rgTopLabel, i * m_rgOneHotLabel.Length, m_rgOneHotLabel.Length);
812 m_log.
FAIL(
"Label mapping is not supported on labels of type 'MULTIPLE'.");
820 int nDstIdx = i * nLen;
822 m_log.
CHECK_EQ(nItemSize, 1,
"Currently only byte sized labels are supported in multi-label scenarios.");
823 Array.Copy(datum.
DataCriteria, 0, m_rgTopLabel, nDstIdx, nLen);
829 if (m_rgDatum !=
null)
831 if (m_rgDatum.Length == 1)
840 int nLabel = datum.
Label;
844 m_rgTopLabel[i * nLabelDim] = (T)Convert.ChangeType(nLabel, typeof(T));
846 for (
int j = 0; j < m_rgDatum.Length; j++)
848 m_rgTopLabel[i * nLabelDim + 1 + j] = (T)Convert.ChangeType(m_rgDatum[j].
Label, typeof(T));
854 m_rgTopLabel[i * nLabelDim] =
m_tOne;
856 m_rgTopLabel[i * nLabelDim] =
m_tZero;
860 m_log.
FAIL(
"Currently image pairing only supports up to 2 images per blob.");
864 m_rgTopLabel[i] = (T)Convert.ChangeType(datum.
Label, typeof(T));
872 if (rgLabels !=
null)
873 rgLabels.Add(datum.
Label);
882 batch.
Data.SetCPUData(m_rgTopData);
885 batch.
Label.SetCPUData(m_rgTopLabel);
898 if (m_rgBatchLabels !=
null)
899 m_rgBatchLabels.Done();
911 string strType =
null;
920 dNew =
m_cursor.GetValue(nLabel, bLoadDataCriteria);
922 while (dNew.
Label != d.
Label && nIdx < nRetries)
925 dNew =
m_cursor.GetValue(nLabel, bLoadDataCriteria);
934 dNew =
m_cursor.GetValue(
null, bLoadDataCriteria);
936 while (dNew.
Label == d.
Label && nIdx < nRetries)
939 dNew =
m_cursor.GetValue(
null, bLoadDataCriteria);
944 strType =
"non-match";
948 m_log.
WriteLine(
"WARNING: The secondary pairing " + strType +
" could not be found after " + nRetries.ToString() +
" retries!");
955 string strFile = p.
debug_save_path.TrimEnd(
'\\') +
"\\dbgimg_iter_" + m_nBatchCount.ToString() +
"_num_" + nNum.ToString() +
"_img_" + nImg.ToString();
960 class LabelCollection : IDisposable
962 ManualResetEvent m_evtReady =
new ManualResetEvent(
false);
963 ManualResetEvent m_evtDone =
new ManualResetEvent(
true);
964 AutoResetEvent m_evtCancel =
new AutoResetEvent(
false);
965 List<int> m_rgLabels =
new List<int>();
966 object m_sync =
new object();
968 public LabelCollection()
972 public void Dispose()
974 if (m_evtReady !=
null)
976 m_evtReady.Dispose();
980 if (m_evtDone !=
null)
986 if (m_evtCancel !=
null)
988 m_evtCancel.Dispose();
1002 List<WaitHandle> rgWait =
new List<WaitHandle>() { m_evtCancel, m_evtReady };
1003 return WaitHandle.WaitAny(rgWait.ToArray());
1007 public int WaitProcessing
1011 List<WaitHandle> rgWait =
new List<WaitHandle>() { m_evtCancel, m_evtDone };
1012 return WaitHandle.WaitAny(rgWait.ToArray());
1021 public void Set(List<int> rg)
1025 m_rgLabels =
new List<int>(rg);
1036 return m_rgLabels.Count;
1041 public List<int> Get()
1045 List<int> rg =
new List<int>(m_rgLabels);
1059 List<int> m_rgLabels;
1067 m_rgLabels =
new List<int>(rgLabels);
1075 get {
return m_rgLabels; }
The BinaryData class is used to pack and unpack DataCriteria binary data, optionally stored within ea...
static List< double > UnPackDoubleList(byte[] rg, DATA_FORMAT fmtExpected)
Unpack the byte array into a list of double values.
static List< float > UnPackFloatList(byte[] rg, DATA_FORMAT fmtExpected)
Unpack the byte array into a list of float values.
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 Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap=null, List< int > rgClrOrder=null)
Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
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 FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
The SimpleDatum class holds a data input within host memory.
int OriginalLabel
Get/set the original known label of the data.
int Channels
Return the number of channels of the data.
bool IsRealData
Returns whether or not the data contains real numbers or byte data.
byte[] DataCriteria
Get/set data criteria associated with the data.
DATA_FORMAT
Defines the data format of the DebugData and DataCriteria when specified.
int Width
Return the width of the data.
int Height
Return the height of the data.
DATA_FORMAT DataCriteriaFormat
Get/set the data format of the data criteria.
int Label
Return the known label of the data.
void SaveInfo(string strFile)
Save the SimpleDatum information to a text file.
The Utility class provides general utility funtions.
The BlobCollection contains a list of Blobs.
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.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
double min_data
Returns the minimum value in the data of the Blob.
double max_data
Returns the maximum value in the data of the Blob.
int height
DEPRECIATED; legacy shape accessor height: use shape(2) instead.
int num_axes
Returns the number of axes in the Blob.
T[] mutable_cpu_diff
Get diff from the GPU and bring it over to the host, or Set diff from the Host and send it over to th...
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 scale_data(double df)
Scale the data by a scaling factor.
int width
DEPRECIATED; legacy shape accessor width: use shape(3) instead.
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.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
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 Cursor is used to traverse through a given data source within the database.
A generic database class used to connect to the underlying database and create a Cursor that traverse...
Abstract Filler class used to fill blobs with values.
void Fill(Blob< T > b)
Fill the blob with values based on the actual filler used.
static Filler< T > Create(CudaDnn< T > cuda, Log log, FillerParameter p)
Create a new Filler instance.
bool m_bOutputLabels
Specifies whether or not the Layer should output labels.
SourceDescriptor m_src
Specifies the SourceDescriptor of the data source.
IXImageDatabaseBase m_imgdb
Specifies the MyCaffeImageDatabase.
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.
void statupPrefetch()
Starts the prefetch 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 DataLayer loads data from the IXImageDatabase database. This layer is initialized with the MyCaff...
override int MaxTopBlobs
Specifies the maximum number of required top (output) Blobs: data, label
void Next()
Retrieves the next item from the database and rolls the cursor over once the end of the dataset is re...
override int ExactNumBottomBlobs
No bottom blobs are used by this layer.
EventHandler< LastBatchLoadedArgs > OnBatchLoad
This event fires (only when set) each time a batch is loaded form this dataset.
override void dispose()
Releases all GPU and host resources used by the Layer.
void Connect(DataLayer< T > src)
The Connect method connects one Data Layer to another so that they can synchronize.
override void final_process(Blob< T > blobTop)
Provides a final processing step that takes place at the end of the base class forward = this is wher...
Stopwatch m_swTimerBatch
Specifies a first timer used to calcualte the batch time.
DB< T > m_db
Specifies the database.
DataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
The DataLayer constructor.
Stopwatch m_swTimerTransaction
Specfies a second timer used to calculate the transaction time.
override int MinTopBlobs
Specifies the minimum number of required top (output) Blobs: data
override bool delayPrefetch
Specifies to delay the prefetch when using a synchronized Data Layer.
double m_dfTransTime
Specifies the transaction time.
double m_dfReadTime
Specifies the read time.
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...
override void DataLayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the DataLayer by starting up the pre-fetching.
Cursor< T > m_cursor
Specifies the database used to traverse through the database.
bool Skip()
Skip to the next value - used when training in a multi-GPU scenario.
void Disconnect()
Disconnect any previously connected Data Layers.
override void preStop()
The preStop override is called just before stopping the internal thread managed by the base class.
Specifies the arguments sent to the OnBatchLoad event used when synchronizing between Data Layers.
List< int > Labels
Returns the labels loaded.
LastBatchLoadedArgs(List< int > rgLabels)
The constructor.
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.
T m_tZero
Specifies a generic type equal to 0.0.
T m_tOne
Specifies a generic type equal to 1.0.
float convertF(T df)
Converts a generic to a float value.
double convertD(T df)
Converts a generic to a double value.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The DataDebugParameter is used by the DataParameter when the 'enable_debugging' = True.
string debug_save_path
(/b optional, default = null) Specifies the path where the debug data images are saved,...
int iterations
(/b optional, default = 1) Specifies the number of iterations to output debug information.
string noisy_save_path
(/b optional, default = null) Specifies the path where the noisy data image is saved,...
bool use_noisy_mean
(optional, default = true) When true the noise is applied to the mean and used as the noisy data....
int noise_data_label
(optional, default = -1) Specifies the label used with each noise filled data used when 'use_noise_fo...
FillerParameter noise_filler
Specifies the noise filler used when 'use_noise_for_nonmatch' = true. By default the 'noise_filter' i...
Specifies the parameter for the data layer.
virtual uint batch_size
Specifies the batch size.
LABEL_TYPE label_type
(optional, default = SINGLE) Specifies the label type: SINGLE - the default which uses the 'Label' fi...
bool synchronize_target
(optional, default = false) Specifies whether or not this is a to be synchronized with another data l...
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....
int images_per_blob
(optional, default = 1) Specifies the number of images to load into each blob channel....
string synchronize_with
(optional, default = null) Specifies a secondary (target) dataset to syncrhonize with.
DataNoiseParameter data_noise_param
Specifies the DataNoiseParameter used when 'enable_noise_for_nonmatch' = True.
bool enable_noise_for_nonmatch
(optional, default = false) When true an image consisting of noise initialized with noise filler.
int one_hot_label_size
When greater than 0 (default = 0), labels are one-hot encoded to a vector of the one-hot label size (...
bool? enable_pair_selection
(optional, default = null) Specifies whether or not to select images in a pair sequence....
int forced_primary_label
(optional, default = -1) When >= 0, this label is used as the primary image label when 'images_per_bl...
DataDebugParameter data_debug_param
Specifies the DataDebugParameter used when 'enable_debug_output' = True.
string source
When used with the DATA parameter, specifies the data 'source' within the database....
bool output_all_labels
(optional, default = false) When using images_per_blob > 1, 'output_all_labels' specifies to output a...
bool enable_debug_output
(optional, default = false) When true the data sent out through the top are saved as images into the ...
bool output_image_information
(optional, default = false) When true image information such as index and label are output....
bool balance_matches
(optional, default = true) When using images_per_blob > 1, 'balance_matches' specifies to query image...
LABEL_TYPE
Defines the label type.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
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
DataParameter data_param
Returns the parameter set when initialized with LayerType.DATA
Phase phase
Specifies the Phase for which this LayerParameter is run.
LayerType
Specifies the layer type.
bool Active
When active, the parameter is used, otherwise it is ignored.
The IXDatabaseBase interface defines the general interface to the in-memory database.
SimpleDatum GetItemMean(int nSrcId)
Returns the item (e.g., image or temporal item) mean for a data source.
Tuple< DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD > GetSelectionMethod()
Returns the label and image selection method used.
void SetSelectionMethod(DB_LABEL_SELECTION_METHOD? lbl, DB_ITEM_SELECTION_METHOD? img)
Sets the label and image selection methods.
DB_VERSION GetVersion()
Returns the version of the MyCaffe Image Database being used.
The IXImageDatabaseBase interface defines the general interface to the in-memory image database.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
DB_ITEM_SELECTION_METHOD
Defines the item (e.g., image or temporal item) selection method.
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.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-...