MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ResNetModelBuilder.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 int m_nTestIter = 100;
25 string m_strTrainDataSource;
26 string m_strTestDataSource;
27 TransformationParameter m_transformTrain = null;
28 TransformationParameter m_transformTest = null;
29 string m_strModel;
30 MODEL m_model = MODEL.RESNET56;
31 bool m_bUsePool5 = false;
32 bool m_bUseDilationConv5 = false;
33 string m_strDataset;
34 List<Tuple<int, bool>> m_rgIpLayers;
35 bool m_bSiamese = false;
36 int m_nChannels = 3;
37
41 public enum MODEL
42 {
46 RESNET56,
50 RESNET101,
54 RESNET152
55 }
56
72 public ResNetModelBuilder(string strBaseDirectory, string strDataset, int nChannels, bool bSiamese, List<Tuple<int, bool>> rgIpLayers, bool bUsePool5, bool bUseDilationConv5, MODEL model, int nBatchSize = 32, int nAccumBatchSize = 32, List<int> rgGpuId = null, NetParameter net = null)
73 : base(strBaseDirectory, net)
74 {
75 if (rgGpuId == null)
76 m_rgGpuID.Add(0);
77 else
78 m_rgGpuID = new List<int>(rgGpuId);
79
80 m_nChannels = nChannels;
81 m_bSiamese = bSiamese;
82 m_rgIpLayers = rgIpLayers;
83 m_model = model;
84 m_strModel = model.ToString();
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 m_dfBaseLr = 0.001;
93
94 m_bUseDilationConv5 = bUseDilationConv5;
95 m_bUsePool5 = bUsePool5;
96 m_strDataset = strDataset;
97
98 //-------------------------------------------------------
99 // Create the transformer for Training.
100 //-------------------------------------------------------
101 m_transformTrain = new TransformationParameter();
102 m_transformTrain.mirror = true;
103 m_transformTrain.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
104 m_transformTrain.mean_value = new List<double>();
105 m_transformTrain.mean_value.Add(104);
106 m_transformTrain.mean_value.Add(117);
107 m_transformTrain.mean_value.Add(123);
108
109 //-------------------------------------------------------
110 // Create the transformer for Testing.
111 //-------------------------------------------------------
112 m_transformTest = new TransformationParameter();
113 m_transformTest.color_order = TransformationParameter.COLOR_ORDER.BGR; // to support caffe models.
114 m_transformTest.mean_value = new List<double>();
115 m_transformTest.mean_value.Add(104);
116 m_transformTest.mean_value.Add(117);
117 m_transformTest.mean_value.Add(123);
118 }
119
127 {
130 m_solver.base_lr = m_dfBaseLr;
131 m_solver.weight_decay = 0.0005;
133 m_solver.stepvalue = new List<int>() { 80000, 100000, 120000 };
134 m_solver.gamma = 0.1;
135 m_solver.momentum = 0.9;
136 m_solver.iter_size = m_nIterSize;
137 m_solver.max_iter = 120000;
138 m_solver.snapshot = 80000;
139 m_solver.display = 10;
141 m_solver.device_id = m_nGpuID;
142 m_solver.debug_info = false;
145
146 // Test parameters.
147 m_solver.test_iter.Add(m_nTestIter);
148 m_solver.test_interval = 10000;
151
152 return m_solver;
153 }
154
155
160 public override NetParameter CreateModel(bool bDeploy = false)
161 {
162 LayerParameter lastLayer = null;
163 LayerParameter data = null;
164 string strData1 = null;
165 string strData2 = null;
166
167 m_strTrainDataSource = m_strDataset + ".training";
168 m_strTestDataSource = m_strDataset + ".testing";
169
170 m_net = createNet(m_strModel);
171
172 string strDataName = "data";
173 string strLayerPostfix = "";
174 bool bNamedParams = false;
175
176 if (m_bSiamese)
177 {
178 strDataName = "pair_data";
179 strLayerPostfix = "_p";
180 bNamedParams = true;
181 }
182
183 if (!bDeploy)
184 addDataLayer(m_strTrainDataSource, Phase.TRAIN, m_nBatchSize, true, m_transformTrain, strDataName, m_bSiamese);
185 data = addDataLayer(m_strTestDataSource, Phase.TEST, m_nBatchSize, true, m_transformTest, strDataName, m_bSiamese);
186
187 if (m_bSiamese)
188 {
190 slice.slice_param.axis = 1;
191 slice.slice_param.slice_point.Add((uint)m_nChannels);
192 slice.exclude.Add(new NetStateRule(Phase.RUN));
193 lastLayer = connectAndAddLayer(data, slice);
194 strData1 = "data";
195 strData2 = "data_p";
196 slice.top.Add(strData1);
197 slice.top.Add(strData2);
198 strDataName = "data";
199 }
200
201 lastLayer = addBody(bDeploy, strDataName, bNamedParams);
202 LayerParameter output1 = lastLayer;
203
204 if (m_bSiamese)
205 {
207 decode.name = "decode1";
208 decode.decode_param.target = param.beta.DecodeParameter.TARGET.CENTROID;
209 decode.decode_param.cache_size = 100;
210 decode.top.Add(decode.name);
211 lastLayer = connectAndAddLayer(lastLayer, decode);
212 decode.bottom.Add(data.top[1]);
213
214 if (!bDeploy)
215 {
217 silence1.name = "silence1";
218 silence1.include.Add(new NetStateRule(Phase.TRAIN));
219 connectAndAddLayer(lastLayer, silence1);
220
221 lastLayer = addBody(false, strDataName + strLayerPostfix, bNamedParams, strLayerPostfix, Phase.RUN);
222 LayerParameter output2 = lastLayer;
223
224 LayerParameter loss = new LayerParameter(LayerParameter.LayerType.CONTRASTIVE_LOSS);
225 loss.name = "loss";
227 loss.top.Add(loss.name);
228 loss.top.Add("match");
229 connectAndAddLayer(lastLayer, loss);
230 loss.bottom.Clear();
231 loss.bottom.Add(output1.top[0]);
232 loss.bottom.Add(output2.top[0]);
233 loss.bottom.Add(data.top[1]);
234 loss.loss_weight.Add(1);
235 loss.loss_weight.Add(0);
236 addExclusion(loss, Phase.RUN);
237
239 silence.name = "silence2";
240 connectAndAddLayer(loss, silence);
241 silence.bottom[0] = "match";
242 addExclusion(silence, Phase.RUN);
243
244 LayerParameter accuracy = new LayerParameter(LayerParameter.LayerType.ACCURACY_DECODE);
245 accuracy.name = "accuracy";
246 accuracy.include.Add(new NetStateRule(Phase.TEST));
247 accuracy.top.Add(accuracy.name);
248 connectAndAddLayer(decode, accuracy);
249 accuracy.bottom.Add(data.top[1]);
250 }
251 }
252 else
253 {
254 if (!bDeploy)
255 {
256 LayerParameter loss = new LayerParameter(LayerParameter.LayerType.SOFTMAXWITH_LOSS);
257 loss.name = "loss";
258 loss.include.Add(new NetStateRule(Phase.TRAIN));
259 loss.top.Add(loss.name);
260 connectAndAddLayer(lastLayer, loss);
261 loss.bottom.Add(data.top[1]);
262 }
263
265 softmax.name = "softmax";
266 softmax.softmax_param.axis = 1;
267 softmax.include.Add(new NetStateRule(Phase.TEST));
268 softmax.include.Add(new NetStateRule(Phase.RUN));
269 softmax.top.Add(softmax.name);
270 lastLayer = connectAndAddLayer(lastLayer, softmax);
271
272 if (!bDeploy)
273 {
274 LayerParameter accuracy = new LayerParameter(LayerParameter.LayerType.ACCURACY);
275 accuracy.name = "accuracy";
276 accuracy.include.Add(new NetStateRule(Phase.TEST));
277 accuracy.top.Add(accuracy.name);
278 connectAndAddLayer(lastLayer, accuracy);
279 accuracy.bottom.Add(data.top[1]);
280 }
281 }
282
283 return m_net;
284 }
285
286 private LayerParameter addBody(bool bDeploy, string strDataName, bool bNamedParams = false, string strLayerPostfix = "", Phase phaseExclude = Phase.NONE)
287 {
288 LayerParameter lastLayer;
289
290 switch (m_model)
291 {
292 case MODEL.RESNET56:
293 lastLayer = addResNetBody(strDataName, 4, 11, m_bUsePool5, m_bUseDilationConv5, bNamedParams, strLayerPostfix, phaseExclude);
294 break;
295
296 case MODEL.RESNET101:
297 lastLayer = addResNetBody(strDataName, 4, 23, m_bUsePool5, m_bUseDilationConv5, bNamedParams, strLayerPostfix, phaseExclude);
298 break;
299
300 case MODEL.RESNET152:
301 lastLayer = addResNetBody(strDataName, 8, 36, m_bUsePool5, m_bUseDilationConv5, bNamedParams, strLayerPostfix, phaseExclude);
302 break;
303
304 default:
305 throw new Exception("The model type '" + m_model.ToString() + "' is not supported.");
306 }
307
308 for (int i = 0; i < m_rgIpLayers.Count; i++)
309 {
311 string strName = "fc" + (i + 1).ToString();
312 ip.name = strName + strLayerPostfix;
314 ip.inner_product_param.num_output = (uint)m_rgIpLayers[i].Item1;
315 ip.inner_product_param.enable_noise = m_rgIpLayers[i].Item2;
316 ip.top.Add(ip.name);
317 ip.parameters.Add(new ParamSpec(1, 1, (bNamedParams) ? strName + "_w" : null));
318 ip.parameters.Add(new ParamSpec(1, 2, (bNamedParams) ? strName + "_b" : null));
319 addExclusion(ip, phaseExclude);
320 lastLayer = connectAndAddLayer(lastLayer, ip);
321 }
322
323 return lastLayer;
324 }
325
330 {
331 return CreateModel(true);
332 }
333
339 protected override LayerParameter addExtraLayers(bool bUseBatchNorm = true, double dfLrMult = 1)
340 {
341 return m_net.layer[m_net.layer.Count - 1];
342 }
343 }
344}
The ModelBuilder is an abstract class that is overridden by a base class used to programically build ...
Definition: ModelBuilder.cs:19
SolverParameter m_solver
Specifies the base solver to use.
Definition: ModelBuilder.cs:31
void addExclusion(LayerParameter p, Phase phase)
Add a phase exclusion.
LayerParameter addResNetBody(string strDataName, int nBlock3Count=4, int nBlock4Count=23, bool bUsePool5=true, bool bUseDilationConv5=false, bool bNamedParams=false, string strLayerPostfix="", Phase phaseExclude=Phase.NONE)
Create a ResNet101 Body.
NetParameter m_net
Specifies the base net to be altered.
Definition: ModelBuilder.cs:27
NetParameter createNet(string strName)
Create the base network parameter for the model and set its name to the 'm_strModel' name.
LayerParameter addDataLayer(string strSource, Phase phase, int nBatchSize=32, bool bOutputLabel=true, TransformationParameter transform=null, string strName="data", bool bSiamese=false)
Add the Data layer.
LayerParameter connectAndAddLayer(string fromLayer, LayerParameter toLayer, string fromLayer2=null)
Connect the from layer to the 'to' layer.
The ResNetModelBuilder adds the extra layers to a 'base' model for the ResNet model.
MODEL
Defines the type of model to create.
override NetParameter CreateModel(bool bDeploy=false)
Create the training model.
ResNetModelBuilder(string strBaseDirectory, string strDataset, int nChannels, bool bSiamese, List< Tuple< int, bool > > rgIpLayers, bool bUsePool5, bool bUseDilationConv5, MODEL model, int nBatchSize=32, int nAccumBatchSize=32, List< int > rgGpuId=null, NetParameter net=null)
The constructor.
override NetParameter CreateDeployModel()
Create the testing SSD model for the pascal dataset.
override SolverParameter CreateSolver()
Create the base solver to use.
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....
double margin
Margin for dissimilar pair.
int axis
Specifies the first axis to be lumped into a single inner product computation; all preceding axes are...
bool enable_noise
Enable/disable noise in the inner-product layer (default = false).
uint num_output
The number of outputs for the layer.
Specifies the base parameter for all layers.
List< ParamSpec > parameters
Specifies the ParamSpec parameters of the LayerParameter.
SliceParameter slice_param
Returns the parameter set when initialized with LayerType.SLICE
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
List< NetStateRule > include
Specifies the NetStateRule's for which this LayerParameter should be included.
List< NetStateRule > exclude
Specifies the NetStateRule's for which this LayerParameter should be excluded.
ContrastiveLossParameter contrastive_loss_param
Returns the parameter set when initialized with LayerType.CONTRASTIVE_LOSS
List< string > top
Specifies the active top connections (in the bottom, out the top)
InnerProductParameter inner_product_param
Returns the parameter set when initialized with LayerType.INNERPRODUCT
DecodeParameter decode_param
Returns the parameter set when initializing with LayerType.DECODE or LayerType.ACCURACY_ENCODING;
List< string > bottom
Specifies the active bottom connections (in the bottom, out the top).
LayerType
Specifies the layer type.
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
Specifies training parameters (multipliers on global learning constants, and the name of other settin...
Definition: ParamSpec.cs:19
List< uint > slice_point
Specifies optional slice points which indicate the indexes in the selected dimensions (the number of ...
int axis
Specifies the axis along wich to slice – may be negative to index from the end (e....
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.
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.
double clip_gradients
Set clip_gradients to >= 0 to clip parameter gradients to that L2 norm, whenever their actual L2 norm...
Stores parameters used to apply transformation to the data layer's data.
COLOR_ORDER
Defines the color ordering used to tranform the input data.
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,...
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
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