2using System.Collections.Generic;
19 object m_syncObj =
new object();
24 bool m_bUseTrainingImagesForTesting =
false;
25 int m_nLastTestingImageIdx = 0;
26 int m_nLastTrainingImageIdx = 0;
27 List<Guid> m_rgUsers =
new List<Guid>();
28 int m_nOriginalDsId = 0;
45 if (user != Guid.Empty)
56 m_TestingImages.
Reset();
57 m_TrainingImages.
Reset();
68 return m_rgUsers.Count;
78 m_rgUsers.Remove(user);
79 return m_rgUsers.Count;
98 if (loadMethod !=
DB_LOAD_METHOD.LOAD_ALL && nImageDbLoadLimit > 0)
99 throw new Exception(
"Currently the load-limit only works with the LOAD_ALLL image loading method.");
108 log.WriteLine(
"WARNING: Cannot create a mean image for data sources that contain variable sized images. The mean check will be skipped.");
109 bSkipMeanCheck =
true;
112 m_TrainingImages = loadImageset(
"Training", m_ds.
TrainingSource, rgAbort, ref imgMean, out m_nLastTrainingImageIdx, nPadW, nPadH, log, loadMethod, nImageDbLoadLimit, m_nLastTrainingImageIdx, (ds ==
null) ?
true :
false, bSkipMeanCheck);
114 m_nLastTrainingImageIdx = 0;
116 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
119 m_TestingImages = loadImageset(
"Testing", m_ds.
TestingSource, rgAbort, ref imgMean, out m_nLastTestingImageIdx, nPadW, nPadH, log, loadMethod, nImageDbLoadLimit, m_nLastTestingImageIdx, (ds ==
null) ?
true :
false, bSkipMeanCheck);
121 m_nLastTestingImageIdx = 0;
123 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
139 foreach (Guid g
in m_rgUsers)
145 ds.m_TestingImages = m_TestingImages.
Clone();
146 ds.m_TrainingImages = m_TrainingImages.
Clone();
147 ds.m_bUseTrainingImagesForTesting = m_bUseTrainingImagesForTesting;
149 if (bReOrganizeByTime)
151 ds.m_nOriginalDsId = ds.DatasetID;
154 int nTestingCount = ds.m_TestingImages.
Count;
155 int nTrainingCount = ds.m_TrainingImages.Count;
157 List<SimpleDatum> rgSd =
new List<SimpleDatum>();
159 rgSd.AddRange(ds.m_TestingImages.Images);
160 rgSd.AddRange(ds.m_TrainingImages.Images);
162 int nCount = rgSd.Where(p => p !=
null).Count();
164 throw new Exception(
"You must first load 'all' images with LOAD_ALL or LOAD_FROM_SERVICE!");
166 rgSd = rgSd.OrderBy(p => p.Description).ThenBy(p => p.TimeStamp).ToList();
168 for (
int i = 0; i < nTrainingCount; i++)
170 ds.m_TrainingImages.Images[i] = rgSd[i];
171 ds.m_TrainingImages.Images[i].Index = i;
174 for (
int i = 0; i < nTestingCount; i++)
176 ds.m_TestingImages.Images[i] = rgSd[i + nTrainingCount];
177 ds.m_TestingImages.Images[i].Index = i;
180 ds.m_TrainingImages.Source.ID *= -1;
181 ds.m_TestingImages.Source.ID *= -1;
182 ds.Descriptor.TrainingSource.ID *= -1;
183 ds.Descriptor.TestingSource.ID *= -1;
185 ds.m_TrainingImages.ReloadLabelSets();
186 ds.m_TestingImages.ReloadLabelSets();
216 get {
return m_bUseTrainingImagesForTesting; }
217 set { m_bUseTrainingImagesForTesting = value; }
220 private ImageSet loadImageset(
string strType,
SourceDescriptor src, WaitHandle[] rgAbort, ref
SimpleDatum imgMean, out
int nLastImageIdx,
int nPadW = 0,
int nPadH = 0,
Log log =
null,
DB_LOAD_METHOD loadMethod =
DB_LOAD_METHOD.LOAD_ALL,
int nImageDbLoadLimit = 0,
int nImageDbLoadLimitStartIdx = 0,
bool bLoadNext =
false,
bool bSkipMeanCheck =
false)
224 RawImageMean imgMeanRaw =
null;
227 nLastImageIdx = nImageDbLoadLimitStartIdx;
234 if (imgMeanRaw ==
null)
237 log.WriteLine(
"WARNING: No image mean exists in the database, changing image database load from " + loadMethod.ToString() +
" to " +
DB_LOAD_METHOD.LOAD_ALL.ToString());
248 log.WriteLine(
"WARNING: Could not find any images with " + strType +
" Source = '" + src.
Name +
"'. If this is a training dataset, you will need to enable the 'UseTrainingSrcForTesting' setting.");
253 log.WriteLine(
"Because there are no images in this set, the image loading method has been changed to LOAD_ON_DEMAND for this dataset.");
259 ImageSet imgset =
new ImageSet(m_factory, src, loadMethod, nImageDbLoadLimit, m_random);
261 if (log !=
null && nCount > 0)
262 log.WriteLine(
"Loading '" + src.
Name +
"' - " + nCount.ToString(
"N0") +
" images.");
270 int nBatchSize = 20000;
271 Stopwatch sw =
new Stopwatch();
274 if (nImageSize > 60000)
276 else if (nBatchSize > 20000)
278 else if (nImageSize > 3000)
281 if (nImageDbLoadLimit <= 0)
282 nImageDbLoadLimit = nCount;
284 List<int> rgIdx = (nCount == 0) ?
new List<int>() : getIndexList(nImageDbLoadLimitStartIdx, nImageDbLoadLimit);
289 while (nIdx < rgIdx.Count)
291 int nImageIdx = rgIdx[nIdx];
292 int nImageCount = Math.Min(rgIdx.Count - nIdx, nBatchSize);
294 List<RawImage> rgImg = m_factory.
GetRawImagesAt(nImageIdx, nImageCount);
296 for (
int j = 0; j < rgImg.Count; j++)
299 imgset.
Add(nIdx + j, sd1);
301 if (sw.Elapsed.TotalMilliseconds > 1000)
305 double dfPct = (double)(nIdx + j) / (double)nCount;
306 log.Progress = dfPct;
307 log.WriteLine(
"image loading at " + dfPct.ToString(
"P") +
"...");
312 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
319 if (loadMethod ==
DB_LOAD_METHOD.LOAD_ALL && rgImg.Count == 0 && nIdx < nCount)
321 log.WriteLine(
"WARNING: Loaded " + nIdx.ToString(
"N0") +
" images, yet " + (nCount - nIdx).ToString(
"N0") +
" images are unaccounted for. You may need to reindex the dataset.");
330 nLastImageIdx = rgIdx[rgIdx.Count - 1] + 1;
334 nLastImageIdx += nImageDbLoadLimit;
337 if (imgMean ==
null && !bSkipMeanCheck)
339 if (imgMeanRaw ==
null)
342 if (imgMeanRaw !=
null)
343 imgMean = m_factory.
LoadDatum(imgMeanRaw, nPadW, nPadH);
347 log.WriteLine(
"Calculating mean...");
349 imgMean = imgset.GetImageMean(log, rgAbort);
355 imgset.SetImageMean(imgMean);
357 imgset.CompleteLoad(nLastImageIdx);
367 private List<int> getIndexList(
int nStartIdx,
int nCount)
369 List<int> rgIdx =
new List<int>();
371 for (
int i = 0; i < nCount; i++)
373 rgIdx.Add(nStartIdx + i);
388 if (m_TestingImages.
SourceID != nSrcId &&
389 m_TrainingImages.
SourceID != nSrcId)
402 if (m_TestingImages.
SourceID != nSrcId &&
403 m_TrainingImages.
SourceID != nSrcId)
417 m_TrainingImages.
Unload();
434 dfTraining = (double)nTrainingLoaded / (
double)nTrainingTotal;
435 dfTesting = (double)nTestingLoaded / (
double)nTestingTotal;
437 int nTotalLoaded = nTrainingLoaded + nTestingLoaded;
438 int nTotalImages = nTrainingTotal + nTestingTotal;
440 return (
double)nTotalLoaded / (double)nTotalImages;
447 protected virtual void Dispose(
bool bDisposing)
451 if (m_TestingImages !=
null)
454 m_TestingImages =
null;
457 if (m_TrainingImages !=
null)
460 m_TrainingImages =
null;
463 if (m_factory !=
null)
485 if (m_TestingImages.
SourceID == nSourceID)
487 if (m_bUseTrainingImagesForTesting)
488 return m_TrainingImages;
490 return m_TestingImages;
493 if (m_TrainingImages.
SourceID == nSourceID)
495 return m_TrainingImages;
510 if (m_bUseTrainingImagesForTesting)
511 return m_TrainingImages;
513 return m_TestingImages;
518 return m_TrainingImages;
537 get {
return m_ds.
ID; }
538 set { m_ds.
ID = value; }
546 get {
return m_nOriginalDsId; }
554 get {
return m_ds.
Name; }
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
The LabelMappingCollection manages a collection of LabelMapping's.
The Log class provides general output in text form.
The SimpleDatum class holds a data input within host memory.
SimpleDatum Add(SimpleDatum d)
Creates a new SimpleDatum and adds another SimpleDatum to it.
int ID
Get/set the database ID of the item.
string Name
Get/set the name of the item.
The DatasetDescriptor class describes a dataset which contains both a training data source and testin...
SourceDescriptor TrainingSource
Get/set the training data source.
SourceDescriptor TestingSource
Get/set the testing data source.
The SourceDescriptor class contains all information describing a data source.
bool IsRealData
Returns whether or not the each data point represents a real or integer number. Integer numbers are u...
int Height
Returns the height of each data item in the data source.
int Width
Returns the width of each data item in the data source.
int ImageCount
Returns the number of images within this data source.
The DatasetEx class provides the in-memory dataset functionality that is used by the image database t...
int DatasetID
Returns the dataset ID of the dataset managesd by the DatasetEx object.
void Dispose()
Releases all resources used.
void ReloadLabelSets()
Reloads bot the training and testing label sets.
bool Initialize(DatasetDescriptor ds, WaitHandle[] rgAbort, int nPadW=0, int nPadH=0, Log log=null, DB_LOAD_METHOD loadMethod=DB_LOAD_METHOD.LOAD_ALL, int nImageDbLoadLimit=0, bool bSkipMeanCheck=false)
Initialize the DatasetEx by loading the training and testing data sources into memory.
ImageSet Find(string strSource)
Returns the ImageSet corresponding to a data source name.
DatasetEx Clone(bool bReOrganizeByTime=false)
Copy the DatasetEx and its contents.
DatasetEx(Guid user, DatasetFactory factory, CryptoRandom random)
The DatasetEx constructor.
string DatasetName
Returns the dataset name of the dataset managesd by the DatasetEx object.
void Reset()
Reset the image indexes for both the training and testing image sets.
virtual void Dispose(bool bDisposing)
Releases all resources used.
SimpleDatum QueryImageMean(int nSrcId)
Query the image mean for a data source.
int OriginalDatasetID
Returns the original DatsetID if this is a cloned re-organized dataset, otherwise 0 is returned.
void Unload()
Unload the images of the training and testing image sets.
ImageSet Find(int nSourceID)
Returns the ImageSet corresponding to a data source ID.
bool UseTrainingImagesForTesting
Get/set whether or not to use the training images when testing.
DatasetDescriptor Descriptor
Returns the dataset descriptor of the dataset managesd by the DatasetEx object.
int RemoveUser(Guid user)
Remove a user of the dataset.
bool SaveImageMean(int nSrcId, SimpleDatum sd, bool bUpdate)
Saves the image mean in a SimpleDatum to the database.
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event is passed to each image set and fires each time the Image set need to ...
void Relabel(LabelMappingCollection col)
Relabels both the testing and training image sets using the label mapping collection.
int AddUser(Guid user)
Adds a user of the dataset.
double GetPercentageLoaded(out double dfTraining, out double dfTesting)
Returns the total percentage of images loaded for testing, training and combined.
The DatasetFactory manages the connection to the Database object.
SimpleDatum QueryImageMean(int nSrcId=0)
Return the SimpleDatum for the image mean from the open data source.
bool SaveImageMean(SimpleDatum sd, bool bUpdate, int nSrcId=0)
Save the SimpleDatum as a RawImageMean in the database.
RawImageMean GetRawImageMean()
Return the RawImageMean for the open data source.
List< RawImage > GetRawImagesAt(int nImageIdx, int nImageCount, int nSrcId=0, string strDescription=null)
Returns a list of RawImages from the database for a data source.
int PutRawImageMean(SimpleDatum sd, bool bUpdate, ConnectInfo ci=null)
Save the SimpleDatum as a RawImageMean in the database for the open data source.
void Close()
Close the current data source used.
void Dispose()
Releases all resources used.
void Open(SourceDescriptor src, int nCacheMax=500, ConnectInfo ci=null)
Open a given data source.
SimpleDatum LoadDatum(int nImageId, int nChannels, bool bDataIsReal, int nLabel, int nSrcId=0)
Loads a new SimpleDataum from a RawImage ID.
The ImageSet class contains the list of image for a data source as well as a list of LabelSets that m...
ImageSet Clone()
Returns a copy of the ImageSet.
string SourceName
Returns the data source name of the image set.
virtual void Dispose(bool bDisposing)
Releases the resouces used.
int GetLoadedCount()
Returns the number of images loaded.
int SourceID
Returns the data source ID of the image set.
void Relabel(LabelMappingCollection col)
Applies the label mapping to the image set.
void Reset()
Resets the indexes and limited loaded images (if used).
int Count
Returns the number of images in the image set.
void ReloadLabelSets()
Reload the label sets.
void Unload()
Unload all images in the image set.
int GetTotalCount()
Returns the total number of images.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
DB_LOAD_METHOD
Defines how to laod the items into the in-memory database.
The MyCaffe.db.image namespace contains all image database related classes.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...