MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ReshapeParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
6using MyCaffe.basecode;
7
8namespace MyCaffe.param
9{
13 [Serializable]
14 [TypeConverter(typeof(ExpandableObjectConverter))]
16 {
17 BlobShape m_shape = new BlobShape();
18 int m_nAxis = 0;
19 int m_nNumAxes = -1;
20
23 {
24 }
25
51 [Description("Specifies the output dimensions.")]
53 {
54 get { return m_shape; }
55 set { m_shape = value; }
56 }
57
79 [Description("Specifies the axis portion of the bottom blob's shape that is replaced by (included in) the reshape. By default (axis == 0 and num_axes == 1), the entire bottom blob is included in the reshape, and hence the shape field must specifiethe entire output shape.")]
80 public int axis
81 {
82 get { return m_nAxis; }
83 set { m_nAxis = value; }
84 }
85
108 [Description("Specifies the number of axes which specifies the extent of the reshape.")]
109 public int num_axes
110 {
111 get { return m_nNumAxes; }
112 set { m_nNumAxes = value; }
113 }
114
116 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
117 {
118 RawProto proto = RawProto.Parse(br.ReadString());
119 ReshapeParameter p = FromProto(proto);
120
121 if (!bNewInstance)
122 Copy(p);
123
124 return p;
125 }
126
128 public override void Copy(LayerParameterBase src)
129 {
131
132 if (p.m_shape != null)
133 m_shape = p.m_shape.Clone();
134
135 m_nAxis = p.m_nAxis;
136 m_nNumAxes = p.m_nNumAxes;
137 }
138
140 public override LayerParameterBase Clone()
141 {
143 p.Copy(this);
144 return p;
145 }
146
152 public override RawProto ToProto(string strName)
153 {
154 RawProtoCollection rgChildren = new RawProtoCollection();
155
156 rgChildren.Add(shape.ToProto("shape"));
157
158 if (axis != 0)
159 rgChildren.Add("axis", axis.ToString());
160
161 if (num_axes != -1)
162 rgChildren.Add("num_axes", num_axes.ToString());
163
164 return new RawProto(strName, "", rgChildren);
165 }
166
173 {
174 string strVal;
176
177 RawProto rpShape = rp.FindChild("shape");
178 if (rpShape != null)
179 p.shape = BlobShape.FromProto(rpShape);
180
181 if ((strVal = rp.FindValue("axis")) != null)
182 p.axis = int.Parse(strVal);
183
184 if ((strVal = rp.FindValue("num_axes")) != null)
185 p.num_axes = int.Parse(strVal);
186
187 return p;
188 }
189
190 private static int count(List<int> rg, int nStart, int nEnd)
191 {
192 int nCount = 1;
193
194 for (int i = nStart; i < nEnd; i++)
195 {
196 nCount *= rg[i];
197 }
198
199 return nCount;
200 }
201
210 public static List<int> CalculateCopyAxes(LayerParameter p, out int nInferredAxis, out int nConstantCount, Log log = null)
211 {
212 List<int> rgCopyAxes = new List<int>();
213
214 nInferredAxis = -1;
215 nConstantCount = 1;
216
217 BlobShape top_blob_shape = p.reshape_param.shape;
218 int top_num_axes = top_blob_shape.dim.Count();
219
220 for (int i = 0; i < top_num_axes; i++)
221 {
222 int top_dim = top_blob_shape.dim[i];
223
224 if (top_dim == 0)
225 {
226 rgCopyAxes.Add(i);
227 }
228 else if (top_dim == -1)
229 {
230 if (log != null)
231 log.CHECK_EQ(nInferredAxis, -1, "new shape contains multiple -1 dims; at most a single (1) value of -1 may be specified.");
232
233 nInferredAxis = i;
234 }
235 else
236 {
237 nConstantCount *= top_dim;
238 }
239 }
240
241 return rgCopyAxes;
242 }
243
254 public static List<int> Reshape(LayerParameter p, List<int> rgShape, List<int> rgCopyAxes, int nInferredAxis, int nConstantCount, Log log = null)
255 {
256 int num_axes1 = rgShape.Count;
257 int input_start_axis = p.reshape_param.axis;
258 int start_axis = (input_start_axis >= 0) ? input_start_axis : num_axes1 + input_start_axis + 1;
259
260 if (log != null)
261 {
262 log.CHECK_GE(start_axis, 0, "axis " + input_start_axis.ToString() + " out of range");
263 log.CHECK_LE(start_axis, num_axes1, "axis " + input_start_axis.ToString() + " out of range for " + num_axes1.ToString() + "-D input blob");
264 }
265
267 if (log != null)
268 log.CHECK_GE(num_axes, -1, "num_axes must be >= 0, or -1 for all");
269
270 int end_axis = (num_axes == -1) ? num_axes1 : (start_axis + num_axes);
271 if (log != null)
272 log.CHECK_LE(end_axis, num_axes1, "end_axis = axis + num_axes is out of range");
273
274 int num_axes_replaced = end_axis - start_axis;
275 int num_axes_retained = num_axes1 - num_axes_replaced;
276 BlobShape top_blob_shape = p.reshape_param.shape;
277 int num_new_axes = top_blob_shape.dim.Count;
278 List<int> rgTopShape = new List<int>();
279 int top_shape_index = 0;
280
281 for (int i = 0; i < start_axis; i++)
282 {
283 rgTopShape.Add(rgShape[i]);
284 top_shape_index++;
285 }
286
287 for (int i = 0; i < num_new_axes; i++)
288 {
289 rgTopShape.Add(top_blob_shape.dim[i]);
290 top_shape_index++;
291 }
292
293 for (int i = end_axis; i < num_axes1; i++)
294 {
295 rgTopShape.Add(rgShape[i]);
296 top_shape_index++;
297 }
298
299 if (log != null)
300 log.CHECK_EQ(top_shape_index, rgTopShape.Count, "The top shape count should equal the top_shape_index.");
301
302 for (int i = 0; i < rgCopyAxes.Count; i++)
303 {
304 int copy_axis_index = rgCopyAxes[i];
305
306 if (log != null)
307 log.CHECK_GT(num_axes1, start_axis + copy_axis_index, "new shape contains a 0, but there was no corresponding bottom axis to copy");
308
309 rgTopShape[start_axis + copy_axis_index] = rgShape[start_axis + copy_axis_index];
310 }
311
312 if (nInferredAxis >= 0)
313 {
314 // A -1 dim was specified; infer the correct dimension by computing the
315 // product of the other dimensions.
316 int explicit_count = nConstantCount;
317 explicit_count *= count(rgShape, 0, start_axis);
318 explicit_count *= count(rgShape, end_axis, rgShape.Count);
319
320 for (int i = 0; i < rgCopyAxes.Count; i++)
321 {
322 int copy_axis_index = rgCopyAxes[i];
323 explicit_count *= rgTopShape[start_axis + copy_axis_index];
324 }
325
326 int nCount = count(rgShape, 0, rgShape.Count);
327
328 if (log != null)
329 log.CHECK_EQ(0, nCount % explicit_count, "bottom count (" + nCount.ToString() + ") must be divisible by the product of the specified dimensions( " + explicit_count.ToString() + ")");
330
331 int inferred_dim = nCount / explicit_count;
332 rgTopShape[start_axis + nInferredAxis] = inferred_dim;
333 }
334
335 return rgTopShape;
336 }
337 }
338}
The Log class provides general output in text form.
Definition: Log.cs:13
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
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
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
BlobShape Clone()
Creates a copy of the BlobShape.
Definition: BlobShape.cs:102
static BlobShape FromProto(RawProto rp)
Parse a new BlobShape from a RawProto.
Definition: BlobShape.cs:167
List< int > dim
The blob shape dimensions.
Definition: BlobShape.cs:93
The LayerParameterBase is the base class for all other layer specific parameters.
Specifies the base parameter for all layers.
ReshapeParameter reshape_param
Returns the parameter set when initialized with LayerType.RESHAPE
Specifies the parameters for the ReshapeLayer.
static List< int > CalculateCopyAxes(LayerParameter p, out int nInferredAxis, out int nConstantCount, Log log=null)
Calculate the Copy Axes, inferred axis and constant count.
static List< int > Reshape(LayerParameter p, List< int > rgShape, List< int > rgCopyAxes, int nInferredAxis, int nConstantCount, Log log=null)
Calculates the new shape.
int num_axes
num_axes specifies the extent of the reshape.
int axis
Specifies the axis portion of the bottom blob's shape that is replaced by (included in) the reshape....
BlobShape shape
Specifies the output dimensions.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
static ReshapeParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
ReshapeParameter()
Constructor for the parameter.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
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