2using System.Collections.Generic;
23 string m_strSource =
null;
26 Dictionary<int, int> m_rgActualMappedLabelCounts =
new Dictionary<int, int>();
27 object m_syncActualMappedLabels =
new object();
28 int m_nLabelCount = 0;
49 throw new Exception(
"Currently only image databases are supported by the LabelMappingLayer.");
61 base.SetNetParameterUsed(np);
76 if (m_strSource !=
null)
77 m_nSourceId = m_db.GetSourceID(m_strSource);
87 if (m_nLabelCount == 0)
88 m_nLabelCount = m_db.GetLabels(m_nSourceId).Count;
90 List<KeyValuePair<int, int>> rgKv;
92 lock (m_syncActualMappedLabels)
94 rgKv = m_rgActualMappedLabelCounts.OrderBy(p => p.Key).ToList();
100 for (
int i = 0; i < m_nLabelCount; i++)
102 if (nIdx < rgKv.Count)
104 str += rgKv[nIdx].Key.ToString() +
"->" + rgKv[nIdx].Value.ToString();
115 return str.TrimEnd(
',',
' ');
160 base.LayerSetUp(colBottom, colTop);
162 if (m_strSource !=
null && m_nProjectID > 0)
164 int nSrcId = m_nSourceId;
168 Dictionary<int, int> rgMapCounts =
new Dictionary<int, int>();
169 Dictionary<int, List<int>> rgMaps =
new Dictionary<int, List<int>>();
170 bool bHasConditions =
false;
174 if (!rgMapCounts.Keys.Contains(map.
NewLabel))
179 if (!rgMaps.Keys.Contains(map.
NewLabel))
180 rgMaps.Add(map.
NewLabel,
new List<int>());
185 bHasConditions =
true;
190 string strLabelCounts = m_db.GetLabelCountsAsTextFromSourceId(nSrcId);
194 m_log.
WriteLine(
"Resetting global relabeling to original labels for source '" + m_strSource +
"'...");
195 m_db.ResetLabels(m_nProjectID, nSrcId);
198 if (rgMaps.Count > 0)
199 m_log.
WriteLine(
"Starting global relabeling for source '" + m_strSource +
"'...");
205 m_db.SetLabelMapping(nSrcId, map);
210 foreach (KeyValuePair<
int, List<int>> kv
in rgMaps)
212 m_db.UpdateLabelMapping(nSrcId, kv.Key, kv.Value);
216 m_db.UpdateLabelCounts(m_nProjectID, nSrcId);
218 if (rgMaps.Count > 0)
219 m_log.
WriteLine(
"Global relabeling completed for source '" + m_strSource +
"'...");
224 bool bReloadImageSet =
false;
228#warning ImageDatabase version 1 Only
232 Dictionary<int, int> rgBoostedLabelCounts =
new Dictionary<int, int>();
235 foreach (
string strLabel
in rgstrBoosts)
237 int nLabel =
int.Parse(strLabel);
239 if (rgCounts.ContainsKey(nLabel))
241 int nCount = rgCounts[nLabel];
242 rgBoostedLabelCounts.Add(nLabel, nCount);
247 rgBoostedLabelCounts.Add(nLabel, 0);
253 foreach (KeyValuePair<int, int> kv
in rgCounts)
257 if (rgBoostedLabelCounts.ContainsKey(kv.Key))
259 int nCount = rgBoostedLabelCounts.Count;
260 dfBoost = (nCount == 0) ? 0 : (1.0 / (
double)nCount);
267 if (strNewLabelBoosts != strLabelBoosts)
268 bReloadImageSet =
true;
275 if (strNewLabelCounts != strLabelCounts || bReloadImageSet)
279 m_log.
WriteLine(
"WARNING: Label boosts are depreciated and soon to be removed.");
283 m_log.
WriteLine(
"WARNING: ImageDatabase Version 2 currently does not support label mapping.");
301 int nCount = colBottom[0].count();
303 m_log.
CHECK_EQ(nCount, colBottom[0].num,
"The count should equal the number of items for the label blobs.");
304 m_log.
CHECK_EQ(nCount, colTop[0].count(),
"The top and bottom should have the same number of items.");
306 double[] rgBottom =
convertD(colBottom[0].update_cpu_data());
308 for (
int i = 0; i < rgBottom.Length; i++)
311 rgBottom[i] = nLabel;
313 lock (m_syncActualMappedLabels)
315 if (!m_rgActualMappedLabelCounts.ContainsKey(nLabel))
316 m_rgActualMappedLabelCounts.Add(nLabel, 1);
318 m_rgActualMappedLabelCounts[nLabel]++;
322 colTop[0].mutable_cpu_data =
convert(rgBottom);
The LabelMapping class represents a single label mapping.
int? ConditionBoostEquals
Get/set the boost condition to test which if met, the new label is set, otherwise it is not.
int NewLabel
Get/set the new label.
int OriginalLabel
Get/set the original label.
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.
The BlobCollection contains a list of Blobs.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
/b DEPRECIATED (use DataLayer DataLabelMappingParameter instead) The LabelMappingLayer converts origi...
LabelMappingLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db)
The InnerProductLayer constructor.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Converts the input label to the new label specified by the label mapping.
string GetActualLabelCounts(string strSrc)
Returns a string describing the actual label counts observed during training.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
The LayerSetUp method adjusts the label boost values according to the number of mappings made to each...
override void SetNetParameterUsed(NetParameter np)
Set the parameters needed from the Net, namely the data source used.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - The LabelMappingLayer does not perform backward.
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.
double convertD(T df)
Converts a generic to a double value.
LayerParameter.LayerType m_type
Specifies the Layer type.
The NeuronLayer is an interface for layers that take one blob as input (x) and produce only equally-s...
string source
When used with the DATA parameter, specifies the data 'source' within the database....
string label_boosts
DEPRECIATED: Specifies the labels for which the label boost is to be set. When set,...
bool reset_database_labels
Specifies whether or not to reset the database labels to the original label values for the data sourc...
List< LabelMapping > mapping
Specifies the label mapping where the original label is mapped to the new label specified.
bool update_database
Specifies whether or not to directly update the database with the label mapping for the data source u...
int MapLabel(int nLabel)
Queries the mapped label for a given label.
Specifies the base parameter for all layers.
LayerType type
Specifies the type of this LayerParameter.
DataParameter data_param
Returns the parameter set when initialized with LayerType.DATA
LayerType
Specifies the layer type.
LabelMappingParameter labelmapping_param
Returns the parameter set when initialized with LayerType.LABELMAPPING
Specifies the parameters use to create a Net
int ProjectID
Specifies the ID of the project that created this net param (if any).
The IXDatabaseBase interface defines the general interface to the in-memory database.
DB_VERSION GetVersion()
Returns the version of the MyCaffe Image Database being used.
The IXImageDatabase interface defines the eneral interface to the in-memory image database.
string GetLabelBoostsAsTextFromProject(int nProjectId, int nSrcId)
Returns the label boosts as a text string for all boosted labels within a data source associated with...
void AddLabelBoost(int nProjectId, int nSrcId, int nLabel, double dfBoost)
Add a label boost for a data source associated with a given project.
void DeleteLabelBoosts(int nProjectId, int nSrcId)
Delete all label boosts for a given data source associated with a given project.
The IXImageDatabaseBase interface defines the general interface to the in-memory image database.
string GetLabelCountsAsTextFromSourceId(int nSrcId)
Returns a string with all label counts for a data source.
Dictionary< int, int > LoadLabelCounts(int nSrcId)
Returns a label lookup of counts for a given data source.
bool ReloadImageSet(int nSrcId)
Reloads the images of a data source.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
DB_VERSION
Defines the image database version to use.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.db.image namespace contains all image database related 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-...