MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BlobProto.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using MyCaffe.basecode;
7using MyCaffe.common;
8
9namespace MyCaffe.param
10{
14 public class BlobProto : BaseParameter, ICloneable, IComparable, IBinaryPersist
15 {
16 BlobShape m_rgShape = null;
17 List<double> m_rgdfData = new List<double>();
18 List<double> m_rgdfDiff = new List<double>();
19 List<float> m_rgfData = new List<float>();
20 List<float> m_rgfDiff = new List<float>();
21
22 // 4D dimensions -- depreciated. Use 'shape' instead.
23 int? m_nNum = null;
24 int? m_nChannels = null;
25 int? m_nHeight = null;
26 int? m_nWidth = null;
27
31 public BlobProto()
32 {
33 }
34
39 public BlobProto(List<int> rgShape)
40 {
41 m_rgShape = new BlobShape(rgShape);
42 }
43
48 public void Save(BinaryWriter bw)
49 {
50 bool bHasShape = (m_rgShape != null) ? true : false;
51
52 bw.Write(bHasShape);
53 if (bHasShape)
54 m_rgShape.Save(bw);
55
56 Utility.Save<double>(bw, m_rgdfData);
57 Utility.Save<double>(bw, m_rgdfDiff);
58 Utility.Save<float>(bw, m_rgfData);
59 Utility.Save<float>(bw, m_rgfDiff);
60
61 bw.Write(m_nNum.HasValue);
62 if (m_nNum.HasValue)
63 bw.Write(m_nNum.Value);
64
65 bw.Write(m_nChannels.HasValue);
66 if (m_nChannels.HasValue)
67 bw.Write(m_nChannels.Value);
68
69 bw.Write(m_nHeight.HasValue);
70 if (m_nHeight.HasValue)
71 bw.Write(m_nHeight.Value);
72
73 bw.Write(m_nWidth.HasValue);
74 if (m_nWidth.HasValue)
75 bw.Write(m_nWidth.Value);
76 }
77
84 public object Load(BinaryReader br, bool bNewInstance)
85 {
86 BlobProto p = this;
87
88 p = new BlobProto();
89
90 if (br.ReadBoolean())
91 p.m_rgShape = BlobShape.Load(br);
92
93 p.m_rgdfData = Utility.Load<double>(br);
94 p.m_rgdfDiff = Utility.Load<double>(br);
95 p.m_rgfData = Utility.Load<float>(br);
96 p.m_rgfDiff = Utility.Load<float>(br);
97
98 if (br.ReadBoolean())
99 p.m_nNum = br.ReadInt32();
100
101 if (br.ReadBoolean())
102 p.m_nChannels = br.ReadInt32();
103
104 if (br.ReadBoolean())
105 p.m_nHeight = br.ReadInt32();
106
107 if (br.ReadBoolean())
108 p.m_nWidth = br.ReadInt32();
109
110 return p;
111 }
112
117 {
118 get { return m_rgShape; }
119 set { m_rgShape = value; }
120 }
121
125 public int? num
126 {
127 get { return m_nNum; }
128 set { m_nNum = value; }
129 }
130
134 public int? channels
135 {
136 get { return m_nChannels; }
137 set { m_nChannels = value; }
138 }
139
143 public int? height
144 {
145 get { return m_nHeight; }
146 set { m_nHeight = value; }
147 }
148
152 public int? width
153 {
154 get { return m_nWidth; }
155 set { m_nWidth = value; }
156 }
157
161 public List<double> double_data
162 {
163 get { return m_rgdfData; }
164 set { m_rgdfData = value; }
165 }
166
170 public List<double> double_diff
171 {
172 get { return m_rgdfDiff; }
173 set { m_rgdfDiff = value; }
174 }
175
179 public List<float> data
180 {
181 get { return m_rgfData; }
182 set { m_rgfData = value; }
183 }
184
188 public List<float> diff
189 {
190 get { return m_rgfDiff; }
191 set { m_rgfDiff = value; }
192 }
193
198 public object Clone()
199 {
200 BlobProto bp = new BlobProto(m_rgShape.dim);
201
202 bp.m_rgdfData = Utility.Clone<double>(m_rgdfData);
203 bp.m_rgdfDiff = Utility.Clone<double>(m_rgdfDiff);
204 bp.m_rgfData = Utility.Clone<float>(m_rgfData);
205 bp.m_rgfDiff = Utility.Clone<float>(m_rgfDiff);
206 bp.num = num;
207 bp.channels = channels;
208 bp.height = height;
209 bp.width = width;
210
211 return bp;
212 }
213
219 public override RawProto ToProto(string strName)
220 {
221 RawProtoCollection rgChildren = new RawProtoCollection();
222
223 if (shape != null)
224 rgChildren.Add(shape.ToProto("shape"));
225
226 rgChildren.Add("num", num);
227 rgChildren.Add("channels", channels);
228 rgChildren.Add("height", height);
229 rgChildren.Add("width", width);
230 rgChildren.Add<double>("double_data", double_data);
231 rgChildren.Add<double>("double_diff", double_diff);
232 rgChildren.Add<float>("data", data);
233 rgChildren.Add<float>("diff", diff);
234
235 return new RawProto(strName, "", rgChildren);
236 }
237
243 public static BlobProto FromProto(RawProto rp)
244 {
245 BlobProto p = new BlobProto();
246
247 RawProto rpShape = rp.FindChild("shape");
248 if (rpShape != null)
249 p.shape = BlobShape.FromProto(rpShape);
250
251 p.num = (int?)rp.FindValue("num", typeof(int));
252 p.channels = (int?)rp.FindValue("channels", typeof(int));
253 p.height = (int?)rp.FindValue("height", typeof(int));
254 p.width = (int?)rp.FindValue("width", typeof(int));
255 p.double_data = rp.FindArray<double>("double_data");
256 p.double_diff = rp.FindArray<double>("double_diff");
257 p.data = rp.FindArray<float>("data");
258 p.diff = rp.FindArray<float>("diff");
259
260 return p;
261 }
262
268 public int CompareTo(object obj)
269 {
270 BlobProto bp = obj as BlobProto;
271
272 if (bp == null)
273 return 1;
274
275 if (!Compare(bp))
276 return 1;
277
278 return 0;
279 }
280
286 public static BlobProto Load(BinaryReader br)
287 {
288 BlobProto b = new BlobProto();
289 return (BlobProto)b.Load(br, true);
290 }
291 }
292}
The BaseParameter class is the base class for all other parameter classes.
virtual bool Compare(BaseParameter p)
Compare this parameter to another parameter.
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
RawProto FindChild(string strName)
Searches for a given node.
Definition: RawProto.cs:231
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static void Save(BinaryWriter bw, List< double > rg)
Save a list of double to a binary writer.
Definition: Utility.cs:337
The BlobProto contains the descripion of a blob.
Definition: BlobProto.cs:15
object Clone()
Copies the BlobProto and returns a new instance.
Definition: BlobProto.cs:198
static BlobProto Load(BinaryReader br)
Load a new BlobProto from a binary reader.
Definition: BlobProto.cs:286
List< float > data
Get/set the data as a List of float.
Definition: BlobProto.cs:180
override RawProto ToProto(string strName)
Converts the BlobProto to a RawProto.
Definition: BlobProto.cs:219
List< double > double_diff
Get/set the diff as a List of double.
Definition: BlobProto.cs:171
object Load(BinaryReader br, bool bNewInstance)
Loads a BlobProto from a binary reader.
Definition: BlobProto.cs:84
BlobShape shape
Specifies the shape of the Blob.
Definition: BlobProto.cs:117
List< double > double_data
Get/set the data as a List of double.
Definition: BlobProto.cs:162
int? num
Specifies the number of inputs (such as images) in the Blob.
Definition: BlobProto.cs:126
List< float > diff
Get/set the diff as a List of float.
Definition: BlobProto.cs:189
int CompareTo(object obj)
Compares the BlobProto to another BlobProto.
Definition: BlobProto.cs:268
int? height
Specifies the height of each input.
Definition: BlobProto.cs:144
int? channels
Specifies the number of images per input.
Definition: BlobProto.cs:135
static BlobProto FromProto(RawProto rp)
Parses a new BlobProto from a RawProto.
Definition: BlobProto.cs:243
BlobProto()
Constructor for the BlobProto.
Definition: BlobProto.cs:31
BlobProto(List< int > rgShape)
Constructor for the BlobProto
Definition: BlobProto.cs:39
int? width
Specifies the width of each input.
Definition: BlobProto.cs:153
void Save(BinaryWriter bw)
Saves the BlobProto to a binary writer.
Definition: BlobProto.cs:48
Specifies the shape of a Blob.
Definition: BlobShape.cs:15
override RawProto ToProto(string strName)
Converts the BlobShape to a RawProto.
Definition: BlobShape.cs:153
static BlobShape FromProto(RawProto rp)
Parse a new BlobShape from a RawProto.
Definition: BlobShape.cs:167
object Load(BinaryReader br, bool bNewInstance)
Load the BlobShape from a binary reader.
Definition: BlobShape.cs:66
List< int > dim
The blob shape dimensions.
Definition: BlobShape.cs:93
void Save(BinaryWriter bw)
Save the BlobShape to a binary writer.
Definition: BlobShape.cs:55
The IBinaryPersist interface provides generic save and load functionality.
Definition: Utility.cs:16
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12