MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
SplitLayer.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{
16 public class SplitLayer<T> : Layer<T>
17 {
18 int m_nCount;
19
27 : base(cuda, log, p)
28 {
30 }
31
35 public override int ExactNumBottomBlobs
36 {
37 get { return 1; }
38 }
39
43 public override int MinTopBlobs
44 {
45 get { return 1; }
46 }
47
53 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
54 {
55 m_bConvertBottom = false;
56
57 // Copy data during setup through
58 // forward pass to ensure variables
59 // are passed through.
60 Reshape(colBottom, colTop);
61 forward(colBottom, colTop);
62 }
63
69 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
70 {
71 m_nCount = colBottom[0].count();
72
73 for (int i = 0; i < colTop.Count; i++)
74 {
75 // Do not allow in-place computation in the SplitLayer. Instead, share data by
76 // reference in the forward pass, and keep separeate diff allocations in
77 // the backward pass. (Technically, it should be possible to share the diff
78 // blob of the first split output with the input, but this seems to cause
79 // some strange effects in practice...)
80 m_log.CHECK(colTop[i].gpu_data != colBottom[0].gpu_data, "Layer does not allow in-place computation.");
81 colTop[i].ReshapeLike(colBottom[0], colBottom[0].HalfSize);
82 m_log.CHECK_EQ(m_nCount, colTop[i].count(), "The count should equal the top[i].count().");
83 }
84 }
85
95 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
96 {
97 int nCount = colBottom[0].count();
98 long hBottom = colBottom[0].gpu_data;
99
100 for (int i = 0; i < colTop.Count; i++)
101 {
102 m_cuda.copy(nCount, hBottom, colTop[i].mutable_gpu_data);
103 }
104 }
105
114 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
115 {
116 if (!rgbPropagateDown[0])
117 return;
118
119 if (colTop.Count == 1)
120 {
121 m_cuda.copy(m_nCount, colTop[0].gpu_diff, colBottom[0].mutable_gpu_diff);
122 return;
123 }
124
125 m_cuda.add(m_nCount, colTop[0].gpu_diff, colTop[1].gpu_diff, colBottom[0].mutable_gpu_diff);
126
127 // Add remaining top blob diffs.
128 for (int i = 2; i < colTop.Count; i++)
129 {
130 long hTopDiff = colTop[i].gpu_diff;
131 long hBottomDiff = colBottom[0].mutable_gpu_diff;
132
133 m_cuda.axpy(m_nCount, m_tOne, hTopDiff, hBottomDiff);
134 }
135 }
136 }
137}
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.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
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
T m_tOne
Specifies a generic type equal to 1.0.
Definition: Layer.cs:72
bool m_bConvertBottom
Specifies whether or not the layer should convert the bottom when using half sized memory.
Definition: Layer.cs:96
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 SplitLayer creates a 'split' path in the network by copying the bottom blob into multiple top blo...
Definition: SplitLayer.cs:17
SplitLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SplitLayer constructor.
Definition: SplitLayer.cs:26
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: split
Definition: SplitLayer.cs:44
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation copying the bottom Blobs with all of the top Blobs.
Definition: SplitLayer.cs:95
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs.
Definition: SplitLayer.cs:114
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: SplitLayer.cs:69
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: SplitLayer.cs:53
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input.
Definition: SplitLayer.cs:36
Specifies the base parameter for all layers.
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