MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
InterpLayer.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{
22 public class InterpLayer<T> : Layer<T>
23 {
24 int m_nNum;
25 int m_nChannels;
26 int m_nHeightIn;
27 int m_nHeightOut;
28 int m_nWidthIn;
29 int m_nWidthOut;
30 int m_nHeightInEff;
31 int m_nWidthInEff;
32 int m_nPadBeg;
33 int m_nPadEnd;
34
43 : base(cuda, log, p)
44 {
46 }
47
51 public override int ExactNumBottomBlobs
52 {
53 get { return 1; }
54 }
55
59 public override int ExactNumTopBlobs
60 {
61 get { return 1; }
62 }
63
69 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
70 {
71 int nNumSpecs = 0;
72
73 nNumSpecs += (m_param.interp_param.zoom_factor.HasValue) ? 1 : 0;
74 nNumSpecs += (m_param.interp_param.shrink_factor.HasValue) ? 1 : 0;
75 nNumSpecs += (m_param.interp_param.height.HasValue && m_param.interp_param.width.HasValue) ? 1 : 0;
76 m_log.CHECK_EQ(nNumSpecs, 1, "Output dimension specified by zoom factor OR shrink factor OR explicitly.");
77
78 m_nPadBeg = m_param.interp_param.pad_beg;
79 m_nPadEnd = m_param.interp_param.pad_end;
80 m_log.CHECK_LE(m_nPadBeg, 0, "Only supports non-positive padding (cropping) for now.");
81 m_log.CHECK_LE(m_nPadEnd, 0, "Only supports non-positive padding (cropping) for now.");
82 }
83
89 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
90 {
91 m_nNum = colBottom[0].num;
92 m_nChannels = colBottom[0].channels;
93 m_nHeightIn = colBottom[0].height;
94 m_nWidthIn = colBottom[0].width;
95
96 m_nHeightInEff = m_nHeightIn + m_nPadBeg + m_nPadEnd;
97 m_nWidthInEff = m_nWidthIn + m_nPadBeg + m_nPadEnd;
98
99 if (m_param.interp_param.zoom_factor.HasValue)
100 {
101 int nZoomFactor = m_param.interp_param.zoom_factor.Value;
102 m_log.CHECK_GE(nZoomFactor, 1, "The zoom factor must be positive.");
103 m_nHeightOut = m_nHeightInEff + (m_nHeightInEff /*- 1*/) * (nZoomFactor - 1);
104 m_nWidthOut = m_nWidthInEff + (m_nWidthInEff /*- 1*/) * (nZoomFactor - 1);
105 }
106 else if (m_param.interp_param.shrink_factor.HasValue)
107 {
108 int nShrinkFactor = m_param.interp_param.shrink_factor.Value;
109 m_log.CHECK_GE(nShrinkFactor, 1, "The shrink factor must be positive.");
110 m_nHeightOut = (m_nHeightInEff - 1) / nShrinkFactor + 1;
111 m_nWidthOut = (m_nWidthInEff - 1) / nShrinkFactor + 1;
112 }
113 else if (m_param.interp_param.height.HasValue && m_param.interp_param.width.HasValue)
114 {
115 m_nHeightOut = m_param.interp_param.height.Value;
116 m_nWidthOut = m_param.interp_param.width.Value;
117 }
118 else
119 {
120 m_log.FAIL("You must specify the zoom factor OR shrink factor OR explicit size.");
121 }
122
123 m_log.CHECK_GT(m_nHeightInEff, 0, "The height should be positive.");
124 m_log.CHECK_GT(m_nWidthInEff, 0, "The width should be positive.");
125 m_log.CHECK_GT(m_nHeightOut, 0, "The height should be positive.");
126 m_log.CHECK_GT(m_nWidthOut, 0, "The width should be positive.");
127
128 colTop[0].Reshape(m_nNum, m_nChannels, m_nHeightOut, m_nWidthOut);
129 }
130
140 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
141 {
142 m_cuda.interp2(m_nNum * m_nChannels, colBottom[0].gpu_data, -m_nPadBeg, -m_nPadBeg, m_nHeightInEff, m_nWidthInEff, m_nHeightIn, m_nWidthIn, colTop[0].mutable_gpu_data, 0, 0, m_nHeightOut, m_nWidthOut, m_nHeightOut, m_nWidthOut, false);
143 }
144
153 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
154 {
155 if (!rgbPropagateDown[0])
156 return;
157
158 colBottom[0].SetDiff(0);
159 m_cuda.interp2(m_nNum * m_nChannels, colBottom[0].mutable_gpu_diff, -m_nPadBeg, -m_nPadBeg, m_nHeightInEff, m_nWidthInEff, m_nHeightIn, m_nWidthIn, colTop[0].gpu_diff, 0, 0, m_nHeightOut, m_nWidthOut, m_nHeightOut, m_nWidthOut, true);
160 }
161 }
162}
The Log class provides general output in text form.
Definition: Log.cs:13
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
Definition: Log.cs:394
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 BlobCollection contains a list of Blobs.
void SetDiff(double df)
Set all blob diff to the value specified.
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 InterpLayer changes the spatial resolution by bi-linear interpolation.
Definition: InterpLayer.cs:23
InterpLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The InterpLayer constructor.
Definition: InterpLayer.cs:42
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: InterpLayer.cs:69
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
Definition: InterpLayer.cs:140
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: input.
Definition: InterpLayer.cs:52
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: InterpLayer.cs:89
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: flatten
Definition: InterpLayer.cs:60
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the concatenate inputs.
Definition: InterpLayer.cs:153
int? zoom_factor
Specifies the height of the output.
int? height
Specifies the height of the output.
int? width
Specifies the width of the output.
int pad_beg
Specifies the padding at the begin of the output.
int? shrink_factor
Specifies the shrink factor of the output.
int pad_end
Specifies the padding at the end of the output.
Specifies the base parameter for all layers.
InterpParameter interp_param
Returns the parameter set when initializing the LayerType.INTERP
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.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