2using System.Collections.Generic;
20 object m_syncObj =
new object();
25 bool m_bUseTrainingImagesForTesting =
false;
26 List<Guid> m_rgUsers =
new List<Guid>();
27 int m_nOriginalDsId = 0;
29 long m_lDefaultQueryState = 0;
30 ManualResetEvent m_evtRefreshScheduleCancel =
new ManualResetEvent(
false);
31 ManualResetEvent m_evtRefreshScheduleRunning =
new ManualResetEvent(
false);
32 ManualResetEvent m_evtRefreshScheduleDone =
new ManualResetEvent(
false);
33 Thread m_refreshScheduleThread =
null;
34 int m_nRefreshUpdatePeriod = 0;
35 double m_dfRegreshReplacementPct = 0;
36 int m_nTrainingRefreshCount = 0;
37 int m_nTestingRefreshCount = 0;
38 bool m_bRefreshTraining =
false;
39 bool m_bRefreshTesting =
false;
58 if (user != Guid.Empty)
73 return m_rgUsers.Count;
83 m_rgUsers.Remove(user);
84 return m_rgUsers.Count;
102 public long Initialize(
DatasetDescriptor ds, WaitHandle[] rgAbort,
int nPadW = 0,
int nPadH = 0,
Log log =
null,
DB_LOAD_METHOD loadMethod =
DB_LOAD_METHOD.LOAD_ALL,
bool bSkipMeanCheck =
false,
int nImageDbLoadLimit = 0,
int nImageDbAutoRefreshScheduledUpdateInMs = 0,
double dfImageDbAutoRefreshScheduledReplacementPct = 0,
bool bVerify =
false)
113 log.WriteLine(
"WARNING: Cannot create a mean image for data sources that contain variable sized images. The mean check will be skipped.",
true);
114 bSkipMeanCheck =
true;
117 bool bSilentLoad = (loadMethod ==
DB_LOAD_METHOD.LOAD_ON_DEMAND_BACKGROUND) ?
true :
false;
121 QueryState qsTraining = m_TrainingImages.
Initialize(bSilentLoad,
true,
true, nImageDbLoadLimit, bVerify);
126 bool bQueryOnly =
false;
130 sdMean = m_TrainingImages.
GetImageMean(log, rgAbort, bQueryOnly);
133 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
138 QueryState qsTesting = m_TestingImages.
Initialize(bSilentLoad,
true,
true, nImageDbLoadLimit, bVerify);
149 if (EventWaitHandle.WaitAny(rgAbort, 0) != EventWaitHandle.WaitTimeout)
152 if (loadMethod ==
DB_LOAD_METHOD.LOAD_ALL && nImageDbLoadLimit > 0 && nImageDbAutoRefreshScheduledUpdateInMs > 0 && dfImageDbAutoRefreshScheduledReplacementPct > 0)
155 m_lDefaultQueryState = m_queryStates.
CreateNewState(qsTraining, qsTesting);
156 return m_lDefaultQueryState;
216 bool bRunning =
false;
242 public void StartRefresh(
bool bTraining =
true,
bool bTesting =
true,
double dfReplacementPct = 0.25)
256 public void StopRefresh(
bool bTraining =
true,
bool bTesting =
true)
275 if (m_refreshScheduleThread !=
null)
278 if (nPeriodInMs == 0 || dfReplacementPct == 0)
283 m_log.
WriteLine(
"WARNING: The shared training data source '" + m_TrainingImages.
Source.
Name +
"' appears to already be open with the '" + m_TrainingImages.
LoadMethod.ToString() +
"', so the load limit with automatic refresh will not be used.");
289 m_log.
WriteLine(
"WARNING: The shared testing data source '" + m_TestingImages.
Source.
Name +
"' appears to already be open with the '" + m_TestingImages.
LoadMethod.ToString() +
"', so the load limit with automatic refresh will not be used.");
293 m_nRefreshUpdatePeriod = nPeriodInMs;
294 m_dfRegreshReplacementPct = dfReplacementPct;
295 m_bRefreshTraining = bTraining;
296 m_bRefreshTesting = bTesting;
297 m_refreshScheduleThread =
new Thread(
new ParameterizedThreadStart(refreshSchedule));
298 m_evtRefreshScheduleCancel.Reset();
299 m_evtRefreshScheduleDone.Reset();
300 m_evtRefreshScheduleRunning.Reset();
301 m_refreshScheduleThread.Start(
new Tuple<int, double, bool, bool>(nPeriodInMs, dfReplacementPct, bTraining, bTesting));
302 return m_evtRefreshScheduleRunning.WaitOne(1000);
313 if (m_refreshScheduleThread ==
null)
316 if (m_evtRefreshScheduleRunning.WaitOne(0))
318 m_evtRefreshScheduleCancel.Set();
319 m_evtRefreshScheduleDone.WaitOne();
322 m_refreshScheduleThread =
null;
335 public bool GetAutomaticRefreshSchedule(out
int nPeriodInMs, out
double dfReplacementPct, out
int nTrainingRefreshCount, out
int nTestingRefreshCount)
338 dfReplacementPct = 0;
339 nTrainingRefreshCount = m_nTrainingRefreshCount;
340 nTestingRefreshCount = m_nTestingRefreshCount;
342 if (m_refreshScheduleThread ==
null)
345 if (!m_evtRefreshScheduleRunning.WaitOne(0))
348 nPeriodInMs = m_nRefreshUpdatePeriod;
349 dfReplacementPct = m_dfRegreshReplacementPct;
354 private void refreshSchedule(
object obj)
356 m_evtRefreshScheduleRunning.Set();
357 m_nTrainingRefreshCount = 0;
358 m_nTestingRefreshCount = 0;
362 Tuple<int, double, bool, bool> arg = obj as Tuple<int, double, bool, bool>;
363 int nPeriodMs = arg.Item1;
364 double dfReplacementPct = arg.Item2;
365 bool bTraining = arg.Item3;
366 bool bTesting = arg.Item4;
367 Stopwatch sw =
new Stopwatch();
371 m_log.
WriteLine(
"INFO: Starting refresh cycle on '" + m_ds.
Name +
"' and running every " + m_nRefreshUpdatePeriod.ToString(
"N0") +
" ms. with replacement percent = " + m_dfRegreshReplacementPct.ToString(
"P"));
373 while (!m_evtRefreshScheduleCancel.WaitOne(1000))
375 if (sw.ElapsedMilliseconds > m_nRefreshUpdatePeriod)
377 if (m_bRefreshTraining)
382 m_TrainingImages.
StartRefresh(m_dfRegreshReplacementPct);
383 m_nTrainingRefreshCount++;
391 if (m_bRefreshTesting)
396 m_TestingImages.
StartRefresh(m_dfRegreshReplacementPct);
397 m_nTestingRefreshCount++;
423 m_evtRefreshScheduleDone.Set();
424 m_evtRefreshScheduleRunning.Reset();
434 get {
return m_lDefaultQueryState; }
441 protected virtual void Dispose(
bool bDisposing)
447 if (m_TestingImages !=
null)
450 m_TestingImages =
null;
453 if (m_TrainingImages !=
null)
456 m_TrainingImages =
null;
459 if (m_factory !=
null)
495 m_lDefaultQueryState = lQueryState;
507 if (lHandle == m_lDefaultQueryState)
521 if (lQueryState == 0)
522 lQueryState = m_lDefaultQueryState;
548 List<DbItem> rgItems = m_TrainingImages.
Relabel(col);
551 rgItems = m_TrainingImages.
Relabel(col);
560 List<DbItem> rgItems = m_TrainingImages.
ResetLabels();
584 get {
return m_bUseTrainingImagesForTesting; }
585 set { m_bUseTrainingImagesForTesting = value; }
597 if (m_TestingImages.
Source.
ID != nSrcId &&
598 m_TrainingImages.
Source.
ID != nSrcId)
611 if (m_TestingImages.
Source.
ID != nSrcId &&
612 m_TrainingImages.
Source.
ID != nSrcId)
625 m_TestingImages.
Unload(bReload);
626 m_TrainingImages.
Unload(bReload);
643 dfTraining = (double)nTrainingLoaded / (
double)nTrainingTotal;
644 dfTesting = (double)nTestingLoaded / (
double)nTestingTotal;
646 int nTotalLoaded = nTrainingLoaded + nTestingLoaded;
647 int nTotalImages = nTrainingTotal + nTestingTotal;
649 return (
double)nTotalLoaded / (double)nTotalImages;
659 if (m_TestingImages.
Source.
ID == nSourceID)
661 if (m_bUseTrainingImagesForTesting)
662 return m_TrainingImages;
664 return m_TestingImages;
667 if (m_TrainingImages.
Source.
ID == nSourceID)
669 return m_TrainingImages;
684 if (m_bUseTrainingImagesForTesting)
685 return m_TrainingImages;
687 return m_TestingImages;
690 if (m_TrainingImages.
Source.
Name == strSource)
692 return m_TrainingImages;
711 get {
return (m_ds ==
null) ? 0 : m_ds.
ID; }
712 set { m_ds.
ID = value; }
720 get {
return m_nOriginalDsId; }
728 get {
return m_ds.
Name; }
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
TYPE
Defines the generic connection location
TYPE Location
Get/set the generic location type.
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.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
The SimpleDatum class holds a data input within host memory.
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.
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.
[V2 Image Database] The DatasetEx2 class provides the in-memory dataset functionality that is used by...
long CreateQueryState(bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.NONE)
Create a new QueryState on the dataset.
int? DatasetID
Returns the dataset ID of the dataset managesd by the DatasetEx object.
bool StartAutomaticRefreshSchedule(bool bTraining, bool bTesting, int nPeriodInMs, double dfReplacementPct)
Start the automatic refresh schedule on the training and/or testing data sources.
void Unload(bool bReload)
Unload the images of the training and testing image sets.
bool SetDefaultQueryState(long lQueryState)
Set the default query state to a new query state.
bool StopAutomaticRefreshSchedule(bool bTraining, bool bTesting)
Stop the automatic refresh schedule on the training and/or testing data sources.
void ResetLabels()
Resets the labels to their original labels.
bool UseTrainingImagesForTesting
Get/set whether or not to use the training images when testing.
QueryState FindQueryState(long lQueryState, ImageSet2.TYPE type)
Returns the query state for a given query state handle and type.
ImageSet2 Find(string strSource)
Returns the ImageSet corresponding to a data source name.
DatasetEx2(Guid user, DatasetFactory factory, CryptoRandom random)
The DatasetEx constructor.
bool FreeQueryState(long lHandle)
Free an existing query state.
void Relabel(LabelMappingCollection col)
Relabels both the testing and training image sets using the label mapping collection.
void StopRefresh(bool bTraining=true, bool bTesting=true)
Stop any refresh operation currently running.
ImageSet2 Find(int nSourceID)
Returns the ImageSet corresponding to a data source ID.
bool SaveImageMean(int nSrcId, SimpleDatum sd, bool bUpdate)
Saves the image mean in a SimpleDatum to the database.
bool GetAutomaticRefreshSchedule(out int nPeriodInMs, out double dfReplacementPct, out int nTrainingRefreshCount, out int nTestingRefreshCount)
Get the automatic refresh schedule status and its period and replacement percentage.
virtual void Dispose(bool bDisposing)
Releases all resources used.
void ReloadIndexing()
Reload the indexing for both the training and testing data sources.
int AddUser(Guid user)
Adds a user of the dataset.
int OriginalDatasetID
Returns the original DatsetID if this is a cloned re-organized dataset, otherwise 0 is returned.
bool WaitForLoadingToComplete(bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for either the training, testing or both data sources to complete loading.
long Initialize(DatasetDescriptor ds, WaitHandle[] rgAbort, int nPadW=0, int nPadH=0, Log log=null, DB_LOAD_METHOD loadMethod=DB_LOAD_METHOD.LOAD_ALL, bool bSkipMeanCheck=false, int nImageDbLoadLimit=0, int nImageDbAutoRefreshScheduledUpdateInMs=0, double dfImageDbAutoRefreshScheduledReplacementPct=0, bool bVerify=false)
Initialize the DatasetEx by loading the training and testing data sources into memory.
long DefaultQueryState
Returns the default query state created when first initializing the dataset.
void Dispose()
Releases all resources used.
bool WaitForRefreshToComplete(bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for either the training, testing or both data sources to complete refreshing.
int RemoveUser(Guid user)
Remove a user of the dataset.
DatasetDescriptor Descriptor
Returns the dataset descriptor of the dataset managesd by the DatasetEx object.
void ResetAllBoosts()
Reset all boosts for both the testing and training image sets.
bool IsRefreshRunning(bool bTraining, bool bTesting)
Returns whether or not the refresh is running on the training and/or testing data source.
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event is passed to each image set and fires each time the Image set need to ...
double GetPercentageLoaded(out double dfTraining, out double dfTesting)
Returns the total percentage of images loaded for testing, training and combined.
SimpleDatum QueryImageMean(int nSrcId)
Query the image mean for a data source.
void StartRefresh(bool bTraining=true, bool bTesting=true, double dfReplacementPct=0.25)
Start an image refresh on the training and/or testing data sources.
string DatasetName
Returns the dataset name of the dataset managesd by the DatasetEx object.
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.
void Dispose()
Releases all resources used.
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static ConnectInfo GlobalDatabaseConnectInfo
Get/set the global database connection info.
[V2 Image Database] The ImageSet2 manages the data source data including the master list of images,...
int GetLoadedCount()
Get the total number of images already loaded in the image set.
QueryState CreateQueryState(bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.NONE)
Create a new QueryState and optionally sort the results.
override void Dispose(bool bDisposing)
Releases the resouces used.
bool WaitForRefreshToComplete(int nWait=int.MaxValue)
Wait for the image refresh to complete loading.
DB_LOAD_METHOD LoadMethod
Get the image load method used on initialization.
List< DbItem > ResetLabels()
Reset all labels of the image set to the original labels.
bool StartRefresh(double dfReplacementPct=0.25)
Start the refresh process which only valid when initialized with LoadLimit > 0.
void Unload(bool bReload)
Unload all images from the master list (freeing memory) and optionally reload the dataset.
void SetImageMean(SimpleDatum sd, bool bSave=false)
Sets the data source image mean.
SimpleDatum GetImageMean(Log log, WaitHandle[] rgAbort, bool bQueryOnly)
Get the image mean for the iamge set, or create one if it does not exist.
bool WaitForLoadingToComplete(int nWait=int.MaxValue)
Wait for the image set to complete loading.
int GetTotalCount()
Get the total number of images in the image set whether loaded or not.
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event fires when the ImageSet needs to calculate the image mean for the imag...
bool IsRefreshRunning
Returns whether or not the refresh is running.
List< DbItem > ResetAllBoosts()
Reset all boosts to their original settings.
QueryState Initialize(bool bSilentLoad, bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, int nMaxLoadCount=0, bool bVerify=false)
Initialize the ImageSet by creating the master list of images, starting its background image loading ...
TYPE
Defines the type of image set.
List< DbItem > Relabel(LabelMappingCollection col)
Reset the image set based on the LabelMappingCollection.
List< DbItem > ReloadIndexing()
Reload the indexing for the image set.
void StopRefresh()
Abort any refresh currently running.
SourceDescriptor Source
Returns the data source of the image set.
The QueryStateCollection manages all query states used by matching the QueryState handles with the Qu...
void ReIndexTraining(List< DbItem > rgItems)
Relabels the training QueryState based on the DbItems.
QueryState GetTrainingState(long lHandle)
Returns the QueryState used with thet Training data source.
bool FreeQueryState(long lHandle)
Free the QueryStates associated with the handle and remove it from the handle list.
QueryState GetTestingState(long lHandle)
Returns the QueryState used with thet Testing data source.
void ReIndexTesting(List< DbItem > rgItems)
Relabels the testing QueryState based on the DbItems.
long CreateNewState(QueryState training, QueryState testing)
Create a new QueryState handle based on the QueryStates passed to this function.
Initially the QueryState is copied from the MasterIndexes and during each query is altered by removin...
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.
IMGDB_SORT
Defines the sorting method.
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-...