Reorganized sources, better accuracy for similarities based on features

This commit is contained in:
emeric
2019-02-05 14:08:46 +01:00
parent 3e142b5507
commit 6fcb693261
34 changed files with 1324 additions and 1218 deletions
@@ -0,0 +1,141 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityFeaturesScannerAddon.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "som/AcousticBrainzUtils.hpp"
#include "utils/Logger.hpp"
namespace Similarity {
namespace {
struct TrackInfo
{
Database::IdType id;
std::string mbid;
};
std::vector<TrackInfo>
getTracksWithMBIDAndMissingFeatures(Wt::Dbo::Session& session)
{
std::vector<TrackInfo> res;
Wt::Dbo::Transaction transaction(session);
auto tracks = Database::Track::getAllWithMBIDAndMissingFeatures(session);
for (auto track : tracks)
res.push_back({track.id(), track->getMBID()});
return res;
}
} // namespace
FeaturesScannerAddon::FeaturesScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool)
: _db(connectionPool)
{
}
std::shared_ptr<Similarity::FeaturesSearcher>
FeaturesScannerAddon::getSearcher()
{
return std::atomic_load(&_searcher);
}
void
FeaturesScannerAddon::trackUpdated(Database::IdType trackId)
{
Wt::Dbo::Transaction transaction(_db.getSession());
auto track = Database::Track::getById(_db.getSession(), trackId);
if (!track)
return;
track.modify()->eraseFeatures();
}
void
FeaturesScannerAddon::preScanComplete()
{
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features...";
auto tracksInfo = getTracksWithMBIDAndMissingFeatures(_db.getSession());
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features DONE (found " << tracksInfo.size() << ")";
for (const auto& trackInfo : tracksInfo)
fetchFeatures(trackInfo.id, trackInfo.mbid);
updateSearcher();
}
void
FeaturesScannerAddon::updateSearcher()
{
Wt::Dbo::Transaction transaction(_db.getSession());
auto tracks = Database::Track::getAllWithFeatures(_db.getSession());
transaction.commit();
if (tracks.empty())
{
LMS_LOG(DBUPDATER, INFO) << "No track suitable for features similarity clustering";
std::atomic_store(&_searcher, std::shared_ptr<FeaturesSearcher>());
return;
}
auto searcher = std::make_shared<Similarity::FeaturesSearcher>(_db.getSession());
std::atomic_store(&_searcher, searcher);
LMS_LOG(DBUPDATER, INFO) << "New features similarity searcher instanciated";
}
bool
FeaturesScannerAddon::fetchFeatures(Database::IdType trackId, const std::string& MBID)
{
std::map<std::string, double> features;
LMS_LOG(DBUPDATER, DEBUG) << "Fetching low level features for track '" << MBID << "'";
std::string data = AcousticBrainz::extractLowLevelFeatures(MBID);
if (data.empty())
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot extract features using AcousticBrainz!";
return false;
}
// TODO check if the expected features are here
Wt::Dbo::Transaction transaction(_db.getSession());
Wt::Dbo::ptr<Database::Track> track = Database::Track::getById(_db.getSession(), trackId);
if (!track)
return false;
LMS_LOG(DBUPDATER, DEBUG) << "Successfully extracted AcousticBrainz lowlevel features for track '" << track->getPath().string() << "'";
Database::TrackFeatures::create(_db.getSession(), track, data);
return true;
}
} // namespace Similarity
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Wt/Dbo/SqlConnectionPool.h>
#include "database/DatabaseHandler.hpp"
#include "scanner/MediaScannerAddon.hpp"
#include "SimilarityFeaturesSearcher.hpp"
namespace Similarity {
class FeaturesScannerAddon final : public Scanner::MediaScannerAddon
{
public:
FeaturesScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool);
std::shared_ptr<FeaturesSearcher> getSearcher();
private:
void refreshSettings() override {}
void trackAdded(Database::IdType trackId) override {}
void trackToRemove(Database::IdType trackId) override {}
void trackUpdated(Database::IdType trackId) override;
void preScanComplete() override;
bool fetchFeatures(Database::IdType trackId, const std::string& MBID);
void updateSearcher();
Database::Handler _db;
std::shared_ptr<FeaturesSearcher> _searcher;
};
FeaturesScannerAddon* setFeaturesScannerAddon(FeaturesScannerAddon addon);
FeaturesScannerAddon* getFeaturesScannerAddon();
} // namespace Similarity
@@ -0,0 +1,305 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityFeaturesSearcher.hpp"
#include <random>
#include "database/Artist.hpp"
#include "database/SimilaritySettings.hpp"
#include "database/Release.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "som/DataNormalizer.hpp"
#include "utils/Logger.hpp"
#include "utils/Utils.hpp"
namespace Similarity {
FeaturesSearcher::FeaturesSearcher(Wt::Dbo::Session& session)
{
Wt::Dbo::Transaction transaction(session);
auto settings = Database::SimilaritySettings::get(session);
struct FeatureInfo
{
std::size_t nbDimensions;
double weight;
};
std::map<std::string, FeatureInfo> featuresInfo;
std::size_t nbDimensions = 0;
for (auto feature : settings->getFeatures())
{
featuresInfo[feature->getName()] = { feature->getNbDimensions(), feature->getWeight() };
nbDimensions += feature->getNbDimensions();
}
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features...";
auto tracks = Database::Track::getAllWithFeatures(session);
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features DONE";
std::vector<SOM::InputVector> samples;
std::vector<Database::IdType> tracksIds;
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features...";
for (auto track : tracks)
{
SOM::InputVector sample;
std::map<std::string, std::vector<double>> features;
for (const auto& featureInfo : featuresInfo)
features[featureInfo.first] = {};
if (!track->getTrackFeatures()->getFeatures(features))
continue;
// Check dimensions for each feature
bool ok = true;
for (const auto& feature : features)
{
auto it = featuresInfo.find(feature.first);
if (it == featuresInfo.end() || it->second.nbDimensions != feature.second.size())
{
LMS_LOG(SIMILARITY, WARNING) << "Dimension mismatch for feature '" << feature.first << "'. Expected " << it->second.nbDimensions << ", got " << feature.second.size();
ok = false;
break;
}
sample.insert( sample.end(), feature.second.begin(), feature.second.end() );
}
if (!ok)
continue;
samples.emplace_back(std::move(sample));
tracksIds.emplace_back(track.id());
}
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features DONE";
transaction.commit();
if (tracksIds.empty())
{
LMS_LOG(SIMILARITY, INFO) << "Nothing to classify!";
return;
}
LMS_LOG(SIMILARITY, DEBUG) << "Normalizing data...";
SOM::DataNormalizer normalizer(nbDimensions);
normalizer.computeNormalizationFactors(samples);
for (auto& sample : samples)
normalizer.normalizeData(sample);
std::size_t size = std::sqrt(samples.size()/2);
LMS_LOG(SIMILARITY, INFO) << "Found " << samples.size() << " tracks, constructing a " << size << "*" << size << " network";
_network = std::make_unique<SOM::Network>(size, size, nbDimensions);
_artistsMap = SOM::Matrix<std::set<Database::IdType>>(size, size);
_releasesMap = SOM::Matrix<std::set<Database::IdType>>(size, size);
_tracksMap = SOM::Matrix<std::set<Database::IdType>>(size, size);
std::vector<double> weights;
for (const auto& featureInfo : featuresInfo)
{
for (std::size_t i = 0; i < featureInfo.second.nbDimensions; ++i)
weights.push_back(1. / featureInfo.second.nbDimensions * featureInfo.second.weight);
}
LMS_LOG(SIMILARITY, DEBUG) << "Training network...";
_network->train(samples, 20);
LMS_LOG(SIMILARITY, DEBUG) << "Training network DONE";
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks...";
for (std::size_t i = 0; i < samples.size(); ++i)
{
Wt::Dbo::Transaction transaction(session);
const auto& sample = samples[i];
auto trackId = tracksIds[i];
auto coords = _network->getClosestRefVectorCoords(sample);
_trackCoords[trackId].insert(coords);
_tracksMap[coords].insert(trackId);
auto track = Database::Track::getById(session, trackId);
if (track->getRelease())
{
_releaseCoords[track->getRelease().id()].insert(coords);
_releasesMap[coords].insert(track->getRelease().id());
}
if (track->getArtist())
{
_artistCoords[track->getArtist().id()].insert(coords);
_artistsMap[coords].insert(track->getArtist().id());
}
}
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks DONE";
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarTracks(const std::set<Database::IdType>& tracksIds, std::size_t maxCount) const
{
return getSimilarObjects(tracksIds, _tracksMap, _trackCoords, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const
{
return getSimilarObjects({releaseId}, _releasesMap, _releaseCoords, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const
{
return getSimilarObjects({artistId}, _artistsMap, _artistCoords, maxCount);
}
#if 0
void
FeaturesSearcher::dump(Wt::Dbo::Session& session, std::ostream& os) const
{
os << "Number of tracks classified: " << _trackIdsCoords.size() << std::endl;
os << "Network size: " << _network.getWidth() << " * " << _network.getHeight() << std::endl;
Wt::Dbo::Transaction transaction(session);
for (std::size_t y = 0; y < _network.getHeight(); ++y)
{
for (std::size_t x = 0; x < _network.getWidth(); ++x)
{
const auto& trackIds = _tracksMap[{x, y}];
for (auto trackId : trackIds)
{
auto track = Database::Track::getById(session, trackId);
if (!track)
continue;
os << "{";
if (track->getArtist())
os << track->getArtist()->getName() << " ";
if (track->getRelease())
os << track->getRelease()->getName();
os << "} ";
}
os << "; ";
}
os << std::endl;
}
}
#endif
static
std::set<SOM::Coords>
getMatchingRefVectorsCoords(const std::set<Database::IdType>& ids, const std::map<Database::IdType, std::set<SOM::Coords>>& objectCoords)
{
std::set<SOM::Coords> res;
if (ids.empty())
return res;
for (auto id : ids)
{
auto it = objectCoords.find(id);
if (it == objectCoords.end())
continue;
for (const auto& coords : it->second)
res.insert(coords);
}
return res;
}
static
std::set<Database::IdType>
getObjectsIds(const std::set<SOM::Coords>& coordsSet, const SOM::Matrix<std::set<Database::IdType>>& objectsMap )
{
std::set<Database::IdType> res;
for (const auto& coords : coordsSet)
{
for (auto id : objectsMap.get(coords))
res.insert(id);
}
return res;
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const std::map<Database::IdType, std::set<SOM::Coords>>& objectCoords,
std::size_t maxCount) const
{
std::vector<Database::IdType> res;
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
std::set<SOM::Coords> searchedRefVectorsCoords = getMatchingRefVectorsCoords(ids, objectCoords);
if (searchedRefVectorsCoords.empty())
return res;
while (1)
{
std::set<Database::IdType> closestObjectIds = getObjectsIds(searchedRefVectorsCoords, objectsMap);
// Remove objects that are already in input
for (auto id : ids)
closestObjectIds.erase(id);
{
std::vector<Database::IdType> objectIdsToAdd(closestObjectIds.begin(), closestObjectIds.end());
std::shuffle(objectIdsToAdd.begin(), objectIdsToAdd.end(), randGenerator);
std::copy(objectIdsToAdd.begin(), objectIdsToAdd.end(), std::back_inserter(res));
}
if (res.size() > maxCount)
res.resize(maxCount);
if (res.size() == maxCount)
break;
// If there is not enough objects, try again with closest neighbour until there is too much distance
auto closestRefVectorCoords = _network->getClosestRefVectorCoords(searchedRefVectorsCoords, _networkRefVectorsDistanceMedian * 0.75);
if (!closestRefVectorCoords)
break;
searchedRefVectorsCoords.insert(*closestRefVectorCoords);
}
return res;
}
} // ns Similarity
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <set>
#include "database/DatabaseHandler.hpp"
#include "database/Types.hpp"
#include "som/Network.hpp"
namespace Similarity {
class FeaturesSearcher
{
public:
FeaturesSearcher(Wt::Dbo::Session& session);
std::vector<Database::IdType> getSimilarTracks(const std::set<Database::IdType>& tracksId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const;
void dump(Wt::Dbo::Session& session, std::ostream& os) const;
private:
std::vector<Database::IdType> getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const std::map<Database::IdType, std::set<SOM::Coords>>& objectCoords,
std::size_t maxCount) const;
std::unique_ptr<SOM::Network> _network;
double _networkRefVectorsDistanceMedian = 0;
SOM::Matrix<std::set<Database::IdType>> _artistsMap;
std::map<Database::IdType, std::set<SOM::Coords>> _artistCoords;
SOM::Matrix<std::set<Database::IdType>> _releasesMap;
std::map<Database::IdType, std::set<SOM::Coords>> _releaseCoords;
SOM::Matrix<std::set<Database::IdType>> _tracksMap;
std::map<Database::IdType, std::set<SOM::Coords>> _trackCoords;
};
} // ns Similarity
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AcousticBrainzUtils.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <curl/curl.h>
#include "utils/Config.hpp"
#include "utils/Logger.hpp"
namespace AcousticBrainz
{
static size_t writeToOStringStream(void *buffer, size_t size, size_t nmemb, void* ctx)
{
std::ostringstream& oss = *reinterpret_cast<std::ostringstream*>(ctx);
oss.write(reinterpret_cast<char*>(buffer), size * nmemb);
return size * nmemb;
}
static std::string
getJsonData(const std::string& mbid)
{
static const std::string defaultAPIURL = "https://acousticbrainz.org/api/v1/";
std::string data;
std::string url = Config::instance().getString("acousticbrainz-api-url", defaultAPIURL) + mbid + "/low-level";
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (!curl)
{
LMS_LOG(SIMILARITY, ERROR) << "CURL init failed";
return data;
}
std::ostringstream oss;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToOStringStream);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
LMS_LOG(SIMILARITY, ERROR) << "CURL perform failed: " << curl_easy_strerror(res);
return data;
}
curl_easy_cleanup(curl);
data = std::move(oss.str());
return data;
}
std::string
extractLowLevelFeatures(const std::string& mbid)
{
return getJsonData(mbid);
}
} // namespace Scanner::AcousticBrainz
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <set>
#include <string>
namespace AcousticBrainz
{
std::string extractLowLevelFeatures(const std::string& MBID);
}
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DataNormalizer.hpp"
#include <algorithm>
#include <numeric>
#include <sstream>
namespace SOM
{
template<typename T>
static
T
variance(const std::vector<T>& vec)
{
std::size_t size = vec.size();
if (size == 1)
return T{0.};
T mean = std::accumulate(vec.begin(), vec.end(), T{0.}) / size;
return std::accumulate(vec.begin(), vec.end(), T{0.},
[mean, size] (T accumulator, const T& val)
{
return accumulator + ((val - mean) * (val - mean) / (size - 1));
});
}
DataNormalizer::DataNormalizer(std::size_t inputDimCount)
: _inputDimCount(inputDimCount)
{
}
void
DataNormalizer::computeNormalizationFactors(const std::vector<InputVector>& inputVectors)
{
if (inputVectors.empty())
throw SOMException("Empty input vectors");
// For each dimension of the input, compute the min/max
_minmax.clear();
_minmax.resize(_inputDimCount);
for (std::size_t dimId = 0; dimId < _inputDimCount; ++dimId)
{
std::vector<InputVector::value_type> values;
for (const auto& inputVector: inputVectors)
{
checkSameDimensions(inputVector, _inputDimCount);
values.push_back(inputVector[dimId]);
}
auto result = std::minmax_element(values.begin(), values.end());
_minmax[dimId] = {*result.first, *result.second};
}
}
InputVector::value_type
DataNormalizer::normalizeValue(InputVector::value_type value, std::size_t dimId) const
{
// clamp
if (value > _minmax[dimId].max)
value = _minmax[dimId].max;
else if (value < _minmax[dimId].min)
value = _minmax[dimId].min;
return (value - _minmax[dimId].min) / (_minmax[dimId].max - _minmax[dimId].min);
}
void
DataNormalizer::normalizeData(InputVector& a) const
{
checkSameDimensions(a, _inputDimCount);
for (std::size_t dimId = 0; dimId < _inputDimCount; ++dimId)
{
a[dimId] = normalizeValue(a[dimId], dimId);
}
}
void
DataNormalizer::dump(std::ostream& os) const
{
for (std::size_t i = 0; i < _inputDimCount; ++i)
os << "(" << _minmax[i].min << ", " << _minmax[i].max << ")";
}
} // namespace SOM
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <ostream>
#include "Network.hpp"
namespace SOM
{
class DataNormalizer
{
public:
DataNormalizer(std::size_t inputDimCount);
void computeNormalizationFactors(const std::vector<InputVector>& dataSamples);
void normalizeData(InputVector& data) const;
std::string serializeTo() const;
void dump(std::ostream& os) const;
private:
void serializeFrom(const std::string& data);
InputVector::value_type normalizeValue(InputVector::value_type value, std::size_t dimensionId) const;
std::size_t _inputDimCount;
struct minmax
{
InputVector::value_type min;
InputVector::value_type max;
};
std::vector<minmax> _minmax; // Indexed min/max used to normalize data
};
} // namespace SOM
+115
View File
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <algorithm>
#include <cassert>
#include <sstream>
#include <vector>
namespace SOM
{
struct Coords
{
std::size_t x;
std::size_t y;
bool operator<(const Coords& other) const
{
if (x == other.x)
return y < other.y;
else
return x < other.x;
}
bool operator==(const Coords& other) const
{
return x == other.x && y == other.y;
}
};
template <typename T>
class Matrix
{
public:
Matrix() = default;
Matrix(std::size_t width, std::size_t height)
: _width(width),
_height(height)
{
_values.resize(_width*_height);
}
Matrix(std::size_t width, std::size_t height, std::vector<T> values)
: _width(width),
_height(height),
_values(std::move(values))
{
assert(_values.size() == _width * _height);
}
void clear()
{
std::vector<T> values(_width*_height);
_values.swap(values);
}
std::size_t getHeight() const { return _height; }
std::size_t getWidth() const { return _width; }
T& get(Coords coords)
{
assert(coords.x < _width);
assert(coords.y < _height);
return _values[coords.x + _width*coords.y];
}
const T& get(Coords coords) const
{
assert(coords.x < _width);
assert(coords.y < _height);
return _values[coords.x + _width*coords.y];
}
T& operator[](Coords coords) { return get(coords); }
const T& operator[](Coords coords) const { return get(coords); }
template <typename Func>
Coords getCoordsMinElement(Func func) const
{
assert(!_values.empty());
auto it = std::min_element(_values.begin(), _values.end(), func);
auto index = std::distance(_values.begin(), it);
return {index % _height, index / _height};
}
private:
std::size_t _width = 0;
std::size_t _height = 0;
std::vector<T> _values;
};
} // ns SOM
+401
View File
@@ -0,0 +1,401 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Network.hpp"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <random>
#include <sstream>
#include "utils/Logger.hpp"
namespace SOM
{
void
checkSameDimensions(const InputVector& a, const InputVector& b)
{
if (a.size() != b.size())
throw SOMException("Bad data dimension count");
}
void
checkSameDimensions(const InputVector& a, std::size_t inputDimCount)
{
if (a.size() != inputDimCount)
throw SOMException("Bad data dimension count");
}
static InputVector::value_type
defaultLearningFactor(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type initialValue = 1;
return initialValue * exp(-((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static InputVector::value_type
euclidianSquareDistance(const InputVector& a, const InputVector& b, const InputVector& weights)
{
checkSameDimensions(a, b);
checkSameDimensions(a, weights);
InputVector::value_type res = 0;
for (std::size_t i = 0; i < a.size(); ++i)
{
res += (a[i] - b[i]) * (a[i] - b[i]) * weights[i];
}
return res;
}
static
InputVector::value_type
sigmaFunc(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type sigma0 = 1;
return sigma0 * exp(- ((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static
InputVector::value_type
defaultNeighbourhoodFunc(InputVector::value_type norm, Network::CurrentIteration iteration)
{
auto sigma = sigmaFunc(iteration);
return exp(-norm / (2 * sigma * sigma));
}
std::ostream&
operator<<(std::ostream& os, const InputVector& a)
{
os << "[";
for (const auto& val : a)
{
os << val << " ";
}
os << "]";
return os;
}
static
InputVector::value_type
norm(const InputVector& a)
{
InputVector::value_type res = 0;
for (const auto& val : a)
{
res += val * val;
}
return sqrt(res);
}
static
InputVector
operator+(const InputVector& a, const InputVector& b)
{
checkSameDimensions(a, b);
InputVector res(a.size(), 0);
for (std::size_t dimId = 0; dimId < a.size(); ++dimId)
{
res[dimId] = a[dimId] + b[dimId];
}
return res;
}
static
InputVector
operator-(const InputVector& a, const InputVector& b)
{
checkSameDimensions(a, b);
InputVector res(a.size(), 0);
for (std::size_t dimId = 0; dimId < a.size(); ++dimId)
{
res[dimId] = a[dimId] - b[dimId];
}
return res;
}
static
InputVector
operator*(const InputVector& a, InputVector::value_type factor)
{
InputVector res(a.size(), 0);
for (std::size_t dimId = 0; dimId < a.size(); ++dimId)
{
res[dimId] = a[dimId] * factor;
}
return res;
}
Network::Network(std::size_t width, std::size_t height, std::size_t inputDimCount)
:
_inputDimCount(inputDimCount),
_weights(inputDimCount, static_cast<InputVector::value_type>(1)),
_refVectors(width, height),
_distanceFunc(euclidianSquareDistance),
_learningFactorFunc(defaultLearningFactor),
_neighbourhoodFunc(defaultNeighbourhoodFunc)
{
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
// init each vector with a random normalized value
std::uniform_real_distribution<InputVector::value_type> dist(0, 1);
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
auto& refVector = _refVectors.get({x,y});
refVector.resize(_inputDimCount);
for (auto& val : refVector)
val = dist(randGenerator);
}
}
}
void
Network::setDataWeights(const InputVector& weights)
{
checkSameDimensions(weights, _inputDimCount);
_weights = weights;
}
double
Network::getRefVectorsDistance(Coords coords1, Coords coords2) const
{
return _distanceFunc(_refVectors.get(coords1), _refVectors.get(coords2), _weights);
}
double
Network::computeRefVectorsDistanceMean() const
{
std::vector<double> values;
values.reserve(2*_refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.push_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.push_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
return std::accumulate(values.begin(), values.end(), 0.) / values.size();
}
double
Network::computeRefVectorsDistanceMedian() const
{
std::vector<double> values;
values.reserve(2*_refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.push_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.push_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
return values[values.size()/2 - 1];
}
void
Network::dump(std::ostream& os) const
{
os << "Width: " << _refVectors.getWidth() << ", Height: " << _refVectors.getHeight() << std::endl;;
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
os << _refVectors.get({x, y}) << " ";
}
os << std::endl;
}
os << std::endl;
}
Coords
Network::getClosestRefVectorCoords(const InputVector& data) const
{
return _refVectors.getCoordsMinElement([&](const auto& a, const auto& b)
{
return (_distanceFunc(a, data, _weights) < _distanceFunc(b, data, _weights));
});
}
boost::optional<Coords>
Network::getClosestRefVectorCoords(const InputVector& data, double maxDistance) const
{
Coords coords = _refVectors.getCoordsMinElement([&](const auto& a, const auto& b)
{
return (_distanceFunc(a, data, _weights) < _distanceFunc(b, data, _weights));
});
if (_distanceFunc(data, _refVectors.get(coords), _weights) > maxDistance)
return boost::none;
return coords;
}
boost::optional<Coords>
Network::getClosestRefVectorCoords(const std::set<Coords>& refVectorsCoords, double maxDistance) const
{
std::set<Coords> neighboursCoords;
for (const Coords& refVectorCoords : refVectorsCoords)
{
if (refVectorCoords.y > 0)
neighboursCoords.insert({ refVectorCoords.x, refVectorCoords.y - 1 });
if (refVectorCoords.y < _refVectors.getHeight() - 1)
neighboursCoords.insert({ refVectorCoords.x, refVectorCoords.y + 1 });
if (refVectorCoords.x > 0)
neighboursCoords.insert({ refVectorCoords.x - 1, refVectorCoords.y });
if (refVectorCoords.x < _refVectors.getWidth() - 1)
neighboursCoords.insert({ refVectorCoords.x + 1, refVectorCoords.y });
}
// remove coords that are in the input coords
for (const auto& refVectorCoords : refVectorsCoords)
neighboursCoords.erase(refVectorCoords);
if (neighboursCoords.empty())
return boost::none;
// Now compute the distance for each neighbour
struct NeighbourInfo
{
Coords coords;
double distance;
};
std::vector<NeighbourInfo> neighboursInfo;
for (const Coords& neighbourCoords : neighboursCoords)
{
auto min = std::min_element(refVectorsCoords.begin(), refVectorsCoords.end(),
[this, neighbourCoords](const auto& a, const auto& b)
{
return (this->getRefVectorsDistance(a, neighbourCoords) < this->getRefVectorsDistance(b, neighbourCoords));
});
double distance = getRefVectorsDistance(neighbourCoords, *min);
if (distance > maxDistance)
continue;
neighboursInfo.push_back({neighbourCoords, distance});
}
if (neighboursInfo.empty())
return boost::none;
auto min = std::min_element(neighboursInfo.begin(), neighboursInfo.end(),
[&](const auto& a, const auto& b)
{
return a.distance < b.distance;
});
return min->coords;
}
static InputVector::value_type
computeCoordsNorm(Coords c1, Coords c2)
{
std::vector<InputVector::value_type> a = { static_cast<InputVector::value_type>(c1.x), static_cast<InputVector::value_type>(c1.y) };
std::vector<InputVector::value_type> b = { static_cast<InputVector::value_type>(c2.x), static_cast<InputVector::value_type>(c2.y) };
return norm(a - b);
}
void
Network::updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, CurrentIteration iteration)
{
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
auto& refVector = _refVectors.get({x, y});
auto delta = input - refVector;
auto n = computeCoordsNorm({x, y}, closestRefVectorCoords);
auto oldRefVector = refVector;
refVector = refVector + delta * (_learningFactorFunc(iteration) * _neighbourhoodFunc(n, iteration));
}
}
}
void
Network::train(const std::vector<InputVector>& inputData, std::size_t nbIterations)
{
std::vector<const InputVector*> inputDataShuffled;
inputDataShuffled.reserve(inputData.size());
for (const auto& input : inputData)
{
inputDataShuffled.push_back(&input);
}
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
for (std::size_t i = 0; i < nbIterations; ++i)
{
std::shuffle(inputDataShuffled.begin(), inputDataShuffled.end(), randGenerator);
for (auto input : inputDataShuffled)
{
Coords closestRefVectorCoords = getClosestRefVectorCoords(*input);
updateRefVectors(closestRefVectorCoords, *input, {i, nbIterations});
}
}
}
} // namespace SOM
+111
View File
@@ -0,0 +1,111 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <set>
#include <ostream>
#include <functional>
#include <boost/optional.hpp>
#include "Matrix.hpp"
#include "utils/Exception.hpp"
namespace SOM
{
using InputVector = std::vector<double>;
void checkSameDimensions(const InputVector& a, const InputVector& b);
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
std::ostream& operator<<(std::ostream& os, const InputVector& a);
class SOMException : public LmsException
{
public:
SOMException(const std::string& msg) : LmsException(msg) {}
};
class Network
{
public:
// Init a network with random values
Network(std::size_t width, std::size_t height, std::size_t inputDimCount);
// Init a network with serialized values
Network(const std::string& data);
std::size_t getWidth() const { return _refVectors.getWidth(); }
std::size_t getHeight() const { return _refVectors.getHeight(); }
std::size_t getInputDimCount() const {return _inputDimCount;}
// Set weight for each dimension (default is 1 for each weight)
void setDataWeights(const InputVector& weights);
// <!> data must be normalized
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations);
Coords getClosestRefVectorCoords(const InputVector& data) const;
boost::optional<Coords> getClosestRefVectorCoords(const InputVector& data, double maxDistance) const;
boost::optional<Coords> getClosestRefVectorCoords(const std::set<Coords>& refVectorsCoords, double maxDistance) const;
double getRefVectorsDistance(Coords coords1, Coords coords2) const;
double computeRefVectorsDistanceMean() const;
double computeRefVectorsDistanceMedian() const;
void dump(std::ostream& os) const;
// For each ref vector, update formula is:
// i is the current iteration
// refVector(i+1) = refVector(i) + LearningFactor(i) * NeighbourhoodFunc(i) * (MatchingRefVector - refVector)
using DistanceFunc = std::function<InputVector::value_type(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
void setDistanceFunc(DistanceFunc distanceFunc);
struct CurrentIteration
{
std::size_t idIteration;
std::size_t iterationCount;
};
using LearningFactorFunc = std::function<InputVector::value_type(CurrentIteration)>;
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
using NeighbourhoodFunc = std::function<InputVector::value_type(InputVector::value_type /* norm(Coords - CoordMatchingRefVector) */, CurrentIteration)>;
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
private:
void updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, CurrentIteration iteration);
std::size_t _inputDimCount;
InputVector _weights; // weight for each dimension
Matrix<InputVector> _refVectors;
DistanceFunc _distanceFunc;
LearningFactorFunc _learningFactorFunc;
NeighbourhoodFunc _neighbourhoodFunc;
};
} // namespace SOM