MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
LRNParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
6using MyCaffe.basecode;
7
8namespace MyCaffe.param
9{
17 [Serializable]
18 [TypeConverter(typeof(ExpandableObjectConverter))]
20 {
21 uint m_nLocalSize = 5;
22 double m_dfAlpha = 1e-4; // caffe default = 1.0, cudnn default = 1e-4
23 double m_dfBeta = 0.75; // caffe default = 0.75, cudnn default = 0.75
24 NormRegion m_normRegion = NormRegion.ACROSS_CHANNELS;
25 double m_dfK = 2.0; // caffe default = 1.0, cudnn default = 2.0
26
30 public enum NormRegion
31 {
35 ACROSS_CHANNELS = 0,
39 WITHIN_CHANNEL = 1
40 }
41
43 public LRNParameter()
44 : base()
45 {
46 }
47
52 public string useCaffeReason()
53 {
54 if (engine == Engine.CAFFE)
55 return "The engine setting is set on CAFFE.";
56
57 if (norm_region == NormRegion.WITHIN_CHANNEL)
58 return "Currently using the normalization region 'WITHIN_CHANNEL' returns inconsistent errors from Caffe.";
59
60 return "";
61 }
62
67 public bool useCudnn()
68 {
69 if (engine == EngineParameter.Engine.CAFFE ||
70 norm_region == LRNParameter.NormRegion.WITHIN_CHANNEL) // Currently CuDNN within channel Forward pass returns inconsistent results from Caffe.
71 return false;
72
73 return true;
74 }
75
79 [Description("Specifies the local size of the normalization window width.")]
80 public uint local_size
81 {
82 get { return m_nLocalSize; }
83 set { m_nLocalSize = value; }
84 }
85
89 [Description("Specifies the alpha value used for variance scaling in the normalization formula. NOTE: cuDNN uses a default of alpha = 1e-4, whereas CAFFE uses a default of alpha = 1.0.")]
90 public double alpha
91 {
92 get { return m_dfAlpha; }
93 set { m_dfAlpha = value; }
94 }
95
99 [Description("Specifies the beta value used as the power parameter in the normalization formula. NOTE: both cuDNN and CAFFE use a default of beta = 0.75.")]
100 public double beta
101 {
102 get { return m_dfBeta; }
103 set { m_dfBeta = value; }
104 }
105
109 [Description("Specifies the region over which to normalize.")]
111 {
112 get { return m_normRegion; }
113 set { m_normRegion = value; }
114 }
115
119 [Description("Specifies the k value used by the normalization parameter. NOTE: cuDNN uses a default of k = 2.0, whereas CAFFE uses a default of k = 2.0.")]
120 public double k
121 {
122 get { return m_dfK; }
123 set { m_dfK = value; }
124 }
125
127 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
128 {
129 RawProto proto = RawProto.Parse(br.ReadString());
130 LRNParameter p = FromProto(proto);
131
132 if (!bNewInstance)
133 Copy(p);
134
135 return p;
136 }
137
139 public override void Copy(LayerParameterBase src)
140 {
141 base.Copy(src);
142
143 if (src is LRNParameter)
144 {
145 LRNParameter p = (LRNParameter)src;
146 m_nLocalSize = p.m_nLocalSize;
147 m_dfAlpha = p.m_dfAlpha;
148 m_dfBeta = p.m_dfBeta;
149 m_normRegion = p.m_normRegion;
150 m_dfK = p.m_dfK;
151 }
152 }
153
155 public override LayerParameterBase Clone()
156 {
157 LRNParameter p = new LRNParameter();
158 p.Copy(this);
159 return p;
160 }
161
163 public override RawProto ToProto(string strName)
164 {
165 RawProto rpBase = base.ToProto("engine");
166 RawProtoCollection rgChildren = new RawProtoCollection();
167
168 rgChildren.Add(rpBase.Children);
169 rgChildren.Add("local_size", local_size.ToString());
170 rgChildren.Add("alpha", alpha.ToString());
171 rgChildren.Add("beta", beta.ToString());
172 rgChildren.Add("norm_region", norm_region.ToString());
173 rgChildren.Add("k", k.ToString());
174
175 return new RawProto(strName, "", rgChildren);
176 }
177
183 public static new LRNParameter FromProto(RawProto rp)
184 {
185 string strVal;
186 LRNParameter p = new LRNParameter();
187
189
190 if ((strVal = rp.FindValue("local_size")) != null)
191 p.local_size = uint.Parse(strVal);
192
193 if ((strVal = rp.FindValue("alpha")) != null)
194 p.alpha = ParseDouble(strVal);
195
196 if ((strVal = rp.FindValue("beta")) != null)
197 p.beta = ParseDouble(strVal);
198
199 if ((strVal = rp.FindValue("norm_region")) != null)
200 {
201 switch (strVal)
202 {
203 case "ACROSS_CHANNELS":
204 p.norm_region = NormRegion.ACROSS_CHANNELS;
205 break;
206
207 case "WITHIN_CHANNEL":
208 p.norm_region = NormRegion.WITHIN_CHANNEL;
209 break;
210
211 default:
212 throw new Exception("Unknown 'norm_region' value: " + strVal);
213 }
214 }
215
216 if ((strVal = rp.FindValue("k")) != null)
217 p.k = ParseDouble(strVal);
218
219 return p;
220 }
221 }
222}
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
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
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 whether to use the NVIDIA cuDnn version or Caffe version of a given forward/backward operat...
Engine engine
Specifies the Engine in use.
EngineParameter()
Constructor for the parameter.
static EngineParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
Engine
Defines the type of engine to use.
Specifies the parameter for the LRNLayer.
Definition: LRNParameter.cs:20
bool useCudnn()
Queries whether or not to use NVIDIA's cuDnn.
Definition: LRNParameter.cs:67
LRNParameter()
Constructor for the parameter.
Definition: LRNParameter.cs:43
NormRegion
Defines the normalization region.
Definition: LRNParameter.cs:31
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
double beta
Specifies the beta value used as the power parameter in the normalization formula....
NormRegion norm_region
Specifies the region over which to normalize.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
static new LRNParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
uint local_size
Specifies the local size of the normalization window width.
Definition: LRNParameter.cs:81
string useCaffeReason()
Returns the reason that Caffe version was used instead of NVIDIA's cuDnn.
Definition: LRNParameter.cs:52
double alpha
Specifies the alpha value used for variance scaling in the normalization formula. NOTE: cuDNN uses a ...
Definition: LRNParameter.cs:91
double k
Specifies the k value used by the normalization parameter. NOTE: cuDNN uses a default of k = 2....
override void Copy(LayerParameterBase src)
Copy on parameter to another.
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