MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
QueryState.cs
1using MyCaffe.basecode;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace MyCaffe.db.image
10{
19 {
20 MasterIndexes m_master;
21 LabelStats m_stat;
22 bool m_bUseUniqueImageIndexes = true;
23 bool m_bUseUniqueLabelIndexes = true;
24
32 public QueryState(MasterIndexes master, bool bUseUniqueLabelIndexes = true, bool bUseUniqueImageIndexes = true, IMGDB_SORT sort = IMGDB_SORT.BYIDX) : base(master, sort)
33 {
34 m_master = master;
35 m_stat = new LabelStats(master.LabelCount);
36 m_bUseUniqueLabelIndexes = bUseUniqueLabelIndexes;
37 m_bUseUniqueImageIndexes = bUseUniqueImageIndexes;
38
39 foreach (LabelDescriptor label in m_src.Labels)
40 {
41 m_stat.Add(label);
42 }
43 }
44
49 public void UpdateStats(SimpleDatum sd)
50 {
51 m_stat.UpdateLabel(sd.Label);
52 m_stat.UpdateBoost(sd.Boost);
53 }
54
61 {
62 Index.SELECTION_TYPE selType = Index.SELECTION_TYPE.RANDOM;
63 bool bBoost = false;
64
65 if ((lblSel & DB_LABEL_SELECTION_METHOD.RANDOM) != DB_LABEL_SELECTION_METHOD.RANDOM)
66 selType = Index.SELECTION_TYPE.SEQUENTIAL;
67
68 if ((lblSel & DB_LABEL_SELECTION_METHOD.BOOST) == DB_LABEL_SELECTION_METHOD.BOOST)
69 bBoost = true;
70
71 return GetNextLabel(selType, bBoost);
72 }
73
80 public override int? GetNextLabel(Index.SELECTION_TYPE type, bool bBoosted = false)
81 {
82 LabelIndex rgIdx = (bBoosted) ? m_rgLabelsBoosted : m_rgLabels;
83 if (rgIdx.Count == 0)
84 return null;
85
86 int? nIdx = rgIdx.GetNextLabel(type, null, m_bUseUniqueLabelIndexes);
87 if (rgIdx.IsEmpty)
88 rgIdx.ReLoad();
89
90 return nIdx;
91 }
92
100 public int? GetNextImage(DB_ITEM_SELECTION_METHOD imgSel, int? nLabel, int nDirectIdx)
101 {
102 if (!nLabel.HasValue && imgSel == DB_ITEM_SELECTION_METHOD.NONE && nDirectIdx >= 0)
103 return GetNextImage(Index.SELECTION_TYPE.DIRECT, null, false, nDirectIdx);
104
105 Index.SELECTION_TYPE selType = Index.SELECTION_TYPE.RANDOM;
106 bool bBoosted = false;
107
108 if ((imgSel & DB_ITEM_SELECTION_METHOD.RANDOM) != DB_ITEM_SELECTION_METHOD.RANDOM)
109 {
110 selType = Index.SELECTION_TYPE.SEQUENTIAL;
111
112 // When using PAIR, advance to the next item.
113 if ((imgSel & DB_ITEM_SELECTION_METHOD.PAIR) == DB_ITEM_SELECTION_METHOD.PAIR)
114 GetNextImage(selType, nLabel, bBoosted, -1);
115 }
116
117 if ((imgSel & DB_ITEM_SELECTION_METHOD.BOOST) == DB_ITEM_SELECTION_METHOD.BOOST)
118 bBoosted = true;
119
120 return GetNextImage(selType, nLabel, bBoosted, -1);
121 }
122
131 public override int? GetNextImage(Index.SELECTION_TYPE type, int? nLabel = null, bool bBoosted = false, int nDirectIdx = -1)
132 {
133 int? nIdx = null;
134
135 if (m_master.LoadLimit > 0)
136 {
137 nIdx = base.GetNextImage(type, nLabel, bBoosted, nDirectIdx);
138 }
139 else
140 {
141 Index idx;
142 if (type == Index.SELECTION_TYPE.DIRECT)
143 {
144 if (nDirectIdx < 0)
145 throw new Exception("Invalid direct index, must be >= 0.");
146
147 idx = GetIndex(null, false);
148 nIdx = idx.GetIndex(nDirectIdx);
149 }
150 else
151 {
152 idx = GetIndex(nLabel, bBoosted);
153 if (idx == null)
154 {
155 SetIndex(m_master.GetIndex(nLabel, bBoosted).Clone(), nLabel, bBoosted);
156 idx = GetIndex(nLabel, bBoosted);
157 nIdx = idx.GetNext(type, m_bUseUniqueImageIndexes);
158 }
159 else
160 {
161 nIdx = idx.GetNext(type, m_bUseUniqueImageIndexes);
162 if (idx.IsEmpty)
163 SetIndex(m_master.GetIndex(nLabel, bBoosted).Clone(), nLabel, bBoosted);
164 }
165 }
166 }
167
168 return nIdx;
169 }
170
175 public Dictionary<int, ulong> GetQueryLabelCounts()
176 {
177 return m_stat.GetCounts();
178 }
179
185 {
186 return m_stat.GetQueryBoostHitPercentsAsText();
187 }
188
194 {
195 return m_stat.GetQueryLabelHitPercentsAsText();
196 }
197
203 {
204 return m_stat.GetQueryLabelEpochAsText();
205 }
206 }
207
211 public class QueryStateCollection : IDisposable
212 {
213 Dictionary<long, Tuple<QueryState, QueryState>> m_rgQueryStates = new Dictionary<long, Tuple<QueryState, QueryState>>();
214 object m_objSync = new object();
215
220 {
221 // Fill with blank item for first handle, for handle = 0 is not used.
222 m_rgQueryStates.Add(0, null);
223 }
224
228 public void Dispose()
229 {
230 foreach (KeyValuePair<long, Tuple<QueryState, QueryState>> kv in m_rgQueryStates)
231 {
232 kv.Value.Item1.Dispose();
233 kv.Value.Item2.Dispose();
234 }
235
236 m_rgQueryStates.Clear();
237 }
238
245 public long CreateNewState(QueryState training, QueryState testing)
246 {
247 long lHandle = 0;
248
249 lock (m_objSync)
250 {
251 if (m_rgQueryStates.Count > 0)
252 lHandle = m_rgQueryStates.Max(p => p.Key) + 1;
253
254 m_rgQueryStates.Add(lHandle, new Tuple<QueryState, QueryState>(training, testing));
255 }
256
257 return lHandle;
258 }
259
265 public bool FreeQueryState(long lHandle)
266 {
267 if (lHandle == 0)
268 return false;
269
270 lock (m_objSync)
271 {
272 if (m_rgQueryStates.ContainsKey(lHandle))
273 {
274 m_rgQueryStates[lHandle].Item1.Dispose();
275 m_rgQueryStates[lHandle].Item2.Dispose();
276 m_rgQueryStates.Remove(lHandle);
277 return true;
278 }
279
280 return false;
281 }
282 }
283
289 public QueryState GetTrainingState(long lHandle)
290 {
291 if (lHandle == 0)
292 throw new Exception("Invalid training QueryState handle, cannot = 0.");
293
294 lock (m_objSync)
295 {
296 return m_rgQueryStates[lHandle].Item1;
297 }
298 }
299
305 public QueryState GetTestingState(long lHandle)
306 {
307 if (lHandle == 0)
308 throw new Exception("Invalid testing QueryState handle, cannot = 0.");
309
310 lock (m_objSync)
311 {
312 return m_rgQueryStates[lHandle].Item2;
313 }
314 }
315
320 public void ReIndexTraining(List<DbItem> rgItems)
321 {
322 lock (m_objSync)
323 {
324 foreach (KeyValuePair<long, Tuple<QueryState, QueryState>> kv in m_rgQueryStates)
325 {
326 if (kv.Value != null)
327 kv.Value.Item1.Reload(rgItems);
328 }
329 }
330 }
331
336 public void ReIndexTesting(List<DbItem> rgItems)
337 {
338 lock (m_objSync)
339 {
340 foreach (KeyValuePair<long, Tuple<QueryState, QueryState>> kv in m_rgQueryStates)
341 {
342 if (kv.Value != null)
343 kv.Value.Item2.Reload(rgItems);
344 }
345 }
346 }
347 }
348}
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
int Boost
Get/set the boost for this data.
int Label
Return the known label of the data.
The LabelDescriptor class describes a single label.
List< LabelDescriptor > Labels
Get/set the list of LabelDescriptors that describe the labels used by the data items.
The MasterIndexes stores the indexes that define the index structure of the data source data.
LabelIndex m_rgLabelsBoosted
Specifies the list of all boosted images listed by label where each label contains an index into all ...
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.
int LoadLimit
Returns the load limit set during initialization.
SourceDescriptor m_src
Specifies the data source descriptor.
LabelIndex m_rgLabels
Specifies the list of images listed by label where each label contains an index into all images with ...
Index GetIndex(int? nLabel=null, bool bBoosted=false)
Returns the Index matching the criteria.
The QueryStateCollection manages all query states used by matching the QueryState handles with the Qu...
Definition: QueryState.cs:212
void ReIndexTraining(List< DbItem > rgItems)
Relabels the training QueryState based on the DbItems.
Definition: QueryState.cs:320
QueryState GetTrainingState(long lHandle)
Returns the QueryState used with thet Training data source.
Definition: QueryState.cs:289
bool FreeQueryState(long lHandle)
Free the QueryStates associated with the handle and remove it from the handle list.
Definition: QueryState.cs:265
void Dispose()
Releases all resources.
Definition: QueryState.cs:228
QueryStateCollection()
The constructor.
Definition: QueryState.cs:219
QueryState GetTestingState(long lHandle)
Returns the QueryState used with thet Testing data source.
Definition: QueryState.cs:305
void ReIndexTesting(List< DbItem > rgItems)
Relabels the testing QueryState based on the DbItems.
Definition: QueryState.cs:336
long CreateNewState(QueryState training, QueryState testing)
Create a new QueryState handle based on the QueryStates passed to this function.
Definition: QueryState.cs:245
Initially the QueryState is copied from the MasterIndexes and during each query is altered by removin...
Definition: QueryState.cs:19
Dictionary< int, ulong > GetQueryLabelCounts()
Returns the query label counts.
Definition: QueryState.cs:175
int? GetNextImage(DB_ITEM_SELECTION_METHOD imgSel, int? nLabel, int nDirectIdx)
Returns the next image in the Index set based on the selection criteria.
Definition: QueryState.cs:100
override? 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.
Definition: QueryState.cs:131
void UpdateStats(SimpleDatum sd)
Update the label stats.
Definition: QueryState.cs:49
override? int GetNextLabel(Index.SELECTION_TYPE type, bool bBoosted=false)
Returns the next label in the Index set selected based on the selection criteria.
Definition: QueryState.cs:80
int? GetNextLabel(DB_LABEL_SELECTION_METHOD lblSel)
Returns the next label in the Index set selected based on the selection criteria.
Definition: QueryState.cs:60
QueryState(MasterIndexes master, bool bUseUniqueLabelIndexes=true, bool bUseUniqueImageIndexes=true, IMGDB_SORT sort=IMGDB_SORT.BYIDX)
The constructor.
Definition: QueryState.cs:32
string GetQueryBoostHitPercentsAsText()
Returns the number of times each boosted image vs. non boosted images are hit.
Definition: QueryState.cs:184
string GetQueryLabelEpochsAsText()
Returns the number of epochs each label has experienced.
Definition: QueryState.cs:202
string GetQueryLabelHitPercentsAsText()
Returns the number of times each label is hit.
Definition: QueryState.cs:193
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
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