2using System.Collections.Generic;
20 List<LabelSet> m_rgLabelSet =
new List<LabelSet>();
21 List<LabelSet> m_rgLabelSetWithData =
new List<LabelSet>();
23 List<int> m_rgIndexes =
new List<int>();
24 List<int> m_rgLabelIndexes =
new List<int>();
25 List<SimpleDatum> m_rgImagesLimitLoaded =
new List<SimpleDatum>();
27 int m_nLastFindIdx = 0;
29 int m_nFixedIndex = -1;
30 object m_syncObj =
new object();
33 Dictionary<int, double> m_rgLabelBoosts =
new Dictionary<int, double>();
34 double m_dfLabelBoostTotal = 0;
36 int m_nLastImageIdxFromLoad = 0;
37 int m_nLoadedCount = 0;
38 LabelStats m_rgLabelStats;
58 m_loadMethod = loadMethod;
59 m_nLoadLimit = nLoadLimit;
64 m_rgLabelStats =
new LabelStats(src.
Labels.Count);
70 m_rgLabelSet.Add(
new LabelSet(label, m_random));
73 m_rgLabelStats.Add(label);
89 protected virtual void Dispose(
bool bDisposing)
91 if (m_factory !=
null)
113 if (m_rgLabelStats ==
null)
114 return new Dictionary<int, ulong>();
116 return m_rgLabelStats.GetCounts(); ;
126 if (m_rgLabelStats !=
null)
128 m_rgLabelStats.UpdateLabel(nLabel);
129 m_rgLabelStats.UpdateBoost(nBoost);
139 if (m_rgLabelStats ==
null)
142 return m_rgLabelStats.GetQueryBoostHitPercentsAsText();
151 if (m_rgLabelStats ==
null)
154 return m_rgLabelStats.GetQueryLabelHitPercentsAsText();
163 if (m_rgLabelStats ==
null)
166 return m_rgLabelStats.GetQueryLabelEpochAsText();
176 List<LabelDescriptor> rgLabels =
new List<LabelDescriptor>();
178 foreach (
LabelSet ls
in m_rgLabelSet)
193 foreach (
LabelSet ls
in m_rgLabelSet)
199 return nLabel.ToString();
227 foreach (
LabelSet ls
in m_rgLabelSet)
229 imgSet.m_rgLabelSet.Add(
new db.image.LabelSet(ls.
Label, m_random));
232 List<SimpleDatum> rgSd =
new List<basecode.SimpleDatum>();
245 imgSet.m_rgImagesLimitLoaded.Add(sd);
247 imgSet.m_rgImagesLimitLoaded.Add(
null);
250 imgSet.m_rgImages = rgSd.ToArray();
252 imgSet.ReloadLabelSets();
267 if (m_rgImagesLimitLoaded.Count > 0)
268 rgImages = m_rgImagesLimitLoaded.ToArray();
270 for (
int i = m_nLastFindIdx; i < rgImages.Length; i++)
272 if (rgImages[i].TimeStamp == dt && rgImages[i].Description == strDesc)
279 for (
int i = 0; i < m_nLastFindIdx; i++)
281 if (rgImages[i].TimeStamp == dt && rgImages[i].Description == strDesc)
296 m_rgImagesLimitLoaded =
new List<SimpleDatum>();
297 m_rgIndexes =
new List<int>();
298 m_rgLabelStats.Reset();
309 if (m_nLoadLimit > 0 && m_rgImagesLimitLoaded.Count == m_nLoadLimit)
312 m_rgImages[nIdx] = d;
315 if (m_nLoadLimit > 0)
316 m_rgImagesLimitLoaded.
Add(d);
320 foreach (
LabelSet ls
in m_rgLabelSet)
334 m_rgLabelSet.Add(ls);
349 if (m_rgImagesLimitLoaded.Count > 0)
350 rgImages = m_rgImagesLimitLoaded.ToArray();
352 foreach (
LabelSet ls
in m_rgLabelSet)
359 foreach (
LabelSet ls
in m_rgLabelSet)
379 m_rgLabelSetWithData =
new List<LabelSet>();
381 foreach (
LabelSet ls
in m_rgLabelSet)
384 m_rgLabelSetWithData.Add(ls);
387 m_nLastImageIdxFromLoad = nLastImageIdx;
395 get {
return m_src; }
403 get {
return m_src.
ID; }
411 get {
return m_src.
Name.Trim(); }
427 get {
return m_rgImages.Length; }
435 get {
return m_rgImages; }
438 private IEnumerable<SimpleDatum> getQuery(
bool bSuperboostOnly,
string strFilterVal =
null,
int? nBoostVal =
null)
440 IEnumerable<SimpleDatum> iQuery = m_rgImages.Where(p => p !=
null);
444 if (nBoostVal.HasValue)
446 int nVal = nBoostVal.Value;
450 nVal = Math.Abs(nVal);
451 iQuery = iQuery.Where(p => p.Boost == nVal);
455 iQuery = iQuery.Where(p => p.Boost >= nVal);
460 iQuery = iQuery.Where(p => p.Boost > 0);
464 if (!
string.IsNullOrEmpty(strFilterVal))
465 iQuery = iQuery.Where(p => p.Description == strFilterVal);
479 public int GetCount(
bool bSuperboostOnly,
string strFilterVal =
null,
int? nBoostVal =
null)
481 IEnumerable<SimpleDatum> iQuery = getQuery(bSuperboostOnly, strFilterVal, nBoostVal);
482 return iQuery.Count();
496 public List<SimpleDatum>
GetImages(
bool bSuperboostOnly,
string strFilterVal =
null,
int? nBoostVal =
null,
int nStartIdx = 0,
int nQueryCount =
int.MaxValue)
498 IEnumerable<SimpleDatum> iQuery = getQuery(bSuperboostOnly, strFilterVal, nBoostVal);
500 if (nStartIdx != 0 || nQueryCount !=
int.MaxValue)
501 iQuery = iQuery.Where(p => p.Index >= nStartIdx && p.Index < nStartIdx + nQueryCount);
503 return iQuery.ToList();
516 public List<SimpleDatum>
GetImages(
bool bSuperboostOnly,
string strFilterVal,
int? nBoostVal,
int[] rgIdx)
518 IEnumerable<SimpleDatum> iQuery = getQuery(bSuperboostOnly, strFilterVal, nBoostVal);
520 iQuery = iQuery.Where(p => rgIdx.Contains(p.Index));
522 return iQuery.ToList();
541 if (m_nLoadLimit > 0 && m_rgImagesLimitLoaded.Count == m_nLoadLimit)
542 rgImages = m_rgImagesLimitLoaded.ToArray();
544 if (rgImages.Length == 0)
545 throw new Exception(
"There are no images in the dataset '" + m_src.
Name +
"' to select from!");
551 if (m_rgLabelSet.Count == 0)
552 throw new Exception(
"There are no label specified in the Labels table for the dataset '" + m_src.
Name +
"'!");
554 LabelSet labelSet = getLabelSet(labelSelectionMethod);
555 if (labelSet !=
null)
556 sd = labelSet.
GetImage(nIdx, imageSelectionMethod);
563 sd =
LabelSet.
GetImage(rgImages, m_rgIndexes, rgImages.Length, nIdx, m_random, imageSelectionMethod, ref m_nLastIdx, ref m_nFixedIndex, out nImageIdx);
571 bool bRawDataLoaded =
false;
580 for (
int i = 0; i < nRetries; i++)
582 sd = m_factory.
LoadImageAt(nImageIdx, bLoadDataCriteria, bLoadDebugData);
585 bRawDataLoaded =
true;
590 if (i < nRetries - 1)
591 nImageIdx = m_random.
Next(rgImages.Length);
595 log.
WriteLine(
"WARNING! The dataset needs to be re-indexed. Could not find the image at index " + nImageIdx.ToString() +
" - attempting several random queries to get an image.");
599 m_factory.
LoadRawData(sd, bLoadDebugData, bLoadDataCriteria);
614 List<SimpleDatum> rgSd = m_rgImages.Where(p => p !=
null && p.ImageID == nImageID).ToList();
633 foreach (
LabelSet ls
in m_rgLabelSet)
644 double dfBoostTotal = m_dfLabelBoostTotal;
645 Dictionary<int, double> rgBoosts = m_rgLabelBoosts;
650 if (m_rgLabelSetWithData.Count == 0)
653 if (m_rgLabelIndexes.Count == 0)
655 for (
int i = 0; i < m_rgLabelSetWithData.Count; i++)
657 m_rgLabelIndexes.
Add(i);
661 nIdx = m_random.
Next(m_rgLabelIndexes.Count);
662 nIdx = m_rgLabelIndexes[nIdx];
663 m_rgLabelIndexes.Remove(nIdx);
665 return m_rgLabelSetWithData[nIdx];
674 double dfVal = m_random.
NextDouble() * dfBoostTotal;
677 nIdx = m_rgLabelSet.Count - 1;
679 for (
int i = 0; i < m_rgLabelSet.Count; i++)
681 int nLabel = m_rgLabelSet[i].Label.ActiveLabel;
683 if (rgBoosts !=
null && rgBoosts.ContainsKey(nLabel))
684 dfTotal += (double)rgBoosts[nLabel];
688 if (dfTotal >= dfVal)
695 return m_rgLabelSet[nIdx];
716 if (m_imgMean !=
null)
719 if (m_rgImages.Length == 0)
722 log.
WriteLine(
"WARNING: Cannot create image mean with no images!");
727 throw new Exception(
"Can only create image mean when using LOAD_ALL.");
729 if (m_nLoadLimit != 0)
730 throw new Exception(
"Can only create image mean when LoadLimit = 0.");
756 List<LabelBoostDescriptor> rgBoosts = m_factory.
GetLabelBoosts(nProjectId, m_src.
ID);
757 m_rgLabelBoosts =
new Dictionary<int, double>();
759 m_dfLabelBoostTotal = 0;
763 m_dfLabelBoostTotal += (double)boost.
Boost;
765 int nLabel = boost.
Label;
766 double dfBoost = (
double)boost.
Boost;
768 if (!m_rgLabelBoosts.ContainsKey(nLabel))
769 m_rgLabelBoosts.Add(nLabel, dfBoost);
771 m_rgLabelBoosts[nLabel] = dfBoost;
867 for (
int i = 0; i < m_rgImages.Length; i++)
869 m_rgImages[i] =
null;
872 foreach (
LabelSet ls
in m_rgLabelSet)
887 return m_nLoadedCount;
896 return m_rgImages.Length;
918 public List<SimpleDatum>
GetImages(DateTime dt,
int nImageCount,
string strFilterVal =
null)
920 if (
string.IsNullOrEmpty(strFilterVal))
921 return m_rgImages.Where(p => p.TimeStamp >= dt).Take(nImageCount).ToList();
923 return m_rgImages.Where(p => p.Description == strFilterVal && p.TimeStamp >= dt).Take(nImageCount).ToList();
935 m_rgImages = m_rgImages.OrderBy(p => p.ImageID).ToArray();
937 m_rgImages = m_rgImages.OrderByDescending(p => p.ImageID).ToArray();
939 m_rgImages = m_rgImages.OrderBy(p => p.Index).ToArray();
941 m_rgImages = m_rgImages.OrderBy(p => p.Description).ToArray();
943 m_rgImages = m_rgImages.OrderBy(p => p.TimeStamp).ToArray();
945 m_rgImages = m_rgImages.OrderBy(p => p.Description).ThenBy(p => p.TimeStamp).ToArray();
The CalculateImageMeanArgs is passed as an argument to the MyCaffeImageDatabase::OnCalculateImageMean...
SimpleDatum ImageMean
Get/set the image mean calculated from the Images.
bool Cancelled
Get/set a flag indicating to cancel the operation.
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
double NextDouble()
Returns a random double within the range .
The LabelMappingCollection manages a collection of LabelMapping's.
The LabelMapping class represents a single label mapping.
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
The SimpleDatum class holds a data input within host memory.
int OriginalLabel
Get/set the original known label of the data.
static SimpleDatum CalculateMean(Log log, SimpleDatum[] rgImg, WaitHandle[] rgAbort)
Calculate the mean of an array of SimpleDatum and return the mean as a new SimpleDatum.
void ResetBoost()
Reset the boost to the original boost.
void SetLabel(int nLabel)
Sets the label.
SimpleDatum Add(SimpleDatum d)
Creates a new SimpleDatum and adds another SimpleDatum to it.
int Boost
Get/set the boost for this data.
int Index
Returns the index of the SimpleDatum.
int Label
Return the known label of the data.
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 ImageCount
Specifies the number of images under this label.
int ActiveLabel
Specifies the active label (used during training).
int Label
Specifies the original label
string Name
Specifies the label name.
The SourceDescriptor class contains all information describing a data source.
bool IsRealData
Returns whether or not the each data point represents a real or integer number. Integer numbers are u...
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.
void AddLabelBoost(int nProjectId, int nLabel, double dfBoost, int nSrcId=0)
Add a label boost to the database for a given project.
void ResetLabels(int nProjectId=0, int nSrcId=0)
Resets all labels back to their original labels for a project.
void UpdateLabelCounts(Dictionary< int, int > rgCounts)
Updates the label counts in the database for the open data source.
SimpleDatum LoadImage(int nImageId, int nSrcId=0)
Returns the image at a given image ID.
string GetLabelCountsAsText(int nSrcId)
Returns the label counts for a given data source.
DatasetFactory()
The DatasetFactory constructor.
void UpdateLabelMapping(int nNewLabel, List< int > rgOriginalLabels, int nSrcId=0)
Update a label mapping in the database for a data source.
SimpleDatum LoadImageAt(int nIdx, bool? bLoadDataCriteria=null, bool? bLoadDebugData=null, int nSrcId=0, int nPadW=0, int nPadH=0)
Load an image at a given index.
void LoadRawData(SimpleDatum sd, bool bLoadDataCriteria, bool bLoadDebugData)
Load the data criteria and/or debug data.
void Close()
Close the current data source used.
void SetLabelMapping(LabelMapping map, int nSrcId=0)
Saves a label mapping in the database for a data source.
Dictionary< int, int > LoadLabelCounts(int nSrcId=0)
Load the label counts from the database for a data source.
void Dispose()
Releases all resources used.
void DeleteLabelBoosts(int nProjectId, int nSrcId=0)
Delete all label boosts for a project.
string GetLabelBoostsAsText(int nProjectId, int nSrcId=0, bool bSort=true)
Returns the Label boosts as a string.
List< LabelBoostDescriptor > GetLabelBoosts(int nProjectId, int nSrcId=0)
Returns a list of all label boosts set on a project.
void Open(SourceDescriptor src, int nCacheMax=500, ConnectInfo ci=null)
Open a given data source.
The ImageSet class contains the list of image for a data source as well as a list of LabelSets that m...
string GetQueryBoostHitPrecentsAsText()
Get the queried boost hit percents as a string.
int FindImageIndex(DateTime dt, string strDesc)
Searches for an image index based on its time-stamp and description.
EventHandler< CalculateImageMeanArgs > OnCalculateImageMean
The OnCalculateImageMean event fires when the ImageSet needs to calculate the image mean for the imag...
Dictionary< int, int > LoadLabelCounts()
Returns the label counts as a dictionary of item pairs (int nLabel, int nCount).
SimpleDatum GetImage(int nImageID)
Returns the SimpleDatum of the image at a given ID.
List< SimpleDatum > GetImages(DateTime dt, int nImageCount, string strFilterVal=null)
Get a set of images, listed in chronological order starting at the next date greater than or equal to...
Dictionary< int, ulong > GetQueryLabelCounts()
Retrieves the label counts.
string GetLabelCountsAsText()
Returns the label counts for the ImageList as text.
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.
ImageSet Clone()
Returns a copy of the ImageSet.
string SourceName
Returns the data source name of the image set.
int GetCount(bool bSuperboostOnly, string strFilterVal=null, int? nBoostVal=null)
Returns the number of images in the image set, optionally with super-boost only.
void DeleteLabelBoosts(int nProjectId)
Deletes the label boosts for a project.
virtual void Dispose(bool bDisposing)
Releases the resouces used.
int GetLoadedCount()
Returns the number of images loaded.
void SetQueryLabelCount(int nLabel, int nBoost)
Increase the query label count for a specific label.
void UpdateLabelCounts(int nProjectId)
Updates the label counts for a project.
SimpleDatum GetImage(int nIdx, DB_LABEL_SELECTION_METHOD labelSelectionMethod, DB_ITEM_SELECTION_METHOD imageSelectionMethod, Log log, bool bLoadDataCriteria=false, bool bLoadDebugData=false)
Returns the image based on its label and image selection method.
int SourceID
Returns the data source ID of the image set.
void Relabel(LabelMappingCollection col)
Applies the label mapping to the image set.
void CompleteLoad(int nLastImageIdx)
Complete the image loading process.
void UpdateLabelBoosts(int nProjectId)
Update the label boosts for a project.
bool Sort(IMGDB_SORT method)
Sort the internal images.
void SetImageMean(SimpleDatum d)
Set the image mean on for the ImageSet.
List< SimpleDatum > GetImages(bool bSuperboostOnly, string strFilterVal=null, int? nBoostVal=null, int nStartIdx=0, int nQueryCount=int.MaxValue)
Returns the array of images in the image set, possibly filtered with the filtering parameters.
void Reset()
Resets the indexes and limited loaded images (if used).
string GetQueryLabelEpocsAsText()
Get the queried label epoc per label as a text string.
ImageSet(DatasetFactory factory, SourceDescriptor src, DB_LOAD_METHOD loadMethod, int nLoadLimit, CryptoRandom random)
The ImageSet constructor.
void UpdateLabelMapping(int nNewLabel, List< int > rgOriginalLabels)
Update the label mapping on the ImageSet.
string GetLabelName(int nLabel)
Returns the label name of a label.
bool Add(int nIdx, SimpleDatum d)
Adds a new image to the image set.
int Count
Returns the number of images in the image set.
SourceDescriptor Source
Returns the data source of the image set.
void Dispose()
Releases the resouces used.
void ResetAllBoosts()
Resets all image boosts to the original boost loaded from the physical database.
void AddLabelBoost(int nProjectId, int nLabel, double dfBoost)
Adds a label boost for a project.
void ReloadLabelSets()
Reload the label sets.
void SetLabelMapping(LabelMapping map)
Set the label mapping of the ImageSet.
SimpleDatum GetImageMean(Log log, WaitHandle[] rgAbort)
Returns the image mean for the ImageSet.
void ResetLabels(int nProjectId)
Resets the labels for a project.
ImageSet()
The Imageset constructor.
void Unload()
Unload all images in the image set.
string GetQueryLabelHitPrecentsAsText()
Get the queried label hit percents as a string.
bool IsRealData
Returns whether or not the image set contains real or byte based data.
int GetTotalCount()
Returns the total number of images.
string GetLabelBoostsAsText(int nProjectId)
Returns the label boosts as text.
SimpleDatum[] Images
Get the array of images.
LabelSet GetLabelSet(int nLabel)
Retuns the LabelSet corresponding to a label.
List< LabelDescriptor > GetLabels()
Returns a list of label descriptors used by the image set.
The LabelBoostDescriptor class describes a label boost.
int Label
Returns the label.
double Boost
Returns the boost.
The LabelSet 'points' into the main image list via references objects that are already created in the...
void Add(SimpleDatum s)
Adds an image to the current index and then advances the internal index.
SimpleDatum GetImage(int nIdx, DB_ITEM_SELECTION_METHOD selectionMethod)
Returns an image from the LabelSet using the image selection method.
int Count
Returns the number of images in the label set.
void Unload()
Unload all images from the label set.
LabelDescriptor Label
Get/set the label of the LabelSet.
void Clear()
Clears the list of images.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
DB_ITEM_SELECTION_METHOD
Defines the item (e.g., image or temporal item) selection method.
DB_LOAD_METHOD
Defines how to laod the items into the in-memory database.
IMGDB_SORT
Defines the sorting method.
DB_LABEL_SELECTION_METHOD
Defines the label selection 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-...