MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
GramLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.layers;
8using MyCaffe.param;
9using MyCaffe.param.nt;
10
11namespace MyCaffe.layers.nt
12{
21 public class GramLayer<T> : Layer<T>
22 {
23 int m_nK;
24 int m_nM;
25 int m_nN;
26 double m_dfAlpha = 1.0;
27 double m_dfBeta = 1.0;
28
36 : base(cuda, log, p)
37 {
39 }
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 m_dfAlpha = m_param.gram_param.alpha;
66 m_dfBeta = m_param.gram_param.beta;
67
68 // Setup the convert to half flags used by the Layer just before calling forward and backward.
70
71 m_log.CHECK_GT(m_dfAlpha, 0, "The 'alpha' parameter must be greater than zero.");
72 m_log.CHECK_GT(m_dfBeta, 0, "The 'beta' parameter must be greater than zero.");
73 }
74
80 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
81 {
82 int nAxis = colBottom[0].CanonicalAxisIndex(m_param.gram_param.axis);
83
84 // Dimensions starting from 'axis' are 'flattened' into a single length 'K' vector.
85 m_nK = colBottom[0].count(nAxis);
86
87 // The first 'axis - 1' dimensions are independent Gram matrices; the total
88 // number of these is 'M' the product over these dimensions.
89 m_nM = colBottom[0].count(0, nAxis - 1);
90
91 // Gram matrices will be 'N' by 'N'
92 m_nN = colBottom[0].shape(nAxis - 1);
93
94 List<int> rgTopShape = Utility.Clone<int>(colBottom[0].shape(), nAxis + 1);
95 rgTopShape[nAxis] = m_nN;
96
97 colTop[0].Reshape(rgTopShape, m_bUseHalfSize);
98 }
99
109 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
110 {
111 long hBottomData = colBottom[0].gpu_data;
112 long hTopData = colTop[0].mutable_gpu_data;
113 T fScale = m_tOne;
114
115 if (m_dfAlpha != 1.0)
116 fScale = (T)Convert.ChangeType(m_dfAlpha, typeof(T));
117
118 for (int i = 0; i < m_nM; i++)
119 {
120 m_cuda.gemm(false, true, m_nN, m_nN, m_nK, fScale, hBottomData, hBottomData, m_tZero, hTopData, i * m_nK * m_nN, i * m_nK * m_nN, i * m_nN * m_nN);
121 }
122
123 if (m_dfBeta != 1.0)
124 colTop[0].scale_data(m_dfBeta);
125 }
126
135 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
136 {
137 if (!rgbPropagateDown[0])
138 return;
139
140 long hTopDiff = colTop[0].gpu_diff;
141 long hBottomDiff = colBottom[0].mutable_gpu_diff;
142 long hBottomData = colBottom[0].gpu_data;
143 T fScale = m_tOne;
144
146 {
147 if (m_dfAlpha != 1.0)
148 fScale = (T)Convert.ChangeType(1.0 / m_dfAlpha, typeof(T));
149 }
150
151 for (int i = 0; i < m_nM; i++)
152 {
153 m_cuda.gemm(false, false, m_nN, m_nK, m_nN, fScale, hTopDiff, hBottomData, m_tZero, hBottomDiff, i * m_nN * m_nN, i * m_nK * m_nN, i * m_nK * m_nN);
154 m_cuda.gemm(true, false, m_nN, m_nK, m_nN, fScale, hTopDiff, hBottomData, m_tOne, hBottomDiff, i * m_nN * m_nN, i * m_nK * m_nN, i * m_nK * m_nN);
155 }
156
158 {
159 if (m_dfBeta != 1.0)
160 colBottom[0].scale_diff(1.0 / m_dfBeta);
161 }
162 }
163 }
164}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
The Utility class provides general utility funtions.
Definition: Utility.cs:35
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
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
T m_tZero
Specifies a generic type equal to 0.0.
Definition: Layer.cs:76
T m_tOne
Specifies a generic type equal to 1.0.
Definition: Layer.cs:72
bool m_bUseHalfSize
Specifies that the half size of the top (if any) should be converted to the base size.
Definition: Layer.cs:84
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 GramLayer computes the Gram matrix used in Neural Style.
Definition: GramLayer.cs:22
GramLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The GramLayer constructor.
Definition: GramLayer.cs:35
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: GramLayer.cs:63
override int ExactNumBottomBlobs
Returns the exact number of bottom blobs (e.g. 1)
Definition: GramLayer.cs:46
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the Gram matrix values.
Definition: GramLayer.cs:109
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
Definition: GramLayer.cs:80
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the absolute value inputs.
Definition: GramLayer.cs:135
override int ExactNumTopBlobs
Returns the exact number of bottom blobs (e.g. 1)
Definition: GramLayer.cs:54
Specifies the base parameter for all layers.
bool use_halfsize
Specifies whether or not to use half sized memory or not.
GramParameter gram_param
Returns the parameter set when initialized with LayerType.GRAM
LayerType
Specifies the layer type.
double beta
Specifies the scaling factor applied after the gram operation.
bool disable_scaling_on_gradient
Specifies whether or not to apply the un-scaling of the alpha and beta values during the during the b...
double alpha
Specifies the scaling factor applied before the gram operation.
int axis
The first axis to be lumped into a single Gram matrix computation; all preceding axes are retained in...
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.nt namespace contains all Neural Transfer related layers.
Definition: LayerFactory.cs:19
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
The MyCaffe.param.nt namespace defines the parameters used by the Nerual Style Transfer 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