MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ConvolutionParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
6using MyCaffe.basecode;
7
8namespace MyCaffe.param
9{
21 [Serializable]
22 [TypeConverter(typeof(ExpandableObjectConverter))]
24 {
25 uint m_nNumOutput = 0;
26 bool m_bBiasTerm = true;
27 uint m_nGroup = 1;
28 FillerParameter m_fillerParam_weights = new FillerParameter("xavier");
29 FillerParameter m_fillerParam_bias = new FillerParameter("constant", 0.1);
30 int m_nAxis = 1;
31 bool m_bForceNDIm2Col = false;
32 int m_nCudnnWorkspaceLimit = 1024 * 1024; // Used with cuDnn only.
33 bool m_bCudnnWorkspaceAllowOnGroups = false;
34 bool m_bCudnnEnableTensorCores = false;
35
38 {
39 }
40
45 public string useCaffeReason(int nNumSpatialAxes = 2)
46 {
47 if (engine == Engine.CAFFE)
48 return "The engine setting is set on CAFFE.";
49
50 if (nNumSpatialAxes != 2)
51 return "Currently only 2 spatial axes (ht x wd) are supported by cuDnn.";
52
53 return "";
54 }
55
61 public bool useCudnn(int nNumSpatialAxes = 2)
62 {
63 if (engine == EngineParameter.Engine.CAFFE)
64 return false;
65
66 if (nNumSpatialAxes != 2)
67 return false;
68
69 return true;
70 }
71
72
80 [Description("Specifies whether or not (default) worspaces are used when the group > 1. Currently using workspaces on groups > 1 can cause cuDnn errors and for this reason defaults to false.")]
82 {
83 get { return m_bCudnnWorkspaceAllowOnGroups; }
84 set { m_bCudnnWorkspaceAllowOnGroups = value; }
85 }
86
90 [Description("Specifies the cuDnn workspace limit to use.\n\n - A positive value, greater than zero, directs cuDnn to use the most efficient algorithm within the memory limit specified.\n - A value equal to zero directs cuDnn to use the most memory efficient algorithm.\n\n The default for this value is 1,048,576 (i.e 1024 * 1024 which is then multiplied by 8 internally). This value is only used by the CUDNN and DEFAULT engines.")]
91 [ReadOnly(true)]
93 {
94 get { return m_nCudnnWorkspaceLimit; }
95 set { m_nCudnnWorkspaceLimit = value; }
96 }
97
104 [Description("Specifies to enable CUDA tensor cores when performing the convolution which is faster but not supported by all GPU's. When not supported, the default math is used.")]
106 {
107 get { return m_bCudnnEnableTensorCores; }
108 set { m_bCudnnEnableTensorCores = value; }
109 }
110
114 [Description("Specifies the number of outputs for the layer.")]
115 public uint num_output
116 {
117 get { return m_nNumOutput; }
118 set { m_nNumOutput = value; }
119 }
120
124 [Description("Specifies the whether to have bias terms or not.")]
125 public bool bias_term
126 {
127 get { return m_bBiasTerm; }
128 set { m_bBiasTerm = value; }
129 }
130
134 [Description("Specifies the group size for group convolution.")]
135 public uint group
136 {
137 get { return m_nGroup; }
138 set { m_nGroup = value; }
139 }
140
144 [Category("Fillers")]
145 [Description("Specifies the filler used to initialize the weights.")]
147 {
148 get { return m_fillerParam_weights; }
149 set { m_fillerParam_weights = value; }
150 }
151
155 [Category("Fillers")]
156 [Description("Specifies the filler used to initialize the bias.")]
158 {
159 get { return m_fillerParam_bias; }
160 set { m_fillerParam_bias = value; }
161 }
162
174 [Description("Specifies the axis to interpret as 'channels' when performing convolution The preceding dimensions are treated as independent inputs; succeeding dimensions are treated as 'spatial'. With (N,C,H,W) inputs and axis == 1 (the default), we perform N independent 2D convolutions, sliding C-channel (or C/g-channels, for groups > 1) filters across the spatial axes (H,W) of the input.")]
175 public int axis
176 {
177 get { return m_nAxis; }
178 set { m_nAxis = value; }
179 }
180
188 [Description("Specifies whether to force use of the general ND convolution, even if a specific implementation for blobs of the appopriate number of spatial dimensions is available. (Currently, there is only a 2D-specific convolution implementation; for input blobs with 'num_axes' != 2, this option is ignored and the ND implementation is used.)")]
189 public bool force_nd_im2col
190 {
191 get { return m_bForceNDIm2Col; }
192 set { m_bForceNDIm2Col = value; }
193 }
194
196 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
197 {
198 RawProto proto = RawProto.Parse(br.ReadString());
200
201 if (!bNewInstance)
202 Copy(p);
203
204 return p;
205 }
206
208 public override void Copy(LayerParameterBase src)
209 {
210 base.Copy(src);
211
212 if (src is ConvolutionParameter)
213 {
215 m_nNumOutput = p.m_nNumOutput;
216 m_bBiasTerm = p.m_bBiasTerm;
217 m_nGroup = p.m_nGroup;
218
219 if (p.m_fillerParam_bias != null)
220 m_fillerParam_bias = p.m_fillerParam_bias.Clone();
221
222 if (p.m_fillerParam_weights != null)
223 m_fillerParam_weights = p.m_fillerParam_weights.Clone();
224
225 m_nAxis = p.m_nAxis;
226 m_bForceNDIm2Col = p.m_bForceNDIm2Col;
227 m_nCudnnWorkspaceLimit = p.m_nCudnnWorkspaceLimit;
228 m_bCudnnWorkspaceAllowOnGroups = p.m_bCudnnWorkspaceAllowOnGroups;
229 m_bCudnnEnableTensorCores = p.m_bCudnnEnableTensorCores;
230 }
231 }
232
234 public override LayerParameterBase Clone()
235 {
237 p.Copy(this);
238 return p;
239 }
240
242 public override RawProto ToProto(string strName)
243 {
244 RawProto rpBase = base.ToProto("kernel");
245 RawProtoCollection rgChildren = new RawProtoCollection();
246
247 rgChildren.Add(rpBase.Children);
248 rgChildren.Add("num_output", num_output.ToString());
249
250 if (bias_term != true)
251 rgChildren.Add("bias_term", bias_term.ToString());
252
253 if (group != 1)
254 rgChildren.Add("group", group.ToString());
255
256 if (m_fillerParam_weights != null)
257 rgChildren.Add(m_fillerParam_weights.ToProto("weight_filler"));
258
259 if (bias_term && m_fillerParam_bias != null)
260 rgChildren.Add(m_fillerParam_bias.ToProto("bias_filler"));
261
262 if (axis != 1)
263 rgChildren.Add("axis", axis.ToString());
264
265 if (force_nd_im2col != false)
266 rgChildren.Add("force_nd_im2col", force_nd_im2col.ToString());
267
268 if (cudnn_workspace_limit != (1024 * 1024))
269 rgChildren.Add("cudnn_workspace_limit", cudnn_workspace_limit.ToString());
270
272 rgChildren.Add("cudnn_worspace_allow_on_groups", cudnn_workspace_allow_on_groups.ToString());
273
275 rgChildren.Add("cudnn_enable_tensor_cores", cudnn_enable_tensor_cores.ToString());
276
277 return new RawProto(strName, "", rgChildren);
278 }
279
282 {
283 string strVal;
285
287
288 if ((strVal = rp.FindValue("num_output")) != null)
289 p.num_output = uint.Parse(strVal);
290
291 if ((strVal = rp.FindValue("bias_term")) != null)
292 p.bias_term = bool.Parse(strVal);
293
294 if ((strVal = rp.FindValue("group")) != null)
295 p.group = uint.Parse(strVal);
296
297 RawProto rpWeightFiller = rp.FindChild("weight_filler");
298 if (rpWeightFiller != null)
299 p.weight_filler = FillerParameter.FromProto(rpWeightFiller);
300
301 RawProto rpBiasFiller = rp.FindChild("bias_filler");
302 if (rpBiasFiller != null)
303 p.bias_filler = FillerParameter.FromProto(rpBiasFiller);
304
305 if ((strVal = rp.FindValue("axis")) != null)
306 p.axis = int.Parse(strVal);
307
308 if ((strVal = rp.FindValue("force_nd_im2col")) != null)
309 p.force_nd_im2col = bool.Parse(strVal);
310
311 if ((strVal = rp.FindValue("cudnn_workspace_limit")) != null)
312 p.cudnn_workspace_limit = int.Parse(strVal);
313
314 if ((strVal = rp.FindValue("cudnn_worspace_allow_on_groups")) != null)
315 p.cudnn_workspace_allow_on_groups = bool.Parse(strVal);
316
317 if ((strVal = rp.FindValue("cudnn_enable_tensor_cores")) != null)
318 p.cudnn_enable_tensor_cores = bool.Parse(strVal);
319
320 return p;
321 }
322 }
323}
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
RawProtoCollection Children
Returns a collection of this nodes child nodes.
Definition: RawProto.cs:96
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 parameters for the ConvolutionLayer. The default weight filler is set to the XavierFill...
bool cudnn_enable_tensor_cores
Specifies to enable the CUDA tensor cores when performing the convolution which is faster but not sup...
FillerParameter weight_filler
The filler for the weight. The default is set to use the 'xavier' filler.
static new ConvolutionParameter FromProto(RawProto rp)
Parse a RawProto into a new instance of the parameter.
uint group
The group size for group convolution.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
bool useCudnn(int nNumSpatialAxes=2)
Queries whether or not to use NVIDIA's cuDnn.
bool force_nd_im2col
Whether to force use of the general ND convolution, even if a specific implementation for blobs of th...
int axis
The axis to interpret as 'channels' when performing convolution. Preceding dimensions are treated as ...
FillerParameter bias_filler
The filler for the bias. The default is set to use the 'constant = 0.1' filler.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
bool bias_term
Whether to have bias terms or not.
int cudnn_workspace_limit
Specifies the workspace limit used by cuDnn. A value of 0 directs cuDNN to use the fastest algorithm ...
override void Copy(LayerParameterBase src)
Copy on parameter to another.
string useCaffeReason(int nNumSpatialAxes=2)
Returns the reason that Caffe version was used instead of NVIDIA's cuDnn.
ConvolutionParameter()
Constructor for the parameter.
uint num_output
The number of outputs for the layer.
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
bool cudnn_workspace_allow_on_groups
When true, allows workspace usage on groups > 1 (default = false).
Specifies whether to use the NVIDIA cuDnn version or Caffe version of a given forward/backward operat...
Engine engine
Specifies the Engine in use.
Engine
Defines the type of engine to use.
Specifies the filler parameters used to create each Filler.
static FillerParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
FillerParameter Clone()
Creates a new copy of this instance of the parameter.
Specifies the basic kernel parameters (used by convolution and pooling)
static new KernelParameter FromProto(RawProto rp)
Parse a RawProto into a new instance of the parameter.
KernelParameter()
Constructor for the parameter.
The LayerParameterBase is the base class for all other layer specific parameters.
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