MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BinaryData.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using static MyCaffe.basecode.SimpleDatum;
8
9namespace MyCaffe.basecode
10{
14 public class BinaryData
15 {
19 public BinaryData()
20 {
21 }
22
28 public static DATA_FORMAT UnPackType(byte[] rg)
29 {
30 using (MemoryStream ms = new MemoryStream(rg))
31 using (BinaryReader br = new BinaryReader(ms))
32 {
33 int nFmt = br.ReadInt32();
34
35 if (nFmt != (int)DATA_FORMAT.LIST_DOUBLE &&
36 nFmt != (int)DATA_FORMAT.LIST_FLOAT)
37 return DATA_FORMAT.NONE;
38
39 return (DATA_FORMAT)nFmt;
40 }
41 }
42
49 public static byte[] Pack(List<double> rg, out DATA_FORMAT fmt)
50 {
51 fmt = DATA_FORMAT.LIST_DOUBLE;
52
53 using (MemoryStream ms = new MemoryStream())
54 using (BinaryWriter bw = new BinaryWriter(ms))
55 {
56 bw.Write((int)fmt);
57 bw.Write(rg.Count);
58
59 for (int i = 0; i < rg.Count; i++)
60 {
61 bw.Write(rg[i]);
62 }
63
64 bw.Flush();
65 return ms.ToArray();
66 }
67 }
68
75 public static List<double> UnPackDoubleList(byte[] rg, DATA_FORMAT fmtExpected)
76 {
77 using (MemoryStream ms = new MemoryStream(rg))
78 using (BinaryReader br = new BinaryReader(ms))
79 {
80 DATA_FORMAT fmt1 = (DATA_FORMAT)br.ReadInt32();
81
82 if (fmtExpected != DATA_FORMAT.LIST_DOUBLE)
83 throw new Exception("The format expected should be DATA_FORMAT.LIST_DOUBLE, but instead it is '" + fmtExpected.ToString() + "'.");
84
85 if (fmt1 != fmtExpected)
86 throw new Exception("Invalid data format, expected '" + fmtExpected.ToString() + "' but found '" + fmt1.ToString() + "'.");
87
88 int nCount = br.ReadInt32();
89 List<double> rgData = new List<double>();
90
91 for (int i = 0; i < nCount; i++)
92 {
93 rgData.Add(br.ReadDouble());
94 }
95
96 return rgData;
97 }
98 }
99
106 public static byte[] Pack(List<float> rg, out DATA_FORMAT fmt)
107 {
108 fmt = DATA_FORMAT.LIST_FLOAT;
109
110 using (MemoryStream ms = new MemoryStream())
111 using (BinaryWriter bw = new BinaryWriter(ms))
112 {
113 bw.Write((int)fmt);
114 bw.Write(rg.Count);
115
116 for (int i = 0; i < rg.Count; i++)
117 {
118 bw.Write(rg[i]);
119 }
120
121 bw.Flush();
122 return ms.ToArray();
123 }
124 }
125
132 public static List<float> UnPackFloatList(byte[] rg, DATA_FORMAT fmtExpected)
133 {
134 using (MemoryStream ms = new MemoryStream(rg))
135 using (BinaryReader br = new BinaryReader(ms))
136 {
137 DATA_FORMAT fmt1 = (DATA_FORMAT)br.ReadInt32();
138
139 if (fmtExpected != DATA_FORMAT.LIST_FLOAT)
140 throw new Exception("The format expected should be DATA_FORMAT.LIST_FLOAT, but instead it is '" + fmtExpected.ToString() + "'.");
141
142 if (fmt1 != fmtExpected)
143 throw new Exception("Invalid data format, expected '" + fmtExpected.ToString() + "' but found '" + fmt1.ToString() + "'.");
144
145 int nCount = br.ReadInt32();
146 List<float> rgData = new List<float>();
147
148 for (int i = 0; i < nCount; i++)
149 {
150 rgData.Add(br.ReadSingle());
151 }
152
153 return rgData;
154 }
155 }
156 }
157}
The BinaryData class is used to pack and unpack DataCriteria binary data, optionally stored within ea...
Definition: BinaryData.cs:15
static List< double > UnPackDoubleList(byte[] rg, DATA_FORMAT fmtExpected)
Unpack the byte array into a list of double values.
Definition: BinaryData.cs:75
static byte[] Pack(List< float > rg, out DATA_FORMAT fmt)
Pack a list of float into a byte array.
Definition: BinaryData.cs:106
static DATA_FORMAT UnPackType(byte[] rg)
Unpack the type packed into the byte array (if any).
Definition: BinaryData.cs:28
BinaryData()
The constructor.
Definition: BinaryData.cs:19
static List< float > UnPackFloatList(byte[] rg, DATA_FORMAT fmtExpected)
Unpack the byte array into a list of float values.
Definition: BinaryData.cs:132
static byte[] Pack(List< double > rg, out DATA_FORMAT fmt)
Pack a list of double into a byte array.
Definition: BinaryData.cs:49
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12