MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MergeLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8using MyCaffe.param.beta;
9
10namespace MyCaffe.layers.beta
11{
19 public class MergeLayer<T> : Layer<T>
20 {
21 int m_nCopyAxis = 0;
22 int m_nOrderMajorAxis = 0;
23 int m_nCopyCount = 1;
24 int m_nSrcStartIdx1 = 0;
25 int m_nDstStartIdx1 = 0;
26 int m_nCopyDim1 = 1;
27 int m_nSrcStartIdx2 = 0;
28 int m_nDstStartIdx2 = 0;
29 int m_nCopyDim2 = 1;
30 int m_nSrcSpatialDim1 = 1;
31 int m_nSrcSpatialDim2 = 1;
32 int m_nSrcSpatialDimStartIdx1 = 0;
33 int m_nDstSpatialDimStartIdx1 = 0;
34 int m_nSrcSpatialDimStartIdx2 = 0;
35 int m_nDstSpatialDimStartIdx2 = 0;
36 int m_nSpatialDimCopyCount = -1;
37 int m_nDstSpatialDim = 1;
38
47 : base(cuda, log, p)
48 {
50 }
51
53 protected override void dispose()
54 {
55 base.dispose();
56 }
57
61 public override int ExactNumBottomBlobs
62 {
63 get { return 2; }
64 }
65
69 public override int ExactNumTopBlobs
70 {
71 get { return 1; }
72 }
73
79 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
80 {
81 m_nCopyAxis = m_param.merge_param.copy_axis;
82 m_nOrderMajorAxis = m_param.merge_param.order_major_axis;
83 m_nCopyCount = m_param.merge_param.copy_count;
84 m_nSrcStartIdx1 = Utility.CanonicalAxisIndex(m_param.merge_param.src_start_idx1, colBottom[0].shape()[m_nCopyAxis]);
85 m_nDstStartIdx1 = Utility.CanonicalAxisIndex(m_param.merge_param.dst_start_idx1, colBottom[0].shape()[m_nCopyAxis]);
86 m_nCopyDim1 = m_param.merge_param.copy_dim1;
87 m_nSrcStartIdx2 = Utility.CanonicalAxisIndex(m_param.merge_param.src_start_idx2, colBottom[1].shape()[m_nCopyAxis]);
88 m_nDstStartIdx2 = Utility.CanonicalAxisIndex(m_param.merge_param.dst_start_idx2, colBottom[1].shape()[m_nCopyAxis]);
89 m_nCopyDim2 = m_param.merge_param.copy_dim2;
90
91 m_nSrcSpatialDim1 = Utility.GetSpatialDim(colBottom[0].shape(), Math.Max(m_nCopyAxis, m_nOrderMajorAxis) + 1);
92 m_nSrcSpatialDim2 = Utility.GetSpatialDim(colBottom[1].shape(), Math.Max(m_nCopyAxis, m_nOrderMajorAxis) + 1);
93 m_nDstSpatialDim = m_param.merge_param.dst_spatialdim;
94 if (m_nDstSpatialDim <= 0)
95 m_nDstSpatialDim = 1;
96
97 m_nSrcSpatialDimStartIdx1 = Utility.CanonicalAxisIndex(m_param.merge_param.src_spatialdim_start_idx1, m_nSrcSpatialDim1);
98 m_nSrcSpatialDimStartIdx2 = Utility.CanonicalAxisIndex(m_param.merge_param.src_spatialdim_start_idx1, m_nSrcSpatialDim2);
99 m_nDstSpatialDimStartIdx1 = Utility.CanonicalAxisIndex(m_param.merge_param.dst_spatialdim_start_idx1, m_nDstSpatialDim);
100 m_nDstSpatialDimStartIdx2 = Utility.CanonicalAxisIndex(m_param.merge_param.dst_spatialdim_start_idx1, m_nDstSpatialDim);
101 m_nSpatialDimCopyCount = m_param.merge_param.spatialdim_copy_count;
102
103 m_log.CHECK_GT(m_nCopyCount, 0, "The copy_count must be > 0!");
104
105 m_log.CHECK_GT(m_nCopyDim1, 0, "The copy dim must be > 0!");
106 m_log.CHECK_GT(m_nCopyDim2, 0, "The copy dim must be > 0!");
107 m_log.CHECK_GE(m_nSrcStartIdx1, 0, "The start_idx1 must be >= 0!");
108 m_log.CHECK_GE(m_nSrcStartIdx2, 0, "The start_idx1 must be >= 0!");
109 m_log.CHECK_GE(m_nDstStartIdx1, 0, "The start_idx1 must be >= 0!");
110 m_log.CHECK_GE(m_nDstStartIdx2, 0, "The start_idx1 must be >= 0!");
111
112 m_log.CHECK_GE(m_nCopyAxis, 0, "The copy axis must be >= 0!");
113 m_log.CHECK_LE(m_nCopyAxis, 1, "The copy axis must be <= 1!");
114 m_log.CHECK_EQ(m_nOrderMajorAxis, 1, "Currently, only order_major_axis=1 is supported.");
115 }
116
122 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
123 {
124 m_log.CHECK_EQ(colBottom.Count, 2, "Two inputs are expected!");
125 List<int> rgTopShape = MergeParameter.Reshape(m_log, m_param.merge_param, colBottom[0].shape(), colBottom[1].shape());
126
127 colTop[0].Reshape(rgTopShape);
128 }
129
137 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
138 {
139 long hBtmData0 = colBottom[0].gpu_data;
140 long hBtmData1 = colBottom[1].gpu_data;
141 long hTopData = colTop[0].mutable_gpu_data;
142
143 int nDstStep = colTop[0].shape()[m_nCopyAxis];
144 int nSrcStep1 = colBottom[0].shape()[m_nCopyAxis];
145 m_cuda.copy_sequence(colBottom[0].count(), hBtmData0, nSrcStep1, m_nSrcStartIdx1, m_nCopyCount, m_nCopyDim1, hTopData, nDstStep, m_nDstStartIdx1, m_nSrcSpatialDim1, m_nDstSpatialDim, m_nSrcSpatialDimStartIdx1, m_nDstSpatialDimStartIdx1, m_nSpatialDimCopyCount);
146
147 int nSrcStep2 = colBottom[1].shape()[m_nCopyAxis];
148 m_cuda.copy_sequence(colBottom[1].count(), hBtmData1, nSrcStep2, m_nSrcStartIdx2, m_nCopyCount, m_nCopyDim2, hTopData, nDstStep, m_nDstStartIdx2, m_nSrcSpatialDim2, m_nDstSpatialDim, m_nSrcSpatialDimStartIdx2, m_nDstSpatialDimStartIdx2, m_nSpatialDimCopyCount);
149 }
150
159 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
160 {
161 colBottom[0].SetDiff(0);
162 colBottom[1].SetDiff(0);
163 long hBtmDiff0 = colBottom[0].mutable_gpu_diff;
164 long hBtmDiff1 = colBottom[1].mutable_gpu_diff;
165 long hTopData = colTop[0].gpu_diff;
166
167 int nSrcStep = colTop[0].shape()[m_nCopyAxis];
168 int nDstStep1 = colBottom[0].shape()[m_nCopyAxis];
169 m_cuda.copy_sequence(colTop[0].count(), hTopData, nSrcStep, m_nDstStartIdx1, m_nCopyCount, m_nCopyDim1, hBtmDiff0, nDstStep1, m_nSrcStartIdx1, m_nDstSpatialDim, m_nSrcSpatialDim1, m_nDstSpatialDimStartIdx1, m_nSrcSpatialDimStartIdx1, m_nSpatialDimCopyCount);
170
171 int nDstStep2 = colBottom[1].shape()[m_nCopyAxis];
172 m_cuda.copy_sequence(colTop[0].count(), hTopData, nSrcStep, m_nDstStartIdx2, m_nCopyCount, m_nCopyDim2, hBtmDiff1, nDstStep2, m_nSrcStartIdx2, m_nDstSpatialDim, m_nSrcSpatialDim2, m_nDstSpatialDimStartIdx2, m_nSrcSpatialDimStartIdx2, m_nSpatialDimCopyCount);
173 }
174 }
175}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
void CHECK_LE(double df1, double df2, string str)
Test whether one number is less than or equal to another.
Definition: Log.cs:263
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static int CanonicalAxisIndex(int nIdx, int nNumAxes)
Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e...
Definition: Utility.cs:50
static int GetSpatialDim(List< int > rg, int nStartIdx=0)
Calculate the spatial dimension of an array starting at a given axis.
Definition: Utility.cs:64
The BlobCollection contains a list of Blobs.
void SetDiff(double df)
Set all blob diff to the value specified.
int Count
Returns the number of items in the collection.
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
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
The MergeLayer merges two bottom blobs with a specified copy pattern and outputs a single blob result...
Definition: MergeLayer.cs:20
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input1 and input2
Definition: MergeLayer.cs:62
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs.
Definition: MergeLayer.cs:159
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: MergeLayer.cs:122
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation.
Definition: MergeLayer.cs:137
MergeLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The MergeLayer constructor.
Definition: MergeLayer.cs:46
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: MergeLayer.cs:53
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: MergeLayer.cs:79
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: merge
Definition: MergeLayer.cs:70
Specifies the base parameter for all layers.
MergeParameter merge_param
Returns the parameter set when initialized with LayerType.MERGE
LayerType
Specifies the layer type.
Specifies the parameters for the MergeLayer.
static List< int > Reshape(Log log, MergeParameter p, List< int > rgShape1, List< int > rgShape2)
Calculate the new shape based on the merge parameter settings and the specified input shapes.
int dst_spatialdim_start_idx1
Specifies the dst1 spatial dim start index (only used when > 0).
int dst_start_idx2
Specifies the dst start index where copying begins for the second copy to dst blob in top(0).
int order_major_axis
Specifies axis providing the major ordering (e.g. axis=1 uses axis 1 as the major ordering with axis ...
int spatialdim_copy_count
Specifies the spatial dim copy count, used when less than the entire spatial dim is to be copied.
int copy_dim1
Specifies the dimension (sans the spatial dimension) to be copied (the full copy size = copy_dim * sp...
int src_start_idx2
Specifies the src start index where copying begins in the second input blob in bottom(1).
int src_start_idx1
Specifies the src start index where copying begins in the first blob in bottom(0).
int copy_dim2
Specifies the dimension (sans the spatial dimension) to be copied (the full copy size = copy_dim * sp...
int copy_axis
Specifies axis along which the indexing is applied when copying.
int dst_start_idx1
Specifies the dst start index where copying begins in the destination blob in top(0).
int copy_count
Specifies the number of skip copies along the copy_axis to copy (e.g. this is the number of skips to ...
int dst_spatialdim
Specifies the dst spatial dim which if not copied into is set to zero.
int src_spatialdim_start_idx1
Specifies the src1 spatial dim start index (only used when > 0).
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.beta namespace contains all beta stage layers.
Definition: LayerFactory.cs:9
The MyCaffe.param.beta parameters are used by the MyCaffe.layer.beta layers.
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