MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
CropLayer.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{
21 public class CropLayer<T> : Layer<T>
22 {
23 Blob<T> m_blobOffsets;
24 Blob<T> m_blobSrcStrides;
25 Blob<T> m_blobDstStrides;
26
35 : base(cuda, log, p)
36 {
38
39 m_blobOffsets = new common.Blob<T>(m_cuda, m_log);
40 m_blobOffsets.Name = m_param.name + "offsets";
41 m_blobSrcStrides = new common.Blob<T>(m_cuda, m_log);
42 m_blobSrcStrides.Name = m_param.name + "src strides";
43 m_blobDstStrides = new common.Blob<T>(m_cuda, m_log);
44 m_blobDstStrides.Name = m_param.name + "dst strides";
45 }
46
48 protected override void dispose()
49 {
50 m_blobOffsets.Dispose();
51 m_blobSrcStrides.Dispose();
52 m_blobDstStrides.Dispose();
53 base.dispose();
54 }
55
57 protected override void setup_internal_blobs(BlobCollection<T> col)
58 {
59 if (col.Count > 0)
60 return;
61
62 col.Add(m_blobOffsets);
63 col.Add(m_blobSrcStrides);
64 col.Add(m_blobDstStrides);
65 }
66
70 public override int ExactNumBottomBlobs
71 {
72 get { return 2; }
73 }
74
78 public override int ExactNumTopBlobs
79 {
80 get { return 1; }
81 }
82
88 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
89 {
90 m_log.CHECK_EQ(colBottom.Count, 2, "Wrong number of bottom blobs, expected 2.");
91 int nInputDim = colBottom[0].num_axes;
92 int nStartAxis = colBottom[0].CanonicalAxisIndex(m_param.crop_param.axis);
93 m_log.CHECK_LT(nStartAxis, nInputDim, "The crop axis is bigger than the input dim.");
94
95 if (m_param.crop_param.offset.Count > 1)
96 {
97 // The number of crop values specified must be equal to the number of
98 // dimensions following axis.
99 m_log.CHECK_EQ(nStartAxis + m_param.crop_param.offset.Count, nInputDim, "The number of offset values specified must be equal to the number of dimensions following axis.");
100 }
101 }
102
108 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
109 {
110 int nInputDim = colBottom[0].num_axes;
111 int nStartAxis = colBottom[0].CanonicalAxisIndex(m_param.crop_param.axis);
112
113 // Initialize offsets to 0 and the new shape to the current shape of the data.
114 List<int> rgNewShape = Utility.Clone<int>(colBottom[0].shape());
115 List<int> rgOffsetsShape = new List<int>() { nInputDim };
116
117 m_blobOffsets.Reshape(rgOffsetsShape);
118 float[] rgOffsetData = convertF(m_blobOffsets.mutable_cpu_data);
119
120 // Determine crop offsets and the new shape post-crop.
121 for (int i = 0; i < nInputDim; i++)
122 {
123 int nCropOffset = 0;
124 int nNewSize = colBottom[0].shape(i);
125
126 if (i >= nStartAxis)
127 {
128 nNewSize = colBottom[1].shape(i);
129 if (m_param.crop_param.offset.Count == 1)
130 {
131 // If only one offset is given, all crops have the same offset.
132 nCropOffset = (int)m_param.crop_param.offset[0];
133 }
134 else if (m_param.crop_param.offset.Count > 1)
135 {
136 // For several offsets, the number of offsets must be equual to the
137 // number of dimensions to crop, that is dimensions after the axis.
138 nCropOffset = (int)m_param.crop_param.offset[i - nStartAxis];
139 }
140
141 // Check that the crop and offset are within the dimension's bounds.
142 m_log.CHECK_GE(colBottom[0].shape(i) - nCropOffset, colBottom[1].shape(i), "The crop for dimension " + i.ToString() + " is out-of-bounds with size " + colBottom[0].shape(i).ToString() + " and offset " + nCropOffset.ToString());
143 }
144
145 rgNewShape[i] = nNewSize;
146 rgOffsetData[i] = nCropOffset;
147 }
148
149 m_blobOffsets.mutable_cpu_data = convert(rgOffsetData);
150
151 colTop[0].Reshape(rgNewShape);
152
153 // Compute strides
154 m_blobSrcStrides.Reshape(rgOffsetsShape);
155 m_blobDstStrides.Reshape(rgOffsetsShape);
156
157 float[] rgSrcStrides = convertF(m_blobSrcStrides.mutable_cpu_data);
158 float[] rgDstStrides = convertF(m_blobDstStrides.mutable_cpu_data);
159
160 for (int i = 0; i < nInputDim; i++)
161 {
162 rgSrcStrides[i] = colBottom[0].count(i + 1, nInputDim);
163 rgDstStrides[i] = colTop[0].count(i + 1, nInputDim);
164 }
165
166 m_blobSrcStrides.mutable_cpu_data = convert(rgSrcStrides);
167 m_blobDstStrides.mutable_cpu_data = convert(rgDstStrides);
168 }
169
183 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
184 {
185 List<int> rgIndices = Utility.Create<int>(colTop[0].num_axes, 0);
186 long hBottomData = colBottom[0].gpu_data;
187 long hTopData = colTop[0].mutable_gpu_data;
188 int nCount = colTop[0].count();
189
190 m_cuda.crop_fwd(nCount, colBottom[0].num_axes, m_blobSrcStrides.gpu_data, m_blobDstStrides.gpu_data, m_blobOffsets.gpu_data, hBottomData, hTopData);
191 }
192
200 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
201 {
202 if (rgbPropagateDown[0])
203 {
204 colBottom[0].SetDiff(0);
205
206 long hTopDiff = colTop[0].gpu_diff;
207 long hBottomDiff = colBottom[0].mutable_gpu_diff;
208 int nCount = colTop[0].count();
209
210 m_cuda.crop_bwd(nCount, colBottom[0].num_axes, m_blobSrcStrides.gpu_data, m_blobDstStrides.gpu_data, m_blobOffsets.gpu_data, hBottomDiff, hTopDiff);
211 }
212 }
213 }
214}
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_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
void CHECK_LT(double df1, double df2, string str)
Test whether one number is less than another.
Definition: Log.cs:275
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static List< int > Create(int nCount, int nStart, int nInc)
Create a new List and fill it with values starting with start and incrementing by inc.
Definition: Utility.cs:721
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
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 Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
Blob(CudaDnn< T > cuda, Log log, bool bIncludeDiff=true, bool bUseHalfSize=false)
The Blob constructor.
Definition: Blob.cs:64
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
Definition: Blob.cs:1461
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The CropLayer takes a Blob and crops it to the shape specified by the second input Blob,...
Definition: CropLayer.cs:22
override void dispose()
Releases all GPU and host resources used by the Layer.
Definition: CropLayer.cs:48
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input, shape
Definition: CropLayer.cs:71
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: CropLayer.cs:108
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the concatenation inputs.
Definition: CropLayer.cs:200
CropLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The CropLayer constructor.
Definition: CropLayer.cs:34
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
Definition: CropLayer.cs:183
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: crop
Definition: CropLayer.cs:79
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
Definition: CropLayer.cs:57
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: CropLayer.cs:88
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
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
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
List< uint > offset
Specifies the offset to set the shift for all/each dimension.
int axis
The axis along which to crop – may be negative to index from the end (e.g., -1 for the last axis)....
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
LayerType
Specifies the layer type.
CropParameter crop_param
Returns the parameter set when initialized with LayerType.CROP
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