MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BatchSampler.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using MyCaffe.basecode;
7using MyCaffe.common;
9
10namespace MyCaffe.param.ssd
11{
19 [Serializable]
20 [TypeConverter(typeof(ExpandableObjectConverter))]
21 public class BatchSampler : BaseParameter, ICloneable, IComparable, IBinaryPersist
22 {
23 bool m_bUseOriginalImage = true;
24 Sampler m_sampler = new Sampler();
25 SamplerConstraint m_constraint = new SamplerConstraint();
26 uint m_nMaxSample = 0;
27 uint m_nMaxTrials = 100;
28
32 public BatchSampler()
33 {
34 }
35
40 public void Save(BinaryWriter bw)
41 {
42 bw.Write(m_bUseOriginalImage);
43 bw.Write(m_nMaxSample);
44 bw.Write(m_nMaxTrials);
45 m_sampler.Save(bw);
46 m_constraint.Save(bw);
47 }
48
55 public object Load(BinaryReader br, bool bNewInstance)
56 {
57 BatchSampler b = this;
58
59 if (bNewInstance)
60 b = new BatchSampler();
61
62 m_bUseOriginalImage = br.ReadBoolean();
63 m_nMaxSample = br.ReadUInt32();
64 m_nMaxTrials = br.ReadUInt32();
65 m_sampler = Sampler.Load(br);
66 m_constraint = SamplerConstraint.Load(br);
67
68 return b;
69 }
70
76 public static BatchSampler Load(BinaryReader br)
77 {
78 BatchSampler b = new BatchSampler();
79 return (BatchSampler)b.Load(br, true);
80 }
81
86 {
87 get { return m_bUseOriginalImage; }
88 set { m_bUseOriginalImage = value; }
89 }
90
94 public uint max_sample
95 {
96 get { return m_nMaxSample; }
97 set { m_nMaxSample = value; }
98 }
99
103 public uint max_trials
104 {
105 get { return m_nMaxTrials; }
106 set { m_nMaxTrials = value; }
107 }
108
113 {
114 get { return m_sampler; }
115 set { m_sampler = value; }
116 }
117
122 {
123 get { return m_constraint; }
124 set { m_constraint = value; }
125 }
126
132 {
133 BatchSampler bs = new BatchSampler();
134
135 bs.m_bUseOriginalImage = m_bUseOriginalImage;
136 bs.m_nMaxSample = m_nMaxSample;
137 bs.m_nMaxTrials = m_nMaxTrials;
138 bs.m_sampler = m_sampler.Clone();
139 bs.m_constraint = m_constraint.Clone();
140
141 return bs;
142 }
143
148 object ICloneable.Clone()
149 {
150 return Clone();
151 }
152
158 public bool Compare(BatchSampler bs)
159 {
160 if (bs.m_bUseOriginalImage != m_bUseOriginalImage)
161 return false;
162
163 if (bs.m_nMaxSample != m_nMaxSample)
164 return false;
165
166 if (bs.m_nMaxTrials != m_nMaxTrials)
167 return false;
168
169 if (!bs.m_sampler.Compare(m_sampler))
170 return false;
171
172 if (!bs.m_constraint.Compare(m_constraint))
173 return true;
174
175 return true;
176 }
177
183 public int CompareTo(object obj)
184 {
185 BatchSampler bs = obj as BatchSampler;
186
187 if (bs == null)
188 return 1;
189
190 if (!Compare(bs))
191 return 1;
192
193 return 0;
194 }
195
201 public override RawProto ToProto(string strName)
202 {
203 RawProtoCollection rgChildren = new RawProtoCollection();
204
205 rgChildren.Add(new RawProto("use_original_image", m_bUseOriginalImage.ToString()));
206 rgChildren.Add(new RawProto("max_sample", m_nMaxSample.ToString()));
207 rgChildren.Add(new RawProto("max_trials", m_nMaxTrials.ToString()));
208
209 rgChildren.Add(m_sampler.ToProto("sampler"));
210 rgChildren.Add(m_constraint.ToProto("sample_constraint"));
211
212 return new RawProto(strName, "", rgChildren);
213 }
214
221 {
222 string strVal;
223 BatchSampler p = new BatchSampler();
224
225 if ((strVal = rp.FindValue("use_original_image")) != null)
226 p.use_original_image = bool.Parse(strVal);
227
228 if ((strVal = rp.FindValue("max_sample")) != null)
229 p.max_sample = uint.Parse(strVal);
230
231 if ((strVal = rp.FindValue("max_trials")) != null)
232 p.max_trials = uint.Parse(strVal);
233
234 RawProto protoSampler = rp.FindChild("sampler");
235 if (protoSampler != null)
236 p.sampler = Sampler.FromProto(protoSampler);
237
238 RawProto protoConstraint = rp.FindChild("sample_constraint");
239 if (protoConstraint != null)
240 p.sample_constraint = SamplerConstraint.FromProto(protoConstraint);
241
242 return p;
243 }
244
249 public override string ToString()
250 {
251 string strOut = "";
252
253 strOut += "use_original_image = " + use_original_image.ToString() + Environment.NewLine;
254 strOut += "max_sample = " + max_sample.ToString() + Environment.NewLine;
255 strOut += "max_trials = " + max_trials.ToString() + Environment.NewLine;
256 strOut += m_sampler.ToString();
257 strOut += m_constraint.ToString();
258
259 return strOut;
260 }
261 }
262}
The BaseParameter class is the base class for all other parameter classes.
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
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
Specifies a sample of batch of bboxes with provided constraints in SSD.
Definition: BatchSampler.cs:22
bool Compare(BatchSampler bs)
Compares this BatchSampler to another.
int CompareTo(object obj)
Compares this BatchSampler to another.
bool use_original_image
Use the original image as the source for sampling.
Definition: BatchSampler.cs:86
static BatchSampler FromProto(RawProto rp)
Parse a new BatchSampler from a RawProto.
static BatchSampler Load(BinaryReader br)
Load the BatchSampler from a binary reader.
Definition: BatchSampler.cs:76
SamplerConstraint sample_constraint
Get/set the sample constraint.
Sampler sampler
Specifies the constraints for sampling the bbox
override RawProto ToProto(string strName)
Converts the BatchSampler to a RawProto.
uint max_trials
Maximum number of trials for sampling to avoid an infinite loop.
BatchSampler Clone()
Creates a copy of the BatchSampler.
void Save(BinaryWriter bw)
Save the BatchSampler to a binary writer.
Definition: BatchSampler.cs:40
object Load(BinaryReader br, bool bNewInstance)
Load the BatchSampler from a binary reader.
Definition: BatchSampler.cs:55
uint max_sample
If provided (greater than zero), break when found certain number of samples satisfying the sample con...
Definition: BatchSampler.cs:95
override string ToString()
Return the string representation of the shape.
BatchSampler()
The BatchSampler constructor.
Definition: BatchSampler.cs:32
Specifies the constratins for selecting sampled bbox used in SSD.
static SamplerConstraint FromProto(RawProto rp)
Parse a new SampleConstraint from a RawProto.
override RawProto ToProto(string strName)
Converts the SampleConstraint to a RawProto.
void Save(BinaryWriter bw)
Save the SampleConstraint to a binary writer.
override string ToString()
Return the string representation of the shape.
object Load(BinaryReader br, bool bNewInstance)
Load the SampleConstraint from a binary reader.
SamplerConstraint Clone()
Creates a copy of the SampleConstraint.
bool Compare(SamplerConstraint bs)
Compares this SampleConstraint to another.
Specifies the sample of a bbox in the normalized space [0,1] with provided constraints used in SSD.
Definition: Sampler.cs:22
override string ToString()
Return the string representation of the shape.
Definition: Sampler.cs:222
void Save(BinaryWriter bw)
Save the Sample to a binary writer.
Definition: Sampler.cs:39
bool Compare(Sampler bs)
Compares this Sample to another.
Definition: Sampler.cs:145
Sampler Clone()
Creates a copy of the Sample.
Definition: Sampler.cs:119
override RawProto ToProto(string strName)
Converts the Sample to a RawProto.
Definition: Sampler.cs:183
object Load(BinaryReader br, bool bNewInstance)
Load the Sample from a binary reader.
Definition: Sampler.cs:53
static Sampler FromProto(RawProto rp)
Parse a new Sample from a RawProto.
Definition: Sampler.cs:200
The IBinaryPersist interface provides generic save and load functionality.
Definition: Utility.cs:16
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
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