2using System.Collections.Generic;
29 int? m_nIgnoreLabel =
null;
30 double m_dfNormalizer = 0;
31 int m_nSoftmaxAxis = 1;
44 m_blobProb =
new Blob<T>(cuda, log);
46 m_blobLogProb =
new Blob<T>(cuda, log);
48 m_blobLoss =
new Blob<T>(cuda, log);
57 if (m_softmaxLayer !=
null)
58 m_softmaxLayer.Dispose();
103 base.LayerSetUp(colBottom, colTop);
116 m_softmaxLayer.Setup(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
124 m_logLayer.
Setup(m_colLogBottomVec, m_colLogTopVec);
134 base.Reshape(colBottom, colTop);
138 m_softmaxLayer.Reshape(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
139 m_logLayer.
Reshape(m_colLogBottomVec, m_colLogTopVec);
142 m_nOuterNum = colBottom[0].count(0, m_nSoftmaxAxis);
143 m_nInnerNum = colBottom[0].count(m_nSoftmaxAxis + 1);
147 m_log.
CHECK_EQ(colBottom[0].count(0, m_nSoftmaxAxis), colBottom[1].count(0, m_nSoftmaxAxis),
"Number of labels must match number of predictions; e.g., if softmax axis == 1 and prediction shape is (N, C, H, W), label count (number of labels) must be N*H*W, with integer values in {0, 1, ..., C-1}.");
149 if (colTop.
Count >= 2)
178 m_softmaxLayer.Forward(m_colSoftmaxBottomVec, m_colSoftmaxTopVec);
181 m_logLayer.
Forward(m_colLogBottomVec, m_colLogTopVec);
184 long hProbData = m_blobLogProb.
gpu_data;
185 long hTarget = colBottom[1].gpu_data;
186 int nInputCount = m_blobProb.
count();
187 int nDim = m_blobProb.
shape()[m_nSoftmaxAxis];
197 long hCountData = colBottom[1].mutable_gpu_diff;
200 m_cuda.softmax_cross_entropy_fwd(colBottom[0].count(), hProbData, hTarget, hLossDiff, hLossData,
m_nOuterNum, nDim,
m_nInnerNum, hCountData, m_nIgnoreLabel.GetValueOrDefault(-1));
201 double dfLoss =
m_cuda.asum_double(colBottom[0].count(), hLossData);
203 double dfValidCount = nCount;
206 dfValidCount =
m_cuda.asum_double(nCount, hCountData);
209 double dfFinalLoss = dfLoss / m_dfNormalizer;
211 colTop[0].
SetData(dfFinalLoss, 0);
214 if (colTop.
Count == 2)
253 if (!rgbPropagateDown[0])
257 float fGrad =
convertF(colTop[0].GetDiff(0));
258 fGrad = -1.0f * fGrad / (float)m_dfNormalizer;
263 m_blobLogProb.
CopyFrom(m_blobLoss,
true);
264 m_logLayer.
Backward(m_colLogTopVec, rgbPropagateDown, m_colLogBottomVec);
267 m_softmaxLayer.Backward(m_colSoftmaxTopVec, rgbPropagateDown, m_colSoftmaxBottomVec);
The Log class provides general output in text form.
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 ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
The Blob is the main holder of data that moves through the Layers of the Net.
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 CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
int count()
Returns the total number of items in the Blob.
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 scale_diff(double df)
Scale the diff by a scaling factor.
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.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
void Backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Given the top Blob error gradients, compute the bottom Blob error gradients.
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
float convertF(T df)
Converts a generic to a float value.
abstract void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Adjust the shapes of top blobs and internal buffers to accomodate the shapes of the bottom blobs.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
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...
bool m_bIgnoreLabels
Set to true when labels are to be ignored.
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 SoftmaxCrossEntropy2Layer computes the cross-entropy (logisitic) loss and is often used for predi...
SoftmaxCrossEntropy2LossLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SoftmaxCrossEntropyLayer constructor.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the softmax cross entropy loss error gradient w.r.t the predictions.
override int MaxTopBlobs
Returns the maximum number of required top (output) Blobs: loss, loss values
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override int MinTopBlobs
Returns the minimum number of required top (output) Blobs: loss.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs as variable.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The forward computation for softmax cross entropy loss.
override void dispose()
Releases all GPU and host resources used by the Layer.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
List< double > loss_weight
Specifies the loss weight.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
void SetType(LayerType type, bool bNewParam=true)
Set the layer type.
LayerType
Specifies the layer type.
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.
Specifies the parameters for the SoftmaxLayer
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
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-...