MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
SsdPascalModelBuilder.cs
1using MyCaffe.basecode;
2using MyCaffe.param;
3using MyCaffe.param.ssd;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace MyCaffe.model
11{
16 {
17 int m_nGpuID = 0;
18 List<int> m_rgGpuID = new List<int>();
19 int m_nBatchSize;
20 int m_nAccumBatchSize;
21 int m_nBatchSizePerDevice;
22 int m_nIterSize;
23 double m_dfBaseLr;
24 double m_dfLrMult = 1;
25 bool m_bUseBatchNorm = false;
26 int m_nNumTestImage = 4952; // Evaluate on the whole test set.
27 int m_nTestBatchSize = 8;
28 int m_nTestIter;
29 string m_strTrainDataSource = "VOC0712.training";
30 string m_strTestDataSource = "VOC0712.testing";
31 string m_strNameSizeFile = "data\\ssd\\VOC0712\\test_name_size.txt";
32 string m_strLabelMapFile = "data\\ssd\\VOC0712\\labelmap_voc.prototxt";
33 string m_strPreTrainModel = "models\\VGGNet\\VGG_ILSVRC_16_layers_fc_reduced.caffemodel";
34 TransformationParameter m_transformTrain = null;
35 TransformationParameter m_transformTest = null;
36 DetectionEvaluateParameter m_detectionEval = null;
37 DetectionOutputParameter m_detectionOut = null;
38
39 // Batch sampler.
40 int m_nResizeWidth = 300;
41 int m_nResizeHeight = 300;
42 List<BatchSampler> m_rgBatchSampler = new List<BatchSampler>();
43 string m_strJob;
44 string m_strModel;
45
46 // MultiBoxLoss Parameters
48 LayerParameter m_multiBoxLossLayer;
49 List<float> m_rgPriorVariance;
50 bool m_bFlip = true;
51 bool m_bClip = false;
52 int m_nNumClasses = 21;
53 bool m_bShareLocation = true;
54 int m_nBackgroundLabelId = 0;
55 double m_dfNegPosRatio = 3.0;
56 double m_dfLocWeight;
57 List<MultiBoxHeadInfo> m_rgMultiBoxInfo;
58
59
70 public SsdPascalModelBuilder(string strBaseDirectory, int nBatchSize = 32, int nAccumBatchSize = 32, List<int> rgGpuId = null, bool bUseBatchNorm = false, LossParameter.NormalizationMode normMode = LossParameter.NormalizationMode.VALID, NetParameter net = null)
71 : base(strBaseDirectory, net)
72 {
73 if (rgGpuId == null)
74 m_rgGpuID.Add(0);
75 else
76 m_rgGpuID = new List<int>(rgGpuId);
77
78 m_strJob = "SSD_" + m_nResizeWidth.ToString() + "x" + m_nResizeHeight.ToString();
79 // The model name is used when initially creating the NetParameter.
80 m_strModel = "VGG_VOC0712_" + m_strJob;
81
82 m_bUseBatchNorm = bUseBatchNorm;
83 m_normalizationMode = normMode;
84
85 m_nBatchSize = nBatchSize;
86 m_nAccumBatchSize = nAccumBatchSize;
87 m_nIterSize = m_nAccumBatchSize / m_nBatchSize;
88
89 m_nBatchSizePerDevice = (m_rgGpuID.Count == 1) ? m_nBatchSize : m_nBatchSize / m_rgGpuID.Count;
90 m_nIterSize = (int)Math.Ceiling((float)m_nAccumBatchSize / (m_nBatchSizePerDevice * m_rgGpuID.Count));
91 m_nGpuID = m_rgGpuID[0];
92
93 // Set the base learning rate.
94 m_dfLocWeight = (m_dfNegPosRatio + 1.0) / 4.0;
95 m_dfBaseLr = (m_bUseBatchNorm) ? 0.0004 : 0.00004;
96
97 switch (m_normalizationMode)
98 {
100 m_dfBaseLr /= m_nBatchSizePerDevice;
101 break;
102
104 m_dfBaseLr *= 25.0 / m_dfLocWeight;
105 break;
106
108 // Roughly there are 2000 prior bboxes per images (TODO: calculate and use exact number).
109 m_dfBaseLr *= 2000;
110 break;
111 }
112
113 // Ideally the test_batch_size should be divisible by the num_test_image,
114 // otherwise mAP will be slightly off the true value.
115 m_nTestIter = (int)Math.Ceiling((float)m_nNumTestImage / (float)m_nTestBatchSize);
116
117 //-------------------------------------------------------
118 // Create the transformer for Training.
119 //-------------------------------------------------------
120 m_transformTrain = new TransformationParameter();
121 m_transformTrain.mirror = true;
122 m_transformTrain.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
123 m_transformTrain.mean_value.Add(104);
124 m_transformTrain.mean_value.Add(117);
125 m_transformTrain.mean_value.Add(123);
126 m_transformTrain.resize_param = new ResizeParameter(true);
127 m_transformTrain.resize_param.prob = 1;
128 m_transformTrain.resize_param.resize_mode = ResizeParameter.ResizeMode.WARP;
129 m_transformTrain.resize_param.height = (uint)m_nResizeHeight;
130 m_transformTrain.resize_param.width = (uint)m_nResizeWidth;
131 m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LINEAR);
132 m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.AREA);
133 m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.NEAREST);
134 m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.CUBIC);
135 m_transformTrain.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LANCZOS4);
136 m_transformTrain.distortion_param = new DistortionParameter(true);
137 m_transformTrain.distortion_param.brightness_prob = 0.5f;
138 m_transformTrain.distortion_param.brightness_delta = 32;
139 m_transformTrain.distortion_param.contrast_prob = 0.5f;
140 m_transformTrain.distortion_param.contrast_lower = 0.5f;
141 m_transformTrain.distortion_param.contrast_upper = 1.5f;
142 m_transformTrain.distortion_param.saturation_prob = 0.5f;
143 m_transformTrain.distortion_param.saturation_lower = 0.5f;
144 m_transformTrain.distortion_param.saturation_upper = 1.5f;
145 m_transformTrain.distortion_param.random_order_prob = 0.0f;
146 m_transformTrain.expansion_param = new ExpansionParameter(true);
147 m_transformTrain.expansion_param.prob = 0.5f;
148 m_transformTrain.expansion_param.max_expand_ratio = 4.0f;
149 m_transformTrain.emit_constraint = new EmitConstraint(true);
150 m_transformTrain.emit_constraint.emit_type = EmitConstraint.EmitType.CENTER;
151
152 //-------------------------------------------------------
153 // Create the transformer for Testing.
154 //-------------------------------------------------------
155 m_transformTest = new TransformationParameter();
156 m_transformTest.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
157 m_transformTest.mean_value.Add(104);
158 m_transformTest.mean_value.Add(117);
159 m_transformTest.mean_value.Add(123);
160 m_transformTest.resize_param = new ResizeParameter(true);
161 m_transformTest.resize_param.prob = 1;
162 m_transformTest.resize_param.resize_mode = ResizeParameter.ResizeMode.WARP;
163 m_transformTest.resize_param.height = (uint)m_nResizeHeight;
164 m_transformTest.resize_param.width = (uint)m_nResizeWidth;
165 m_transformTest.resize_param.interp_mode.Add(ResizeParameter.InterpMode.LINEAR);
166
167 //-------------------------------------------------------
168 // Create the batch samplers.
169 //-------------------------------------------------------
170 BatchSampler sampler = createSampler(1, 1);
171 m_rgBatchSampler.Add(sampler);
172
173 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.1f);
174 m_rgBatchSampler.Add(sampler);
175
176 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.3f);
177 m_rgBatchSampler.Add(sampler);
178
179 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.5f);
180 m_rgBatchSampler.Add(sampler);
181
182 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.7f);
183 m_rgBatchSampler.Add(sampler);
184
185 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, 0.9f);
186 m_rgBatchSampler.Add(sampler);
187
188 sampler = createSampler(50, 1, 0.3f, 1.0f, 0.5f, 2.0f, null, 1.0f);
189 m_rgBatchSampler.Add(sampler);
190
191 //-------------------------------------------------------
192 // Create the Multi-box parameters.
193 //-------------------------------------------------------
194 m_multiBoxLossLayer = new LayerParameter(LayerParameter.LayerType.MULTIBOX_LOSS);
197 m_multiBoxLossLayer.multiboxloss_param.neg_pos_ratio = (float)m_dfNegPosRatio;
198 m_multiBoxLossLayer.multiboxloss_param.num_classes = (uint)m_nNumClasses;
199 m_multiBoxLossLayer.multiboxloss_param.loc_weight = (float)m_dfLocWeight;
200 m_multiBoxLossLayer.multiboxloss_param.share_location = m_bShareLocation;
201 m_multiBoxLossLayer.multiboxloss_param.match_type = MultiBoxLossParameter.MatchType.PER_PREDICTION;
202 m_multiBoxLossLayer.multiboxloss_param.overlap_threshold = 0.5f;
203 m_multiBoxLossLayer.multiboxloss_param.use_prior_for_matching = true;
204 m_multiBoxLossLayer.multiboxloss_param.background_label_id = (uint)m_nBackgroundLabelId;
205 m_multiBoxLossLayer.multiboxloss_param.use_difficult_gt = true;
206 m_multiBoxLossLayer.multiboxloss_param.mining_type = MultiBoxLossParameter.MiningType.MAX_NEGATIVE;
207 m_multiBoxLossLayer.multiboxloss_param.neg_overlap = 0.5f;
208 m_multiBoxLossLayer.multiboxloss_param.code_type = PriorBoxParameter.CodeType.CENTER_SIZE;
209 m_multiBoxLossLayer.multiboxloss_param.ignore_cross_boundary_bbox = false;
210 m_multiBoxLossLayer.loss_param.normalization = m_normalizationMode;
211
212 if (m_multiBoxLossLayer.multiboxloss_param.code_type == PriorBoxParameter.CodeType.CENTER_SIZE)
213 m_rgPriorVariance = new List<float>() { 0.1f, 0.1f, 0.2f, 0.2f };
214 else
215 m_rgPriorVariance = new List<float>() { 0.1f };
216
217 //-------------------------------------------------------
218 // Create the Detection Output parameters.
219 //-------------------------------------------------------
220 m_detectionOut = new DetectionOutputParameter();
221 m_detectionOut.num_classes = (uint)m_nNumClasses;
222 m_detectionOut.share_location = m_bShareLocation;
223 m_detectionOut.background_label_id = m_nBackgroundLabelId;
224 m_detectionOut.nms_param = new NonMaximumSuppressionParameter(true);
225 m_detectionOut.nms_param.nms_threshold = 0.45f;
226 m_detectionOut.nms_param.top_k = 400;
227 m_detectionOut.save_output_param = new SaveOutputParameter(true);
228 m_detectionOut.save_output_param.output_directory = m_strBaseDir + "\\results";
229 m_detectionOut.save_output_param.output_name_prefix = "comp4_det_test_";
230 m_detectionOut.save_output_param.label_map_file = getFileName(m_strLabelMapFile, null);
231 m_detectionOut.save_output_param.name_size_file = getFileName(m_strNameSizeFile, null);
232 m_detectionOut.save_output_param.num_test_image = (uint)m_nNumTestImage;
233 m_detectionOut.keep_top_k = 200;
234 m_detectionOut.confidence_threshold = 0.01f;
235 m_detectionOut.code_type = m_multiBoxLossLayer.multiboxloss_param.code_type;
236
237 //-------------------------------------------------------
238 // Create the Detection Evaluation parameters.
239 //-------------------------------------------------------
240 m_detectionEval = new DetectionEvaluateParameter();
241 m_detectionEval.num_classes = (uint)m_nNumClasses;
242 m_detectionEval.background_label_id = (uint)m_nBackgroundLabelId;
243 m_detectionEval.overlap_threshold = 0.5f;
244 m_detectionEval.evaulte_difficult_gt = false;
245 m_detectionEval.name_size_file = getFileName(m_strNameSizeFile, null);
246
247 //-------------------------------------------------------
248 // Setup the MultiBox head layer info.
249 //-------------------------------------------------------
250 // conv4_3 ==> 38 x 38
251 // fc7 ==> 19 x 19
252 // conv6_2 ==> 10 x 10
253 // conv7_2 ==> 5 x 5
254 // conv8_2 ==> 3 x 3
255 // conv9_2 ==> 1 x 1
256 List<string> rgstrMboxSourceLayers = new List<string>() { "conv4_3", "fc7", "conv6_2", "conv7_2", "conv8_2", "conv9_2" };
257 List<double> rgAspectWid = new List<double>() { 2, 2, 2, 2, 2, 2 };
258 List<double> rgAspectHt = new List<double>() { 2, 3, 3, 3, 2, 2 };
259 // L2 normalize conv4_3
260 List<double> rgNormalization = new List<double>() { 20, -1, -1, -1, -1, -1 };
261 List<double> rgStepsW = new List<double>() { 8, 16, 32, 64, 100, 300 };
262 List<double> rgStepsH = new List<double>() { 8, 16, 32, 64, 100, 300 };
263 int nMinDim = 300;
264 // in percent %
265 double dfMinRatio = 20;
266 double dfMaxRatio = 90;
267 double dfRatioStep = (int)Math.Floor((dfMaxRatio - dfMinRatio) / (rgstrMboxSourceLayers.Count - 2));
268 List<double> rgMinSizes = new List<double>();
269 List<double> rgMaxSizes = new List<double>();
270
271 for (double dfRatio = dfMinRatio; dfRatio < dfMaxRatio + 1; dfRatio += dfRatioStep)
272 {
273 rgMinSizes.Add(nMinDim * dfRatio / 100.0);
274 rgMaxSizes.Add(nMinDim * (dfRatio + dfRatioStep) / 100.0);
275 }
276
277 rgMinSizes.Insert(0, nMinDim * 10 / 100.0);
278 rgMaxSizes.Insert(0, nMinDim * 20 / 100.0);
279
280 m_rgMultiBoxInfo = new List<MultiBoxHeadInfo>();
281
282 for (int i = 0; i < rgstrMboxSourceLayers.Count; i++)
283 {
284 string strSrc = rgstrMboxSourceLayers[i];
285 double dfMinSize = rgMinSizes[i];
286 double dfMaxSize = rgMaxSizes[i];
287 double dfStepW = rgStepsW[i];
288 double dfStepH = rgStepsH[i];
289 double dfAspectW = rgAspectWid[i];
290 double dfAspectH = rgAspectHt[i];
291 double dfNorm = rgNormalization[i];
292
293 m_rgMultiBoxInfo.Add(new MultiBoxHeadInfo(strSrc, dfMinSize, dfMaxSize, dfStepW, dfStepH, dfAspectW, dfAspectH, dfNorm, null));
294 }
295 }
296
297 private BatchSampler createSampler(int nMaxTrials, int nMaxSample, float fMinScale = 1.0f, float fMaxScale = 1.0f, float fMinAspectRatio = 1.0f, float fMaxAspectRatio = 1.0f, float? fMinJaccardOverlap = null, float? fMaxJaccardOverlap = null)
298 {
299 BatchSampler sampler = new BatchSampler();
300 sampler.max_trials = (uint)nMaxTrials;
301 sampler.max_sample = (uint)nMaxSample;
302 sampler.sampler.min_scale = fMinScale;
303 sampler.sampler.max_scale = fMaxScale;
304 sampler.sampler.min_aspect_ratio = fMinAspectRatio;
305 sampler.sampler.max_aspect_ratio = fMaxAspectRatio;
306
307 if (fMinJaccardOverlap.HasValue)
308 sampler.sample_constraint.min_jaccard_overlap = fMinJaccardOverlap.Value;
309
310 if (fMaxJaccardOverlap.HasValue)
311 sampler.sample_constraint.max_jaccard_overlap = fMaxJaccardOverlap.Value;
312
313 return sampler;
314 }
315
323 {
326 m_solver.base_lr = m_dfBaseLr;
327 m_solver.weight_decay = 0.0005;
329 m_solver.stepvalue = new List<int>() { 80000, 100000, 120000 };
330 m_solver.gamma = 0.1;
331 m_solver.momentum = 0.9;
332 m_solver.iter_size = m_nIterSize;
333 m_solver.max_iter = 120000;
334 m_solver.snapshot = 80000;
335 m_solver.display = 10;
337 m_solver.device_id = m_nGpuID;
338 m_solver.debug_info = false;
340
341 // Test parameters.
342 m_solver.test_iter.Add(m_nTestIter);
343 m_solver.test_interval = 10000;
346 m_solver.ap_version = ApVersion.ELEVENPOINT;
347
348 return m_solver;
349 }
350
351
355 public override NetParameter CreateModel(bool bDeploy = false)
356 {
357 string strLabelMapFile = getFileName(m_strLabelMapFile, null);
358 LayerParameter data = null;
359
360 m_net = createNet(m_strModel);
361
362 if (!bDeploy)
363 addAnnotatedDataLayer(m_strTrainDataSource, Phase.TRAIN, m_nBatchSizePerDevice, true, strLabelMapFile, SimpleDatum.ANNOTATION_TYPE.NONE, m_transformTrain, m_rgBatchSampler);
364
365 data = addAnnotatedDataLayer(m_strTestDataSource, Phase.TEST, 1, true, strLabelMapFile, SimpleDatum.ANNOTATION_TYPE.NONE, m_transformTest);
366
367 LayerParameter lastLayer = addVGGNetBody(data, true, true, true, true, false, false);
368 lastLayer = addExtraLayers(m_bUseBatchNorm, m_dfLrMult);
369
370 List<LayerParameter> rgMboxLayers = createMultiBoxHead(data, m_nNumClasses, m_rgMultiBoxInfo, m_rgPriorVariance, false, m_bUseBatchNorm, m_dfLrMult, true, 0, 0, m_bShareLocation, m_bFlip, m_bClip, 0.5, 3, 1);
371
372 // Create the MultiboxLossLayer.
373 if (!bDeploy)
374 {
375 string strName = "mbox_loss";
376 LayerParameter mbox_loss = new LayerParameter(LayerParameter.LayerType.MULTIBOX_LOSS);
377 mbox_loss.name = strName;
378 mbox_loss.multiboxloss_param = m_multiBoxLossLayer.multiboxloss_param;
379 mbox_loss.loss_param = m_multiBoxLossLayer.loss_param;
380 mbox_loss.include.Add(new NetStateRule(Phase.TRAIN));
381 mbox_loss.propagate_down = new List<bool>() { true, true, false, false };
382 mbox_loss.top.Add(mbox_loss.name);
383 connectAndAddLayer(rgMboxLayers, mbox_loss);
384 mbox_loss.bottom.Add(data.top[1]); // GT.
385 }
386
387
388 //-------------------------------------------------------
389 // Add testing layers.
390 //-------------------------------------------------------
391 string strConfName = "mbox_conf";
392 lastLayer = findLayer(strConfName);
393
394 if (m_multiBoxLossLayer.multiboxloss_param.conf_loss_type == MultiBoxLossParameter.ConfLossType.SOFTMAX)
395 {
396 string strReshapeName = strConfName + "_reshape";
398 reshape.name = strReshapeName;
399 reshape.top.Add(reshape.name);
400 reshape.reshape_param.shape = new BlobShape(new List<int>() { 0, -1, (int)m_multiBoxLossLayer.multiboxloss_param.num_classes });
401 if (!bDeploy)
402 {
403 reshape.include.Add(new NetStateRule(Phase.TEST));
404 reshape.include.Add(new NetStateRule(Phase.RUN));
405 }
406 lastLayer = connectAndAddLayer(lastLayer, reshape);
407
408 string strSoftmaxName = strConfName + "_softmax";
410 softmax.name = strSoftmaxName;
411 softmax.top.Add(softmax.name);
412 softmax.softmax_param.axis = 2;
413 if (!bDeploy)
414 {
415 softmax.include.Add(new NetStateRule(Phase.TEST));
416 softmax.include.Add(new NetStateRule(Phase.RUN));
417 }
418 lastLayer = connectAndAddLayer(lastLayer, softmax);
419
420 string strFlattentName = strConfName + "_flatten";
422 flatten.name = strFlattentName;
423 flatten.top.Add(flatten.name);
424 flatten.flatten_param.axis = 1;
425 if (!bDeploy)
426 {
427 flatten.include.Add(new NetStateRule(Phase.TEST));
428 flatten.include.Add(new NetStateRule(Phase.RUN));
429 }
430 lastLayer = connectAndAddLayer(lastLayer, flatten);
431
432 rgMboxLayers[1] = lastLayer;
433 }
434 else
435 {
436 string strSigmoidName = strConfName + "_sigmoid";
438 sigmoid.name = strSigmoidName;
439 sigmoid.top.Add(sigmoid.name);
440 if (!bDeploy)
441 {
442 sigmoid.include.Add(new NetStateRule(Phase.TEST));
443 sigmoid.include.Add(new NetStateRule(Phase.RUN));
444 }
445
446 lastLayer = connectAndAddLayer(lastLayer, sigmoid);
447 rgMboxLayers[1] = lastLayer;
448 }
449
450 LayerParameter detectionOut = new LayerParameter(LayerParameter.LayerType.DETECTION_OUTPUT);
451 detectionOut.name = "detection_output";
452 detectionOut.top.Add(detectionOut.name);
453 detectionOut.detection_output_param = m_detectionOut;
454 if (!bDeploy)
455 {
456 detectionOut.include.Add(new NetStateRule(Phase.TEST));
457 detectionOut.include.Add(new NetStateRule(Phase.RUN));
458 }
459 lastLayer = connectAndAddLayer(rgMboxLayers, detectionOut);
460
461 if (!bDeploy)
462 {
463 LayerParameter detectionEval = new LayerParameter(LayerParameter.LayerType.DETECTION_EVALUATE);
464 detectionEval.name = "detection_eval";
465 detectionEval.top.Add(detectionEval.name);
466 detectionEval.include.Add(new NetStateRule(Phase.TEST));
467 detectionEval.detection_evaluate_param = m_detectionEval;
468 lastLayer = connectAndAddLayer(lastLayer, detectionEval);
469 lastLayer.bottom.Add(data.top[1]);
470 }
471
472 return m_net;
473 }
474
479 {
480 return CreateModel(true);
481 }
482
488 protected override LayerParameter addExtraLayers(bool bUseBatchNorm = true, double dfLrMult = 1)
489 {
490 bool bUseRelU = true;
491 string strOutLayer;
492 string strFromLayer = m_net.layer[m_net.layer.Count - 1].name;
493 LayerParameter lastLayer;
494
495 // 10 x 10
496 strOutLayer = "conv6_1";
497 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 256, 1, 0, 1, dfLrMult);
498 strFromLayer = strOutLayer;
499
500 strOutLayer = "conv6_2";
501 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 512, 3, 1, 2, dfLrMult);
502 strFromLayer = strOutLayer;
503
504 // 5 x 5
505 strOutLayer = "conv7_1";
506 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 128, 1, 0, 1, dfLrMult);
507 strFromLayer = strOutLayer;
508
509 strOutLayer = "conv7_2";
510 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 256, 3, 1, 2, dfLrMult);
511 strFromLayer = strOutLayer;
512
513 // 3 x 3
514 strOutLayer = "conv8_1";
515 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 128, 1, 0, 1, dfLrMult);
516 strFromLayer = strOutLayer;
517
518 strOutLayer = "conv8_2";
519 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 256, 3, 0, 1, dfLrMult);
520 strFromLayer = strOutLayer;
521
522 // 1 x 1
523 strOutLayer = "conv9_1";
524 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 128, 1, 0, 1, dfLrMult);
525 strFromLayer = strOutLayer;
526
527 strOutLayer = "conv9_2";
528 lastLayer = addConvBNLayer(strFromLayer, strOutLayer, bUseBatchNorm, bUseRelU, 256, 3, 0, 1, dfLrMult);
529 strFromLayer = strOutLayer;
530
531 return lastLayer;
532 }
533 }
534}
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
Definition: SimpleDatum.cs:204
The ModelBuilder is an abstract class that is overridden by a base class used to programically build ...
Definition: ModelBuilder.cs:19
LayerParameter addVGGNetBody(LayerParameter lastLayer, bool bNeedFc=true, bool bFullConv=true, bool bReduced=true, bool bDilated=true, bool bNoPool=false, bool bDropout=false, List< string > rgstrFreezeLayers=null, bool bDilatePool4=false)
Adds the full VGG body to the network, connecting it to the 'lastLayer'.
SolverParameter m_solver
Specifies the base solver to use.
Definition: ModelBuilder.cs:31
LayerParameter addAnnotatedDataLayer(string strSource, Phase phase, int nBatchSize=32, bool bOutputLabel=true, string strLabelMapFile="", SimpleDatum.ANNOTATION_TYPE anno_type=SimpleDatum.ANNOTATION_TYPE.NONE, TransformationParameter transform=null, List< BatchSampler > rgSampler=null)
Add the Annotated Data layer.
NetParameter m_net
Specifies the base net to be altered.
Definition: ModelBuilder.cs:27
List< LayerParameter > createMultiBoxHead(LayerParameter data, int nNumClasses, List< MultiBoxHeadInfo > rgInfo, List< float > rgPriorVariance, bool bUseObjectness=false, bool bUseBatchNorm=true, double dfLrMult=1.0, bool useScale=true, int nImageHt=0, int nImageWd=0, bool bShareLocation=true, bool bFlip=true, bool bClip=true, double dfOffset=0.5, int nKernelSize=1, int nPad=0, string strConfPostfix="", string strLocPostfix="")
Create the multi-box head layers.
LayerParameter addConvBNLayer(string strInputLayer, string strOutputLayer, bool bUseBatchNorm, bool bUseRelU, int nNumOutput, int nKernelSize, int nPad, int nStride, double dfLrMult=1.0, int nDilation=1, SCALE_BIAS useScale=SCALE_BIAS.SCALE, string strConvPrefix="", string strConvPostfix="", string strBnPrefix="", string strBnPostfix="_bn", string strScalePrefix="", string strScalePostFix="_scale", string strBiasPrefix="", string strBiasPostfix="_bias", bool bNamedParams=false, string strLayerPostfix="", Phase phaseExclude=Phase.NONE)
Add convolution, batch-norm layers.
string m_strBaseDir
Specifies the base directory that contains the data and models.
Definition: ModelBuilder.cs:23
NetParameter createNet(string strName)
Create the base network parameter for the model and set its name to the 'm_strModel' name.
LayerParameter findLayer(string strName)
Find a layer with a given name.
LayerParameter connectAndAddLayer(string fromLayer, LayerParameter toLayer, string fromLayer2=null)
Connect the from layer to the 'to' layer.
string getFileName(string strFile, string strSubDir)
Returns the full path of the filename using the base directory original set when creating the ModelBu...
Definition: ModelBuilder.cs:89
The MultiBoxHeadInfo contains information used to build the multi-box head of layers.
The SsdPascalModelBuilder adds the extra layers to a 'base' model for the Pascal model used with SSD.
override SolverParameter CreateSolver()
Create the base solver to use.
override NetParameter CreateDeployModel()
Create the testing SSD model for the pascal dataset.
SsdPascalModelBuilder(string strBaseDirectory, int nBatchSize=32, int nAccumBatchSize=32, List< int > rgGpuId=null, bool bUseBatchNorm=false, LossParameter.NormalizationMode normMode=LossParameter.NormalizationMode.VALID, NetParameter net=null)
The constructor.
override LayerParameter addExtraLayers(bool bUseBatchNorm=true, double dfLrMult=1)
Add extra layers (for SSD with the Pascal dataset) on top of a 'base' network (e.g....
override NetParameter CreateModel(bool bDeploy=false)
Create the training SSD model for the pascal dataset.
Specifies the shape of a Blob.
Definition: BlobShape.cs:15
int axis
Specifies the first axis to flatten: all preceding axes are retained in the output....
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
DetectionOutputParameter detection_output_param
Returns the parmeter set when initialized with LayerType.DETECTION_OUTPUT
MultiBoxLossParameter multiboxloss_param
Returns the parameter set when initializing with LayerType.MULTIBOX_LOSS
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
List< bool > propagate_down
Specifies whether or not the LayerParameter (or protions of) should be backpropagated.
List< NetStateRule > include
Specifies the NetStateRule's for which this LayerParameter should be included.
List< string > top
Specifies the active top connections (in the bottom, out the top)
ReshapeParameter reshape_param
Returns the parameter set when initialized with LayerType.RESHAPE
DetectionEvaluateParameter detection_evaluate_param
Returns the parmeter set when initialized with LayerType.DETECTION_EVALUATE
FlattenParameter flatten_param
Returns the parameter set when initialized with LayerType.FLATTEN
List< string > bottom
Specifies the active bottom connections (in the bottom, out the top).
LayerType
Specifies the layer type.
LossParameter loss_param
Returns the parameter set when initialized with LayerType.LOSS
Stores the parameters used by loss layers.
NormalizationMode
How to normalize the loss for loss layers that aggregate across batches, spatial dimensions,...
NormalizationMode? normalization
Specifies the normalization mode (default = VALID).
Specifies the parameters use to create a Net
Definition: NetParameter.cs:18
List< LayerParameter > layer
The layers that make up the net. Each of their configurations, including connectivity and behavior,...
Specifies a NetStateRule used to determine whether a Net falls within a given include or exclude patt...
Definition: NetStateRule.cs:20
BlobShape shape
Specifies the output dimensions.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
The SolverParameter is a parameter for the solver, specifying the train and test networks.
int max_iter
The maximum number of iterations.
List< int > test_iter
The number of iterations for each test.
bool debug_info
If true, print information about the state of the net that may help with debugging learning problems.
LearningRatePolicyType
Defines the learning rate policy to use.
SolverType
Defines the type of solver.
LearningRatePolicyType LearningRatePolicy
The learning rate decay policy.
int device_id
The device id that will be used when run on the GPU.
ApVersion ap_version
Specifies the AP Version to use for average precision when using Single-Shot Detection (SSD) - (defau...
int average_loss
Display the loss averaged over the last average_loss iterations.
int test_interval
The number of iterations between two testing phases.
int iter_size
Accumulate gradients over 'iter_size' x 'batch_size' instances.
EvaluationType
Defines the evaluation method used in the SSD algorithm.
double gamma
Specifies the 'gamma' parameter to compute the 'step', 'exp', 'inv', and 'sigmoid' learning policy (d...
bool snapshot_after_train
If false, don't save a snapshot after training finishes.
EvaluationType eval_type
Specifies the evaluation type to use when using Single-Shot Detection (SSD) - (default = NONE,...
bool test_initialization
If true, run an initial test pass before the first iteration, ensuring memory availability and printi...
int display
The number of iterations between displaying info. If display = 0, no info will be displayed.
double weight_decay
Specifies the weight decay (default = 0.0005).
List< int > stepvalue
The step values for learning rate policy 'multistep'.
double momentum
Specifies the momentum value - used by all solvers EXCEPT the 'AdaGrad' and 'RMSProp' solvers....
int snapshot
Specifies the snapshot interval.
double base_lr
The base learning rate (default = 0.01).
SolverType type
Specifies the solver type.
Stores parameters used to apply transformation to the data layer's data.
COLOR_ORDER
Defines the color ordering used to tranform the input data.
DistortionParameter distortion_param
Optionally, specifies the distortion policy, otherwise this is null.
List< double > mean_value
If specified can be repeated once (would subtract it from all the channels or can be repeated the sam...
bool mirror
Specify if we want to randomly mirror the data.
COLOR_ORDER color_order
Specifies the color ordering to use. Native Caffe models often uses COLOR_ORDER.BGR,...
ExpansionParameter expansion_param
Optionally, specifies the expansion policy, otherwise this is null.
ResizeParameter resize_param
Optionally, specifies the resize policy, otherwise this is null.
EmitConstraint emit_constraint
Optionally, specifies the emit constraint on emitting annotation after transformation,...
Specifies a sample of batch of bboxes with provided constraints in SSD.
Definition: BatchSampler.cs:22
SamplerConstraint sample_constraint
Get/set the sample constraint.
Sampler sampler
Specifies the constraints for sampling the bbox
uint max_trials
Maximum number of trials for sampling to avoid an infinite loop.
uint max_sample
If provided (greater than zero), break when found certain number of samples satisfying the sample con...
Definition: BatchSampler.cs:95
Specifies the parameters for the DetectionEvaluateLayer.
uint background_label_id
Specifies the background class.
bool evaulte_difficult_gt
Specifies whether or not to consider the ground truth for evaluation.
string name_size_file
Specifies the file which contains a list of names and sizes in the same order of the input database....
uint num_classes
Specifies the number of classes that are actually predicted - required!
float overlap_threshold
Specifies the threshold for deciding true/false positive.
Specifies the parameters for the DetectionOutputLayer.
PriorBoxParameter.CodeType code_type
Specifies the coding method for the bbox.
float? confidence_threshold
Specifies the threshold for deciding which detections to consider - only those which are larger than ...
NonMaximumSuppressionParameter nms_param
Specifies the parameter used for non maximum suppression.
bool share_location
Specifies whether or not to sare the bounding box is shared among different classes (default = true).
uint num_classes
Specifies the number of classes that are actually predicted - required!
SaveOutputParameter save_output_param
Specifies the parameter used for saving the detection results.
int background_label_id
Specifies the background class.
int keep_top_k
Specifies the number of total bboxes to be kept per image after nms step, -1 means keeping all bboxes...
Specifies the parameters for the DistortionParameter used with SSD.
float contrast_lower
Get/set lower bound for random contrast factor (default = 0.5).
float random_order_prob
Get/set the probability of randomly ordering the image channels (default = 0).
float brightness_delta
Get/set amount to add to the pixel values within [-delta,delta] (default = 0)
float saturation_prob
Get/set probability of adjusting the saturation (default = 0).
float saturation_lower
Get/set lower bound for random saturation factor (default = 0.5).
float saturation_upper
Get/set upper bound for random saturation factor (default = 1.5).
float brightness_prob
Get/set probability of adjusting the brightness (default = 0).
float contrast_prob
Get/set probability of adjusting the contrast (default = 0).
float contrast_upper
Get/set upper bound for random contrast factor (default = 1.5).
Specifies the parameters for the EmitConstraint used with SSD.
EmitType emit_type
Get/set the emit type.
EmitType
Specifies the emit type.
Specifies the parameters for the ExpansionParameter used with SSD.
float max_expand_ratio
Get/set the ratio to expand the image.
float prob
Get/set probability of using this expansion policy.
Specifies the parameters for the MultiBoxLossParameter.
float loc_weight
Get/set the weight for the localization loss (default = 1.0).
float overlap_threshold
Get/set the overlap threshold (default = 0.5).
bool use_difficult_gt
Get/set whether or not to consider the difficult ground truth (defalt = true).
MatchType
Defines the matching method used during training.
PriorBoxParameter.CodeType code_type
Get/set the coding method for the bounding box.
float neg_overlap
Get/set the negative overlap upperbound for the unmatched predictions (default = 0....
LocLossType
Defines the localization loss types.
float neg_pos_ratio
Get/set the negative/positive ratio (default = 3.0).
bool share_location
Get/sets whether or not the bounding box is shared among different classes (default = true).
MiningType
Defines the mining type used during training.
LocLossType loc_loss_type
Get/set the localization loss type (default = SMOOTH_L1).
uint background_label_id
Get/set the background label id.
ConfLossType
Defines the confidence loss types.
bool ignore_cross_boundary_bbox
Get/set whether or not to ignore cross boundary bbox during matching (default = false)....
ConfLossType conf_loss_type
Get/set the confidence loss type (default = SOFTMAX).
bool use_prior_for_matching
Get/set whether or not to use prior for matching.
uint num_classes
Get/set the number of classes to be predicted - required!
MiningType mining_type
Get/set the mining type used during training (default = MAX_NEGATIVE).
MatchType match_type
Get/set the matching method used during training (default = PER_PREDICTION).
Specifies the parameters for the NonMaximumSuppressionParameter used with SSD.
int? top_k
Get/set the maximum number of results kept.
float nms_threshold
Get/set the threshold to be used in nms.
Specifies the parameters for the PriorBoxParameter.
CodeType
Defines the encode/decode type.
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.
uint width
Get/set the resizing width.
ResizeMode
Defines the resizing mode.
ResizeMode resize_mode
Get/set the resizing mode.
float prob
Get/set probability of using this resize policy.
float? max_jaccard_overlap
Get/set the maximum Jaccard overlap between sampled bbox and all boxes in AnnotationGroup.
float? min_jaccard_overlap
Get/set the minimum Jaccard overlap between sampled bbox and all boxes in AnnotationGroup.
float max_scale
Get/set the maximum scale of the sampled bbox.
Definition: Sampler.cs:92
float max_aspect_ratio
Get/set the maximum aspect ratio of the sampled bbox.
Definition: Sampler.cs:110
float min_scale
Get/set the minimum scale of the sampled bbox.
Definition: Sampler.cs:83
float min_aspect_ratio
Get/set the minimum aspect ratio of the sampled bbox.
Definition: Sampler.cs:101
Specifies the parameters for the SaveOutputLayer.
string output_directory
Specifies the output directory - if not empty, the results will be saved.
string name_size_file
Optionally, specifies the output name size file.
string label_map_file
Optionally, specifies the output label map file.
string output_name_prefix
Specifies the output name prefix.
uint? num_test_image
Specifies the number of test images.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
ApVersion
Defines the different way of computing average precision.
Definition: Interfaces.cs:234
The MyCaffe.model namespace contains all classes used to programically create new model scripts.
Definition: ModelBuilder.cs:14
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
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