MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ImageSet2.cs
1using MyCaffe.basecode;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text;
7using System.Threading;
8using System.Threading.Tasks;
9
10namespace MyCaffe.db.image
11{
16 public class ImageSet2 : ImageSetBase, IDisposable
17 {
18 CryptoRandom m_random;
19 DB_LOAD_METHOD m_loadMethod;
20 MasterList m_masterList = null;
21 MasterIndexes m_masterIdx = null;
22 List<WaitHandle> m_rgAbort = new List<WaitHandle>();
23 ManualResetEvent m_evtCancel = new ManualResetEvent(false);
24 Log m_log;
25 TYPE m_type;
26
30 public event EventHandler<CalculateImageMeanArgs> OnCalculateImageMean;
31
35 public enum TYPE
36 {
40 TRAIN,
44 TEST
45 }
46
57 public ImageSet2(TYPE type, Log log, DatasetFactory factory, SourceDescriptor src, DB_LOAD_METHOD loadMethod, CryptoRandom random, WaitHandle[] rgAbort)
58 : base(factory, src)
59 {
60 m_type = type;
61 m_log = log;
62 m_loadMethod = loadMethod;
63 m_random = random;
64
65 m_rgAbort.Add(m_evtCancel);
66 if (rgAbort.Length > 0)
67 m_rgAbort.AddRange(rgAbort);
68 }
69
74 protected override void Dispose(bool bDisposing)
75 {
76 m_evtCancel.Set();
77
78 if (m_masterIdx != null)
79 {
80 m_masterIdx.Dispose();
81 m_masterIdx = null;
82 }
83
84 if (m_masterList != null)
85 {
86 m_masterList.Dispose();
87 m_masterList = null;
88 }
89
90 base.Dispose(bDisposing);
91 }
92
102 public QueryState Initialize(bool bSilentLoad, bool bUseUniqueLabelIndexes = true, bool bUseUniqueImageIndexes = true, int nMaxLoadCount = 0, bool bVerify = false)
103 {
104 if (m_masterList == null)
105 {
106 m_masterList = new MasterList(m_random, m_log, m_src, m_factory, m_rgAbort, nMaxLoadCount);
107
108 if (OnCalculateImageMean != null)
110
111 if (m_loadMethod == DB_LOAD_METHOD.LOAD_ALL || m_loadMethod == DB_LOAD_METHOD.LOAD_EXTERNAL || m_loadMethod == DB_LOAD_METHOD.LOAD_ON_DEMAND_BACKGROUND)
112 m_masterList.Load(bSilentLoad);
113 }
114
115 if (m_masterIdx == null || m_masterIdx.LoadLimit != nMaxLoadCount)
116 m_masterIdx = new MasterIndexes(m_random, m_src, nMaxLoadCount);
117
118 QueryState state = new QueryState(m_masterIdx, bUseUniqueLabelIndexes, bUseUniqueImageIndexes);
119
120 if (m_loadMethod == DB_LOAD_METHOD.LOAD_ALL)
121 {
122 m_masterList.WaitForLoadingToComplete(m_rgAbort);
123
124 if (bVerify)
125 m_masterList.Verify(m_masterIdx);
126 }
127
128 return state;
129 }
130
136 public bool WaitForLoadingToComplete(int nWait = int.MaxValue)
137 {
138 return m_masterList.WaitForLoadingToComplete(m_rgAbort, nWait);
139 }
140
145 {
146 get { return m_loadMethod; }
147 }
148
153 {
154 get { return m_masterList.IsRefreshRunning; }
155 }
156
162 public bool StartRefresh(double dfReplacementPct = 0.25)
163 {
164 return m_masterList.StartRefresh(dfReplacementPct);
165 }
166
172 public bool WaitForRefreshToComplete(int nWait = int.MaxValue)
173 {
174 return m_masterList.WaitForRefreshToComplete(m_rgAbort, nWait);
175 }
176
180 public void StopRefresh()
181 {
182 m_masterList.StopRefresh();
183 }
184
189 {
190 get { return m_type; }
191 }
192
197 public bool Load()
198 {
199 return m_masterList.Load();
200 }
201
206 public void Unload(bool bReload)
207 {
208 m_masterList.Unload(bReload);
209 }
210
218 public QueryState CreateQueryState(bool bUseUniqueLabelIndexes = true, bool bUseUniqueImageIndexes = true, IMGDB_SORT sort = IMGDB_SORT.NONE)
219 {
220 return new QueryState(m_masterIdx, bUseUniqueLabelIndexes, bUseUniqueImageIndexes, sort);
221 }
222
227 public int GetTotalCount()
228 {
229 return m_masterList.GetTotalCount();
230 }
231
236 public int GetLoadedCount()
237 {
238 return m_masterList.GetLoadedCount();
239 }
240
248 public SimpleDatum GetImageMean(Log log, WaitHandle[] rgAbort, bool bQueryOnly)
249 {
250 return m_masterList.GetImageMean(log, rgAbort, bQueryOnly);
251 }
252
257 public List<DbItem> ReloadIndexing()
258 {
259 List<DbItem> rgItems = m_masterList.ReloadIndexing();
260 m_masterIdx.Reload(rgItems);
261 return rgItems;
262 }
263
268 public List<DbItem> ResetLabels()
269 {
270 List<DbItem> rgItems = m_masterList.ResetLabels();
271 m_masterIdx.Reload(rgItems);
272 return rgItems;
273 }
274
280 public List<DbItem> Relabel(LabelMappingCollection col)
281 {
282 List<DbItem> rgItems = m_masterList.Relabel(col);
283 m_masterIdx.Reload(rgItems);
284 return rgItems;
285 }
286
291 public List<DbItem> ResetAllBoosts()
292 {
293 List<DbItem> rgItems = m_masterList.ResetAllBoosts();
294 m_masterIdx.Reload(rgItems);
295 return rgItems;
296 }
297
302 public List<LabelDescriptor> GetLabels()
303 {
304 return m_src.Labels;
305 }
306
312 public string GetLabelName(int nLabel)
313 {
314 foreach (LabelDescriptor label in m_src.Labels)
315 {
316 if (label.ActiveLabel == nLabel)
317 return label.Name;
318 }
319
320 return nLabel.ToString();
321 }
322
328 public void SetImageMean(SimpleDatum sd, bool bSave = false)
329 {
330 m_masterList.SetImageMean(sd, bSave);
331 }
332
339 public int FindImageIndex(DateTime dt, string strDesc)
340 {
341 List<DbItem> rgItems = m_masterIdx.FindImageIndexes(dt);
342 if (rgItems.Count == 0)
343 return -1;
344
345 return m_masterList.FindImageIndex(rgItems, strDesc);
346 }
347
353 public SimpleDatum GetImage(int nImageId)
354 {
355 return m_masterList.FindImage(nImageId);
356 }
357
370 public SimpleDatum GetImage(QueryState state, DB_LABEL_SELECTION_METHOD labelSelectionMethod, DB_ITEM_SELECTION_METHOD imageSelectionMethod, Log log, int? nLabel = null, int nDirectIdx = -1, bool bLoadDataCriteria = false, bool bLoadDebugData = false)
371 {
372 if ((imageSelectionMethod & DB_ITEM_SELECTION_METHOD.BOOST) == DB_ITEM_SELECTION_METHOD.BOOST &&
373 (labelSelectionMethod & DB_LABEL_SELECTION_METHOD.RANDOM) == DB_LABEL_SELECTION_METHOD.RANDOM)
374 labelSelectionMethod |= DB_LABEL_SELECTION_METHOD.BOOST;
375
376 if (!nLabel.HasValue && (labelSelectionMethod & DB_LABEL_SELECTION_METHOD.RANDOM) == DB_LABEL_SELECTION_METHOD.RANDOM)
377 nLabel = state.GetNextLabel(labelSelectionMethod);
378
379 int? nIdx = state.GetNextImage(imageSelectionMethod, nLabel, nDirectIdx);
380
381 if (!nIdx.HasValue || nIdx.Value < 0)
382 {
383 nIdx = state.GetNextImage(imageSelectionMethod, nLabel, nDirectIdx);
384 if (!nIdx.HasValue || nIdx.Value < 0)
385 {
386 string strBoosted = ((imageSelectionMethod & DB_ITEM_SELECTION_METHOD.BOOST) == DB_ITEM_SELECTION_METHOD.BOOST) ? "Boosted" : "";
387 string strLabel = (nLabel.HasValue) ? " for label '" + nLabel.Value.ToString() + "'." : ".";
388 throw new Exception("Failed to find the image index! The data source '" + m_src.Name + "' has no " + strBoosted + " images" + strLabel + ". You may need to re-index the dataset.");
389 }
390 }
391
392 SimpleDatum sd = m_masterList.GetImage(nIdx.Value, bLoadDataCriteria, bLoadDebugData, m_loadMethod);
393 state.UpdateStats(sd);
394
395 return sd;
396 }
397
408 public int GetCount(QueryState state, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
409 {
410 return m_masterList.GetCount(state, strFilterVal, nBoostVal, bBoostValIsExact);
411 }
412
426 public List<SimpleDatum> GetImages(QueryState state, int nStartIdx, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false, bool bAttemptDirectLoad = false)
427 {
428 return m_masterList.GetImages(state, nStartIdx, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact, bAttemptDirectLoad);
429 }
430
443 public List<SimpleDatum> GetImages(QueryState state, DateTime dtStart, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
444 {
445 return m_masterList.GetImages(state, dtStart, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact);
446 }
447
458 public List<SimpleDatum> GetImages(bool bSuperboostOnly, string strFilterVal, int? nBoostVal, int[] rgIdx)
459 {
460 return m_masterList.GetImages(bSuperboostOnly, strFilterVal, nBoostVal, rgIdx);
461 }
462
469 public List<SimpleResult> GetAllResults(bool bRequireExtraData, int nMax)
470 {
471 return m_factory.QueryAllResults(m_src.ID, bRequireExtraData, nMax);
472 }
473 }
474}
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
The LabelMappingCollection manages a collection of LabelMapping's.
Definition: LabelMapping.cs:15
The Log class provides general output in text form.
Definition: Log.cs:13
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
int ID
Get/set the database ID of the item.
string Name
Get/set the name of the item.
The LabelDescriptor class describes a single label.
int ActiveLabel
Specifies the active label (used during training).
string Name
Specifies the label name.
The SourceDescriptor class contains all information describing a data source.
List< LabelDescriptor > Labels
Get/set the list of LabelDescriptors that describe the labels used by the data items.
The DatasetFactory manages the connection to the Database object.
List< SimpleResult > QueryAllResults(int nSrcID, bool bRequireExtraData=false, int nMax=-1)
Load all results for a given data source.
[V2 Image Database] The ImageSet2 manages the data source data including the master list of images,...
Definition: ImageSet2.cs:17
int GetLoadedCount()
Get the total number of images already loaded in the image set.
Definition: ImageSet2.cs:236
QueryState CreateQueryState(bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.NONE)
Create a new QueryState and optionally sort the results.
Definition: ImageSet2.cs:218
ImageSet2(TYPE type, Log log, DatasetFactory factory, SourceDescriptor src, DB_LOAD_METHOD loadMethod, CryptoRandom random, WaitHandle[] rgAbort)
The ImageSet2 constructor.
Definition: ImageSet2.cs:57
override void Dispose(bool bDisposing)
Releases the resouces used.
Definition: ImageSet2.cs:74
bool WaitForRefreshToComplete(int nWait=int.MaxValue)
Wait for the image refresh to complete loading.
Definition: ImageSet2.cs:172
SimpleDatum GetImage(int nImageId)
Get the image at a given image ID.
Definition: ImageSet2.cs:353
DB_LOAD_METHOD LoadMethod
Get the image load method used on initialization.
Definition: ImageSet2.cs:145
List< DbItem > ResetLabels()
Reset all labels of the image set to the original labels.
Definition: ImageSet2.cs:268
bool StartRefresh(double dfReplacementPct=0.25)
Start the refresh process which only valid when initialized with LoadLimit > 0.
Definition: ImageSet2.cs:162
List< SimpleDatum > GetImages(QueryState state, int nStartIdx, int nQueryCount=int.MaxValue, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false, bool bAttemptDirectLoad=false)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
Definition: ImageSet2.cs:426
void Unload(bool bReload)
Unload all images from the master list (freeing memory) and optionally reload the dataset.
Definition: ImageSet2.cs:206
bool Load()
Starts loading the image set on the background thread if it is not already loading.
Definition: ImageSet2.cs:197
List< LabelDescriptor > GetLabels()
Returns a list of all labels used by the data source.
Definition: ImageSet2.cs:302
void SetImageMean(SimpleDatum sd, bool bSave=false)
Sets the data source image mean.
Definition: ImageSet2.cs:328
SimpleDatum GetImageMean(Log log, WaitHandle[] rgAbort, bool bQueryOnly)
Get the image mean for the iamge set, or create one if it does not exist.
Definition: ImageSet2.cs:248
bool WaitForLoadingToComplete(int nWait=int.MaxValue)
Wait for the image set to complete loading.
Definition: ImageSet2.cs:136
SimpleDatum GetImage(QueryState state, DB_LABEL_SELECTION_METHOD labelSelectionMethod, DB_ITEM_SELECTION_METHOD imageSelectionMethod, Log log, int? nLabel=null, int nDirectIdx=-1, bool bLoadDataCriteria=false, bool bLoadDebugData=false)
Returns the image based on its label and image selection method.
Definition: ImageSet2.cs:370
int GetCount(QueryState state, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the number of images in the image set, optionally with super-boost only.
Definition: ImageSet2.cs:408
int GetTotalCount()
Get the total number of images in the image set whether loaded or not.
Definition: ImageSet2.cs:227
string GetLabelName(int nLabel)
Return the label name of a given label.
Definition: ImageSet2.cs:312
List< SimpleDatum > GetImages(QueryState state, DateTime dtStart, int nQueryCount=int.MaxValue, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
Definition: ImageSet2.cs:443
int FindImageIndex(DateTime dt, string strDesc)
Find the index of an image with the tiven date and (optionally) description.
Definition: ImageSet2.cs:339
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event fires when the ImageSet needs to calculate the image mean for the imag...
Definition: ImageSet2.cs:30
bool IsRefreshRunning
Returns whether or not the refresh is running.
Definition: ImageSet2.cs:153
List< SimpleResult > GetAllResults(bool bRequireExtraData, int nMax)
Returns all results from the RawImageResults table.
Definition: ImageSet2.cs:469
List< SimpleDatum > GetImages(bool bSuperboostOnly, string strFilterVal, int? nBoostVal, int[] rgIdx)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
Definition: ImageSet2.cs:458
List< DbItem > ResetAllBoosts()
Reset all boosts to their original settings.
Definition: ImageSet2.cs:291
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 ...
Definition: ImageSet2.cs:102
TYPE
Defines the type of image set.
Definition: ImageSet2.cs:36
TYPE ImageSetType
Returns the type of use for the ImageSet.
Definition: ImageSet2.cs:189
List< DbItem > Relabel(LabelMappingCollection col)
Reset the image set based on the LabelMappingCollection.
Definition: ImageSet2.cs:280
List< DbItem > ReloadIndexing()
Reload the indexing for the image set.
Definition: ImageSet2.cs:257
void StopRefresh()
Abort any refresh currently running.
Definition: ImageSet2.cs:180
The ImageSetBase class contains the list of image for a data source as well as a list of LabelSets th...
Definition: ImageSetBase.cs:17
DatasetFactory m_factory
Specifies the DatasetFactory used to work with the underlying database.
Definition: ImageSetBase.cs:21
SourceDescriptor m_src
Specifies the data source used with this Image Set.
Definition: ImageSetBase.cs:25
The MasterIndexes stores the indexes that define the index structure of the data source data.
void Dispose()
Release all resources used.
void Reload(List< DbItem > rgItems)
Reload all images by re-loading the master index list.
int LoadLimit
Returns the load limit set during initialization.
List< DbItem > FindImageIndexes(DateTime dt)
Returns all DbItems that point to images iwth a given date.
The MasterList is responsible for loading and managing access to the master list of images for a data...
Definition: MasterList.cs:17
bool Load(bool bSilent=false)
Start loading the dataset.
Definition: MasterList.cs:145
List< DbItem > ResetAllBoosts()
Reset all image boosts.
Definition: MasterList.cs:421
int FindImageIndex(List< DbItem > rgItems, string strDesc)
Find the image index based by searching the rgItems for an image that contains the description specif...
Definition: MasterList.cs:441
SimpleDatum FindImage(int nImageId)
Find an image based on its image ID (e.g. the image ID in the database).
Definition: MasterList.cs:469
bool IsRefreshRunning
Returns true if the refresh is running, false otherwise.
Definition: MasterList.cs:264
SimpleDatum GetImageMean(Log log, WaitHandle[] rgAbort, bool bQueryOnly)
Returns the image mean for the ImageSet.
Definition: MasterList.cs:324
bool WaitForRefreshToComplete(List< WaitHandle > rgAbort, int nWait)
Wait for the refres to complete.
Definition: MasterList.cs:230
void StopRefresh()
Stop the refresh thread if running.
Definition: MasterList.cs:274
List< DbItem > ReloadIndexing()
Reload the image indexing.
Definition: MasterList.cs:375
void Dispose()
Release all resources used.
Definition: MasterList.cs:82
bool WaitForLoadingToComplete(List< WaitHandle > rgAbort, int nWait=int.MaxValue)
Wait for the image loading to complete - this is used when performing LOAD_ALL.
Definition: MasterList.cs:671
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event fires when the ImageSet needs to calculate the image mean for the imag...
Definition: MasterList.cs:44
bool StartRefresh(double dfReplacementPct=0.25)
Start the refresh thread which will run if the number of images stored in memory is less than the tot...
Definition: MasterList.cs:199
int GetCount(QueryState state, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the number of images in the image set, optionally with super-boosted values only.
Definition: MasterList.cs:573
int GetTotalCount()
Return the total number of images whether loaded or not, in the data source.
Definition: MasterList.cs:300
List< DbItem > Relabel(LabelMappingCollection col)
Relabel the images based on the LabelMappingCollection.
Definition: MasterList.cs:403
void Unload(bool bReLoad)
Unload the data source images.
Definition: MasterList.cs:168
SimpleDatum GetImage(int nIdx, bool bLoadDataCriteria, bool bLoadDebugData, DB_LOAD_METHOD loadMethod)
Get the image with a specific image index.
Definition: MasterList.cs:524
void Verify(MasterIndexes idx)
Verify the loaded images against the master indexes.
Definition: MasterList.cs:97
List< DbItem > ResetLabels()
Reset the labels of all images to the original labels.
Definition: MasterList.cs:384
List< SimpleDatum > GetImages(QueryState state, int nStartIdx, int nQueryCount=int.MaxValue, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false, bool bAttemptDirectLoad=false)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
Definition: MasterList.cs:592
void SetImageMean(SimpleDatum d, bool bSave=false)
Set the image mean.
Definition: MasterList.cs:288
int GetLoadedCount()
Return the currently loaded images in the data source.
Definition: MasterList.cs:312
Initially the QueryState is copied from the MasterIndexes and during each query is altered by removin...
Definition: QueryState.cs:19
int? GetNextImage(DB_ITEM_SELECTION_METHOD imgSel, int? nLabel, int nDirectIdx)
Returns the next image in the Index set based on the selection criteria.
Definition: QueryState.cs:100
void UpdateStats(SimpleDatum sd)
Update the label stats.
Definition: QueryState.cs:49
int? GetNextLabel(DB_LABEL_SELECTION_METHOD lblSel)
Returns the next label in the Index set selected based on the selection criteria.
Definition: QueryState.cs:60
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.
Definition: Annotation.cs:12
DB_ITEM_SELECTION_METHOD
Defines the item (e.g., image or temporal item) selection method.
Definition: Interfaces.cs:278
DB_LOAD_METHOD
Defines how to laod the items into the in-memory database.
Definition: Interfaces.cs:154
@ TEST
Run a testing phase.
@ TRAIN
Run a training phase.
IMGDB_SORT
Defines the sorting method.
Definition: Interfaces.cs:358
DB_LABEL_SELECTION_METHOD
Defines the label selection method.
Definition: Interfaces.cs:333
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12