MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ContrastiveLossParameter.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
7
8namespace MyCaffe.param
9{
24 [Serializable]
25 [TypeConverter(typeof(ExpandableObjectConverter))]
27 {
28 double m_dfMargin = 1.0;
29 bool m_bLegacyVersion = false;
30 bool m_bOutputMatches = false;
31 double m_dfMatchingDistScale = 1.0;
32 CENTROID_LEARNING m_centroidLearning = CENTROID_LEARNING.NONE;
33 DISTANCE_CALCULATION m_distanceCalc = DISTANCE_CALCULATION.EUCLIDEAN;
34
42 {
46 EUCLIDEAN,
50 MANHATTAN
51 }
52
57 {
61 NONE,
65 MATCHING,
69 NONMATCHING,
73 ALL
74 }
75
78 {
79 }
80
84 [Description("Specifies the margin for dissimilar pair.")]
85 public double margin
86 {
87 get { return m_dfMargin; }
88 set { m_dfMargin = value; }
89 }
90
101 [Description("Specifies to use the legacy version or not. When true the legacy version '(margin - d^2)' is used. Otherwise the default is to use the version '(margin - d)^2' proposed in the Hadsell paper.")]
102 //[ReadOnly(true)] // currently legacy version causes bug in auto test on backwards
103 public bool legacy_version
104 {
105 get { return m_bLegacyVersion; }
106 set { m_bLegacyVersion = value; }
107 }
108
112 [Description("Optionally, specifies to output match information (default = false).")]
113 public bool output_matches
114 {
115 get { return m_bOutputMatches; }
116 set { m_bOutputMatches = value; }
117 }
118
122 [Description("Optionally, specifies to use centroid learning as soon as the centroids (from the DecodeLayer) are ready - e.g. they have an asum > 0 (default = false, meaning no centroid learning occurs).")]
124 {
125 get { return m_centroidLearning; }
126 set { m_centroidLearning = value; }
127 }
128
132 [Description("Optionally, specifies the distance calculation to use when calculating the distance between encoding pairs (default = EUCLIDEAN).")]
134 {
135 get { return m_distanceCalc; }
136 set { m_distanceCalc = value; }
137 }
138
142 [Description("Optionally, specifies the scaling applied to distances between matching pairs (default = 1.0, which ignores the scaling).")]
144 {
145 get { return m_dfMatchingDistScale; }
146 set { m_dfMatchingDistScale = value; }
147 }
148
150 public override object Load(System.IO.BinaryReader br, bool bNewInstance = true)
151 {
152 RawProto proto = RawProto.Parse(br.ReadString());
154
155 if (!bNewInstance)
156 Copy(p);
157
158 return p;
159 }
160
162 public override void Copy(LayerParameterBase src)
163 {
165 m_dfMargin = p.m_dfMargin;
166 m_bLegacyVersion = p.m_bLegacyVersion;
167 m_bOutputMatches = p.m_bOutputMatches;
168 m_centroidLearning = p.m_centroidLearning;
169 m_distanceCalc = p.m_distanceCalc;
170 m_dfMatchingDistScale = p.m_dfMatchingDistScale;
171 }
172
174 public override LayerParameterBase Clone()
175 {
177 p.Copy(this);
178 return p;
179 }
180
186 public override RawProto ToProto(string strName)
187 {
188 RawProtoCollection rgChildren = new RawProtoCollection();
189
190 if (margin != 1.0)
191 rgChildren.Add("margin", margin.ToString());
192
193 if (legacy_version != false)
194 rgChildren.Add("legacy_version", legacy_version.ToString());
195
196 if (output_matches)
197 rgChildren.Add("output_matches", output_matches.ToString());
198
200 rgChildren.Add("centroid_learning", centroid_learning.ToString());
201
202 rgChildren.Add("distance_calculation", distance_calculation.ToString());
203 rgChildren.Add("matching_distance_scale", matching_distance_scale.ToString());
204
205 return new RawProto(strName, "", rgChildren);
206 }
207
214 {
215 string strVal;
217
218 if ((strVal = rp.FindValue("margin")) != null)
219 p.margin = ParseDouble(strVal);
220
221 if ((strVal = rp.FindValue("legacy_version")) != null)
222 p.legacy_version = bool.Parse(strVal);
223
224 if ((strVal = rp.FindValue("output_matches")) != null)
225 p.output_matches = bool.Parse(strVal);
226
227 if ((strVal = rp.FindValue("centroid_learning")) != null)
228 {
229 if (strVal == CENTROID_LEARNING.ALL.ToString())
231 else if (strVal == CENTROID_LEARNING.MATCHING.ToString())
233 else if (strVal == CENTROID_LEARNING.NONMATCHING.ToString())
234 p.centroid_learning = CENTROID_LEARNING.NONMATCHING;
235 }
236
237 if ((strVal = rp.FindValue("distance_calculation")) != null)
238 {
239 if (strVal == DISTANCE_CALCULATION.MANHATTAN.ToString())
241 }
242
243 if ((strVal = rp.FindValue("matching_distance_scale")) != null)
245
246 return p;
247 }
248 }
249}
static double ParseDouble(string strVal)
Parse double values using the US culture if the decimal separator = '.', then using the native cultur...
The RawProtoCollection class is a list of RawProto objects.
void Add(RawProto p)
Adds a RawProto to the collection.
The RawProto class is used to parse and output Google prototxt file data.
Definition: RawProto.cs:17
static RawProto Parse(string str)
Parses a prototxt and places it in a new RawProto.
Definition: RawProto.cs:306
string FindValue(string strName)
Searches for a falue of a node within this nodes children.
Definition: RawProto.cs:105
Specifies the parameters for the ContrastiveLossLayer.
bool output_matches
Optionally, specifies to output match information (default = false).
bool legacy_version
The first implementation of this cost did not exactly match the cost of Hadsell et al 2006 – using (m...
double matching_distance_scale
Optionally, specifies the scale applied to the matching distance when calculating the loss (default =...
ContrastiveLossParameter()
Constructor for the parameter.
static ContrastiveLossParameter FromProto(RawProto rp)
Parses the parameter from a RawProto.
CENTROID_LEARNING centroid_learning
Optionally, specifies to use centroid learning as soon as the centroids (from the DecodeLayer) are re...
double margin
Margin for dissimilar pair.
override void Copy(LayerParameterBase src)
Copy on parameter to another.
DISTANCE_CALCULATION distance_calculation
Optionally, specifies the distance calculation to use when calculating the distance between encoding ...
DISTANCE_CALCULATION
Defines the distance calculation to use.
CENTROID_LEARNING
Defines the type of centroid learning to use.
override LayerParameterBase Clone()
Creates a new copy of this instance of the parameter.
override RawProto ToProto(string strName)
Convert the parameter into a RawProto.
override object Load(System.IO.BinaryReader br, bool bNewInstance=true)
Load the parameter from a binary reader.
The LayerParameterBase is the base class for all other layer specific parameters.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
@ ALL
Applies to all phases.
@ NONE
No training category specified.
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