MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
AccuracyDecodeLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
10{
17 public class AccuracyDecodeLayer<T> : Layer<T>
18 {
30 : base(cuda, log, p)
31 {
32 m_type = LayerParameter.LayerType.ACCURACY_DECODE;
33 }
34
36 protected override void dispose()
37 {
38 base.dispose();
39 }
40
44 public override int ExactNumBottomBlobs
45 {
46 get { return 2; }
47 }
48
52 public override int ExactNumTopBlobs
53 {
54 get { return 1; }
55 }
56
62 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
63 {
64 m_log.CHECK_EQ((int)m_param.accuracy_param.top_k, 1, "Accuracy Encoding Layer only supports a topk = 1.");
65 m_log.CHECK_EQ((int)m_param.accuracy_param.axis, 1, "Accuracy Encoding Layer expects axis to = 1.");
66 }
67
73 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
74 {
75 List<int> rgTopShape = new List<int>(); // Accuracy is a scalar; 0 axes.
76 colTop[0].Reshape(rgTopShape);
77 colTop[0].type = BLOB_TYPE.ACCURACY;
78 }
79
98 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
99 {
100 m_log.CHECK_EQ(colBottom[0].num, colBottom[1].num, "The bottom[0] and bottom[1] must have the same num.");
101
102 int nNum = colBottom[0].num;
103 int nClasses = colBottom[0].channels;
104 int nLabelDim = colBottom[1].channels;
105 float[] rgDecode = convertF(colBottom[0].update_cpu_data()); // num of items where each item contains a distance for each class.
106 float[] rgLabel = convertF(colBottom[1].update_cpu_data()); // num of labels where labels are in pairs of label dim (we only use the first label).
107 int nCorrectCount = 0;
108 int nTotal = 0;
109
110 for (int i = 0; i < nNum; i++)
111 {
112 int nExpectedLabel = (int)rgLabel[i * nLabelDim];
113 int nActualLabel = -1;
114 float fMin = float.MaxValue;
115
116 for (int j = 0; j < nClasses; j++)
117 {
118 float fDist = rgDecode[i * nClasses + j];
119 if (fDist < fMin)
120 {
121 fMin = fDist;
122 nActualLabel = j;
123 }
124 }
125
126 if (m_param.accuracy_param.IgnoreLabel(nExpectedLabel))
127 continue;
128
129 if (nActualLabel == nExpectedLabel)
130 nCorrectCount++;
131
132 nTotal++;
133 }
134
135 double dfAccuracy = (double)nCorrectCount / (double)nTotal;
136
137 colTop[0].SetData(dfAccuracy, 0);
138 colTop[0].Tag = m_param.accuracy_param.top_k;
139 }
140
142 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
143 {
144 if (rgbPropagateDown[0])
145 throw new NotImplementedException();
146 }
147 }
148}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
void SetData(double df)
Set all blob data 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
float convertF(T df)
Converts a generic to a float value.
Definition: Layer.cs:1359
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
The AccuracyDecodeLayer compares the labels output by the DecodeLayer with the expected labels output...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward compuation.
override void dispose()
Releases all GPU and host resources used by the Layer.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented – AccuracyDecodeLayer cannot be used as a loss.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int ExactNumTopBlobs
Returns the number of top blobs: accuracy
AccuracyDecodeLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
Constructor.
override int ExactNumBottomBlobs
Returns the number of bottom blobs used: predicted, label
uint top_k
When computing accuracy, count as correct by comparing the true label to the top_k scoring classes....
int axis
The 'label' axis of the prediction blob, whos argmax corresponds to the predicted label – may be nega...
bool IgnoreLabel(int nLabel)
Returns 'true' if the label is to be ignored.
Specifies the base parameter for all layers.
AccuracyParameter accuracy_param
Returns the parameter set when initialized with LayerType.ACCURACY
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
BLOB_TYPE
Defines the tpe of data held by a given Blob.
Definition: Interfaces.cs:62
The MyCaffe.layers.beta namespace contains all beta stage layers.
Definition: LayerFactory.cs:9
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