MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
LabelMap.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections;
4using System.Collections.Generic;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace MyCaffe.param.ssd
11{
19 [Serializable]
20 [TypeConverter(typeof(ExpandableObjectConverter))]
21 public class LabelMap
22 {
23 List<LabelMapItem> m_rgItems = new List<LabelMapItem>();
24
28 public LabelMap()
29 {
30 }
31
37 public LabelMapItem FindByLabel(int nLabel)
38 {
39 foreach (LabelMapItem li in m_rgItems)
40 {
41 if (li.label == nLabel)
42 return li;
43 }
44
45 return null;
46 }
47
53 public LabelMapItem FindByName(string strName)
54 {
55 foreach (LabelMapItem li in m_rgItems)
56 {
57 if (li.name == strName)
58 return li;
59 }
60
61 return null;
62 }
63
71 public Dictionary<int, string> MapToName(Log log, bool bStrict, bool bDisplayName)
72 {
73 Dictionary<int, string> rgLabelToName = new Dictionary<int, string>();
74
75 for (int i = 0; i < m_rgItems.Count; i++)
76 {
77 string strName = (bDisplayName) ? m_rgItems[i].display : m_rgItems[i].name;
78 int nLabel = m_rgItems[i].label;
79
80 if (bStrict)
81 {
82 if (rgLabelToName.ContainsKey(nLabel))
83 log.FAIL("There are duplicates of the label: " + nLabel.ToString());
84
85 rgLabelToName.Add(nLabel, strName);
86 }
87 else
88 {
89 if (rgLabelToName.ContainsKey(nLabel))
90 rgLabelToName[nLabel] = strName;
91 else
92 rgLabelToName.Add(nLabel, strName);
93 }
94 }
95
96 return rgLabelToName;
97 }
98
105 public Dictionary<string, int> MapToLabel(Log log, bool bStrict)
106 {
107 Dictionary<string, int> rgNameToLabel = new Dictionary<string, int>();
108
109 for (int i = 0; i < m_rgItems.Count; i++)
110 {
111 string strName = m_rgItems[i].name;
112 int nLabel = m_rgItems[i].label;
113
114 if (bStrict)
115 {
116 if (rgNameToLabel.ContainsKey(strName))
117 log.FAIL("There are duplicates of the name: " + strName.ToLower());
118
119 rgNameToLabel.Add(strName, nLabel);
120 }
121 else
122 {
123 if (rgNameToLabel.ContainsKey(strName))
124 rgNameToLabel[strName] = nLabel;
125 else
126 rgNameToLabel.Add(strName, nLabel);
127 }
128 }
129
130 return rgNameToLabel;
131 }
132
136 public List<LabelMapItem> item
137 {
138 get { return m_rgItems; }
139 }
140
145 public void Copy(LabelMap src)
146 {
147 m_rgItems = new List<LabelMapItem>();
148
149 foreach (LabelMapItem item in src.item)
150 {
151 m_rgItems.Add(item.Clone());
152 }
153 }
154
160 {
161 LabelMap p = new LabelMap();
162 p.Copy(this);
163 return p;
164 }
165
171 public RawProto ToProto(string strName)
172 {
173 RawProtoCollection rgChildren = new RawProtoCollection();
174
176 foreach (LabelMapItem item in m_rgItems)
177 {
178 col.Add(item.ToProto("item"));
179 }
180
181 rgChildren.Add(col);
182
183 return new RawProto(strName, "", rgChildren);
184 }
185
191 public static LabelMap FromProto(RawProto rp)
192 {
193 LabelMap p = new LabelMap();
194
195 RawProtoCollection col = rp.FindChildren("item");
196 foreach (RawProto child in col)
197 {
199 p.item.Add(item);
200 }
201
202 return p;
203 }
204 }
205
209 public class LabelMapItem
210 {
211 string m_strName;
212 string m_strDisplay;
213 int m_nLabel;
214
221 public LabelMapItem(int nLabel = 0, string strName = null, string strDisplay = null)
222 {
223 m_strName = strName;
224 m_nLabel = nLabel;
225 m_strDisplay = strDisplay;
226 }
227
231 public string name
232 {
233 get { return m_strName; }
234 set { m_strName = value; }
235 }
236
240 public string display
241 {
242 get { return m_strDisplay; }
243 set { m_strDisplay = value; }
244 }
245
249 public int label
250 {
251 get { return m_nLabel; }
252 set { m_nLabel = value; }
253 }
254
259 public void Copy(LabelMapItem src)
260 {
261 m_strName = src.m_strName;
262 m_strDisplay = src.m_strDisplay;
263 m_nLabel = src.m_nLabel;
264 }
265
271 {
272 return new LabelMapItem(m_nLabel, m_strName, m_strDisplay);
273 }
274
280 public RawProto ToProto(string strName)
281 {
282 RawProtoCollection rgChildren = new RawProtoCollection();
283
284 rgChildren.Add(new RawProto("name", m_strName));
285 rgChildren.Add(new RawProto("label", m_nLabel.ToString()));
286
287 if (!string.IsNullOrEmpty(m_strDisplay))
288 rgChildren.Add(new RawProto("display", m_strDisplay));
289
290 return new RawProto(strName, "", rgChildren);
291 }
292
299 {
300 LabelMapItem item = new LabelMapItem();
301 string strVal;
302
303 if ((strVal = rp.FindValue("name")) != null)
304 item.name = strVal;
305
306 if ((strVal = rp.FindValue("label")) != null)
307 item.label = int.Parse(strVal);
308
309 if ((strVal = rp.FindValue("display")) != null)
310 item.display = strVal;
311
312 return item;
313 }
314 }
315}
The Log class provides general output in text form.
Definition: Log.cs:13
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
Definition: RawProto.cs:17
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
Definition: RawProto.cs:263
Specifies the LabelMap used with SSD.
Definition: LabelMap.cs:22
Dictionary< string, int > MapToLabel(Log log, bool bStrict)
Map the names to their labels.
Definition: LabelMap.cs:105
LabelMap()
The constructor.
Definition: LabelMap.cs:28
LabelMapItem FindByLabel(int nLabel)
Find a label with its label id.
Definition: LabelMap.cs:37
Dictionary< int, string > MapToName(Log log, bool bStrict, bool bDisplayName)
Map the labels into a dictionary.
Definition: LabelMap.cs:71
List< LabelMapItem > item
Specifies the list of label items.
Definition: LabelMap.cs:137
RawProto ToProto(string strName)
Convert this object to a raw proto.
Definition: LabelMap.cs:171
LabelMapItem FindByName(string strName)
Find a label with a given name.
Definition: LabelMap.cs:53
static LabelMap FromProto(RawProto rp)
Parses the parameter from a RawProto.
Definition: LabelMap.cs:191
LabelMap Clone()
Return a copy of this object.
Definition: LabelMap.cs:159
void Copy(LabelMap src)
Copy the source object.
Definition: LabelMap.cs:145
The LabelMapItem class stores the information for a single label.
Definition: LabelMap.cs:210
int label
Get/set the label id.
Definition: LabelMap.cs:250
LabelMapItem Clone()
Return a copy of this object.
Definition: LabelMap.cs:270
string name
Get/set the label name.
Definition: LabelMap.cs:232
RawProto ToProto(string strName)
Convert this object to a raw proto.
Definition: LabelMap.cs:280
void Copy(LabelMapItem src)
Copy the source object.
Definition: LabelMap.cs:259
LabelMapItem(int nLabel=0, string strName=null, string strDisplay=null)
The constructor.
Definition: LabelMap.cs:221
static LabelMapItem FromProto(RawProto rp)
Parses the parameter from a RawProto.
Definition: LabelMap.cs:298
string display
Optionally, get/set the display name for the label.
Definition: LabelMap.cs:241
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12