MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ResizeParameter.cs
1using System;
2using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using MyCaffe.basecode;
9
10namespace MyCaffe.param.ssd
11{
19 [Serializable]
20 [TypeConverter(typeof(ExpandableObjectConverter))]
22 {
23 float m_fProb = 0;
24 ResizeMode m_mode = ResizeMode.WARP;
25 PadMode m_pad = PadMode.CONSTANT;
26 List<InterpMode> m_rgInterp = new List<InterpMode>();
27 uint m_nHeight = 0;
28 uint m_nWidth = 0;
29 uint m_nHeightScale = 0;
30 uint m_nWidthScale = 0;
31 List<float> m_rgfPadValue = new List<float>();
32
36 public enum ResizeMode
37 {
41 WARP = 1,
45 FIT_SMALL_SIZE = 2,
49 FIT_LARGE_SIZE_AND_PAD = 3
50 }
51
55 public enum PadMode
56 {
60 CONSTANT = 1,
64 MIRRORED = 2,
68 REPEAT_NEAREST = 3
69 }
70
74 public enum InterpMode
75 {
79 LINEAR = 1,
83 AREA = 2,
87 NEAREST = 3,
91 CUBIC = 4,
95 LANCZOS4 = 5
96 }
97
102 public ResizeParameter(bool bActive) : base(bActive)
103 {
104 }
105
109 [Description("Get/set probability of using this resize policy.")]
110 public float prob
111 {
112 get { return m_fProb; }
113 set { m_fProb = value; }
114 }
115
119 [Description("Get/set the resizing mode.")]
121 {
122 get { return m_mode; }
123 set { m_mode = value; }
124 }
125
129 [Description("Get/set the resizing height.")]
130 public uint height
131 {
132 get { return m_nHeight; }
133 set { m_nHeight = value; }
134 }
135
139 [Description("Get/set the resizing width.")]
140 public uint width
141 {
142 get { return m_nWidth; }
143 set { m_nWidth = value; }
144 }
145
149 [Description("Get/set the resizing height scale used with FIT_SMALL_SIZE mode.")]
150 public uint height_scale
151 {
152 get { return m_nHeightScale; }
153 set { m_nHeightScale = value; }
154 }
155
159 [Description("Get/set the resizing width scale used with FIT_SMALL_SIZE_mode.")]
160 public uint width_scale
161 {
162 get { return m_nWidthScale; }
163 set { m_nWidthScale = value; }
164 }
165
169 [Description("Get/set the pad mode for FIT_LARGE_SIZE_AND_PAD mode.")]
171 {
172 get { return m_pad; }
173 set { m_pad = value; }
174 }
175
179 [Description("Get/set the pad value which is repeated once for all channels, or provided one pad value per channel.")]
180 public List<float> pad_value
181 {
182 get { return m_rgfPadValue; }
183 set { m_rgfPadValue = value; }
184 }
185
189 [Description("Get/set the interp mode which is repeated once for all channels, or provided once per channel.")]
190 public List<InterpMode> interp_mode
191 {
192 get { return m_rgInterp; }
193 set { m_rgInterp = value; }
194 }
195
196
203 public ResizeParameter Load(BinaryReader br, bool bNewInstance = true)
204 {
205 RawProto proto = RawProto.Parse(br.ReadString());
206 ResizeParameter p = FromProto(proto);
207
208 if (!bNewInstance)
209 Copy(p);
210
211 return p;
212 }
213
218 public override void Copy(OptionalParameter src)
219 {
220 base.Copy(src);
221
222 if (src is ResizeParameter)
223 {
225 m_fProb = p.prob;
226 m_mode = p.resize_mode;
227 m_pad = p.pad_mode;
228
229 m_rgInterp = new List<InterpMode>();
230 foreach (InterpMode interp in p.m_rgInterp)
231 {
232 m_rgInterp.Add(interp);
233 }
234
235 m_nHeight = p.height;
236 m_nWidth = p.width;
237 m_nHeightScale = p.height_scale;
238 m_nWidthScale = p.width_scale;
239
240 m_rgfPadValue = new List<float>();
241 foreach (float fPad in p.pad_value)
242 {
243 m_rgfPadValue.Add(fPad);
244 }
245 }
246 }
247
253 {
254 ResizeParameter p = new param.ssd.ResizeParameter(Active);
255 p.Copy(this);
256 return p;
257 }
258
264 public override RawProto ToProto(string strName)
265 {
266 RawProto rpBase = base.ToProto("option");
267 RawProtoCollection rgChildren = new RawProtoCollection();
268
269 rgChildren.Add(rpBase);
270 rgChildren.Add(new RawProto("active", Active.ToString()));
271 rgChildren.Add(new RawProto("prob", prob.ToString()));
272 rgChildren.Add(new RawProto("resize_mode", m_mode.ToString()));
273 rgChildren.Add(new RawProto("pad_mode", m_pad.ToString()));
274 rgChildren.Add(new RawProto("height", m_nHeight.ToString()));
275 rgChildren.Add(new RawProto("width", m_nWidth.ToString()));
276 rgChildren.Add(new RawProto("height_scale", m_nHeightScale.ToString()));
277 rgChildren.Add(new RawProto("width_scale", m_nWidthScale.ToString()));
278
279 foreach (InterpMode interp in m_rgInterp)
280 {
281 rgChildren.Add(new RawProto("interp_mode", interp.ToString()));
282 }
283
284 foreach (float fPad in m_rgfPadValue)
285 {
286 rgChildren.Add(new RawProto("pad_value", fPad.ToString()));
287 }
288
289 return new RawProto(strName, "", rgChildren);
290 }
291
297 public static new ResizeParameter FromProto(RawProto rp)
298 {
299 ResizeParameter p = new ResizeParameter(false);
300 string strVal;
301
302 RawProto rpOption = rp.FindChild("option");
303 if (rpOption != null)
304 ((OptionalParameter)p).Copy(OptionalParameter.FromProto(rpOption));
305
306 if ((strVal = rp.FindValue("active")) != null)
307 p.Active = bool.Parse(strVal);
308
309 if ((strVal = rp.FindValue("prob")) != null)
310 p.prob = BaseParameter.ParseFloat(strVal);
311
312 if ((strVal = rp.FindValue("resize_mode")) != null)
313 {
314 switch (strVal)
315 {
316 case "WARP":
317 p.resize_mode = ResizeMode.WARP;
318 break;
319
320 case "FIT_SMALL_SIZE":
321 p.resize_mode = ResizeMode.FIT_SMALL_SIZE;
322 break;
323
324 case "FIT_LARGE_SIZE_AND_PAD":
325 p.resize_mode = ResizeMode.FIT_LARGE_SIZE_AND_PAD;
326 break;
327
328 default:
329 throw new Exception("Unknown resize_mode '" + strVal + "'!");
330 }
331 }
332
333 if ((strVal = rp.FindValue("height")) != null)
334 p.height = uint.Parse(strVal);
335
336 if ((strVal = rp.FindValue("width")) != null)
337 p.width = uint.Parse(strVal);
338
339 if ((strVal = rp.FindValue("height_scale")) != null)
340 p.height_scale = uint.Parse(strVal);
341
342 if ((strVal = rp.FindValue("width_scale")) != null)
343 p.width_scale = uint.Parse(strVal);
344
345 if ((strVal = rp.FindValue("pad_mode")) != null)
346 {
347 switch (strVal)
348 {
349 case "CONSTANT":
350 p.pad_mode = PadMode.CONSTANT;
351 break;
352
353 case "MIRRORED":
354 p.pad_mode = PadMode.MIRRORED;
355 break;
356
357 case "REPEAT_NEAREST":
358 p.pad_mode = PadMode.REPEAT_NEAREST;
359 break;
360
361 default:
362 throw new Exception("Unknown pad_mode '" + strVal + "'!");
363 }
364 }
365
366 p.pad_value = new List<float>();
367 RawProtoCollection col = rp.FindChildren("pad_value");
368 foreach (RawProto rp1 in col)
369 {
370 if ((strVal = rp.FindValue("pad_value")) != null)
371 p.pad_value.Add(BaseParameter.ParseFloat(strVal));
372 }
373
374 p.interp_mode = new List<InterpMode>();
375 RawProtoCollection col1 = rp.FindChildren("interp_mode");
376 foreach (RawProto pr1 in col1)
377 {
378 strVal = pr1.Value;
379
380 switch (strVal)
381 {
382 case "LINEAR":
383 p.interp_mode.Add(InterpMode.LINEAR);
384 break;
385
386 case "AREA":
387 p.interp_mode.Add(InterpMode.AREA);
388 break;
389
390 case "NEAREST":
391 p.interp_mode.Add(InterpMode.NEAREST);
392 break;
393
394 case "CUBIC":
395 p.interp_mode.Add(InterpMode.CUBIC);
396 break;
397
398 case "LANCZOS4":
399 p.interp_mode.Add(InterpMode.LANCZOS4);
400 break;
401
402 default:
403 throw new Exception("Unknown interp_mode '" + strVal + "'!");
404 }
405 }
406
407 return p;
408 }
409 }
410}
The BaseParameter class is the base class for all other parameter classes.
static float ParseFloat(string strVal)
Parse float values using the US culture if the decimal separator = '.', then using the native culture...
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
string Value
Get/set the value of the node.
Definition: RawProto.cs:79
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
RawProtoCollection FindChildren(params string[] rgstrName)
Searches for all children with a given name in this node's children.
Definition: RawProto.cs:263
The OptionalParameter is the base class for parameters that are optional such as the MaskParameter,...
static OptionalParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
OptionalParameter(bool bActive=false)
The constructor.
bool Active
When active, the parameter is used, otherwise it is ignored.
Specifies the parameters for the ResizeParameter for use with SSD.
InterpMode
Defines the interpolation mode.
uint height
Get/set the resizing height.
List< InterpMode > interp_mode
Get/set the interp mode which is repeated once for all channels, or provided once per channel.
PadMode pad_mode
Get/set the pad mode for FIT_LARGE_SIZE_AND_PAD mode.
uint width
Get/set the resizing width.
ResizeParameter Clone()
Return a copy of this object.
override void Copy(OptionalParameter src)
Copy the source object.
ResizeParameter Load(BinaryReader br, bool bNewInstance=true)
Load the and return a new ResizeParameter.
ResizeMode
Defines the resizing mode.
static new ResizeParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
PadMode
Defines the padding mode.
ResizeMode resize_mode
Get/set the resizing mode.
float prob
Get/set probability of using this resize policy.
override RawProto ToProto(string strName)
Convert this object to a raw proto.
ResizeParameter(bool bActive)
The constructor.
uint width_scale
Get/set the resizing width scale used with FIT_SMALL_SIZE_mode.
uint height_scale
Get/set the resizing height scale used with FIT_SMALL_SIZE mode.
List< float > pad_value
Get/set the pad value which is repeated once for all channels, or provided one pad value per channel.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12