MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
VisionLayers.cs
1using MyCaffe.param;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
9{
10 class ConvolutionLayerInfo : LayerInfo
11 {
12 public ConvolutionLayerInfo(LayerParameter layer, VariableCollection inputs) : base(layer, inputs)
13 {
14 int nPad = (layer.convolution_param.pad != null && layer.convolution_param.pad.Count > 0) ? (int)layer.convolution_param.pad[0] : 0;
15 int nKernel = (layer.convolution_param.kernel_size != null && layer.convolution_param.kernel_size.Count > 0) ? (int)layer.convolution_param.kernel_size[0] : 1;
16 int nStride = (layer.convolution_param.stride != null && layer.convolution_param.stride.Count > 0) ? (int)layer.convolution_param.stride[0] : 1;
17
18 m_outputs[0].Shape[1] = (int)layer.convolution_param.num_output;
19 m_outputs[0].Shape[2] = (int)Math.Floor((double)(m_inputs[0].Shape[2] + 2 * nPad - nKernel) / nStride) + 1;
20 m_outputs[0].Shape[3] = (int)Math.Floor((double)(m_inputs[0].Shape[3] + 2 * nPad - nKernel) / nStride) + 1;
21 }
22
23 public override string Generate(GENERATE gen)
24 {
25 int nPad = (m_layer.convolution_param.pad != null && m_layer.convolution_param.pad.Count > 0) ? (int)m_layer.convolution_param.pad[0] : 0;
26 int nKernel = (m_layer.convolution_param.kernel_size != null && m_layer.convolution_param.kernel_size.Count > 0) ? (int)m_layer.convolution_param.kernel_size[0] : 1;
27 int nStride = (m_layer.convolution_param.stride != null && m_layer.convolution_param.stride.Count > 0) ? (int)m_layer.convolution_param.stride[0] : 1;
28 int nDilation = (m_layer.convolution_param.dilation != null && m_layer.convolution_param.dilation.Count > 0) ? (int)m_layer.convolution_param.dilation[0] : 1;
29
30 string strCode = "";
31 if (gen == GENERATE.DEFINITION)
32 strCode += " self." + m_layer.name + " = nn.Conv2d(in_channels=" + m_inputs[0].Shape[1] + ", out_channels=" + m_layer.convolution_param.num_output.ToString() + ", kernel_size=" + nKernel.ToString() + ", stride=" + nStride.ToString() + ", padding=" + nPad.ToString() + ", dilation=" + nDilation.ToString() + ", groups=" + m_layer.convolution_param.group.ToString() + ", bias=" + m_layer.convolution_param.bias_term.ToString() + ")" + Environment.NewLine;
33 else if (gen == GENERATE.INITWEIGHTS)
34 strCode += initWeights("", m_layer.name, m_layer.convolution_param.bias_term, m_layer.convolution_param.weight_filler, m_layer.convolution_param.bias_filler);
35 else if (gen == GENERATE.FORWARD)
36 strCode += " " + m_outputs.AsText + " = self." + m_layer.name + "(" + m_inputs.AsText + ")" + Environment.NewLine;
37
38 return strCode;
39 }
40 }
41
42 class PoolingLayerInfo : LayerInfo
43 {
44 public PoolingLayerInfo(LayerParameter layer, VariableCollection inputs) : base(layer, inputs)
45 {
46 int nPad = (layer.pooling_param.pad != null && layer.pooling_param.pad.Count > 0) ? (int)layer.pooling_param.pad[0] : 0;
47 int nKernel = (layer.pooling_param.kernel_size != null && layer.pooling_param.kernel_size.Count > 0) ? (int)layer.pooling_param.kernel_size[0] : 1;
48 int nStride = (layer.pooling_param.stride != null && layer.pooling_param.stride.Count > 0) ? (int)layer.pooling_param.stride[0] : 1;
49
50 m_outputs[0].Shape[2] = (int)Math.Floor((double)(m_inputs[0].Shape[2] + 2 * nPad - nKernel) / nStride) + 1;
51 m_outputs[0].Shape[3] = (int)Math.Floor((double)(m_inputs[0].Shape[3] + 2 * nPad - nKernel) / nStride) + 1;
52 }
53
54 public override string Generate(GENERATE gen)
55 {
56 int nPad = (m_layer.pooling_param.pad != null && m_layer.pooling_param.pad.Count > 0) ? (int)m_layer.pooling_param.pad[0] : 0;
57 int nKernel = (m_layer.pooling_param.kernel_size != null && m_layer.pooling_param.kernel_size.Count > 0) ? (int)m_layer.pooling_param.kernel_size[0] : 1;
58 int nStride = (m_layer.pooling_param.stride != null && m_layer.pooling_param.stride.Count > 0) ? (int)m_layer.pooling_param.stride[0] : 1;
59 int nDilation = (m_layer.pooling_param.dilation != null && m_layer.pooling_param.dilation.Count > 0) ? (int)m_layer.pooling_param.dilation[0] : 1;
60
61 string strCode = "";
62 if (gen == GENERATE.DEFINITION)
63 strCode += " self." + m_layer.name + " = nn.MaxPool2d(kernel_size=" + nKernel.ToString() + ", stride=" + nStride.ToString() + ", padding=" + nPad.ToString() + ", dilation=" + nDilation.ToString() + ")" + Environment.NewLine;
64 else if (gen == GENERATE.INITWEIGHTS)
65 {
66 }
67 else if (gen == GENERATE.FORWARD)
68 strCode += " " + m_outputs.AsText + " = self." + m_layer.name + "(" + m_inputs.AsText + ")" + Environment.NewLine;
69
70 return strCode;
71 }
72 }
73}
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 > 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
PoolingParameter pooling_param
Returns the parameter set when initialized with LayerType.POOLING
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