2using System.Collections.Generic;
43 int? m_nIgnoreLabel =
null;
44 double m_dfNormalizer = 0;
58 m_blobSigmoidOutput =
new Blob<T>(cuda, log);
60 m_blobLoss =
new Blob<T>(cuda, log);
72 m_sigmoidLayer.Dispose();
75 if (m_blobTarget !=
null)
112 base.LayerSetUp(colBottom, colTop);
114 m_colSigmoidBottomVec.
Clear();
115 m_colSigmoidBottomVec.
Add(colBottom[0]);
116 m_colSigmoidTopVec.
Clear();
117 m_colSigmoidTopVec.
Add(m_blobSigmoidOutput);
118 m_sigmoidLayer.Setup(m_colSigmoidBottomVec, m_colSigmoidTopVec);
130 base.Reshape(colBottom, colTop);
134 if (colBottom[0].count() != colBottom[1].count())
136 if (colBottom[1].count() != colBottom[0].num)
137 m_log.
FAIL(
"SIGMOID_CROSS_ENTROPY_LOSS layer inputs must have the same count, or the target must have 'num' items of indexes.");
140 if (m_blobTarget ==
null)
143 m_blobTarget.
Name =
"full_label";
149 m_sigmoidLayer.Reshape(m_colSigmoidBottomVec, m_colSigmoidTopVec);
177 if (m_blobTarget !=
null)
179 m_log.
CHECK_EQ(colBottom[0].num, colBottom[1].count(),
"SIGMOID_CROSS_ENTROPY_LOSS layer inputs must have the same count, or the target must have 'num' items of indexes.");
182 float[] rgfTarget =
convertF(colBottom[1].mutable_cpu_data);
183 for (
int i = 0; i < colBottom[1].num; i++)
185 int nTargetIdx = (int)rgfTarget[i];
191 m_colSigmoidBottomVec[0] = colBottom[0];
192 m_sigmoidLayer.Forward(m_colSigmoidBottomVec, m_colSigmoidTopVec);
195 int nCount = colBottom[0].count();
198 long hInputData = colBottom[0].gpu_data;
199 long hTarget = (m_blobTarget !=
null) ? m_blobTarget.
gpu_data : colBottom[1].gpu_data;
203 long hLossData = colBottom[0].mutable_gpu_diff;
204 long hCountData = (m_blobTarget !=
null) ? m_blobTarget.
mutable_gpu_diff : colBottom[1].mutable_gpu_diff;
206 m_cuda.sigmoid_cross_entropy_fwd(nCount, hInputData, hTarget, hLossData, m_nIgnoreLabel.HasValue, m_nIgnoreLabel.GetValueOrDefault(-1), hCountData);
208 double dfValidCount = nCount;
211 dfValidCount =
m_cuda.asum_double(nCount, hCountData);
213 double dfLoss =
m_cuda.asum_double(nCount, hLossData);
216 colTop[0].
SetData(dfLoss / m_dfNormalizer, 0);
219 if (colTop.
Count == 2)
222 colTop[1].ShareData(m_blobLoss);
229 if (m_blobTarget !=
null)
267 if (rgbPropagateDown[1])
270 if (rgbPropagateDown[0])
273 int nCount = colBottom[0].count();
274 long hSigmoidOutputData = m_blobSigmoidOutput.
gpu_data;
275 long hTarget = (m_blobTarget !=
null) ? m_blobTarget.
gpu_data : colBottom[1].gpu_data;
276 long hBottomDiff = colBottom[0].mutable_gpu_diff;
278 m_cuda.copy(nCount, hSigmoidOutputData, hBottomDiff);
282 if (m_nIgnoreLabel.HasValue)
283 m_cuda.sigmoid_cross_entropy_bwd(nCount, m_nIgnoreLabel.Value, hTarget, hBottomDiff);
286 double dfLossWeight =
convertD(colTop[0].GetDiff(0)) / m_dfNormalizer;
287 m_cuda.scal(nCount, dfLossWeight, hBottomDiff);
The Log class provides general output in text form.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
void SetData(double df)
Set all blob data to the value specified.
void SetDiff(double df)
Set all blob diff to the value specified.
int Count
Returns the number of items in the collection.
void Clear(bool bDispose=false)
Remove all items from the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
float convertF(T df)
Converts a generic to a float value.
double convertD(T df)
Converts a generic to a double value.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The LossLayer provides an interface for Layer's that take two blobs as input – usually (1) prediction...
int m_nOuterNum
Specifies the outer num, such as the batch count (e.g. count(0, axis)). Each derivative class must se...
int m_nInnerNum
Specifies the inner num, such as the channel + height + width (e.g. count(axis + 1))....
virtual double get_normalizer(LossParameter.NormalizationMode normalization_mode, int nValidCount)
Returns the normalizer used to normalize the loss.
LossParameter.NormalizationMode m_normalization
Specifies the normalization mode used to normalize the loss.
The SigmoidCrossEntropyLayer computes the cross-entropy (logisitic) loss and is often used for predic...
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the sigmoid cross-entropy loss error gradient w.r.t. the predictions.
override void dispose()
Releases all GPU and host resources used by the Layer.
SigmoidCrossEntropyLossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SigmoidCrossEntropyLayer constructor.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs as variable.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: loss.
override int MaxTopBlobs
Returns the maximum number of required top (output) Blobs: loss, loss values
The SigmoidLayer is a neuron layer that calculates the sigmoid function, a classc choice for neural n...
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
LayerType
Specifies the layer type.
override string ToString()
Returns a string representation of the LayerParameter.
LossParameter loss_param
Returns the parameter set when initialized with LayerType.LOSS
virtual LayerParameter Clone(bool bCloneBlobs)
Creates a new copy of this instance of the parameter.
Stores the parameters used by loss layers.
NormalizationMode
How to normalize the loss for loss layers that aggregate across batches, spatial dimensions,...
int? ignore_label
If specified, the ignore instances with the given label.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
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-...