MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
Labelset.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Drawing;
6using MyCaffe.basecode;
8
9namespace MyCaffe.db.image
10{
14 public class LabelSet : IDisposable
15 {
16 CryptoRandom m_random = null;
17 LabelDescriptor m_label;
18 SimpleDatum[] m_rgImages;
19 List<int> m_rgIdx = new List<int>();
20 int m_nCurrentIdx = 0;
21
28 {
29 m_random = random;
30 m_label = lbl;
31 m_rgImages = new SimpleDatum[lbl.ImageCount];
32 }
33
38 {
39 get { return m_label; }
40 set { m_label = value; }
41 }
42
46 public int Count
47 {
48 get { return m_rgImages.Length; }
49 }
50
54 public void Clear()
55 {
56 m_rgImages = new SimpleDatum[m_rgImages.Length];
57 m_nCurrentIdx = 0;
58 }
59
65 public SimpleDatum this[int nIdx]
66 {
67 get { return m_rgImages[nIdx]; }
68 set { m_rgImages[nIdx] = value; }
69 }
70
74 public bool IsLoaded
75 {
76 get
77 {
78 if (m_nCurrentIdx == m_rgImages.Length)
79 return true;
80 return false;
81 }
82 }
83
88 public void Add(SimpleDatum s)
89 {
90 if (m_nCurrentIdx < m_rgImages.Length)
91 {
92 m_rgImages[m_nCurrentIdx] = s;
93 m_nCurrentIdx++;
94 }
95 }
96
103 public SimpleDatum GetImage(int nIdx, DB_ITEM_SELECTION_METHOD selectionMethod)
104 {
105 if ((selectionMethod & DB_ITEM_SELECTION_METHOD.RANDOM) != DB_ITEM_SELECTION_METHOD.RANDOM)
106 throw new ArgumentException("Label balancing image selection only supports the RANDOM and RANDOM+BOOST selection methods");
107
108 if (m_nCurrentIdx == 0)
109 return null;
110
111 int nLastIdx = 0;
112 int nFixedIdx = -1;
113 int nImageIdx = 0;
114
115 return GetImage(m_rgImages, m_rgIdx, m_nCurrentIdx, nIdx, m_random, selectionMethod, ref nLastIdx, ref nFixedIdx, out nImageIdx);
116 }
117
131 public static SimpleDatum GetImage(SimpleDatum[] rgImages, List<int> rgIdx, int nCount, int nIdx, CryptoRandom random, DB_ITEM_SELECTION_METHOD selectionMethod, ref int nLastIndex, ref int nFixedIndex, out int nImageIdx)
132 {
133 if ((selectionMethod & DB_ITEM_SELECTION_METHOD.BOOST) == DB_ITEM_SELECTION_METHOD.BOOST)
134 {
135 IEnumerable<SimpleDatum> iQuery = rgImages.Where(p => p != null && p.Boost > 0);
136 List<SimpleDatum> rgItems = new List<SimpleDatum>(iQuery);
137
138 if (rgItems.Count > 0)
139 {
140 if (rgIdx.Count > rgItems.Count)
141 rgIdx.Clear();
142
143 if (rgIdx.Count == 0)
144 {
145 for (int i = 0; i < rgItems.Count; i++)
146 {
147 rgIdx.Add(i);
148 }
149 }
150
151 if ((selectionMethod & DB_ITEM_SELECTION_METHOD.RANDOM) == DB_ITEM_SELECTION_METHOD.RANDOM)
152 {
153 nIdx = rgIdx[random.Next(rgIdx.Count)];
154 rgIdx.Remove(nIdx);
155 }
156
157 SimpleDatum sd = rgItems[nIdx];
158 nImageIdx = nIdx;
159
160 return sd;
161 }
162 }
163
164 int nMin = ((selectionMethod & DB_ITEM_SELECTION_METHOD.PAIR) == DB_ITEM_SELECTION_METHOD.PAIR) ? 2 : 1;
165 if (rgIdx.Count < nMin)
166 {
167 rgIdx.Clear();
168
169 for (int i = 0; i < rgImages.Length; i++)
170 {
171 rgIdx.Add(i);
172 }
173 }
174
175 if ((selectionMethod & DB_ITEM_SELECTION_METHOD.PAIR) == DB_ITEM_SELECTION_METHOD.PAIR)
176 {
177 nIdx = nLastIndex + 1;
178
179 if (nIdx == rgIdx.Count)
180 nIdx = 0;
181
182 rgIdx.Remove(nIdx);
183 }
184 else if ((selectionMethod & DB_ITEM_SELECTION_METHOD.RANDOM) == DB_ITEM_SELECTION_METHOD.RANDOM)
185 {
186 nIdx = rgIdx[random.Next(rgIdx.Count)];
187 rgIdx.Remove(nIdx);
188 }
189 else if (selectionMethod == DB_ITEM_SELECTION_METHOD.FIXEDINDEX)
190 {
191 nFixedIndex = nIdx;
192 }
193 else if ((selectionMethod & DB_ITEM_SELECTION_METHOD.CLEARFIXEDINDEX) == DB_ITEM_SELECTION_METHOD.CLEARFIXEDINDEX)
194 {
195 nFixedIndex = -1;
196 }
197
198 if (nFixedIndex >= 0)
199 nIdx = nFixedIndex;
200
201 if (nIdx >= rgImages.Length)
202 nIdx = nIdx % rgImages.Length;
203
204 nLastIndex = nIdx;
205 nImageIdx = nIdx;
206
207 return rgImages[nIdx];
208 }
209
213 public void Unload()
214 {
215 for (int i = 0; i < m_rgImages.Length; i++)
216 {
217 m_rgImages[i] = null;
218 }
219 }
220
225 protected virtual void Dispose(bool bDisposing)
226 {
227 if (m_random != null)
228 {
229 m_random.Dispose();
230 m_random = null;
231 }
232 }
233
237 public void Dispose()
238 {
239 Dispose(true);
240 }
241 }
242}
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
Definition: CryptoRandom.cs:14
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
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.
The LabelDescriptor class describes a single label.
int ImageCount
Specifies the number of images under this label.
The LabelSet 'points' into the main image list via references objects that are already created in the...
Definition: Labelset.cs:15
static SimpleDatum GetImage(SimpleDatum[] rgImages, List< int > rgIdx, int nCount, int nIdx, CryptoRandom random, DB_ITEM_SELECTION_METHOD selectionMethod, ref int nLastIndex, ref int nFixedIndex, out int nImageIdx)
Returns an image from a list of images.
Definition: Labelset.cs:131
void Dispose()
Releases all resources used.
Definition: Labelset.cs:237
void Add(SimpleDatum s)
Adds an image to the current index and then advances the internal index.
Definition: Labelset.cs:88
SimpleDatum GetImage(int nIdx, DB_ITEM_SELECTION_METHOD selectionMethod)
Returns an image from the LabelSet using the image selection method.
Definition: Labelset.cs:103
LabelSet(LabelDescriptor lbl, CryptoRandom random)
The LabelSet constructor.
Definition: Labelset.cs:27
int Count
Returns the number of images in the label set.
Definition: Labelset.cs:47
virtual void Dispose(bool bDisposing)
Releases all resources used.
Definition: Labelset.cs:225
bool IsLoaded
Returns whether or not the label set is fully loaded or not (which is the case when first using LOAD_...
Definition: Labelset.cs:75
void Unload()
Unload all images from the label set.
Definition: Labelset.cs:213
LabelDescriptor Label
Get/set the label of the LabelSet.
Definition: Labelset.cs:38
void Clear()
Clears the list of images.
Definition: Labelset.cs:54
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
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