MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
FlattenLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
9namespace MyCaffe.layers
10{
27 public class FlattenLayer<T> : Layer<T>
28 {
37 : base(cuda, log, p)
38 {
40 }
41
45 public override int ExactNumBottomBlobs
46 {
47 get { return 1; }
48 }
49
53 public override int ExactNumTopBlobs
54 {
55 get { return 1; }
56 }
57
63 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
64 {
65 }
66
72 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
73 {
74 m_log.CHECK(colTop[0] != colBottom[0], "Layer does not allow in-place computation.");
75
76 int nStartAxis = colBottom[0].CanonicalAxisIndex(m_param.flatten_param.axis);
77 int nEndAxis = colBottom[0].CanonicalAxisIndex(m_param.flatten_param.end_axis);
78
79 //List<int> rgTopShape = new List<int>();
80 //for (int i = 0; i < nStartAxis; i++)
81 //{
82 // rgTopShape.Add(colBottom[0].shape(i));
83 //}
84
85 //int nFlattenDim = colBottom[0].count(nStartAxis, nEndAxis + 1);
86 //rgTopShape.Add(nFlattenDim);
87
88 //for (int i = nEndAxis + 1; i < colBottom[0].num_axes; i++)
89 //{
90 // rgTopShape.Add(colBottom[0].shape(i));
91 //}
92
93 List<int> rgTopShape = FlattenParameter.Reshape(m_param.flatten_param.axis, m_param.flatten_param.end_axis, colBottom[0].shape(), nStartAxis, nEndAxis);
94
95 colTop[0].Reshape(rgTopShape);
96 m_log.CHECK_EQ(colTop[0].count(), colBottom[0].count(), "The top[0] and bottom[0] should have the same count.");
97 }
98
108 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
109 {
110 m_cuda.copy(colTop[0].count(), colBottom[0].gpu_data, colTop[0].mutable_gpu_data);
111// colTop[0].ShareData(colBottom[0]);
112 }
113
122 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
123 {
124 m_cuda.copy(colBottom[0].count(), colTop[0].gpu_diff, colBottom[0].mutable_gpu_diff);
125// colBottom[0].ShareDiff(colTop[0]);
126 }
127 }
128}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK(bool b, string str)
Test a flag for true.
Definition: Log.cs:227
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The FlattenLayer reshapes the input Blob into flat vectors This layer is initialized with the MyCaffe...
Definition: FlattenLayer.cs:28
FlattenLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The FlattenLayer constructor.
Definition: FlattenLayer.cs:36
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: FlattenLayer.cs:63
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input.
Definition: FlattenLayer.cs:46
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: flatten
Definition: FlattenLayer.cs:54
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the concatenate inputs.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: FlattenLayer.cs:72
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
Specifies the parameters for the FlattenLayer.
static List< int > Reshape(int nParamAxis, int nParamEndAxis, List< int > rgShape, int nStartAxis=-1, int nEndAxis=-1)
Calculate the reshape array given the parameters.
int axis
Specifies the first axis to flatten: all preceding axes are retained in the output....
int end_axis
Specifies the last axis to flatten: all following axes are retained in the output....
Specifies the base parameter for all layers.
FlattenParameter flatten_param
Returns the parameter set when initialized with LayerType.FLATTEN
LayerType
Specifies the layer type.
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.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
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