MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ResultDescriptor.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
9{
13 [Serializable]
15 {
16 int m_nIdx;
17 int m_nLabel;
18 int m_nSourceID;
19 DateTime m_dt;
20 List<KeyValuePair<int, double>> m_rgResults = new List<KeyValuePair<int, double>>();
21
34 public ResultDescriptor(int nID, string strName, string strOwner, int nIdx, int nLabel, int nResultCount, byte[] rgResults, int nSrcId, DateTime dt)
35 : base(nID, strName, strOwner)
36 {
37 m_nIdx = nIdx;
38 m_nLabel = nLabel;
39 m_nSourceID = nSrcId;
40 m_dt = dt;
41 m_rgResults = createResults(nResultCount, rgResults);
42 }
43
47 public int Index
48 {
49 get { return m_nIdx; }
50 }
51
55 public int Label
56 {
57 get { return m_nLabel; }
58 }
59
63 public int ResultCount
64 {
65 get { return m_rgResults.Count; }
66 }
67
71 public List<KeyValuePair<int, double>> Results
72 {
73 get { return m_rgResults; }
74 }
75
79 public int SourceID
80 {
81 get { return m_nSourceID; }
82 }
83
87 public DateTime TimeStamp
88 {
89 get { return m_dt; }
90 }
91
98 public static byte[] CreateResults(List<Result> rgResults, bool bInvert)
99 {
100 List<byte> rgData = new List<byte>();
101 double dfMax = double.MinValue;
102 double dfMin = double.MaxValue;
103
104 if (bInvert)
105 {
106 foreach (Result kv in rgResults)
107 {
108 if (dfMax < kv.Score)
109 dfMax = kv.Score;
110
111 if (dfMin > kv.Score)
112 dfMin = kv.Score;
113 }
114 }
115
116 using (MemoryStream ms = new MemoryStream())
117 using (BinaryWriter bw = new BinaryWriter(ms))
118 {
119 bw.Write(rgResults.Count);
120
121 foreach (Result kv in rgResults)
122 {
123 bw.Write(kv.Label);
124
125 double dfValue = kv.Score;
126
127 if (bInvert)
128 dfValue = dfMax - dfValue;
129
130 bw.Write(dfValue);
131 }
132
133 ms.Flush();
134 return ms.ToArray();
135 }
136 }
137
143 public static List<Result> GetResults(byte[] rgData)
144 {
145 List<Result> rgRes = new List<Result>();
146
147 using (MemoryStream ms = new MemoryStream(rgData))
148 using (BinaryReader br = new BinaryReader(ms))
149 {
150 return GetResults(br);
151 }
152 }
153
159 public static List<Result> GetResults(BinaryReader br)
160 {
161 List<Result> rgRes = new List<Result>();
162
163 int nCount = br.ReadInt32();
164
165 for (int i = 0; i < nCount; i++)
166 {
167 int nLabel = br.ReadInt32();
168 double dfVal = br.ReadDouble();
169
170 Result r = new Result(nLabel, dfVal);
171 rgRes.Add(r);
172 }
173
174 return rgRes;
175 }
176
182 public static byte[] CreateResults(List<Tuple<SimpleDatum, List<Result>>> rgrgResults)
183 {
184 using (MemoryStream ms = new MemoryStream())
185 using (BinaryWriter bw = new BinaryWriter(ms))
186 {
187 bw.Write(rgrgResults.Count);
188
189 for (int i = 0; i < rgrgResults.Count; i++)
190 {
191 bw.Write(rgrgResults[i].Item1.ImageID);
192 bw.Write(rgrgResults[i].Item1.Index);
193 bw.Write(rgrgResults[i].Item1.TimeStamp.ToFileTimeUtc());
194 bw.Write(CreateResults(rgrgResults[i].Item2, false));
195 }
196
197 ms.Flush();
198 return ms.ToArray();
199 }
200 }
201
208 public static List<Tuple<SimpleDatum, List<Result>>> GetResults(int nBatchCount, byte[] rgData)
209 {
210 if (nBatchCount <= 0)
211 throw new Exception("The batch count must be >= 1!");
212
213 List<Tuple<SimpleDatum, List<Result>>> rgRes1 = new List<Tuple<SimpleDatum, List<Result>>>();
214
215 using (MemoryStream ms = new MemoryStream(rgData))
216 using (BinaryReader br = new BinaryReader(ms))
217 {
218 int nCount = br.ReadInt32();
219 if (nCount != nBatchCount)
220 throw new Exception("The batch count does not match the expected count of " + nCount.ToString());
221
222 for (int i = 0; i < nCount; i++)
223 {
224 int nImageID = br.ReadInt32();
225 int nIdx = br.ReadInt32();
226 long lTime = br.ReadInt64();
227 DateTime dt = DateTime.FromFileTimeUtc(lTime);
228 List<Result> rgRes = GetResults(br);
229
230 SimpleDatum sd = new SimpleDatum();
231 sd.SetImageID(nImageID);
232 sd.Index = nIdx;
233 sd.TimeStamp = dt;
234
235 rgRes1.Add(new Tuple<SimpleDatum, List<Result>>(sd, rgRes));
236 }
237 }
238
239 return rgRes1;
240 }
241
242 private List<KeyValuePair<int, double>> createResults(int nCount, byte[] rgData)
243 {
244 List<KeyValuePair<int, double>> rgResults = new List<KeyValuePair<int, double>>();
245 int nIdx = 0;
246
247 for (int i = 0; i < nCount; i++)
248 {
249 int nVal = BitConverter.ToInt32(rgData, nIdx);
250 nIdx += sizeof(int);
251 double dfVal = BitConverter.ToDouble(rgData, nIdx);
252 nIdx += sizeof(double);
253
254 rgResults.Add(new KeyValuePair<int, double>(nVal, dfVal));
255 }
256
257 return rgResults;
258 }
259
264 public override string ToString()
265 {
266 return m_dt.ToString() + " ~ " + m_nLabel.ToString();
267 }
268 }
269}
The Result class contains a single result.
Definition: Result.cs:14
int Label
Returns the label.
Definition: Result.cs:36
double Score
Returns the score of the run.
Definition: Result.cs:53
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
void SetImageID(int nID)
Set the image ID.
DateTime TimeStamp
Get/set the Timestamp.
int Index
Returns the index of the SimpleDatum.
The BaseDescriptor is the base class for all other descriptors, where descriptors are used to describ...
The ResultDescriptor class describes the results of a run.
static List< Tuple< SimpleDatum, List< Result > > > GetResults(int nBatchCount, byte[] rgData)
Extracts the raw image result batch from the result binary data.
ResultDescriptor(int nID, string strName, string strOwner, int nIdx, int nLabel, int nResultCount, byte[] rgResults, int nSrcId, DateTime dt)
The ResultDescriptor constructor.
static List< Result > GetResults(BinaryReader br)
Extract the results from the binary data.
int Label
Returns the expected label of the result.
static byte[] CreateResults(List< Result > rgResults, bool bInvert)
The CreateResults function converts the list of (int nLabel, double dfResult) pairs into a array of b...
DateTime TimeStamp
Returns the time-stamp of the result.
static byte[] CreateResults(List< Tuple< SimpleDatum, List< Result > > > rgrgResults)
The CreateResults function converts the batch of lists of (int nLabel, double dfResult) pairs into a ...
int SourceID
Returns the data source ID.
static List< Result > GetResults(byte[] rgData)
Extract the results from the binary data.
List< KeyValuePair< int, double > > Results
Returns the raw result data that is converted into the full list of (int nLabel, double dfResult) pai...
int ResultCount
Returns the number of items (classes) participating in the results.
int Index
Returns the index of the results.
override string ToString()
Creates the string representation of the descriptor.
The descriptors namespace contains all descriptor used to describe various items stored within the da...