2using System.Collections.Generic;
20 int m_nCacheSize = 100;
22 int m_nEncodingDim = 0;
27 Dictionary<int, int> m_rgLabelCounts =
new Dictionary<int, int>();
44 m_blobEncodings =
new Blob<T>(cuda, log);
46 m_blobDistSq =
new Blob<T>(cuda, log,
false);
48 m_blobSummerVec =
new Blob<T>(cuda, log,
false);
50 m_blobData =
new Blob<T>(cuda, log);
71 col.
Add(m_blobEncodings);
72 col.
Add(m_blobDistSq);
73 col.
Add(m_blobSummerVec);
104 m_log.
WriteLine(
"WARNING: The Accuracy Encoding Layer does not use the 'ignore_label' parameter.");
107 m_log.
CHECK_GT(m_nCacheSize, 0,
"The cache size must be > 0.");
115 List<int> rgCentroidShape =
new List<int>() { 0 };
118 blobCentroids.
Reshape(2, m_nEncodingDim, 1, 1);
133 int nNum = colBottom[0].num;
134 bool bFirstReshape = (nNum != m_nNum) ?
true :
false;
136 m_nEncodingDim = colBottom[0].channels;
138 m_log.
CHECK_EQ(colBottom[1].num, m_nNum,
"The number of labels does not match the number of items at bottom[0].");
140 List<int> rgTopShape =
new List<int>();
145 m_blobSummerVec.
Reshape(colBottom[0].channels, 1, 1, 1);
170 m_log.
CHECK_EQ(colBottom[1].count() % 2, 0,
"The bottom[1] count must be a factor of 2 for {lbl1, lbl2}.");
171 int nItemNum = colBottom[0].num;
173 double dfAlpha = 1.0 / (double)nItemCount;
175 double dfAccuracy = 0;
176 double[] rgBottomLabel =
convertD(colBottom[1].update_cpu_data());
177 int nCorrectCount = 0;
178 int nComparedCount = 0;
180 int nMaxLabel = rgBottomLabel.Max(p => (
int)p);
181 int nMaxKey = (m_rgLabelCounts.Count == 0) ? 0 : m_rgLabelCounts.Max(p => p.Key);
182 if (nMaxLabel > nMaxKey)
184 int nNumLabels = nMaxLabel + 1;
186 m_colBlobs[0].Reshape(nNumLabels, m_nEncodingDim, 1, 1);
188 m_blobData.
Reshape(nNumLabels, m_nEncodingDim, 1, 1);
189 m_blobDistSq.
Reshape(nNumLabels, 1, 1, 1);
190 m_rgLabelCounts.Clear();
193 for (
int i = 0; i < colBottom[0].num; i++)
195 int nLabel = (int)rgBottomLabel[i * 2];
196 int nLabelItemCount = 0;
198 if (m_rgLabelCounts.ContainsKey(nLabel))
199 nLabelItemCount = m_rgLabelCounts[nLabel];
203 if (nLabelItemCount == 0)
206 m_cuda.copy(m_nEncodingDim, colBottom[0].gpu_data,
m_colBlobs[0].mutable_gpu_data, i * m_nEncodingDim, nLabel * m_nEncodingDim);
209 else if (nLabelItemCount < nItemCount)
211 dfAlpha = 1.0 / (nLabelItemCount + 1);
213 m_cuda.add(m_nEncodingDim, colBottom[0].gpu_data,
m_colBlobs[0].gpu_data,
m_colBlobs[0].mutable_gpu_data, dfAlpha, 1.0 - dfAlpha, i * m_nEncodingDim, nLabel * m_nEncodingDim, nLabel * m_nEncodingDim);
218 m_cuda.add(m_nEncodingDim, colBottom[0].gpu_data,
m_colBlobs[0].gpu_data,
m_colBlobs[0].mutable_gpu_data, dfAlpha, 1.0 - dfAlpha, i * m_nEncodingDim, nLabel * m_nEncodingDim, nLabel * m_nEncodingDim);
223 if (!m_rgLabelCounts.ContainsKey(nLabel))
224 m_rgLabelCounts.Add(nLabel, 1);
226 m_rgLabelCounts[nLabel]++;
229 int nCount = m_blobData.
count();
230 int nItems = m_blobData.
num;
231 m_cuda.fill(nItems, m_nEncodingDim, colBottom[0].gpu_data, i * m_nEncodingDim, nCount, m_blobData.
mutable_gpu_data);
254 int nDetectedLabel = -1;
255 double dfMin =
double.MaxValue;
257 for (
int l = 0; l < rgLabelDist.Length; l++)
259 if (rgLabelDist[l] < dfMin)
261 dfMin = rgLabelDist[l];
266 if (nDetectedLabel == nLabel)
272 dfAccuracy = (nComparedCount == 0) ? 0 : (
double)nCorrectCount / nComparedCount;
274 colTop[0].
SetData(dfAccuracy, 0);
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
void CHECK_GT(double df1, double df2, string str)
Test whether one number is greater than 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.
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.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
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.
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...
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
bool reshape_when_sharing
When true, this Blob is reshaped to the source when sharing the source data (default = false).
int count()
Returns the total number of items in the Blob.
string Name
Get/set the name of the Blob.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
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 convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
bool shareParameter(Blob< T > b, List< int > rgMinShape, bool bAllowEndsWithComparison=false)
Attempts to share a parameter Blob if another parameter Blob with the same name and accpetable size i...
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.
BlobCollection< T > m_colBlobs
Specifies the learnable parameter Blobs of the Layer.
The AccuracyEncodingLayer computes the classification accuracy for an encoding used in a classificati...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward compuation.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
AccuracyEncodingLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
Constructor.
override int ExactNumTopBlobs
Returns the number of top blobs: accuracy
override void dispose()
Releases all GPU and host resources used by the Layer.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented – EncodingAccuracyLayer cannot be used as a loss.
override int ExactNumBottomBlobs
Returns the number of bottom blobs used: predicted, label
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
uint top_k
When computing accuracy, count as correct by comparing the true label to the top_k scoring classes....
List< int > ignore_labels
If specified, ignore instances with the given label(s).
int axis
The 'label' axis of the prediction blob, whos argmax corresponds to the predicted label – may be nega...
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
AccuracyParameter accuracy_param
Returns the parameter set when initialized with LayerType.ACCURACY
DecodeParameter decode_param
Returns the parameter set when initializing with LayerType.DECODE or LayerType.ACCURACY_ENCODING;
LayerType
Specifies the layer type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
BLOB_TYPE
Defines the tpe of data held by a given Blob.
The MyCaffe.layers.beta namespace contains all beta stage 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-...