/usr/include/shark/Data/LabelOrder.h is in libshark-dev 3.0.1+ds1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | //===========================================================================
/*!
*
*
* \brief This will relabel a given dataset to have labels 0..N-1 (and vice versa)
*
*
*
* \author Aydin Demircioglu
* \date 2014
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
//===========================================================================
#ifndef SHARK_LABELORDER_H
#define SHARK_LABELORDER_H
#include <shark/Core/INameable.h>
#include <shark/Core/ISerializable.h>
#include <shark/Data/Dataset.h>
namespace shark
{
/// \brief This will normalize the labels of a given dataset to 0..N-1
///
/// \par This will normalize the labels of a given dataset to 0..N-1
/// and store the ordering in a member variable.
/// After processing, the dataset will afterwards have labels ranging
/// from 0 to N-1, with N the number of classes, so usual Shark
/// trainers can work with it.
/// One can then revert the original labeling just by calling restoreOriginalLabels
class LabelOrder : public INameable
{
private:
public:
LabelOrder() {};
virtual ~LabelOrder() {};
/// \brief From INameable: return the class name.
std::string name() const
{ return "LabelOrder"; }
/// \brief This will normalize the labels and store the ordering in the
/// member variables. The dataset will afterwards have labels ranging
/// from 0 to N-1, with N the number of classes.
/// This will overwrite any previously stored label ordering in the object.
///
/// \param[in,out] dataset dataset that will be relabeled
void normalizeLabels(LabeledData<RealVector, unsigned int> &dataset)
{
// determine the min and max labels of the given dataset
int minLabel = std::numeric_limits<int>::max();
int maxLabel = -1;
for(std::size_t i = 0; i < dataset.numberOfElements(); ++i)
{
int label = dataset.labels().element(i);
// we react allergic to negative labels
if(label < 0)
throw SHARKEXCEPTION("Negative label found. Will not process negative labels!");
if(label < minLabel)
minLabel = label;
if(label > maxLabel)
maxLabel = label;
}
// now we create an vector that can hold the label ordering
m_labelOrder.clear();
// and one array that tracks what we already encountered
std::vector<unsigned int> foundLabels(maxLabel - minLabel + 1, -1);
// and insert all labels we encounter
unsigned int currentPosition = 0;
for(std::size_t i = 0; i < dataset.numberOfElements(); i++)
{
// is it a new label?
unsigned int label = dataset.labels().element(i);
if(foundLabels[label - minLabel] == -1)
{
foundLabels[label - minLabel] = currentPosition;
m_labelOrder.push_back(label);
currentPosition++;
}
}
// now map every label
for(std::size_t i = 0; i < dataset.numberOfElements(); i++)
{
int label = dataset.labels().element(i);
dataset.labels().element(i) = foundLabels[label - minLabel];
}
}
/// \brief This will restore the original labels of the dataset. This
/// must be called with data compatible the original dataset, so that the labels will
/// fit. The label ordering will not be destroyed after calling this function, so
/// it can be called multiple times, e.g. to testsets or similar data.
///
/// \param[in,out] dataset dataset to relabel (restore labels)
void restoreOriginalLabels(LabeledData<RealVector, unsigned int> &dataset)
{
// now map every label
for(std::size_t i = 0; i < dataset.numberOfElements(); ++i)
{
int label = dataset.labels().element(i);
// check if the reordering fit the data
if(label >= (int) m_labelOrder.size())
throw SHARKEXCEPTION("Dataset labels does not fit to the stored ordering!");
// relabel
label = m_labelOrder[label];
dataset.labels().element(i) = label;
}
}
/// \brief Get label ordering directly
///
/// \param[out] labelOrder vector to store the current label order.
void getLabelOrder(std::vector<int> &labelOrder)
{
labelOrder = m_labelOrder;
}
/// \brief Get label ordering directly
///
/// \param[out] labelOrder vector to store the current label order.
void getLabelOrder (std::vector<unsigned int> &labelOrder)
{
labelOrder = std::vector<unsigned int>( m_labelOrder.begin(), m_labelOrder.end() );
}
/// \brief Set label ordering directly
///
/// \param[in] labelOrder vector with the new label order
void setLabelOrder(std::vector<int> &labelOrder)
{
m_labelOrder = labelOrder;
}
/// \brief Set label ordering directly
///
/// \param[in] labelOrder vector with the new label order
void setLabelOrder (std::vector<unsigned int> &labelOrder)
{
m_labelOrder = std::vector<int>( labelOrder.begin(), labelOrder.end() );
}
protected:
std::vector<int> m_labelOrder;
};
}
#endif
|