4using System.Collections.Generic;
7using System.Threading.Tasks;
21 List<int> m_rgGpuID =
new List<int>();
25 int m_nTestIter = 100;
26 string m_strTrainDataSource;
27 string m_strTestDataSource;
33 List<Tuple<int, bool>> m_rgIpLayers;
36 int m_nBaseWidth = 64;
61 : base(strBaseDirectory, net)
66 m_rgGpuID =
new List<int>(rgGpuId);
68 m_rgIpLayers = rgIpLayers;
70 m_strModel = model.ToString();
71 m_nBatchSize = nBatchSize;
74 m_nGpuID = m_rgGpuID[0];
77 m_strDataset = strDataset;
83 m_transformTrain.
mirror =
true;
85 m_transformTrain.
mean_value =
new List<double>();
95 m_transformTest.
mean_value =
new List<double>();
118 m_strTrainDataSource = m_strDataset +
".training";
119 m_strTestDataSource = m_strDataset +
".testing";
123 string strDataName =
"data";
124 bool bNamedParams =
false;
127 addDataLayer(m_strTrainDataSource,
Phase.TRAIN, m_nBatchSize,
true, m_transformTrain, strDataName);
128 data =
addDataLayer(m_strTestDataSource,
Phase.TEST, m_nBatchSize,
true, m_transformTest, strDataName);
130 lastLayer = addBody(bDeploy, strDataName, bNamedParams);
144 softmax.
name =
"softmax";
154 accuracy.
name =
"accuracy";
156 accuracy.
top.Add(accuracy.
name);
164 private LayerParameter addBody(
bool bDeploy,
string strDataName,
bool bNamedParams =
false,
string strLayerPostfix =
"",
Phase phaseExclude =
Phase.NONE)
171 lastLayer = addResNetOctConvBody(strDataName,
new int[] { 2, 2, 2, 2 });
175 throw new Exception(
"The model type '" + m_model.ToString() +
"' is not supported.");
178 for (
int i = 0; i < m_rgIpLayers.Count; i++)
181 string strName =
"fc" + (i + 1).ToString();
182 ip.
name = strName + strLayerPostfix;
196 private LayerParameter addResNetOctConvBody(
string strDataName,
int[] rgBlocks,
bool bNamedParams =
false,
string strLayerPostfix =
"",
Phase phaseExclude =
Phase.NONE)
198 string strConvPrefix =
"";
199 string strConvPostfix =
"";
200 string strBnPrefix =
"bn_";
201 string strBnPostfix =
"";
202 string strScalePrefix =
"scale_";
203 string strScalePostfix =
"";
205 LayerParameter lastLayer =
addConvBNLayer(strDataName,
"conv1",
true,
true, m_nInplanes, 7, 3, 2, 1, 1,
SCALE_BIAS.NONE, strConvPrefix, strConvPostfix, strBnPrefix, strBnPostfix, strScalePrefix, strScalePostfix,
"",
"_bias", bNamedParams, strLayerPostfix, phaseExclude);
211 lastLayer = make_layer(lastLayer, 1, 64, rgBlocks[0], 1,
true,
false);
212 lastLayer = make_layer(lastLayer, 2, 128, rgBlocks[1], 2,
false,
false);
213 lastLayer = make_layer(lastLayer, 3, 256, rgBlocks[2], 2,
false,
false);
214 lastLayer = make_layer(lastLayer, 4, 512, rgBlocks[3], 2,
false,
true);
221 string strName =
"fc1";
222 ip.
name = strName + strLayerPostfix;
237 lastLayer = addBlock(lastLayer, nIdx, ref nBlockIdx, nPlanes, nStride, bInput, bOutput);
239 for (
int i = 0; i < nBlocks; i++)
241 lastLayer = addBlock(lastLayer, nIdx, ref nBlockIdx, nPlanes, nStride, bInput, bOutput);
247 private LayerParameter addBlock(
LayerParameter lastLayer,
int nIdx, ref
int nBlockIdx,
int nPlanes,
int nStride,
bool bInput,
bool bOutput)
249 double dfAlphaIn = (bInput) ? 0.0 : 0.5;
250 double dfAlphaOut = 0.5;
252 int nWidth = (int)(nPlanes * (m_nBaseWidth / 64.0)) * m_nGroups;
254 string strName =
"conv1_" + nIdx.
ToString() +
"_" + nBlockIdx.ToString();
255 lastLayer =
addOctConvBNLayer(lastLayer.
name, strName, nWidth, 1, 0, 1, (bInput) ? 0 : dfAlphaIn, dfAlphaOut);
259 strName =
"conv2_" + nIdx.ToString() +
"_" + nBlockIdx.ToString();
262 dfAlphaOut = (bOutput) ? 0.0 : 0.5;
264 strName =
"conv3_" + nIdx.ToString() +
"_" + nBlockIdx.ToString();
265 lastLayer =
addOctConvBNLayer(lastLayer.
name, strName, nPlanes * m_nExpansion, 1, 0, 1, dfAlphaIn, dfAlphaOut);
267 if (nStride != 1 || m_nInplanes != nPlanes * m_nExpansion)
269 strName =
"ds_" + nIdx.ToString() +
"_" + nBlockIdx.ToString();
270 lastLayer =
addOctConvBNLayer(lastLayer.
name, strName, nPlanes * m_nExpansion, 1, 0, nStride, dfAlphaIn, dfAlphaOut);
297 protected LayerParameter addOctConvBNLayer(
string strInputLayer,
string strOutputLayer,
int nNumOutput,
int nKernelSize,
int nPad,
int nStride,
double dfAlphaIn,
double dfAlphaOut,
bool bUseBias =
true,
string strConvPrefix =
"",
string strConvPostfix =
"",
string strBnPrefix =
"",
string strBnPostfix =
"_bn",
string strLayerPostfix =
"",
Phase phaseExclude =
Phase.NONE)
300 bool bNamedParams =
false;
303 string strName = strConvPrefix + strOutputLayer + strConvPostfix;
310 convLayer.
name = strName + strLayerPostfix;
316 convLayer.
top.Add(convLayer.
name);
322 convLayer.
top.Add(convLayer.
name +
"_l");
327 convLayer.
parameters.Add(
new ParamSpec(dfLrMult, 1.0, (bNamedParams) ? strName +
"_w" :
null));
332 strName = strBnPrefix + strOutputLayer + strBnPostfix;
333 bnLayer.
name = strName + strLayerPostfix;
343 string strInputLayer2 =
null;
345 strInputLayer2 = strInputLayer +
"_l";
351 reluLayer.
name = convLayer.
name +
"_relu";
358 strName = strBnPrefix + strOutputLayer + strBnPostfix;
359 bnLayer.
name = strName + strLayerPostfix +
"_l";
366 bnLayer.
top.Add(bnLayer.
name +
"_l");
372 reluLayer.
name = convLayer.
name +
"_relu_l";
The ModelBuilder is an abstract class that is overridden by a base class used to programically build ...
SolverParameter m_solver
Specifies the base solver to use.
void addExclusion(LayerParameter p, Phase phase)
Add a phase exclusion.
SCALE_BIAS
Defines the scale bias type to use.
NetParameter m_net
Specifies the base net to be altered.
LayerParameter createPooling(string strName, PoolingParameter.PoolingMethod method, int nKernelSize, int nPad=0, int nStride=1)
Create a new pooling layer parameter.
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.
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 ResNetOctConvModelBuilder is used to build Octave Convolution based RESNET models.
MODEL
Defines the type of model to create.
override NetParameter CreateDeployModel()
Create the deploy model.
ResNetOctConvModelBuilder(string strBaseDirectory, string strDataset, List< Tuple< int, bool > > rgIpLayers, MODEL model, int nBatchSize=32, List< int > rgGpuId=null, NetParameter net=null)
The constructor.
override SolverParameter CreateSolver()
Create the base solver to use.
override NetParameter CreateModel(bool bDeploy=false)
Create the training model.
override LayerParameter addExtraLayers(bool bUseBatchNorm=true, double dfLrMult=1)
Add an extra layer.
LayerParameter addOctConvBNLayer(string strInputLayer, string strOutputLayer, int nNumOutput, int nKernelSize, int nPad, int nStride, double dfAlphaIn, double dfAlphaOut, bool bUseBias=true, string strConvPrefix="", string strConvPostfix="", string strBnPrefix="", string strBnPostfix="_bn", string strLayerPostfix="", Phase phaseExclude=Phase.NONE)
Add octave convolution, batch-norm layers.
double eps
Specifies a small value to add to the variance estimate so that we don't divide by zero.
double moving_average_fraction
Specifies how much the moving average decays each iteration. Smaller values make the moving average d...
bool? use_global_stats
If false, normalization is performed over the current mini-batch and global statistics are accumulate...
double alpha_out
Specifies alpha applied to the output channels.
double alpha_in
Specifies alpha applied to the input channels.
FillerParameter weight_filler
The filler for the weight. The default is set to use the 'xavier' filler.
FillerParameter bias_filler
The filler for the bias. The default is set to use the 'constant = 0.1' filler.
bool bias_term
Whether to have bias terms or not.
uint num_output
The number of outputs for the layer.
Specifies the filler parameters used to create each Filler.
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.
List< uint > kernel_size
Kernel size is given as a single value for equal dimensions in all spatial dimensions,...
List< uint > dilation
Factor used to dilate the kernel, (implicitly) zero-filling the resulting holes. (Kernel dilation is ...
List< uint > stride
Stride is given as a single value for equal dimensions in all spatial dimensions, or once per spatial...
List< uint > pad
Pad is given as a single value for equal dimensions in all spatial dimensions, or once per spatial di...
Specifies the base parameter for all layers.
ConvolutionParameter convolution_param
Returns the parameter set when initialized with LayerType.CONVOLUTION
List< ParamSpec > parameters
Specifies the ParamSpec parameters of the LayerParameter.
string name
Specifies the name of this LayerParameter.
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< 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
ConvolutionOctaveParameter convolution_octave_param
Returns the parameter set when initialized with LayerType.CONVOLUTION_OCTAVE
BatchNormParameter batch_norm_param
Returns the parameter set when initialized with LayerType.BATCHNORM
List< string > bottom
Specifies the active bottom connections (in the bottom, out the top).
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
Specifies the parameters use to create a Net
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...
Specifies training parameters (multipliers on global learning constants, and the name of other settin...
Specifies the parameters for the PoolingLayer.
PoolingMethod
Defines the pooling method.
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...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Phase
Defines the Phase under which to run a Net.
The MyCaffe.model namespace contains all classes used to programically create new model scripts.
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-...