MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MergeParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
7
14namespace MyCaffe.param.beta
15{
19 [Serializable]
20 [TypeConverter(typeof(ExpandableObjectConverter))]
22 {
23 int m_nCopyAxis = 0;
24 int m_nOrderingMajorAxis = 1;
25 int m_nCopyCount = 0;
26 int m_nCopyDim1 = 0;
27 int m_nSrcStartIdx1 = 0;
28 int m_nDstStartIdx1 = 0;
29 int m_nCopyDim2 = 0;
30 int m_nSrcStartIdx2 = 0;
31 int m_nDstStartIdx2 = 0;
32 int m_nSrcSpatialDimStartIdx1 = 0;
33 int m_nDstSpatialDimStartIdx1 = 0;
34 int m_nSrcSpatialDimStartIdx2 = 0;
35 int m_nDstSpatialDimStartIdx2 = 0;
36 int m_nSpatialDimCopyCount = -1;
37 int m_nDstSpatialDim = 0;
38
41 {
42 }
43
50 [Description("Specifies axis providing the major ordering (e.g. axis=1 uses axis 1 as the major ordering with axis 0 following).")]
52 {
53 get { return m_nOrderingMajorAxis; }
54// set { m_nOrderingMajorAxis = value; } // Currenly LSTM type ordering only supported with order_major_axis=1.
55 }
56
63 [Description("Specifies axis providing the major ordering (e.g. axis=1 uses axis 1 as the major ordering with axis 0 following).")]
64 public int copy_axis
65 {
66 get { return m_nCopyAxis; }
67// set { m_nCopyAxis = value; } // Currently LSTM type ordering only supported with copy_axis=0.
68 }
69
73 [Description("Specifies the number of skip copies along the copy_axis to copy (e.g. this is the number of skips to perform and is usually = the batch size).")]
74 public int copy_count
75 {
76 get { return m_nCopyCount; }
77 set { m_nCopyCount = value; }
78 }
79
83 [Description("Specifies the src start index where copying begins in the first blob in bottom(0).")]
84 public int src_start_idx1
85 {
86 get { return m_nSrcStartIdx1; }
87 set { m_nSrcStartIdx1 = value; }
88 }
89
93 [Description("Specifies the dst start index where copying begins in the destination blob in top(0).")]
94 public int dst_start_idx1
95 {
96 get { return m_nDstStartIdx1; }
97 set { m_nDstStartIdx1 = value; }
98 }
99
103 [Description("Specifies the src1 spatial dim start index (only used when > 0)")]
105 {
106 get { return m_nSrcSpatialDimStartIdx1; }
107 set { m_nSrcSpatialDimStartIdx1 = value; }
108 }
109
113 [Description("Specifies the dst1 spatial dim start index (only used when > 0)")]
115 {
116 get { return m_nDstSpatialDimStartIdx1; }
117 set { m_nDstSpatialDimStartIdx1 = value; }
118 }
119
123 [Description("Specifies the src2 spatial dim start index (only used when > 0)")]
125 {
126 get { return m_nSrcSpatialDimStartIdx2; }
127 set { m_nSrcSpatialDimStartIdx2 = value; }
128 }
129
133 [Description("Specifies the dst2 spatial dim start index (only used when > 0)")]
135 {
136 get { return m_nDstSpatialDimStartIdx2; }
137 set { m_nDstSpatialDimStartIdx2 = value; }
138 }
139
143 [Description("Specifies the spatial dim copy count, used when less than the entire spatial dim is to be copied.")]
145 {
146 get { return m_nSpatialDimCopyCount; }
147 set { m_nSpatialDimCopyCount = value; }
148 }
149
153 [Description("Specifies the dst spatial dim which if not copied into is set to zero.")]
154 public int dst_spatialdim
155 {
156 get { return m_nDstSpatialDim; }
157 set { m_nDstSpatialDim = value; }
158 }
159
163 [Description("Specifies the dimension (sans the spatial dimension) to be copied from bottom(0) (the full copy size = copy_dim1 * spatial_dim which is calculated using axis dims after the copy axis).")]
164 public int copy_dim1
165 {
166 get { return m_nCopyDim1; }
167 set { m_nCopyDim1 = value; }
168 }
169
173 [Description("Specifies src the start index where copying begins in the second input blob in bottom(1).")]
174 public int src_start_idx2
175 {
176 get { return m_nSrcStartIdx2; }
177 set { m_nSrcStartIdx2 = value; }
178 }
179
183 [Description("Specifies the dst start index where copying begins for the second copy to dst blob in top(0).")]
184 public int dst_start_idx2
185 {
186 get { return m_nDstStartIdx2; }
187 set { m_nDstStartIdx2 = value; }
188 }
189
193 [Description("Specifies the dimension (sans the spatial dimension) to be copied from bottom(0) (the full copy size = copy_dim1 * spatial_dim which is calculated using axis dims after the copy axis).")]
194 public int copy_dim2
195 {
196 get { return m_nCopyDim2; }
197 set { m_nCopyDim2 = value; }
198 }
199
206 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
207 {
208 RawProto proto = RawProto.Parse(br.ReadString());
209 MergeParameter p = FromProto(proto);
210
211 if (!bNewInstance)
212 Copy(p);
213
214 return p;
215 }
216
221 public override void Copy(LayerParameterBase src)
222 {
224 m_nCopyAxis = p.m_nCopyAxis;
225 m_nOrderingMajorAxis = p.m_nOrderingMajorAxis;
226 m_nCopyCount = p.m_nCopyCount;
227 m_nSrcStartIdx1 = p.m_nSrcStartIdx1;
228 m_nSrcStartIdx2 = p.m_nSrcStartIdx2;
229 m_nDstStartIdx1 = p.m_nDstStartIdx1;
230 m_nDstStartIdx2 = p.m_nDstStartIdx2;
231 m_nCopyDim1 = p.m_nCopyDim1;
232 m_nCopyDim2 = p.m_nCopyDim2;
233 m_nSrcSpatialDimStartIdx1 = p.m_nSrcSpatialDimStartIdx1;
234 m_nDstSpatialDimStartIdx1 = p.m_nDstSpatialDimStartIdx1;
235 m_nSrcSpatialDimStartIdx2 = p.m_nSrcSpatialDimStartIdx2;
236 m_nDstSpatialDimStartIdx2 = p.m_nDstSpatialDimStartIdx2;
237 m_nSpatialDimCopyCount = p.m_nSpatialDimCopyCount;
238 m_nDstSpatialDim = p.m_nDstSpatialDim;
239 }
240
245 public override LayerParameterBase Clone()
246 {
248 p.Copy(this);
249 return p;
250 }
251
257 public override RawProto ToProto(string strName)
258 {
259 RawProtoCollection rgChildren = new RawProtoCollection();
260
261 rgChildren.Add("copy_axis", copy_axis.ToString());
262 rgChildren.Add("order_major_axis", order_major_axis.ToString());
263 rgChildren.Add("copy_count", copy_count.ToString());
264 rgChildren.Add("src_start_idx1", src_start_idx1.ToString());
265 rgChildren.Add("dst_start_idx1", dst_start_idx1.ToString());
266 rgChildren.Add("copy_dim1", copy_dim1.ToString());
267 rgChildren.Add("src_start_idx2", src_start_idx2.ToString());
268 rgChildren.Add("dst_start_idx2", dst_start_idx2.ToString());
269 rgChildren.Add("copy_dim2", copy_dim2.ToString());
270 rgChildren.Add("src_spatialdim_idx1", src_spatialdim_start_idx1.ToString());
271 rgChildren.Add("dst_spatialdim_idx1", dst_spatialdim_start_idx1.ToString());
272 rgChildren.Add("src_spatialdim_idx2", src_spatialdim_start_idx2.ToString());
273 rgChildren.Add("dst_spatialdim_idx2", dst_spatialdim_start_idx2.ToString());
274 rgChildren.Add("spatialdim_copy_count", spatialdim_copy_count.ToString());
275 rgChildren.Add("dst_spatialdim", dst_spatialdim.ToString());
276
277 return new RawProto(strName, "", rgChildren);
278 }
279
286 {
287 string strVal;
289
290// if ((strVal = rp.FindValue("copy_axis")) != null)
291// p.copy_axis = int.Parse(strVal);
292
293// if ((strVal = rp.FindValue("order_major_axis")) != null)
294// p.order_major_axis = int.Parse(strVal);
295
296 if ((strVal = rp.FindValue("copy_count")) != null)
297 p.copy_count = int.Parse(strVal);
298
299 if ((strVal = rp.FindValue("src_start_idx1")) != null)
300 p.src_start_idx1 = int.Parse(strVal);
301
302 if ((strVal = rp.FindValue("dst_start_idx1")) != null)
303 p.dst_start_idx1 = int.Parse(strVal);
304
305 if ((strVal = rp.FindValue("copy_dim1")) != null)
306 p.copy_dim1 = int.Parse(strVal);
307
308 if ((strVal = rp.FindValue("src_start_idx2")) != null)
309 p.src_start_idx2 = int.Parse(strVal);
310
311 if ((strVal = rp.FindValue("dst_start_idx2")) != null)
312 p.dst_start_idx2 = int.Parse(strVal);
313
314 if ((strVal = rp.FindValue("copy_dim2")) != null)
315 p.copy_dim2 = int.Parse(strVal);
316
317 if ((strVal = rp.FindValue("src_spatialdim_start_idx1")) != null)
318 p.src_spatialdim_start_idx1 = int.Parse(strVal);
319
320 if ((strVal = rp.FindValue("dst_spatialdim_start_idx1")) != null)
321 p.dst_spatialdim_start_idx1 = int.Parse(strVal);
322
323 if ((strVal = rp.FindValue("src_spatialdim_start_idx2")) != null)
324 p.src_spatialdim_start_idx2 = int.Parse(strVal);
325
326 if ((strVal = rp.FindValue("dst_spatialdim_start_idx2")) != null)
327 p.dst_spatialdim_start_idx2 = int.Parse(strVal);
328
329 if ((strVal = rp.FindValue("spatialdim_copy_count")) != null)
330 p.spatialdim_copy_count = int.Parse(strVal);
331
332 if ((strVal = rp.FindValue("dst_spatialdim")) != null)
333 p.dst_spatialdim = int.Parse(strVal);
334
335 return p;
336 }
337
346 public static List<int> Reshape(Log log, MergeParameter p, List<int> rgShape1, List<int> rgShape2)
347 {
348 while (rgShape2.Count > rgShape1.Count && rgShape2.Count > 0)
349 {
350 if (rgShape2[rgShape2.Count - 1] == 1)
351 rgShape2.RemoveAt(rgShape2.Count - 1);
352 }
353
354 while (rgShape1.Count > rgShape2.Count && rgShape1.Count > 0)
355 {
356 if (rgShape1[rgShape1.Count - 1] == 1)
357 rgShape1.RemoveAt(rgShape1.Count - 1);
358 }
359
360 log.CHECK_EQ(rgShape1.Count, rgShape2.Count, "The inputs must have the same number of axes.");
361 log.CHECK_LT(p.copy_axis, rgShape1.Count, "There must be more axes than the copy axis!");
362
363 int nSrcStartIdx1 = Utility.CanonicalAxisIndex(p.src_start_idx1, rgShape1[p.copy_axis]);
364 int nSrcStartIdx2 = Utility.CanonicalAxisIndex(p.src_start_idx2, rgShape2[p.copy_axis]);
365 int nDstStartIdx1 = Utility.CanonicalAxisIndex(p.dst_start_idx1, rgShape1[p.copy_axis]);
366 int nDstStartIdx2 = Utility.CanonicalAxisIndex(p.dst_start_idx2, rgShape2[p.copy_axis]);
367
368 List<int> rgNewShape = new List<int>();
369 for (int i = 0; i < rgShape1.Count; i++)
370 {
371 rgNewShape.Add(1);
372 }
373
374 for (int i = 0; i < p.copy_axis; i++)
375 {
376 log.CHECK_EQ(rgShape1[i], rgShape2[i], "Inputs must have the same dimensions up to the copy axis.");
377 rgNewShape[i] = rgShape1[i];
378 }
379
380 int nCopy1 = p.copy_dim1;
381 int nCopy2 = p.copy_dim2;
382 int nIdx = p.copy_axis;
383
384 rgNewShape[nIdx] = nCopy1 + nCopy2;
385 nIdx++;
386 rgNewShape[nIdx] = rgShape1[nIdx];
387 nIdx++;
388
389 for (int i = nIdx; i < rgNewShape.Count; i++)
390 {
391 if (p.m_nDstSpatialDim > 0)
392 {
393 rgNewShape[i] = p.m_nDstSpatialDim;
394 break;
395 }
396
397 if (p.spatialdim_copy_count <= 0)
398 {
399 log.CHECK_EQ(rgShape1[i], rgShape2[i], "Inputs must have the same dimensions after the copy axis.");
400 rgNewShape[i] = rgShape1[i];
401 }
402 }
403
404 return rgNewShape;
405 }
406 }
407}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void CHECK_LT(double df1, double df2, string str)
Test whether one number is less than another.
Definition: Log.cs:275
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
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
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 int CanonicalAxisIndex(int nIdx, int nNumAxes)
Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e...
Definition: Utility.cs:50
The LayerParameterBase is the base class for all other layer specific parameters.
Specifies the parameters for the MergeLayer.
static List< int > Reshape(Log log, MergeParameter p, List< int > rgShape1, List< int > rgShape2)
Calculate the new shape based on the merge parameter settings and the specified input shapes.
int dst_spatialdim_start_idx1
Specifies the dst1 spatial dim start index (only used when > 0).
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
int dst_start_idx2
Specifies the dst start index where copying begins for the second copy to dst blob in top(0).
int dst_spatialdim_start_idx2
Specifies the dst2 spatial dim start index (only used when > 0).
static MergeParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
int src_spatialdim_start_idx2
Specifies the src2 spatial dim start index (only used when > 0).
int order_major_axis
Specifies axis providing the major ordering (e.g. axis=1 uses axis 1 as the major ordering with axis ...
int spatialdim_copy_count
Specifies the spatial dim copy count, used when less than the entire spatial dim is to be copied.
int copy_dim1
Specifies the dimension (sans the spatial dimension) to be copied (the full copy size = copy_dim * sp...
int src_start_idx2
Specifies the src start index where copying begins in the second input blob in bottom(1).
override void Copy(LayerParameterBase src)
Copy on parameter to another.
int src_start_idx1
Specifies the src start index where copying begins in the first blob in bottom(0).
int copy_dim2
Specifies the dimension (sans the spatial dimension) to be copied (the full copy size = copy_dim * sp...
int copy_axis
Specifies axis along which the indexing is applied when copying.
MergeParameter()
Constructor for the parameter.
int dst_start_idx1
Specifies the dst start index where copying begins in the destination blob in top(0).
int copy_count
Specifies the number of skip copies along the copy_axis to copy (e.g. this is the number of skips to ...
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
int dst_spatialdim
Specifies the dst spatial dim which if not copied into is set to zero.
int src_spatialdim_start_idx1
Specifies the src1 spatial dim start index (only used when > 0).
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.param.beta parameters are used by the MyCaffe.layer.beta layers.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12