4using System.Collections.Generic;
7using System.Threading.Tasks;
40 List<DbItem> m_rgImageIdx =
null;
55 m_nLoadLimit = nLoadLimit;
61 load(m_rgImageIdx.Where(p => p !=
null).ToList());
64 private void load(List<DbItem> rgItems)
66 m_index =
new Index(
"ALL", m_random, rgItems);
67 List<DbItem> rgBoosted = rgItems.Where(p => p.Boost > 0).ToList();
68 m_boosted =
new Index(
"BOOSTED", m_random, rgBoosted, -1,
true);
70 List<LabelDescriptor> rgLabels =
m_src.
Labels.OrderBy(p => p.ActiveLabel).ToList();
72 m_rgLabels =
new LabelIndex(
"LABELED", m_randomLabel, m_random,
m_src,
false, rgItems);
73 m_rgLabelsBoosted =
new LabelIndex(
"LABELED BOOSTED", m_randomLabelBoosted, m_random,
m_src,
true, rgBoosted);
85 m_random = idx.m_random;
86 m_nLoadLimit = idx.m_nLoadLimit;
101 if (m_factory !=
null)
114 get {
return m_rgImageIdx; }
122 get {
return m_nLoadLimit; }
140 return m_index.FindImageIndexes(dt);
158 public Index
GetIndex(
int? nLabel =
null,
bool bBoosted =
false)
160 if (!nLabel.HasValue)
170 return m_rgLabels.GetNextIndex(Index.SELECTION_TYPE.DIRECT, nLabel);
182 public void SetIndex(Index idx,
int? nLabel =
null,
bool bBoosted =
false)
184 if (!nLabel.HasValue)
209 public List<int>
GetIndexes(
int nStartIdx,
int nQueryCount =
int.MaxValue,
string strFilterVal =
null,
int? nBoostVal =
null,
bool bBoostValIsExact =
false)
211 if (m_nLoadLimit > 0)
212 throw new Exception(
"The GetIndexes method is not valid when using LoadLimit > 0.");
214 Index idx =
GetIndex(
null, nBoostVal.HasValue);
215 List<DbItem> rgIdx = idx.FindImageIndexes(nStartIdx, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact);
216 return rgIdx.Select(p => p.Index).ToList();
228 public List<int>
GetIndexes(DateTime dtStart,
int nQueryCount =
int.MaxValue,
string strFilterVal =
null,
int? nBoostVal =
null,
bool bBoostValIsExact =
false)
230 if (m_nLoadLimit > 0)
231 throw new Exception(
"The GetIndexes method is not valid when using LoadLimit > 0.");
233 Index idx =
GetIndex(
null, nBoostVal.HasValue);
234 List<DbItem> rgIdx = idx.FindImageIndexes(dtStart, nQueryCount, strFilterVal, nBoostVal, bBoostValIsExact);
235 return rgIdx.Select(p => p.Index).ToList();
244 public virtual int?
GetNextLabel(Index.SELECTION_TYPE type,
bool bBoosted =
false)
247 if (rgIdx.Count == 0)
250 return rgIdx.GetNextLabel(type,
null,
false);
261 public virtual int?
GetNextImage(Index.SELECTION_TYPE type,
int? nLabel =
null,
bool bBoosted =
false,
int nDirectIdx = -1)
263 if (m_nLoadLimit == 0)
265 Index idx =
GetIndex(nLabel, bBoosted);
266 return idx.GetNext(type);
270 if (type != Index.SELECTION_TYPE.RANDOM)
271 throw new Exception(
"Only RANDOM selections are valid when using LoadLimit > 0.");
274 throw new Exception(
"Label selections are not valid when using LoadLimit > 0");
276 if (bBoosted !=
false)
277 throw new Exception(
"Boosted queries are not valid when using LoadLimit > 0.");
279 if (nDirectIdx != -1)
280 throw new Exception(
"DirectIndex queries are not valid when using LoadLimit > 0.");
282 return m_random.
Next(m_nLoadLimit);
305 if (strOut.Length > 0)
312 if (strOut.Length > 0)
319 if (strOut.Length > 0)
321 strOut +=
"ID(desc)";
327 if (strOut.Length > 0)
337#pragma warning disable 1591
339 public class LabelIndex
345 Dictionary<int, int> m_rgLabelToIdxMap =
new Dictionary<int, int>();
346 Dictionary<int, int> m_rgIdxToLabelMap =
new Dictionary<int, int>();
347 Index[] m_rgLabels =
null;
348 bool m_bBoosted =
false;
349 List<int> m_rgIdx =
new List<int>();
351 object m_objSync =
new object();
356 m_randomLabel = randomLabel;
357 m_randomData = randomData;
359 m_bBoosted = bBoosted;
361 m_rgIdx =
new List<int>();
362 m_rgLabelToIdxMap =
new Dictionary<int, int>();
363 m_rgIdxToLabelMap =
new Dictionary<int, int>();
365 List<LabelDescriptor> rgLabels = src.
Labels.Where(p => p.ImageCount > 0).OrderBy(p => p.ActiveLabel).ToList();
366 if (rgLabels.Count > 0)
368 m_rgLabels =
new Index[rgLabels.Count];
370 for (
int i = 0; i < rgLabels.Count; i++)
372 int nLabel = rgLabels[i].ActiveLabel;
373 List<DbItem> rgLabelList = rgItems.Where(p => p.Label == nLabel).ToList();
375 if (i < rgLabels.Count - 1)
376 rgItems = rgItems.Where(p => p.Label != nLabel).ToList();
378 m_rgLabels[i] =
new Index(strName +
" label " + nLabel.ToString(), randomData, rgLabelList, nLabel,
false);
379 if (rgLabelList.Count > 0)
382 m_rgLabelToIdxMap[nLabel] = i;
383 m_rgIdxToLabelMap[i] = nLabel;
388 public LabelIndex(LabelIndex idx)
390 m_strName = idx.m_strName +
" copy";
391 m_randomLabel = idx.m_randomLabel;
392 m_randomData = idx.m_randomData;
394 m_bBoosted = idx.m_bBoosted;
396 m_rgIdx =
new List<int>();
398 if (idx.m_rgLabels !=
null && idx.m_rgLabels.Length > 0)
400 m_rgLabels =
new Index[idx.m_rgLabels.Length];
402 bool bFillLabelMap =
false;
403 if (m_rgLabelToIdxMap ==
null || m_rgLabelToIdxMap.Count == 0 || m_rgIdxToLabelMap ==
null || m_rgIdxToLabelMap.Count == 0)
405 m_rgLabelToIdxMap =
new Dictionary<int, int>();
406 m_rgIdxToLabelMap =
new Dictionary<int, int>();
407 bFillLabelMap =
true;
410 for (
int i = 0; i < idx.m_rgLabels.Length; i++)
412 m_rgLabels[i] = idx.m_rgLabels[i].Clone();
413 if (m_rgLabels[i].Count > 0)
418 int nLabel = m_rgLabels[i].Label;
419 m_rgLabelToIdxMap[nLabel] = i;
420 m_rgIdxToLabelMap[i] = nLabel;
428 if (m_rgIdx.Count == m_rgLabels.Length)
433 for (
int i = 0; i < m_rgLabels.Length; i++)
435 if (m_rgLabels[i].Count > 0)
441 public void SetIndex(
int nLabel, Index idx)
443 int nIdx = m_rgLabelToIdxMap[nLabel];
444 m_rgLabels[nIdx] = idx;
449 get {
return (m_rgLabels ==
null) ? 0 : m_rgLabels.Length; }
454 get {
return m_bBoosted; }
459 get {
return (m_rgIdx.Count == 0) ? true :
false; }
462 public LabelIndex Clone()
464 return new LabelIndex(
this);
467 public int? GetNextLabel(Index.SELECTION_TYPE type,
int? nLabel,
bool bRemove =
false)
469 if (m_rgIdx.Count == 0)
476 else if (type == Index.SELECTION_TYPE.SEQUENTIAL)
478 int nIdx = m_rgIdx[m_nIdx];
481 if (m_nIdx >= m_rgIdx.Count)
484 return m_rgIdxToLabelMap[nIdx];
493 if (m_rgIdx.Count == 0)
495 for (
int i = 0; i < m_rgLabels.Length; i++)
497 if (m_rgLabels[i].Count > 0)
502 int nIdx = m_rgIdx[nIdxLoc];
504 if (m_rgIdx.Count > 1)
506 nIdxLoc = m_randomLabel.
Next(0, m_rgIdx.Count - 1,
true);
507 nIdx = m_rgIdx[nIdxLoc];
511 m_rgIdx.RemoveAt(nIdxLoc);
513 return m_rgIdxToLabelMap[nIdx];
518 public Index GetNextIndex(Index.SELECTION_TYPE type,
int? nLabel,
bool bRemove =
false)
520 nLabel = GetNextLabel(type, nLabel, bRemove);
521 if (!nLabel.HasValue)
524 if (!m_rgLabelToIdxMap.ContainsKey(nLabel.Value))
527 int nIdx = m_rgLabelToIdxMap[nLabel.Value];
529 return m_rgLabels[nIdx];
532 public override string ToString()
543 bool m_bBoosted =
false;
545 List<DbItem> m_rgItems;
546 double m_dfProbability = 0;
547 object m_objSync =
new object();
549 public enum SELECTION_TYPE
556 public Index(
string strName,
CryptoRandom random, List<DbItem> rgItems,
int nLabel = -1,
bool bBoosted =
false,
double dfProbability = 0)
562 m_bBoosted = bBoosted;
563 m_dfProbability = dfProbability;
568 get {
return m_rgItems.Count; }
571 public double Probability
573 get {
return m_dfProbability; }
574 set { m_dfProbability = value; }
579 get {
return m_nLabel; }
584 get {
return m_bBoosted; }
589 get {
return (m_rgItems.Count == 0) ? true :
false; }
594 List<DbItem> rgItems =
new List<DbItem>();
596 for (
int i = 0; i < m_rgItems.Count; i++)
598 rgItems.Add(m_rgItems[i].Clone());
604 rgItems = rgItems.OrderBy(p => p.ID).ToList();
608 rgItems = rgItems.OrderByDescending(p => p.ID).ToList();
612 rgItems = rgItems.OrderBy(p => p.Index).ToList();
616 rgItems = rgItems.OrderBy(p => p.Desc).ThenBy(p => p.Index).ToList();
620 rgItems = rgItems.OrderBy(p => p.Time).ToList();
625 rgItems = rgItems.OrderBy(p => p.Desc).ThenBy(p => p.Time).ToList();
629 return new Index(m_strName +
" copy", m_random, rgItems, m_nLabel, m_bBoosted, m_dfProbability);
632 public List<DbItem> FindImageIndexes(
int nStartIdx,
int nQueryCount =
int.MaxValue,
string strFilter =
null,
int? nBoostVal =
null,
bool bBoostValIsExact =
false)
634 IEnumerable<DbItem> iQuery = m_rgItems.Where(p => p.Index >= nStartIdx);
636 if (strFilter !=
null)
637 iQuery = iQuery.Where(p => p.Desc == strFilter);
639 if (nBoostVal.HasValue)
641 if (bBoostValIsExact)
642 iQuery = iQuery.Where(p => p.Boost == nBoostVal.Value);
644 iQuery = iQuery.Where(p => p.Boost >= nBoostVal.Value);
647 return iQuery.Take(nQueryCount).ToList();
650 public List<DbItem> FindImageIndexes(DateTime dtStart,
int nQueryCount =
int.MaxValue,
string strFilter =
null,
int? nBoostVal =
null,
bool bBoostValIsExact =
false)
652 IEnumerable<DbItem> iQuery = m_rgItems.Where(p => p.Time >= dtStart);
654 if (strFilter !=
null)
655 iQuery = iQuery.Where(p => p.Desc == strFilter);
657 if (nBoostVal.HasValue)
659 if (bBoostValIsExact)
660 iQuery = iQuery.Where(p => p.Boost == nBoostVal.Value);
662 iQuery = iQuery.Where(p => p.Boost >= nBoostVal.Value);
665 return iQuery.Take(nQueryCount).ToList();
668 public List<DbItem> FindImageIndexes(DateTime dt)
670 return m_rgItems.Where(p => p.Time == dt).ToList();
673 public int? GetNext(SELECTION_TYPE type,
bool bRemove =
false)
675 if (m_rgItems.Count == 0)
678 if (type == SELECTION_TYPE.SEQUENTIAL)
680 int nIdx = m_rgItems[m_nIdx].Index;
683 if (m_nIdx >= m_rgItems.Count)
692 int nCount = m_rgItems.Count;
696 int nIdx = m_random.
Next(nCount);
697 if (m_rgItems[nIdx] ==
null)
699 m_rgItems.RemoveAt(nIdx);
700 nCount = m_rgItems.Count;
704 nIdx = m_random.
Next(nCount);
707 int nFinalIdx = m_rgItems[nIdx].Index;
710 m_rgItems.RemoveAt(nIdx);
713 if (m_nIdx == m_rgItems.Count)
721 public int? GetIndex(
int nDirectIdx)
723 if (nDirectIdx < 0 || nDirectIdx >= m_rgItems.Count)
727 if (m_nIdx == m_rgItems.Count)
730 return m_rgItems[nDirectIdx].Index;
733 public override string ToString()
735 return m_strName +
": Count = " + m_rgItems.Count().ToString() +
" CurIdx = " + m_nIdx.ToString() +
"; Label = " + m_nLabel.ToString() +
"; Boosted = " + m_bBoosted.ToString() +
" => (" + m_rgItems.Count.ToString() +
") p = " + m_dfProbability.ToString(
"P");
739#pragma warning restore 1591
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
METHOD
Defines the random number generation method to use.
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
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.
int ImageCount
Returns the number of images within this data source.
The DatasetFactory manages the connection to the Database object.
List< DbItem > LoadImageIndexes(bool bBoostedOnly, bool bIncludeActive=true, bool bIncludeInactive=false)
Returns a list of the image indexes of all boosted images in the Data Source.
void Close()
Close the current data source used.
void Dispose()
Releases all resources used.
void Open(SourceDescriptor src, int nCacheMax=500, ConnectInfo ci=null)
Open a given data source.
The MasterIndexes stores the indexes that define the index structure of the data source data.
void Dispose()
Release all resources used.
override string ToString()
Returns a string representation of the master indexes.
LabelIndex m_rgLabelsBoosted
Specifies the list of all boosted images listed by label where each label contains an index into all ...
virtual ? int GetNextImage(Index.SELECTION_TYPE type, int? nLabel=null, bool bBoosted=false, int nDirectIdx=-1)
Returns the next image in the Index set based on the selection criteria.
virtual ? int GetNextLabel(Index.SELECTION_TYPE type, bool bBoosted=false)
Returns the next label in the Index set selected based on the selection criteria.
List< int > GetIndexes(DateTime dtStart, int nQueryCount=int.MaxValue, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the indexes fitting the criteria.
int LabelCount
Returns the number of labels.
void SetIndex(Index idx, int? nLabel=null, bool bBoosted=false)
Set a given index based on the criteria.
void Reload(List< DbItem > rgItems)
Reload all images by re-loading the master index list.
int LoadLimit
Returns the load limit set during initialization.
SourceDescriptor m_src
Specifies the data source descriptor.
MasterIndexes(MasterIndexes idx, IMGDB_SORT sort)
The constructor used to copy another MasterIndexes and optionally specify a sorting for the indexes.
Index m_index
Specifies the index into all of the data source images.
LabelIndex m_rgLabels
Specifies the list of images listed by label where each label contains an index into all images with ...
List< DbItem > RawIndexes
Returns the raw indexes.
MasterIndexes(CryptoRandom random, SourceDescriptor src, int nLoadLimit=0)
The constructor.
List< DbItem > FindImageIndexes(DateTime dt)
Returns all DbItems that point to images iwth a given date.
List< int > GetIndexes(int nStartIdx, int nQueryCount=int.MaxValue, string strFilterVal=null, int? nBoostVal=null, bool bBoostValIsExact=false)
Returns the indexes fitting the criteria.
Index GetIndex(int? nLabel=null, bool bBoosted=false)
Returns the Index matching the criteria.
Index m_boosted
Specifies the list of all boosted images.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
@ RANDOM
Randomly select the images, ignore the input index.
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-...