MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MyCaffeImageDatabase2.cs
1using System;
2using System.Collections.Generic;
4using System.Diagnostics;
5using System.Linq;
6using System.Text;
7using System.Drawing;
8using System.Threading;
9using MyCaffe.basecode;
11
15namespace MyCaffe.db.image
16{
28 {
29 CryptoRandom m_random = null;
30 DatasetFactory m_factory;
31 string m_strID = "";
32 int m_nStrIDHashCode = 0;
33 EventWaitHandle m_evtInitializing = null;
34 EventWaitHandle m_evtInitialized = null;
35 EventWaitHandle m_evtAbortInitialization = null;
36 bool m_bEnabled = false;
37 static object m_syncObject = new object();
38 static Dictionary<int, DatasetExCollection2> m_colDatasets = new Dictionary<int, DatasetExCollection2>();
39 static Dictionary<int, LabelMappingCollection> m_rgLabelMappings = new Dictionary<int, LabelMappingCollection>();
40 Dictionary<int, SimpleDatum> m_rgMeanCache = new Dictionary<int, SimpleDatum>();
41 double m_dfSuperBoostProbability = 0;
42 DB_ITEM_SELECTION_METHOD m_imageSelectionMethod = DB_ITEM_SELECTION_METHOD.RANDOM;
43 DB_LABEL_SELECTION_METHOD m_labelSelectionMethod = DB_LABEL_SELECTION_METHOD.RANDOM;
44 Log m_log;
45 DB_LOAD_METHOD m_loadMethod = DB_LOAD_METHOD.LOAD_ON_DEMAND;
46 int m_nLoadLimit = 0;
47 bool m_bSkipMeanCheck = false;
48 int m_nPadW = 0;
49 int m_nPadH = 0;
50 Guid m_userGuid;
51
52
56 public event EventHandler<CalculateImageMeanArgs> OnCalculateImageMean;
57
64 public MyCaffeImageDatabase2(Log log = null, string strId = "default", int nSeed = 0)
65 {
66 m_factory = new DatasetFactory();
67 m_userGuid = Guid.NewGuid();
68 m_log = log;
69 InitializeComponent();
70 init(strId, nSeed);
71
72 if (log != null)
73 log.WriteLine("INFO: Using MyCaffe Image Database VERSION 2.");
74 }
75
80 public MyCaffeImageDatabase2(IContainer container)
81 {
82 container.Add(this);
83
84 InitializeComponent();
85 init();
86 }
87
93 {
94 return DB_VERSION.IMG_V2;
95 }
96
97 #region Initialization and Cleanup
98
104 {
106 }
107
115 public long InitializeWithDsName(SettingsCaffe s, string strDs, string strEvtCancel = null)
116 {
117 return InitializeWithDs(s, new DatasetDescriptor(strDs), strEvtCancel);
118 }
119
127 public long InitializeWithDs(SettingsCaffe s, DatasetDescriptor ds, string strEvtCancel = null)
128 {
129 string strDsName = ds.Name;
130
131 if (String.IsNullOrEmpty(strDsName))
132 {
134 if (strDsName == null)
135 throw new Exception("Could not find the dataset! You must specify either the Datast name or at least the training or testing source name!");
136 }
137
138 int nDsId = m_factory.GetDatasetID(strDsName);
139
140 return InitializeWithDsId(s, nDsId, strEvtCancel);
141 }
142
152 public long InitializeWithDsId(SettingsCaffe s, int nDataSetID, string strEvtCancel = null, int nPadW = 0, int nPadH = 0)
153 {
154 Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD> selMethod = GetSelectionMethod(s);
155
156 m_nPadW = nPadW;
157 m_nPadH = nPadH;
158 m_labelSelectionMethod = selMethod.Item1;
159 m_imageSelectionMethod = selMethod.Item2;
160 m_dfSuperBoostProbability = s.SuperBoostProbability;
161 m_loadMethod = s.DbLoadMethod;
162 m_nLoadLimit = s.DbLoadLimit;
163 m_bSkipMeanCheck = s.SkipMeanCheck;
164
165 if (m_loadMethod == DB_LOAD_METHOD.LOAD_EXTERNAL)
166 m_loadMethod = DB_LOAD_METHOD.LOAD_ON_DEMAND_BACKGROUND;
167
168 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtInitialized, m_evtInitializing, m_evtAbortInitialization }, 0);
169
170 lock (m_syncObject)
171 {
172 if (nWait != WaitHandle.WaitTimeout)
173 {
174 if (nWait == 0) // already initialized.
175 {
176 if (m_colDatasets.ContainsKey(m_nStrIDHashCode))
177 {
178 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
179
180 if (m_log != null)
181 m_log.WriteLine("The MyCaffe Image Database is already initialized.");
182
183 DatasetEx2 ds = col.FindDataset(nDataSetID);
184 if (ds != null)
185 {
186 ds.AddUser(m_userGuid);
187 return ds.DefaultQueryState;
188 }
189 }
190 }
191 else
192 {
193 return 0;
194 }
195 }
196
197 try
198 {
199 m_evtInitializing.Set();
200
202
203 DatasetDescriptor ds = m_factory.LoadDataset(nDataSetID);
204 if (ds == null)
205 throw new Exception("Could not find dataset with ID = " + nDataSetID.ToString());
206
207 List<WaitHandle> rgAbort = new List<WaitHandle>() { m_evtAbortInitialization };
208 CancelEvent evtCancel;
209
210 if (strEvtCancel != null)
211 {
212 evtCancel = new CancelEvent(strEvtCancel);
213 rgAbort.AddRange(evtCancel.Handles);
214 }
215
216 DatasetExCollection2 col = null;
217
218 if (m_colDatasets.ContainsKey(m_nStrIDHashCode))
219 col = m_colDatasets[m_nStrIDHashCode];
220 else
221 col = new DatasetExCollection2();
222
223 if (m_evtAbortInitialization.WaitOne(0))
224 {
225 col.Dispose();
226 return 0;
227 }
228
229 DatasetEx2 ds0 = new DatasetEx2(m_userGuid, m_factory, m_random);
230
231 if (OnCalculateImageMean != null)
233
234 if (m_log != null)
235 m_log.WriteLine("Loading dataset '" + ds.Name + "'...", true);
236
237 long lQueryHandle = ds0.Initialize(ds, rgAbort.ToArray(), nPadW, nPadH, m_log, m_loadMethod, m_bSkipMeanCheck, m_nLoadLimit, s.DbAutoRefreshScheduledUpdateInMs, s.DbAutoRefreshScheduledReplacementPercent, s.VeriyDatasetOnLoad);
238 if (lQueryHandle == 0)
239 {
240 col.Dispose();
241 return 0;
242 }
243
244 if (m_log != null)
245 m_log.WriteLine("Dataset '" + ds.Name + "' loaded.", true);
246
247 col.Add(ds0);
248
249 if (!m_colDatasets.ContainsKey(m_nStrIDHashCode))
250 m_colDatasets.Add(m_nStrIDHashCode, col);
251
253
254 return lQueryHandle;
255 }
256 finally
257 {
258 m_evtInitialized.Set();
259 m_evtInitializing.Reset();
260 }
261 }
262 }
263
272 public bool InitializeWithDsName1(SettingsCaffe s, string strDs, string strEvtCancel = null, PropertySet prop = null)
273 {
274 long lHandle = InitializeWithDsName(s, strDs, strEvtCancel);
275 if (lHandle == 0)
276 return false;
277
278 return true;
279 }
280
289 public bool InitializeWithDs1(SettingsCaffe s, DatasetDescriptor ds, string strEvtCancel = null, PropertySet prop = null)
290 {
291 long lHandle = InitializeWithDs(s, ds, strEvtCancel);
292 if (lHandle == 0)
293 return false;
294
295 return true;
296 }
297
308 public bool InitializeWithDsId1(SettingsCaffe s, int nDataSetID, string strEvtCancel = null, int nPadW = 0, int nPadH = 0, PropertySet prop = null)
309 {
310 long lHandle = InitializeWithDsId(s, nDataSetID, strEvtCancel, nPadW, nPadH);
311 if (lHandle == 0)
312 return false;
313
314 return true;
315 }
316
317 private void init(string strId = "", int nSeed = 0)
318 {
319 int nProcessID = Process.GetCurrentProcess().Id;
320
321 m_random = new CryptoRandom(CryptoRandom.METHOD.DEFAULT, nSeed);
322 m_strID = strId;
323 m_nStrIDHashCode = strId.GetHashCode();
324 m_evtInitializing = new EventWaitHandle(false, EventResetMode.ManualReset, "__CAFFE_IMAGEDATABASE__INITIALIZING__" + nProcessID.ToString());
325 m_evtInitialized = new EventWaitHandle(false, EventResetMode.ManualReset, "__CAFFE_IMAGEDATABASE__INITIALIZED__" + nProcessID.ToString());
326 m_evtAbortInitialization = new EventWaitHandle(false, EventResetMode.ManualReset, "__CAFFE_IMAGEDATABASE__ABORT_INITIALIZE__" + nProcessID.ToString());
327 }
328
334 public void CleanUp(int nDsId = 0, bool bForce = false)
335 {
336 lock (m_syncObject)
337 {
338 if (m_evtInitializing != null && m_evtInitializing.WaitOne(0))
339 {
340 m_evtAbortInitialization.Set();
341 return;
342 }
343
344 List<int> rgRemove = new List<int>();
345
346 foreach (KeyValuePair<int, DatasetExCollection2> col in m_colDatasets)
347 {
348 DatasetExCollection2 colDs = col.Value;
349
350 if (colDs.RemoveUser(m_userGuid) || bForce)
351 {
352 rgRemove.Add(col.Key);
353 colDs.Dispose();
354 }
355 }
356
357 foreach (int nKey in rgRemove)
358 {
359 m_colDatasets.Remove(nKey);
360 }
361
362 if (m_colDatasets.Count == 0)
363 {
364 if (m_evtInitialized.WaitOne(0))
365 m_evtInitialized.Reset();
366 }
367 }
368 }
369
370 private void dispose()
371 {
372 CleanUp(0, false);
373
374 if (m_evtInitialized != null)
375 {
376 m_evtInitialized.Dispose();
377 m_evtInitialized = null;
378 }
379
380 if (m_evtInitializing != null)
381 {
382 m_evtInitializing.Dispose();
383 m_evtInitializing = null;
384 }
385
386 if (m_evtAbortInitialization != null)
387 {
388 m_evtAbortInitialization.Dispose();
389 m_evtAbortInitialization = null;
390 }
391
392 if (m_random != null)
393 {
394 m_random.Dispose();
395 m_random = null;
396 }
397 }
398
407 public bool WaitForDatasetToLoad(int nDsId, bool bTraining, bool bTesting, int nWait = int.MaxValue)
408 {
409 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
410 if (nWait1 == 0)
411 return false;
412
413 return m_colDatasets[m_nStrIDHashCode].WaitForDatasetToLoad(nDsId, bTraining, bTesting, nWait);
414 }
415
424 public bool WaitForDatasetToLoad(string strDs, bool bTraining, bool bTesting, int nWait = int.MaxValue)
425 {
426 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
427 if (nWait1 == 0)
428 return false;
429
430 return m_colDatasets[m_nStrIDHashCode].WaitForDatasetToLoad(strDs, bTraining, bTesting, nWait);
431 }
432
438 public bool ReloadIndexing(int nDsId)
439 {
440 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
441 if (nWait1 == 0)
442 return false;
443
444 m_colDatasets[m_nStrIDHashCode].ReloadIndexing(nDsId);
445 return true;
446 }
447
453 public bool ReloadIndexing(string strDs)
454 {
455 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
456 if (nWait1 == 0)
457 return false;
458
459 m_colDatasets[m_nStrIDHashCode].ReloadIndexing(strDs);
460 return true;
461 }
462
468 public static void CreateDatabase(ConnectInfo ci, string strPath)
469 {
470 if (ci == null)
472
473 DatabaseManagement dbMgr = new DatabaseManagement(ci, strPath);
474 Exception excpt = dbMgr.CreateDatabase();
475
476 if (excpt != null)
477 throw excpt;
478 }
479
480 #endregion // Initialization and Cleanup
481
482 #region Refreshing
483
492 public bool WaitForDatasetToRefresh(int nDsId, bool bTraining, bool bTesting, int nWait = int.MaxValue)
493 {
494 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
495 if (nWait1 == 0)
496 return false;
497
498 return m_colDatasets[m_nStrIDHashCode].WaitForDatasetToRefresh(nDsId, bTraining, bTesting, nWait);
499 }
500
509 public bool WaitForDatasetToRefresh(string strDs, bool bTraining, bool bTesting, int nWait = int.MaxValue)
510 {
511 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
512 if (nWait1 == 0)
513 return false;
514
515 return m_colDatasets[m_nStrIDHashCode].WaitForDatasetToRefresh(strDs, bTraining, bTesting, nWait);
516 }
517
525 public bool IsRefreshRunning(int nDsId, bool bTraining, bool bTesting)
526 {
527 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
528 if (nWait1 == 0)
529 return false;
530
531 return m_colDatasets[m_nStrIDHashCode].IsRefreshRunning(nDsId, bTraining, bTesting);
532 }
533
541 public bool IsRefreshRunning(string strDs, bool bTraining, bool bTesting)
542 {
543 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
544 if (nWait1 == 0)
545 return false;
546
547 return m_colDatasets[m_nStrIDHashCode].IsRefreshRunning(strDs, bTraining, bTesting);
548 }
549
561 public bool StartRefresh(string strDs, bool bTraining, bool bTesting, double dfReplacementPct)
562 {
563 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
564 if (nWait1 == 0)
565 return false;
566
567 return m_colDatasets[m_nStrIDHashCode].StartRefresh(strDs, bTraining, bTesting, dfReplacementPct);
568 }
569
580 public bool StopRefresh(string strDs, bool bTraining, bool bTesting)
581 {
582 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
583 if (nWait1 == 0)
584 return false;
585
586 return m_colDatasets[m_nStrIDHashCode].StopRefresh(strDs, bTraining, bTesting);
587 }
588
600 public bool StartRefresh(int nDsID, bool bTraining, bool bTesting, double dfReplacementPct)
601 {
602 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
603 if (nWait1 == 0)
604 return false;
605
606 return m_colDatasets[m_nStrIDHashCode].StartRefresh(nDsID, bTraining, bTesting, dfReplacementPct);
607 }
608
619 public bool StopRefresh(int nDsID, bool bTraining, bool bTesting)
620 {
621 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
622 if (nWait1 == 0)
623 return false;
624
625 return m_colDatasets[m_nStrIDHashCode].StopRefresh(nDsID, bTraining, bTesting);
626 }
627
637 public bool StartAutomaticRefreshSchedule(string strDs, bool bTraining, bool bTesting, int nPeriodInMs, double dfReplacementPct)
638 {
639 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
640 if (nWait1 == 0)
641 return false;
642
643 if (nPeriodInMs <= 0 || dfReplacementPct <= 0)
644 return false;
645
646 return m_colDatasets[m_nStrIDHashCode].StartAutomaticRefreshSchedule(strDs, bTraining, bTesting, nPeriodInMs, dfReplacementPct);
647 }
648
656 public bool StopAutomaticRefreshSchedule(string strDs, bool bTraining, bool bTesting)
657 {
658 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
659 if (nWait1 == 0)
660 return false;
661
662 return m_colDatasets[m_nStrIDHashCode].StopAutomaticRefreshSchedule(strDs, bTraining, bTesting);
663 }
664
674 public bool GetScheduledAutoRefreshInformation(string strDs, out int nPeriodInMs, out double dfReplacementPct, out int nTrainingRefreshCount, out int nTestingRefreshCount)
675 {
676 nPeriodInMs = 0;
677 dfReplacementPct = 0;
678 nTrainingRefreshCount = 0;
679 nTestingRefreshCount = 0;
680
681 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
682 if (nWait1 == 0)
683 return false;
684
685 return m_colDatasets[m_nStrIDHashCode].GetScheduledAutoRefreshInformation(strDs, out nPeriodInMs, out dfReplacementPct, out nTrainingRefreshCount, out nTestingRefreshCount);
686 }
687
697 public bool StartAutomaticRefreshSchedule(int nDsID, bool bTraining, bool bTesting, int nPeriodInMs, double dfReplacementPct)
698 {
699 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
700 if (nWait1 == 0)
701 return false;
702
703 if (nPeriodInMs <= 0 || dfReplacementPct <= 0)
704 return false;
705
706 return m_colDatasets[m_nStrIDHashCode].StartAutomaticRefreshSchedule(nDsID, bTraining, bTesting, nPeriodInMs, dfReplacementPct);
707 }
708
716 public bool StopAutomaticRefreshSchedule(int nDsID, bool bTraining, bool bTesting)
717 {
718 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
719 if (nWait1 == 0)
720 return false;
721
722 return m_colDatasets[m_nStrIDHashCode].StopAutomaticRefreshSchedule(nDsID, bTraining, bTesting);
723 }
724
734 public bool GetScheduledAutoRefreshInformation(int nDsID, out int nPeriodInMs, out double dfReplacementPct, out int nTrainingRefreshCount, out int nTestingRefreshCount)
735 {
736 nPeriodInMs = 0;
737 dfReplacementPct = 0;
738 nTrainingRefreshCount = 0;
739 nTestingRefreshCount = 0;
740
741 int nWait1 = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
742 if (nWait1 == 0)
743 return false;
744
745 return m_colDatasets[m_nStrIDHashCode].GetScheduledAutoRefreshInformation(nDsID, out nPeriodInMs, out dfReplacementPct, out nTrainingRefreshCount, out nTestingRefreshCount);
746 }
747
748 #endregion
749
750 #region Query States
751
760 public long CreateQueryState(int nDsId, bool bUseUniqueLabelIndexes = true, bool bUseUniqueImageIndexes = true, IMGDB_SORT sort = IMGDB_SORT.NONE)
761 {
762 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
763 if (nWait == 0)
764 return 0;
765
766 return m_colDatasets[m_nStrIDHashCode].CreateQueryState(nDsId, bUseUniqueLabelIndexes, bUseUniqueImageIndexes, sort);
767 }
768
777 public long CreateQueryState(string strDs, bool bUseUniqueLabelIndexes = true, bool bUseUniqueImageIndexes = true, IMGDB_SORT sort = IMGDB_SORT.NONE)
778 {
779 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
780 if (nWait == 0)
781 return 0;
782
783 return m_colDatasets[m_nStrIDHashCode].CreateQueryState(strDs, bUseUniqueLabelIndexes, bUseUniqueImageIndexes, sort);
784 }
785
792 public bool SetDefaultQueryState(int nDsId, long lQueryState)
793 {
794 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
795 if (nWait == 0)
796 return false;
797
798 return m_colDatasets[m_nStrIDHashCode].SetDefaultQueryState(nDsId, lQueryState);
799 }
800
807 public bool SetDefaultQueryState(string strDs, long lQueryState)
808 {
809 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
810 if (nWait == 0)
811 return false;
812
813 return m_colDatasets[m_nStrIDHashCode].SetDefaultQueryState(strDs, lQueryState);
814 }
815
822 public bool FreeQueryState(int nDsId, long lHandle)
823 {
824 return m_colDatasets[m_nStrIDHashCode].FreeQueryState(nDsId, lHandle);
825 }
826
833 public bool FreeQueryState(string strDs, long lHandle)
834 {
835 return m_colDatasets[m_nStrIDHashCode].FreeQueryState(strDs, lHandle);
836 }
837
844 public string GetBoostQueryHitPercentsAsTextFromSourceName(long lQueryState, string strSource)
845 {
846 return m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, strSource).GetQueryBoostHitPercentsAsText();
847 }
848
855 public string GetLabelQueryHitPercentsAsTextFromSourceName(long lQueryState, string strSource)
856 {
857 return m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, strSource).GetQueryLabelHitPercentsAsText();
858 }
859
866 public string GetLabelQueryEpocsAsTextFromSourceName(long lQueryState, string strSource)
867 {
868 return m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, strSource).GetQueryLabelEpochsAsText();
869 }
870
876 public string GetBoostQueryHitPercentsAsTextFromSourceName(string strSource)
877 {
879 }
880
886 public string GetLabelQueryHitPercentsAsTextFromSourceName(string strSource)
887 {
889 }
890
896 public string GetLabelQueryEpocsAsTextFromSourceName(string strSource)
897 {
898 return GetLabelQueryEpocsAsTextFromSourceName(0, strSource);
899 }
900
901 #endregion // Query States
902
903 #region Properties
904
909 {
910 get { return m_log; }
911 set { m_log = value; }
912 }
913
918 {
919 return m_factory.LoadDataCriteria;
920 }
921
926 {
927 return m_factory.LoadDebugData;
928 }
929
935 public int GetImageCount(int nSrcId)
936 {
937 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
938
939 if (nWait == 0)
940 return 0;
941
942 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetTotalCount();
943 }
944
955 public int GetItemCount(int nSrcId, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
956 {
957 return GetImageCount(0, nSrcId, strFilterVal, nBoostVal, bBoostValIsExact);
958 }
959
971 public int GetImageCount(long lQueryState, int nSrcId, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
972 {
973 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
974 if (nWait == 0)
975 return 0;
976
977 QueryState qstate = m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, nSrcId);
978 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
979
980 return imgSet.GetCount(qstate, strFilterVal, nBoostVal, bBoostValIsExact);
981 }
982
988 public static Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD> GetSelectionMethod(SettingsCaffe s)
989 {
990 DB_ITEM_SELECTION_METHOD imageSelectionMethod = DB_ITEM_SELECTION_METHOD.NONE;
991 DB_LABEL_SELECTION_METHOD labelSelectionMethod = DB_LABEL_SELECTION_METHOD.NONE;
992
994 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.RANDOM;
995
996 if (s.SuperBoostProbability > 0)
997 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.BOOST;
998
1000 {
1001 labelSelectionMethod |= DB_LABEL_SELECTION_METHOD.RANDOM;
1002
1003 if (s.EnableLabelBoosting)
1004 labelSelectionMethod |= DB_LABEL_SELECTION_METHOD.BOOST;
1005 }
1006 else
1007 {
1009 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.PAIR;
1010 }
1011
1012 return new Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD>(labelSelectionMethod, imageSelectionMethod);
1013 }
1014
1020 public static Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD> GetSelectionMethod(ProjectEx p)
1021 {
1022 DB_ITEM_SELECTION_METHOD imageSelectionMethod = DB_ITEM_SELECTION_METHOD.NONE;
1023 DB_LABEL_SELECTION_METHOD labelSelectionMethod = DB_LABEL_SELECTION_METHOD.NONE;
1024
1026 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.RANDOM;
1027
1029 {
1030 labelSelectionMethod |= DB_LABEL_SELECTION_METHOD.RANDOM;
1031
1032 if (p.EnableLabelBoosting)
1033 labelSelectionMethod |= DB_LABEL_SELECTION_METHOD.BOOST;
1034 }
1035 else
1036 {
1037 if (p.EnablePairSelection)
1038 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.PAIR;
1039 }
1040
1041 return new Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD>(labelSelectionMethod, imageSelectionMethod);
1042 }
1043
1048 public Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD> GetSelectionMethod()
1049 {
1050 return new Tuple<DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD>(m_labelSelectionMethod, m_imageSelectionMethod);
1051 }
1052
1059 {
1060 if (lbl.HasValue)
1061 m_labelSelectionMethod = lbl.Value;
1062
1063 if (img.HasValue)
1064 m_imageSelectionMethod = img.Value;
1065 }
1066
1071 {
1072 get { return m_colDatasets[m_nStrIDHashCode].UseTrainingSourcesForTesting; }
1073 set { m_colDatasets[m_nStrIDHashCode].EnableUsingTrainingSourcesForTesting(value); }
1074 }
1075
1080 {
1081 get { return m_dfSuperBoostProbability; }
1082 set { m_dfSuperBoostProbability = value; }
1083 }
1084
1089 {
1090 get
1091 {
1092 if (m_dfSuperBoostProbability > 0)
1093 {
1094 double dfRandom = m_random.NextDouble(0, 1);
1095
1096 if (dfRandom <= m_dfSuperBoostProbability)
1097 return true;
1098 }
1099
1100 return false;
1101 }
1102 }
1103
1107 public bool IsEnabled
1108 {
1109 get { return m_bEnabled; }
1110 }
1111
1116 public void Enable(bool bEnable)
1117 {
1118 m_bEnabled = bEnable;
1119 }
1120
1124 public bool IsInitialized
1125 {
1126 get { return m_evtInitialized.WaitOne(0); }
1127 }
1128
1129 #endregion // Properties
1130
1131 #region Image Acquisition
1132
1146 public List<SimpleDatum> GetItemsFromIndex(int nSrcId, int nStartIdx, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false, bool bAttemptDirectLoad = false)
1147 {
1148 return GetImagesFromIndex(0, nSrcId, nStartIdx, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact, bAttemptDirectLoad);
1149 }
1150
1163 public List<SimpleDatum> GetItemsFromTime(int nSrcId, DateTime dtStart, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
1164 {
1165 return GetImagesFromTime(0, nSrcId, dtStart, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact);
1166 }
1167
1182 public List<SimpleDatum> GetImagesFromIndex(long lQueryState, int nSrcId, int nStartIdx, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false, bool bAttemptDirectLoad = false)
1183 {
1184 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1185 if (nWait == 0)
1186 return null;
1187
1188 QueryState qstate = m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, nSrcId);
1189 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
1190
1191 return imgSet.GetImages(qstate, nStartIdx, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact, bAttemptDirectLoad);
1192 }
1193
1207 public List<SimpleDatum> GetImagesFromTime(long lQueryState, int nSrcId, DateTime dtStart, int nQueryCount = int.MaxValue, string strFilterVal = null, int? nBoostVal = null, bool bBoostValIsExact = false)
1208 {
1209 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1210 if (nWait == 0)
1211 return null;
1212
1213 QueryState qstate = m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, nSrcId);
1214 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
1215
1216 return imgSet.GetImages(qstate, dtStart, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact);
1217 }
1218
1230 public List<SimpleDatum> GetItems(int nSrcId, int[] rgIdx, string strFilterVal, int? nBoostVal, bool bBoostValIsExact = false)
1231 {
1232 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1233 if (nWait == 0)
1234 return null;
1235
1236 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetImages(nBoostVal.HasValue, strFilterVal, nBoostVal, rgIdx);
1237 }
1238
1251 public SimpleDatum QueryImage(long lQueryState, int nSrcId, int nIdx, DB_LABEL_SELECTION_METHOD? labelSelectionOverride = null, DB_ITEM_SELECTION_METHOD? imageSelectionOverride = null, int? nLabel = null, bool bLoadDataCriteria = false, bool bLoadDebugData = false)
1252 {
1253 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1254 if (nWait == 0)
1255 return null;
1256
1257 DB_LABEL_SELECTION_METHOD labelSelectionMethod = m_labelSelectionMethod;
1258 DB_ITEM_SELECTION_METHOD imageSelectionMethod = m_imageSelectionMethod;
1259
1260 if (labelSelectionOverride.HasValue)
1261 labelSelectionMethod = labelSelectionOverride.Value;
1262
1263 if (imageSelectionOverride.HasValue)
1264 imageSelectionMethod = imageSelectionOverride.Value;
1265
1267 imageSelectionMethod |= DB_ITEM_SELECTION_METHOD.BOOST;
1268
1269 QueryState qstate = m_colDatasets[m_nStrIDHashCode].FindQueryState(lQueryState, nSrcId);
1270 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
1271
1272 return imgSet.GetImage(qstate, labelSelectionMethod, imageSelectionMethod, m_log, nLabel, nIdx, bLoadDataCriteria, bLoadDataCriteria);
1273 }
1274
1286 public SimpleDatum QueryItem(int nSrcId, int nIdx, DB_LABEL_SELECTION_METHOD? labelSelectionOverride = null, DB_ITEM_SELECTION_METHOD? imageSelectionOverride = null, int? nLabel = null, bool bLoadDataCriteria = false, bool bLoadDebugData = false)
1287 {
1288 return QueryImage(0, nSrcId, nIdx, labelSelectionOverride, imageSelectionOverride, nLabel, bLoadDataCriteria, bLoadDebugData);
1289 }
1290
1297 public SimpleDatum GetItem(int nImageID, params int[] rgSrcId)
1298 {
1299 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1300
1301 if (nWait == 0)
1302 return null;
1303
1304 foreach (int nSrcId in rgSrcId)
1305 {
1306 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
1307 SimpleDatum sd = imgSet.GetImage(nImageID);
1308
1309 if (sd != null)
1310 return sd;
1311 }
1312
1313 return null;
1314 }
1315
1327 public int FindItemIndex(int nSrcId, DateTime dt, string strDescription)
1328 {
1329 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).FindImageIndex(dt, strDescription);
1330 }
1331
1332 #endregion // Image Acquisition
1333
1334 #region Image Mean
1335
1341 public SimpleDatum GetItemMean(int nSrcId)
1342 {
1343 if (m_evtAbortInitialization.WaitOne(0))
1344 return null;
1345
1346 if (!m_evtInitialized.WaitOne(0))
1347 {
1348 if (m_rgMeanCache.Keys.Contains(nSrcId))
1349 return m_rgMeanCache[nSrcId];
1350 }
1351
1352 SimpleDatum sd = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetImageMean(null, null, true);
1353
1354 if (!m_rgMeanCache.ContainsKey(nSrcId))
1355 m_rgMeanCache.Add(nSrcId, sd);
1356 else
1357 m_rgMeanCache[nSrcId] = sd;
1358
1359 return sd;
1360 }
1361
1367 public void SetImageMean(int nSrcId, SimpleDatum d)
1368 {
1369 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1370
1371 if (nWait == 0)
1372 return;
1373
1374 m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).SetImageMean(d);
1375 }
1376
1383 {
1384 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDataset(nDatasetId);
1385 if (ds == null)
1386 return null;
1387
1389 }
1390
1401 {
1402 SimpleDatum sd = QueryItemMean(nSrcId);
1403
1404 if (sd != null)
1405 return sd;
1406
1407 sd = GetItemMean(nSrcId);
1408 SaveImageMean(nSrcId, sd, false);
1409
1410 return sd;
1411 }
1412
1419 public void SaveImageMean(int nSrcId, SimpleDatum d, bool bUpdate)
1420 {
1421 if (m_colDatasets.ContainsKey(m_nStrIDHashCode))
1422 m_colDatasets[m_nStrIDHashCode].SaveImageMean(nSrcId, d, bUpdate);
1423 }
1424
1430 public SimpleDatum QueryItemMean(int nSrcId)
1431 {
1432 if (!m_colDatasets.ContainsKey(m_nStrIDHashCode))
1433 return null;
1434
1435 return m_colDatasets[m_nStrIDHashCode].QueryImageMean(nSrcId);
1436 }
1437
1443 public bool DoesImageMeanExists(int nSrcId)
1444 {
1445 using (DNNEntities entities = EntitiesConnection.CreateEntities())
1446 {
1447 List<Source> rgSrc = entities.Sources.Where(p => p.ID == nSrcId).ToList();
1448 if (rgSrc.Count == 0)
1449 return false;
1450
1451 IQueryable<RawImageMean> iQuery = entities.RawImageMeans.Where(p => p.SourceID == nSrcId);
1452 if (iQuery != null)
1453 {
1454 List<RawImageMean> rgMean = iQuery.ToList();
1455 if (rgMean.Count > 0)
1456 return true;
1457 }
1458
1459 return false;
1460 }
1461 }
1462
1463 #endregion // Image Mean
1464
1465 #region Datasets
1466
1477 public long LoadDatasetByName(string strDs, string strEvtCancel = null)
1478 {
1479 int nDsId = m_factory.GetDatasetID(strDs);
1480 return LoadDatasetByID(nDsId, strEvtCancel);
1481 }
1482
1493 public long LoadDatasetByID(int nDsId, string strEvtCancel = null)
1494 {
1495 if (!m_evtInitialized.WaitOne(0))
1496 throw new Exception("The image database must be initialized first before a secondary dataset can be loaded.");
1497
1498 if (m_evtInitializing.WaitOne(0))
1499 throw new Exception("The image database is in the process of being initialized.");
1500
1501 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDataset(nDsId);
1502 if (ds != null)
1503 return 0;
1504
1505 DatasetDescriptor desc = m_factory.LoadDataset(nDsId);
1506 if (desc == null)
1507 throw new Exception("Could not find dataset with ID = " + nDsId.ToString());
1508
1509 if (!m_colDatasets.ContainsKey(m_nStrIDHashCode))
1510 throw new Exception("The image database was not initialized properly.");
1511
1512 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
1513 DatasetEx2 ds0 = new DatasetEx2(m_userGuid, m_factory, m_random);
1514
1515 if (OnCalculateImageMean != null)
1517
1518 if (m_log != null)
1519 m_log.WriteLine("Loading dataset '" + desc.Name + "'...");
1520
1521 CancelEvent evtCancel = new CancelEvent(strEvtCancel);
1522 List<WaitHandle> rgAbort = new List<WaitHandle>(evtCancel.Handles);
1523
1524 long lQueryState = ds0.Initialize(desc, rgAbort.ToArray(), 0, 0, m_log, m_loadMethod, false, m_nLoadLimit);
1525 if (lQueryState == 0)
1526 {
1527 col.Dispose();
1528 return 0;
1529 }
1530
1531 if (m_log != null)
1532 m_log.WriteLine("Dataset '" + desc.Name + "' loaded.");
1533
1534 col.Add(ds0);
1535
1536 return lQueryState;
1537 }
1538
1549 public bool LoadDatasetByID1(int nDsId, string strEvtCancel = null)
1550 {
1551 long lHandle = LoadDatasetByID(nDsId, strEvtCancel);
1552 if (lHandle == 0)
1553 return false;
1554
1555 return true;
1556 }
1557
1568 public bool LoadDatasetByName1(string strDs, string strEvtCancel = null)
1569 {
1570 long lHandle = LoadDatasetByName(strDs, strEvtCancel);
1571 if (lHandle == 0)
1572 return false;
1573
1574 return true;
1575 }
1576
1582 public bool ReloadDataset(int nDsId)
1583 {
1584 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDataset(nDsId);
1585 if (ds != null)
1586 {
1587 ds.ResetLabels();
1588 return true;
1589 }
1590
1591 return false;
1592 }
1593
1599 public bool UnloadDatasetByName(string strDataset)
1600 {
1601 bool bRemoved = false;
1602
1603 lock (m_syncObject)
1604 {
1605 if (m_colDatasets.ContainsKey(m_nStrIDHashCode))
1606 {
1607 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
1608 DatasetEx2 ds = col.FindDataset(strDataset);
1609 if (ds != null)
1610 {
1611 if (m_log != null)
1612 m_log.WriteLine("Unloading dataset '" + ds.DatasetName + "'.");
1613
1614 ds.Unload(false);
1615 GC.Collect();
1616 bRemoved = true;
1617 }
1618 }
1619 }
1620
1621 return bRemoved;
1622 }
1623
1630 public bool UnloadDatasetById(int nDataSetID)
1631 {
1632 bool bRemoved = false;
1633
1634 lock (m_syncObject)
1635 {
1636 if (m_colDatasets.ContainsKey(m_nStrIDHashCode))
1637 {
1638 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
1639
1640 foreach (DatasetEx2 ds in col)
1641 {
1642 if (ds != null)
1643 {
1644 if (ds.DatasetID == nDataSetID || nDataSetID == -1)
1645 {
1646 if (m_log != null)
1647 m_log.WriteLine("Unloading dataset '" + ds.DatasetName + "'.");
1648
1649 ds.Unload(false);
1650 GC.Collect();
1651 bRemoved = true;
1652 }
1653 }
1654 }
1655 }
1656 }
1657
1658 return bRemoved;
1659 }
1660
1668 public double GetDatasetLoadedPercentByName(string strDataset, out double dfTraining, out double dfTesting)
1669 {
1670 dfTraining = 0;
1671 dfTesting = 0;
1672
1673 if (!m_colDatasets.ContainsKey(m_nStrIDHashCode))
1674 return 0;
1675
1676 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
1677 DatasetEx2 ds = col.FindDataset(strDataset);
1678
1679 if (ds == null)
1680 return 0;
1681
1682 return ds.GetPercentageLoaded(out dfTraining, out dfTesting);
1683 }
1684
1692 public double GetDatasetLoadedPercentById(int nDatasetID, out double dfTraining, out double dfTesting)
1693 {
1694 dfTraining = 0;
1695 dfTesting = 0;
1696
1697 if (!m_colDatasets.ContainsKey(m_nStrIDHashCode))
1698 return 0;
1699
1700 DatasetExCollection2 col = m_colDatasets[m_nStrIDHashCode];
1701 DatasetEx2 ds = col.FindDataset(nDatasetID);
1702
1703 if (ds == null)
1704 return 0;
1705
1706 return ds.GetPercentageLoaded(out dfTraining, out dfTesting);
1707 }
1708
1715 {
1716 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDataset(nDsId);
1717 return ds.Descriptor;
1718 }
1719
1726 {
1727 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDataset(strDs);
1728 return ds.Descriptor;
1729 }
1730
1736 public int GetDatasetID(string strDs)
1737 {
1739 if (ds == null)
1740 return 0;
1741
1742 return ds.ID;
1743 }
1744
1750 public string GetDatasetName(int nDsId)
1751 {
1753 if (ds == null)
1754 return null;
1755
1756 return ds.Name;
1757 }
1758
1764 public bool ReloadImageSet(int nSrcID)
1765 {
1766 ImageSet2 imgset = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcID);
1767 if (imgset != null)
1768 {
1769 imgset.ResetLabels();
1770 return true;
1771 }
1772
1773 return false;
1774 }
1775
1776 #endregion // Datasets
1777
1778 #region Sources
1779
1786 {
1787 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId);
1788 if (imgSet != null)
1789 return imgSet.Source;
1790
1791 return null;
1792 }
1793
1799 public SourceDescriptor GetSourceByName(string strSrc)
1800 {
1801 ImageSet2 imgSet = m_colDatasets[m_nStrIDHashCode].FindImageset(strSrc);
1802 if (imgSet != null)
1803 return imgSet.Source;
1804
1805 return null;
1806 }
1807
1813 public int GetSourceID(string strSrc)
1814 {
1815 SourceDescriptor desc = GetSourceByName(strSrc);
1816 if (desc == null)
1817 return 0;
1818
1819 return desc.ID;
1820 }
1821
1827 public string GetSourceName(int nSrcId)
1828 {
1829 SourceDescriptor desc = GetSourceById(nSrcId);
1830 if (desc == null)
1831 return null;
1832
1833 return desc.Name;
1834 }
1835
1836 #endregion // Sources
1837
1838 #region Labels
1839
1845 public List<LabelDescriptor> GetLabels(int nSrcId)
1846 {
1847 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetLabels();
1848 }
1849
1856 public string GetLabelName(int nSrcId, int nLabel)
1857 {
1858 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetLabelName(nLabel);
1859 }
1860
1866 public void SetLabelMapping(int nSrcId, LabelMapping map)
1867 {
1868 m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).SetLabelMapping(map);
1869 }
1870
1877 public void UpdateLabelMapping(int nSrcId, int nNewLabel, List<int> rgOriginalLabels)
1878 {
1879 m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).UpdateLabelMapping(nNewLabel, rgOriginalLabels);
1880 }
1881
1887 public void ResetLabels(int nProjectId, int nSrcId)
1888 {
1889 m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).ResetLabels(nProjectId);
1890 }
1891
1897 public void UpdateLabelCounts(int nProjectID, int nSrcId)
1898 {
1899 m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).UpdateLabelCounts(nProjectID);
1900 }
1901
1907 public Dictionary<int, int> LoadLabelCounts(int nSrcId)
1908 {
1909 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).LoadLabelCounts();
1910 }
1911
1917 public string GetLabelCountsAsTextFromSourceId(int nSrcId)
1918 {
1919 return m_colDatasets[m_nStrIDHashCode].FindImageset(nSrcId).GetLabelCountsAsText();
1920 }
1921
1927 public string GetLabelCountsAsTextFromSourceName(string strSource)
1928 {
1929 return m_colDatasets[m_nStrIDHashCode].FindImageset(strSource).GetLabelCountsAsText();
1930 }
1931
1932 #endregion // Labels
1933
1934 #region Boosts
1935
1943 public void ResetAllBoosts(int nSrcId)
1944 {
1945 int nWait = WaitHandle.WaitAny(new WaitHandle[] { m_evtAbortInitialization, m_evtInitialized });
1946 if (nWait == 0)
1947 return;
1948
1949 DatasetEx2 ds = m_colDatasets[m_nStrIDHashCode].FindDatasetFromSource(nSrcId);
1950 ds.ResetAllBoosts();
1951 }
1952
1953 #endregion // Boosts
1954
1955 #region Results
1956
1964 public List<SimpleResult> GetAllResults(string strSource, bool bRequireExtraData, int nMax = -1)
1965 {
1966 return m_colDatasets[m_nStrIDHashCode].FindImageset(strSource).GetAllResults(bRequireExtraData, nMax);
1967 }
1968
1969 #endregion // Results
1970 }
1971}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
WaitHandle[] Handles
Returns the internal wait handle of the CancelEvent.
Definition: CancelEvent.cs:302
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
Definition: ConnectInfo.cs:14
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
METHOD
Defines the random number generation method to use.
Definition: CryptoRandom.cs:25
double NextDouble()
Returns a random double within the range .
Definition: CryptoRandom.cs:83
The LabelMapping class represents a single label mapping.
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
The ProjectEx class manages a project containing the solver description, model description,...
Definition: ProjectEx.cs:15
bool EnableRandomSelection
Returns whether or not random image selection is enabled. When enabled, images are randomly selected ...
Definition: ProjectEx.cs:648
bool EnablePairSelection
Returns whether or not pair selection is enabled. When using pair selection, images are queried in pa...
Definition: ProjectEx.cs:658
bool EnableLabelBalancing
Returns whether or not label balancing is enabled. When enabled, first the label set is randomly sele...
Definition: ProjectEx.cs:630
bool EnableLabelBoosting
Returns whether or not label boosting is enabled. When using Label boosting, images are selected from...
Definition: ProjectEx.cs:639
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
The SettingsCaffe defines the settings used by the MyCaffe CaffeControl.
bool EnableRandomInputSelection
Get/set random image selection. When enabled, images are randomly selected from the entire set,...
double DbAutoRefreshScheduledReplacementPercent
Get/set the automatic refresh scheduled update replacement percentage used on refresh (default = 0....
int DbLoadLimit
Get/set the image database load limit.
bool EnableLabelBoosting
DEPRECIATED: Get/set label boosting. When using Label boosting, images are selected from boosted labe...
double SuperBoostProbability
Get/set the superboost probability used when selecting boosted images.
DB_LOAD_METHOD DbLoadMethod
Get/set the image database loading method.
bool UseTrainingSourceForTesting
Get/set whether or not to use the training datasource when testing.
bool EnablePairInputSelection
Get/set pair image selection. When using pair selection, images are queried in pairs where the first ...
bool ItemDbLoadDebugData
Specifies whether or not to load the debug data from file (default = false).
int DbAutoRefreshScheduledUpdateInMs
Get/set the automatic refresh scheduled udpate period (default = 10000, only applies when ImageDbLoad...
bool ItemDbLoadDataCriteria
Specifies whether or not to load the image criteria data from file (default = false).
bool SkipMeanCheck
Skip checking for the mean of the dataset - this is not recommended for if the mean does not exist,...
bool VeriyDatasetOnLoad
Get/set whether or not to verify the dataset on load (only applies when using the LOAD_ALL loading me...
bool EnableLabelBalancing
Get/set label balancing. When enabled, first the label set is randomly selected and then the image is...
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
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.
string? TrainingSourceName
Returns the training source name, or null if not specifies.
string? TestingSourceName
Returns the testing source name or null if not specified.
The SourceDescriptor class contains all information describing a data source.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The DatabaseManagement class is used to create the image database.
Exception CreateDatabase(bool bUpdateDatabase=false)
The CreateDatabae creates a new instance of the database in Microsoft SQL.
[V2 Image Database] The DatasetEx2 class provides the in-memory dataset functionality that is used by...
Definition: DatasetEx2.cs:18
int? DatasetID
Returns the dataset ID of the dataset managesd by the DatasetEx object.
Definition: DatasetEx2.cs:710
void Unload(bool bReload)
Unload the images of the training and testing image sets.
Definition: DatasetEx2.cs:621
void ResetLabels()
Resets the labels to their original labels.
Definition: DatasetEx2.cs:558
int AddUser(Guid user)
Adds a user of the dataset.
Definition: DatasetEx2.cs:70
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.
Definition: DatasetEx2.cs:102
long DefaultQueryState
Returns the default query state created when first initializing the dataset.
Definition: DatasetEx2.cs:433
DatasetDescriptor Descriptor
Returns the dataset descriptor of the dataset managesd by the DatasetEx object.
Definition: DatasetEx2.cs:702
void ResetAllBoosts()
Reset all boosts for both the testing and training image sets.
Definition: DatasetEx2.cs:570
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event is passed to each image set and fires each time the Image set need to ...
Definition: DatasetEx2.cs:46
double GetPercentageLoaded(out double dfTraining, out double dfTesting)
Returns the total percentage of images loaded for testing, training and combined.
Definition: DatasetEx2.cs:636
string DatasetName
Returns the dataset name of the dataset managesd by the DatasetEx object.
Definition: DatasetEx2.cs:727
[V2 Image Database] The DatasetExCollection2 contains a list of DatasetEx2 objects.
DatasetEx2 FindDataset(int nDatasetID)
Searches for the dataset with a dataset ID.
virtual void Dispose(bool bDisposing)
Releases all resources used by the collection.
bool RemoveUser(Guid user)
Removes a user from the list of users using the DatasetExCollection.
void Add(DatasetEx2 ds)
Adds a DatasetEx to the collection.
The DatasetFactory manages the connection to the Database object.
void SetLoadingParameters(bool bLoadDataCriteria, bool bLoadDebugData)
Sets the loading parameters that are used to determine which data to load with each image.
DatasetDescriptor LoadDataset(string strDataset, ConnectInfo ci=null)
Load a dataset descriptor from a dataset name.
bool LoadDebugData
Returns whether or not the image debug data is to be loaded when loading each image.
int GetDatasetID(string strDsName, ConnectInfo ci=null)
Returns a datasets ID given its name.
bool LoadDataCriteria
Returns whether or not the image data criteria is to be loaded when loading each image.
string FindDatasetNameFromSourceName(string strTrainSrc, string strTestSrc)
Searches for the data set name based on the training and testing source names.
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static DNNEntities CreateEntities(ConnectInfo ci=null)
Returns the DNNEntities to use.
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,...
Definition: ImageSet2.cs:17
SimpleDatum GetImage(int nImageId)
Get the image at a given image ID.
Definition: ImageSet2.cs:353
List< DbItem > ResetLabels()
Reset all labels of the image set to the original labels.
Definition: ImageSet2.cs:268
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
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
SourceDescriptor Source
Returns the data source of the image set.
Definition: ImageSetBase.cs:65
[V2 Image Database] The MyCaffeImageDatabase2 provides an enhanced in-memory image database used for ...
bool IsRefreshRunning(int nDsId, bool bTraining, bool bTesting)
Returns true if the refresh operation running.
SimpleDatum QueryItem(int nSrcId, int nIdx, DB_LABEL_SELECTION_METHOD? labelSelectionOverride=null, DB_ITEM_SELECTION_METHOD? imageSelectionOverride=null, int? nLabel=null, bool bLoadDataCriteria=false, bool bLoadDebugData=false)
Query an image in a given data source.
bool GetLoadItemDebugData()
Returns whether or not the image debug data is loaded with each image.
SimpleDatum QueryItemMeanFromDataset(int nDatasetId)
Returns the image mean for the Training data source of a given data set.
bool DoesImageMeanExists(int nSrcId)
Returns whether or not the image mean exists in the disk-based database for a given data source.
void SetConnection(ConnectInfo ci)
Set the database connection to use.
int GetDatasetID(string strDs)
Returns a data set ID given its name.
bool UnloadDatasetById(int nDataSetID)
The UnloadDataset method removes the dataset specified from memory.
List< SimpleDatum > GetImagesFromIndex(long lQueryState, int nSrcId, 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.
bool UseTrainingImagesForTesting
Get/set whether or not to use training images for the test set (default = false).
bool UnloadDatasetByName(string strDataset)
The UnloadDataset method removes the dataset specified from memory.
SimpleDatum QueryItemMeanFromDb(int nSrcId)
Queries the image mean for a data source from the database on disk.
int GetSourceID(string strSrc)
Returns a data source ID given its name.
double SuperBoostProbability
Get/set the super-boost probability which increases/decreases the probability of selecting a boosted ...
bool InitializeWithDsName1(SettingsCaffe s, string strDs, string strEvtCancel=null, PropertySet prop=null)
Initializes the image database.
bool WaitForDatasetToRefresh(string strDs, bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for the dataset refreshing to complete.
string GetLabelQueryHitPercentsAsTextFromSourceName(long lQueryState, string strSource)
Returns a string with the query hit percent for each label (e.g. the percentage that each label has b...
void SetSelectionMethod(DB_LABEL_SELECTION_METHOD? lbl, DB_ITEM_SELECTION_METHOD? img)
Sets the label and image selection methods.
SourceDescriptor GetSourceById(int nSrcId)
Returns the SourceDescriptor for a given data source ID.
DB_VERSION GetVersion()
Returns the version of the MyCaffe Image Database being used.
string GetBoostQueryHitPercentsAsTextFromSourceName(long lQueryState, string strSource)
Returns the percentage of boosted images queried as text.
bool IsInitialized
Returns whether or not the image database is initialized.
bool WaitForDatasetToRefresh(int nDsId, bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for the dataset refreshing to complete.
string GetLabelQueryEpocsAsTextFromSourceName(long lQueryState, string strSource)
Returns a string with the query epoch counts for each label (e.g. the number of times all images with...
bool StopAutomaticRefreshSchedule(int nDsID, bool bTraining, bool bTesting)
Stop the automatic refresh schedule running on a dataset.
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event fires each time the MyCaffeImageDatabase wants to access the image mea...
bool StartRefresh(int nDsID, bool bTraining, bool bTesting, double dfReplacementPct)
Start a refresh on the dataset by replacing a specified percentage of the images with images from the...
DatasetDescriptor GetDatasetById(int nDsId)
Returns the DatasetDescriptor for a given data set ID.
SimpleDatum GetItemMean(int nSrcId)
Returns the image mean for a data source.
bool FreeQueryState(string strDs, long lHandle)
Frees a query state from a given dataset.
bool StartRefresh(string strDs, bool bTraining, bool bTesting, double dfReplacementPct)
Start a refresh on the dataset by replacing a specified percentage of the images with images from the...
bool InitializeWithDs1(SettingsCaffe s, DatasetDescriptor ds, string strEvtCancel=null, PropertySet prop=null)
Initializes the image database.
long LoadDatasetByID(int nDsId, string strEvtCancel=null)
Load another 'secondary' dataset.
bool FreeQueryState(int nDsId, long lHandle)
Frees a query state from a given dataset.
List< LabelDescriptor > GetLabels(int nSrcId)
Returns a list of LabelDescriptors associated with the labels within a data source.
bool WaitForDatasetToLoad(string strDs, bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for the dataset loading to complete.
bool LoadDatasetByName1(string strDs, string strEvtCancel=null)
Load another, 'secondary' dataset.
int FindItemIndex(int nSrcId, DateTime dt, string strDescription)
Searches fro the image index of an image within a data source matching a DateTime/description pattern...
string GetLabelCountsAsTextFromSourceName(string strSource)
Returns a string with all label counts for a data source.
bool IsRefreshRunning(string strDs, bool bTraining, bool bTesting)
Returns true if the refresh operation running.
bool StopRefresh(string strDs, bool bTraining, bool bTesting)
Stop a refresh operation running on the dataset.
SimpleDatum GetItem(int nImageID, params int[] rgSrcId)
Returns the image with a given Raw Image ID.
SourceDescriptor GetSourceByName(string strSrc)
Returns the SourceDescriptor for a given data source name.
void CleanUp(int nDsId=0, bool bForce=false)
Releases the image database, and if this is the last instance using the in-memory database,...
long CreateQueryState(string strDs, bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.NONE)
Create a query state for a data set, optionally using a specific sorting method.
int GetImageCount(int nSrcId)
Returns the number of images in a given data source.
long InitializeWithDsId(SettingsCaffe s, int nDataSetID, string strEvtCancel=null, int nPadW=0, int nPadH=0)
Initializes the image database.
void SaveImageMean(int nSrcId, SimpleDatum d, bool bUpdate)
Saves the image mean to a data source on the database on disk.
MyCaffeImageDatabase2(IContainer container)
The MyCaffeImageDatabase constructor.
double GetDatasetLoadedPercentByName(string strDataset, out double dfTraining, out double dfTesting)
Returns the percentage that a dataset is loaded into memory.
int GetImageCount(long lQueryState, int nSrcId, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the number of images in a given data source, optionally only counting the boosted images.
static Tuple< DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD > GetSelectionMethod(SettingsCaffe s)
Returns the label/image selection methods based on the SettingsCaffe settings.
static Tuple< DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD > GetSelectionMethod(ProjectEx p)
Returns the label/image selection methods based on the ProjectEx settings.
List< SimpleDatum > GetImagesFromTime(long lQueryState, int nSrcId, 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.
bool IsEnabled
Returns whether or not the image database is enabled.
string GetLabelCountsAsTextFromSourceId(int nSrcId)
Returns a string with all label counts for a data source.
string GetLabelQueryEpocsAsTextFromSourceName(string strSource)
Returns a string with the query epoch counts for each label (e.g. the number of times all images with...
bool StartAutomaticRefreshSchedule(int nDsID, bool bTraining, bool bTesting, int nPeriodInMs, double dfReplacementPct)
Start the automatic refresh cycle to occur on specified period increments.
void SetLabelMapping(int nSrcId, LabelMapping map)
Sets the label mapping to the database for a given data source.
int GetItemCount(int nSrcId, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the number of images in a given data source, optionally only counting the boosted images.
string GetLabelQueryHitPercentsAsTextFromSourceName(string strSource)
Returns a string with the query hit percent for each label (e.g. the percentage that each label has b...
bool InitializeWithDsId1(SettingsCaffe s, int nDataSetID, string strEvtCancel=null, int nPadW=0, int nPadH=0, PropertySet prop=null)
Initializes the image database.
SimpleDatum QueryImage(long lQueryState, int nSrcId, int nIdx, DB_LABEL_SELECTION_METHOD? labelSelectionOverride=null, DB_ITEM_SELECTION_METHOD? imageSelectionOverride=null, int? nLabel=null, bool bLoadDataCriteria=false, bool bLoadDebugData=false)
Query an image in a given data source.
bool GetScheduledAutoRefreshInformation(int nDsID, out int nPeriodInMs, out double dfReplacementPct, out int nTrainingRefreshCount, out int nTestingRefreshCount)
Returns whether or not a scheduled refresh is running and if so at what period and replacement percen...
MyCaffeImageDatabase2(Log log=null, string strId="default", int nSeed=0)
The MyCaffeImageDatabase2 constructor.
string GetLabelName(int nSrcId, int nLabel)
Returns the text name of a given label within a data source.
void UpdateLabelMapping(int nSrcId, int nNewLabel, List< int > rgOriginalLabels)
Updates the label mapping in the database for a given data source.
bool StopRefresh(int nDsID, bool bTraining, bool bTesting)
Stop a refresh operation running on the dataset.
bool ReloadIndexing(int nDsId)
Reload the indexing for a data set.
bool StartAutomaticRefreshSchedule(string strDs, bool bTraining, bool bTesting, int nPeriodInMs, double dfReplacementPct)
Start the automatic refresh cycle to occur on specified period increments.
bool SetDefaultQueryState(string strDs, long lQueryState)
Set the default query state to the query state specified for the dataset specified.
void ResetAllBoosts(int nSrcId)
Reset all in-memory image boosts.
bool GetScheduledAutoRefreshInformation(string strDs, out int nPeriodInMs, out double dfReplacementPct, out int nTrainingRefreshCount, out int nTestingRefreshCount)
Returns whether or not a scheduled refresh is running and if so at what period and replacement percen...
long InitializeWithDs(SettingsCaffe s, DatasetDescriptor ds, string strEvtCancel=null)
Initializes the image database.
List< SimpleDatum > GetItems(int nSrcId, int[] rgIdx, string strFilterVal, int? nBoostVal, bool bBoostValIsExact=false)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
bool ReloadImageSet(int nSrcID)
Reloads the images of a data source.
Dictionary< int, int > LoadLabelCounts(int nSrcId)
Returns a label lookup of counts for a given data source.
bool WaitForDatasetToLoad(int nDsId, bool bTraining, bool bTesting, int nWait=int.MaxValue)
Wait for the dataset loading to complete.
DatasetDescriptor GetDatasetByName(string strDs)
Returns the DatasetDescriptor for a given data set name.
void Enable(bool bEnable)
Sets whether or not the image database is enabled.
bool GetLoadItemDataCriteria()
Returns whether or not the image data criteria is loaded with each image.
bool LoadDatasetByID1(int nDsId, string strEvtCancel=null)
Load another, 'secondary' dataset.
long InitializeWithDsName(SettingsCaffe s, string strDs, string strEvtCancel=null)
Initializes the image database.
string GetDatasetName(int nDsId)
Returns a data set name given its ID.
bool StopAutomaticRefreshSchedule(string strDs, bool bTraining, bool bTesting)
Stop the automatic refresh schedule running on a dataset.
void SetImageMean(int nSrcId, SimpleDatum d)
Sets the image mean for a data source.
void UpdateLabelCounts(int nProjectID, int nSrcId)
Updates the number of images of each label within a data source.
bool SetDefaultQueryState(int nDsId, long lQueryState)
Set the default query state to the query state specified for the dataset specified.
List< SimpleDatum > GetItemsFromTime(int nSrcId, 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.
bool SelectFromBoostOnly
Returns whether or not to select ONLY from boosted images.
long CreateQueryState(int nDsId, bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.NONE)
Create a query state for a data set, optionally using a specific sorting method.
string GetBoostQueryHitPercentsAsTextFromSourceName(string strSource)
Returns a string with the query hit percent for each boost (e.g. the percentage that each boost value...
bool ReloadIndexing(string strDs)
Reload the indexing for a data set.
double GetDatasetLoadedPercentById(int nDatasetID, out double dfTraining, out double dfTesting)
Returns the percentage that a dataset is loaded into memory.
static void CreateDatabase(ConnectInfo ci, string strPath)
Create the database used by the MyCaffeImageDatabase.
List< SimpleResult > GetAllResults(string strSource, bool bRequireExtraData, int nMax=-1)
Query all results for a given data source.
long LoadDatasetByName(string strDs, string strEvtCancel=null)
Load another 'secondary' dataset.
SimpleDatum QueryItemMean(int nSrcId)
Query the image mean for a data source and mask out (set to 0) all of the image except for the last c...
Tuple< DB_LABEL_SELECTION_METHOD, DB_ITEM_SELECTION_METHOD > GetSelectionMethod()
Returns the label and image selection method used.
bool ReloadDataset(int nDsId)
Reload a data set.
List< SimpleDatum > GetItemsFromIndex(int nSrcId, 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.
string GetSourceName(int nSrcId)
Returns a data source name given its ID.
void ResetLabels(int nProjectId, int nSrcId)
Resets all labels within a data source, used by a project, to their original labels.
Initially the QueryState is copied from the MasterIndexes and during each query is altered by removin...
Definition: QueryState.cs:19
The Component class is a standard Microsoft.NET class that implements the IComponent interface and is...
Definition: Component.cs:18
The IXImageDatabase2 interface defines the general interface to the in-memory image database (v2).
Definition: Interfaces.cs:1092
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
DB_VERSION
Defines the image database version to use.
Definition: Interfaces.cs:397
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