MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
PriorBoxParameter.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 List<float> m_rgMinSize = new List<float>();
24 List<float> m_rgMaxSize = new List<float>();
25 List<float> m_rgAspectRatio = new List<float>();
26 bool m_bFlip = true;
27 bool m_bClip = false;
28 List<float> m_rgVariance = new List<float>();
29 uint? m_nImgSize = null;
30 uint? m_nImgH = null;
31 uint? m_nImgW = null;
32 float? m_fStep = null;
33 float? m_fStepH = null;
34 float? m_fStepW = null;
35 float m_fOffset = 0.5f;
36
40 public enum CodeType
41 {
45 CORNER = 1,
49 CENTER_SIZE = 2,
53 CORNER_SIZE = 3
54 }
55
61 public static CodeType CodeTypeFromString(string str)
62 {
63 switch (str)
64 {
65 case "CORNER":
66 return CodeType.CORNER;
67
68 case "CENTER_SIZE":
69 return CodeType.CENTER_SIZE;
70
71 case "CORNER_SIZE":
72 return CodeType.CORNER_SIZE;
73
74 default:
75 throw new Exception("Unknown CodeType '" + str + "'!");
76 }
77 }
78
83 {
84 }
85
89 [Description("Specifies the minimum box size (in pixels) and is required!")]
90 public List<float> min_size
91 {
92 get { return m_rgMinSize; }
93 set { m_rgMinSize = value; }
94 }
95
99 [Description("Specifies the maximum box size (in pixels) and is required!")]
100 public List<float> max_size
101 {
102 get { return m_rgMaxSize; }
103 set { m_rgMaxSize = value; }
104 }
105
110 [Description("Specifies various aspect ratios. Duplicate ratios are ignored. If none are provided, a default ratio of 1 is used.")]
111 public List<float> aspect_ratio
112 {
113 get { return m_rgAspectRatio; }
114 set { m_rgAspectRatio = value; }
115 }
116
122 [Description("Specifies whether or not to flip each aspect ratio. For example, if there is an aspect ratio 'r' we will generate aspect ratio '1.0/r' as well.")]
123 public bool flip
124 {
125 get { return m_bFlip; }
126 set { m_bFlip = value; }
127 }
128
132 [Description("Specifies whether or not to clip the prior so that it is within [0,1].")]
133 public bool clip
134 {
135 get { return m_bClip; }
136 set { m_bClip = value; }
137 }
138
142 [Description("Specifies the variance for adjusting the prior boxes.")]
143 public List<float> variance
144 {
145 get { return m_rgVariance; }
146 set { m_rgVariance = value; }
147 }
148
158 [Description("Specifies the image size. By default we calculate the img_height, img_width, step_x and step_y based on bottom[0] (feat) and bottom[1] (img). Unless these values are explicitly provided here. Either the img_h and img_w are used or the img_size, but not both.")]
159 public uint? img_size
160 {
161 get { return m_nImgSize; }
162 set { m_nImgSize = value; }
163 }
164
174 [Description("Specifies the image height. By default we calculate the img_height, img_width, step_x and step_y based on bottom[0] (feat) and bottom[1] (img). Unless these values are explicitly provided here. Either the img_h and img_w are used or the img_size, but not both.")]
175 public uint? img_h
176 {
177 get { return m_nImgH; }
178 set { m_nImgH = value; }
179 }
180
190 [Description("Specifies the image width. By default we calculatethe img_height, img_width, step_x and step_y based on bottom[0] (feat) and bottom[1] (img). Unless these values are explicitly provided here. Either the img_h and img_w are used or the img_size, but not both.")]
191 public uint? img_w
192 {
193 get { return m_nImgW; }
194 set { m_nImgW = value; }
195 }
196
200 [Description("Specifies the excplicit step size to use.")]
201 public float? step
202 {
203 get { return m_fStep; }
204 set { m_fStep = value; }
205 }
206
213 [Description("Specifies the explicit step size to use along height. Either the step_h and step_w are used or the step, but not both.")]
214 public float? step_h
215 {
216 get { return m_fStepH; }
217 set { m_fStepH = value; }
218 }
219
226 [Description("Specifies the explicit step size to use along width. Either the step_h and step_w are used or the step, but not both.")]
227 public float? step_w
228 {
229 get { return m_fStepW; }
230 set { m_fStepW = value; }
231 }
232
236 [Description("Specifies the offset to the top left corner of each cell.")]
237 public float offset
238 {
239 get { return m_fOffset; }
240 set { m_fOffset = value; }
241 }
242
249 public override object Load(BinaryReader br, bool bNewInstance = true)
250 {
251 RawProto proto = RawProto.Parse(br.ReadString());
252 PriorBoxParameter p = FromProto(proto);
253
254 if (!bNewInstance)
255 Copy(p);
256
257 return p;
258 }
259
264 public override void Copy(LayerParameterBase src)
265 {
267
268 m_rgMinSize = Utility.Clone<float>(p.min_size);
269 m_rgMaxSize = Utility.Clone<float>(p.max_size);
270 m_rgAspectRatio = Utility.Clone<float>(p.aspect_ratio);
271 m_bFlip = p.flip;
272 m_bClip = p.clip;
273 m_rgVariance = Utility.Clone<float>(p.variance);
274 m_nImgSize = p.img_size;
275 m_nImgH = p.img_h;
276 m_nImgW = p.img_w;
277 m_fStep = p.step;
278 m_fStepH = p.step_h;
279 m_fStepW = p.step_w;
280 m_fOffset = p.offset;
281 }
282
287 public override LayerParameterBase Clone()
288 {
289 PriorBoxParameter p = new param.ssd.PriorBoxParameter();
290 p.Copy(this);
291 return p;
292 }
293
299 public override RawProto ToProto(string strName)
300 {
301 RawProtoCollection rgChildren = new RawProtoCollection();
302
303 rgChildren.Add<float>("min_size", min_size);
304 rgChildren.Add<float>("max_size", max_size);
305 rgChildren.Add<float>("aspect_ratio", aspect_ratio);
306 rgChildren.Add("flip", flip.ToString());
307 rgChildren.Add("clip", clip.ToString());
308 rgChildren.Add<float>("variance", variance);
309
310 if (img_size.HasValue)
311 {
312 rgChildren.Add("img_size", img_size.Value.ToString());
313 }
314 else
315 {
316 if (img_h.HasValue)
317 rgChildren.Add("img_h", img_h.Value.ToString());
318 if (img_w.HasValue)
319 rgChildren.Add("img_w", img_w.Value.ToString());
320 }
321
322 if (step.HasValue)
323 {
324 rgChildren.Add("step", step.Value.ToString());
325 }
326 else
327 {
328 if (step_h.HasValue)
329 rgChildren.Add("step_h", step_h.Value.ToString());
330 if (step_w.HasValue)
331 rgChildren.Add("step_w", step_w.Value.ToString());
332 }
333
334 rgChildren.Add("offset", offset.ToString());
335
336 return new RawProto(strName, "", rgChildren);
337 }
338
339
340
347 {
349 string strVal;
350
351 p.min_size = rp.FindArray<float>("min_size");
352 p.max_size = rp.FindArray<float>("max_size");
353 p.aspect_ratio = rp.FindArray<float>("aspect_ratio");
354
355 if ((strVal = rp.FindValue("flip")) != null)
356 p.flip = bool.Parse(strVal);
357
358 if ((strVal = rp.FindValue("clip")) != null)
359 p.clip = bool.Parse(strVal);
360
361 p.variance = rp.FindArray<float>("variance");
362
363 if ((strVal = rp.FindValue("img_size")) != null)
364 {
365 p.img_size = uint.Parse(strVal);
366 }
367 else
368 {
369 if ((strVal = rp.FindValue("img_h")) != null)
370 p.img_h = uint.Parse(strVal);
371
372 if ((strVal = rp.FindValue("img_w")) != null)
373 p.img_w = uint.Parse(strVal);
374 }
375
376 if ((strVal = rp.FindValue("step")) != null)
377 {
378 p.step = ParseFloat(strVal);
379 }
380 else
381 {
382 if ((strVal = rp.FindValue("step_h")) != null)
383 p.step_h = ParseFloat(strVal);
384
385 if ((strVal = rp.FindValue("step_w")) != null)
386 p.step_w = ParseFloat(strVal);
387 }
388
389 if ((strVal = rp.FindValue("offset")) != null)
390 p.offset = ParseFloat(strVal);
391
392 return p;
393 }
394
400 public static List<float> GetAspectRatios(PriorBoxParameter p)
401 {
402 List<float> rgAspectRatios = new List<float>();
403
404 rgAspectRatios.Add(1.0f);
405
406 for (int i = 0; i < p.aspect_ratio.Count; i++)
407 {
408 float fAr = p.aspect_ratio[i];
409 bool bAlreadyExists = false;
410
411 for (int j = 0; j < rgAspectRatios.Count; j++)
412 {
413 if (Math.Abs(fAr - rgAspectRatios[j]) < 1e-6f)
414 {
415 bAlreadyExists = true;
416 break;
417 }
418 }
419
420 if (!bAlreadyExists)
421 {
422 rgAspectRatios.Add(fAr);
423 if (p.flip)
424 rgAspectRatios.Add(1.0f / fAr);
425 }
426 }
427
428 return rgAspectRatios;
429 }
430
439 public static List<int> Reshape(PriorBoxParameter p, int nLayerWid, int nLayerHt, int? nNumPriors = null)
440 {
441 List<int> rgTopShape = Utility.Create<int>(3, 1);
442 if (!nNumPriors.HasValue)
443 {
444 int nMinCount = (p.min_size == null) ? 0 : p.min_size.Count;
445 List<float> rgAspectRatios = GetAspectRatios(p);
446 int nAspectCount = rgAspectRatios.Count;
447
448 nNumPriors = nMinCount * nAspectCount;
449
450 if (p.max_size != null)
451 nNumPriors += p.max_size.Count;
452 }
453
454 // Since all images in a batch have the same height and width, we only need to
455 // generate one set of priors which can be shared across all images.
456 rgTopShape[0] = 1;
457
458 // 2 channels.
459 // First channel stores the mean of each prior coordinate.
460 // Second channel stores the variance of each prior coordiante.
461 rgTopShape[1] = 2;
462 rgTopShape[2] = nLayerWid * nLayerHt * nNumPriors.Value * 4;
463
464 return rgTopShape;
465 }
466 }
467}
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
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 List< int > Create(int nCount, int nStart, int nInc)
Create a new List and fill it with values starting with start and incrementing by inc.
Definition: Utility.cs:721
The LayerParameterBase is the base class for all other layer specific parameters.
Specifies the parameters for the PriorBoxParameter.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
float offset
Specifies the offset to the top left corner of each cell.
uint? img_size
Specifies the image size. By default we calculate the img_height, img_width, step_x and step_y based ...
uint? img_h
Specifies the image height. By default we calculate the img_height, img_width, step_x and step_y base...
static PriorBoxParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
static List< int > Reshape(PriorBoxParameter p, int nLayerWid, int nLayerHt, int? nNumPriors=null)
Calculate the reshape size based on the parameters.
List< float > variance
Specifies the variance for adjusting the prior boxes.
static List< float > GetAspectRatios(PriorBoxParameter p)
Return the list of aspect ratios to use based on the parameters.
List< float > max_size
Specifies the maximum box size (in pixels) and is required!
bool flip
Specifies whether or not to flip each aspect ratio. For example, if there is an aspect ratio 'r' we w...
float? step_w
Specifies the explicit step size to use along width.
uint? img_w
Specifies the image width. By default we calculate the img_height, img_width, step_x and step_y based...
override object Load(BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
float? step
Specifies the excplicit step size to use.
float? step_h
Specifies the explicit step size to use along height.
List< float > aspect_ratio
Specifies various aspect ratios. Duplicate ratios are ignored. If none are provided,...
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
bool clip
Specifies whether or not to clip the prior so that it is within [0,1].
static CodeType CodeTypeFromString(string str)
Convert a string into a CodeType.
List< float > min_size
Specifies the minimum box size (in pixels) and is required!
CodeType
Defines the encode/decode type.
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