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
+41 -13
View File
@@ -19,40 +19,68 @@
#include "SimilaritySearcher.hpp"
#include "features/SimilarityFeaturesScannerAddon.hpp"
#include "cluster/SimilarityClusterSearcher.hpp"
#include "database/SimilaritySettings.hpp"
namespace Similarity {
Searcher::Searcher(SOMScannerAddon& somAddon)
Searcher::Searcher(FeaturesScannerAddon& somAddon)
: _somAddon(somAddon)
{}
std::vector<Database::IdType>
Searcher::getSimilarTracks(const std::vector<Database::IdType>& tracksId, std::size_t maxCount)
static
Database::SimilaritySettings::PreferredMethod getPreferredMethod(Wt::Dbo::Session& session)
{
auto somSearcher = _somAddon.getSearcher();
if (!somSearcher)
return {};
Wt::Dbo::Transaction transaction(session);
return Database::SimilaritySettings::get(session)->getPreferredMethod();
}
return somSearcher->getSimilarTracks(tracksId, maxCount);
std::vector<Database::IdType>
Searcher::getSimilarTracks(Wt::Dbo::Session& session, const std::set<Database::IdType>& trackIds, std::size_t maxCount)
{
auto method = getPreferredMethod(session);
auto somSearcher = _somAddon.getSearcher();
if (method == Database::SimilaritySettings::PreferredMethod::Features
|| (method == Database::SimilaritySettings::PreferredMethod::Auto && somSearcher))
{
return somSearcher->getSimilarTracks(trackIds, maxCount);
}
else
return ClusterSearcher::getSimilarTracks(session, trackIds, maxCount);
}
std::vector<Database::IdType>
Searcher::getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
{
auto method = getPreferredMethod(session);
auto somSearcher = _somAddon.getSearcher();
if (!somSearcher)
return {};
return somSearcher->getSimilarReleases(session, releaseId, maxCount);
if (method == Database::SimilaritySettings::PreferredMethod::Features
|| (method == Database::SimilaritySettings::PreferredMethod::Auto && somSearcher))
{
return somSearcher->getSimilarReleases(releaseId, maxCount);
}
else
return ClusterSearcher::getSimilarReleases(session, releaseId, maxCount);
}
std::vector<Database::IdType>
Searcher::getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
{
auto method = getPreferredMethod(session);
auto somSearcher = _somAddon.getSearcher();
if (!somSearcher)
return {};
return somSearcher->getSimilarArtists(session, artistId, maxCount);
if (method == Database::SimilaritySettings::PreferredMethod::Features
|| (method == Database::SimilaritySettings::PreferredMethod::Auto && somSearcher))
{
return somSearcher->getSimilarArtists(artistId, maxCount);
}
else
return ClusterSearcher::getSimilarArtists(session, artistId, maxCount);
}
} // ns Similarity
+7 -4
View File
@@ -19,25 +19,28 @@
#pragma once
#include <set>
#include <Wt/Dbo/Session.h>
#include "database/Types.hpp"
#include "som/SimilaritySOMScannerAddon.hpp"
namespace Similarity {
class FeaturesScannerAddon;
class Searcher
{
public:
Searcher(SOMScannerAddon& somAddon);
Searcher(FeaturesScannerAddon& somAddon);
std::vector<Database::IdType> getSimilarTracks(const std::vector<Database::IdType>& tracksId, std::size_t maxCount);
// Closest results first
std::vector<Database::IdType> getSimilarTracks(Wt::Dbo::Session& session, const std::set<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount);
private:
SOMScannerAddon& _somAddon;
FeaturesScannerAddon& _somAddon;
};
} // ns Similarity
@@ -22,19 +22,24 @@
#include <random>
#include <chrono>
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/Track.hpp"
#include "utils/Utils.hpp"
namespace Similarity {
namespace ClusterSearcher {
std::vector<Database::IdType>
ClusterSearcher::getSimilarTracks(Wt::Dbo::Session& session, const std::vector<Database::IdType>& tracksId, std::size_t maxCount)
getSimilarTracks(Wt::Dbo::Session& session, const std::set<Database::IdType>& trackIds, std::size_t maxCount)
{
std::vector<Database::IdType> res;
Wt::Dbo::Transaction transaction(session);
std::vector<Database::IdType> clusterIds;
for (auto trackId : tracksId)
for (auto trackId : trackIds)
{
auto track = Database::Track::getById(session, trackId);
if (!track)
@@ -51,26 +56,12 @@ ClusterSearcher::getSimilarTracks(Wt::Dbo::Session& session, const std::vector<D
std::vector<Database::IdType> sortedClusterIds;
uniqueAndSortedByOccurence(clusterIds.begin(), clusterIds.end(), std::back_inserter(sortedClusterIds));
#if 0
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
std::set<Database::IdType> trackIds;
for (auto clusterId : clusterIds)
{
auto ids = tracklist->getTrackIds();
trackIds = std::set<Database::IdType>(ids.begin(), ids.end());
}
auto cluster = Database::Cluster::getById(session, clusterId);
if (!cluster)
continue;
// Get all the tracks of the tracklist, get the cluster that is mostly used
// and reuse it to get the next track
auto clusters = tracklist->getClusters();
if (clusters.empty())
return;
for (auto cluster : clusters)
{
std::set<Database::IdType> clusterTrackIds = cluster->getTrackIds();
std::set<Database::IdType> candidateTrackIds;
@@ -81,30 +72,102 @@ ClusterSearcher::getSimilarTracks(Wt::Dbo::Session& session, const std::vector<D
if (candidateTrackIds.empty())
continue;
std::uniform_int_distribution<int> dist(0, candidateTrackIds.size() - 1);
for (auto trackId : candidateTrackIds)
{
if (res.size() >= maxCount)
break;
auto trackToAdd = Database::Track::getById(LmsApp->getDboSession(), *std::next(candidateTrackIds.begin(), dist(randGenerator)));
enqueueTrack(trackToAdd);
res.push_back(trackId);
}
return;
if (res.size() >= maxCount)
break;
}
LMS_LOG(UI, INFO) << "No more track to be added!";
#endif
return {};
return res;
}
std::vector<Database::IdType>
ClusterSearcher::getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
return {};
Wt::Dbo::Transaction transaction(session);
auto release = Database::Release::getById(session, releaseId);
if (!release)
return res;
auto releaseTracks = release->getTracks();
std::set<Database::IdType> releaseTrackIds;
for (const auto& releaseTrack : releaseTracks)
releaseTrackIds.insert(releaseTrack.id());
auto trackIds = getSimilarTracks(session, releaseTrackIds, maxCount * 5);
for (auto trackId : trackIds)
{
auto track = Database::Track::getById(session, trackId);
if (!track)
continue;
auto trackRelease = track->getRelease();
if (!trackRelease || trackRelease.id() == releaseId)
continue;
if (std::find(res.begin(), res.end(), trackRelease.id()) != res.end())
continue;
res.push_back(trackRelease.id());
if (res.size() == maxCount)
break;
}
return res;
}
std::vector<Database::IdType>
ClusterSearcher::getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
{
return {};
std::vector<Database::IdType> res;
Wt::Dbo::Transaction transaction(session);
auto artist = Database::Artist::getById(session, artistId);
if (!artist)
return res;
auto artistTracks = artist->getTracks();
std::set<Database::IdType> artistTrackIds;
for (const auto& artistTrack : artistTracks)
artistTrackIds.insert(artistTrack.id());
auto trackIds = getSimilarTracks(session, artistTrackIds, maxCount * 5);
for (auto trackId : trackIds)
{
auto track = Database::Track::getById(session, trackId);
if (!track)
continue;
auto trackArtist = track->getArtist();
if (!trackArtist || trackArtist.id() == artistId)
continue;
if (std::find(res.begin(), res.end(), trackArtist.id()) != res.end())
continue;
res.push_back(trackArtist.id());
if (res.size() == maxCount)
break;
}
return res;
}
} // namespace ClusterSearcher
} // namespace Similarity
@@ -19,18 +19,17 @@
#pragma once
#include <vector>
#include <set>
#include "database/Types.hpp"
namespace Similarity {
class ClusterSearcher
namespace ClusterSearcher
{
public:
std::vector<Database::IdType> getSimilarTracks(Wt::Dbo::Session& session, const std::vector<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarTracks(Wt::Dbo::Session& session, const std::set<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount);
};
} // namespace Similarity
@@ -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
@@ -24,20 +24,21 @@
#include "database/DatabaseHandler.hpp"
#include "scanner/MediaScannerAddon.hpp"
#include "SimilaritySOMSearcher.hpp"
#include "SimilarityFeaturesSearcher.hpp"
namespace Similarity {
class SOMScannerAddon : public Scanner::MediaScannerAddon
class FeaturesScannerAddon final : public Scanner::MediaScannerAddon
{
public:
SOMScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool);
FeaturesScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool);
std::shared_ptr<SOMSearcher> getSearcher();
std::shared_ptr<FeaturesSearcher> getSearcher();
private:
void refreshSettings() override;
void refreshSettings() override {}
void trackAdded(Database::IdType trackId) override {}
void trackToRemove(Database::IdType trackId) override {}
void trackUpdated(Database::IdType trackId) override;
@@ -45,17 +46,14 @@ class SOMScannerAddon : public Scanner::MediaScannerAddon
bool fetchFeatures(Database::IdType trackId, const std::string& MBID);
void clusterize();
void updateSearcher();
std::size_t _settingsVersion;
std::set<std::string> _featuresName;
Database::Handler _db;
std::shared_ptr<SOMSearcher> _finder;
Database::Handler _db;
std::shared_ptr<FeaturesSearcher> _searcher;
};
SOMScannerAddon* setSOMScannerAddon(SOMScannerAddon addon);
SOMScannerAddon* getSOMScannerAddon();
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
@@ -40,31 +40,6 @@ static size_t writeToOStringStream(void *buffer, size_t size, size_t nmemb, void
return size * nmemb;
}
static bool
getFeaturesFromJsonData(const std::string& jsonData, const std::set<std::string>& featuresName, std::map<std::string, double>& features)
{
try
{
boost::property_tree::ptree root;
std::istringstream iss(jsonData);
boost::property_tree::read_json(iss, root);
for (const auto& featureName : featuresName)
{
features[featureName] = root.get<double>(featureName);
}
return true;
}
catch (std::exception& e)
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot extract feature: " << e.what();
return false;
}
}
static std::string
getJsonData(const std::string& mbid)
{
@@ -79,7 +54,7 @@ getJsonData(const std::string& mbid)
curl = curl_easy_init();
if (!curl)
{
LMS_LOG(DBUPDATER, ERROR) << "CURL init failed";
LMS_LOG(SIMILARITY, ERROR) << "CURL init failed";
return data;
}
@@ -92,7 +67,7 @@ getJsonData(const std::string& mbid)
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
LMS_LOG(DBUPDATER, ERROR) << "CURL perform failed: " << curl_easy_strerror(res);
LMS_LOG(SIMILARITY, ERROR) << "CURL perform failed: " << curl_easy_strerror(res);
return data;
}
@@ -103,10 +78,10 @@ getJsonData(const std::string& mbid)
return data;
}
bool
extractFeatures(const std::string& mbid, const std::set<std::string>& featuresName, std::map<std::string, double>& features)
std::string
extractLowLevelFeatures(const std::string& mbid)
{
return getFeaturesFromJsonData(getJsonData(mbid), featuresName, features);
return getJsonData(mbid);
}
} // namespace Scanner::AcousticBrainz
@@ -25,7 +25,6 @@
namespace AcousticBrainz
{
bool extractFeatures(const std::string& MBID, const std::set<std::string>& featuresName, std::map<std::string, double>& features);
std::string extractLowLevelFeatures(const std::string& MBID);
}
@@ -50,11 +50,6 @@ DataNormalizer::DataNormalizer(std::size_t inputDimCount)
{
}
DataNormalizer::DataNormalizer(const std::string& data)
{
serializeFrom(data);
}
void
DataNormalizer::computeNormalizationFactors(const std::vector<InputVector>& inputVectors)
{
@@ -103,34 +98,6 @@ DataNormalizer::normalizeData(InputVector& a) const
}
}
std::string
DataNormalizer::serializeTo() const
{
std::ostringstream oss;
oss << _inputDimCount << " ";
for (std::size_t i = 0; i < _inputDimCount; ++i)
oss << _minmax[i].min << " " << _minmax[i].max;
return oss.str();
}
void
DataNormalizer::serializeFrom(const std::string& data)
{
std::istringstream iss(data);
iss >> _inputDimCount;
_minmax.resize(_inputDimCount);
for (std::size_t i = 0; i < _inputDimCount; ++i)
{
iss >> _minmax[i].min;
iss >> _minmax[i].max;
}
}
void
DataNormalizer::dump(std::ostream& os) const
{
@@ -32,7 +32,6 @@ class DataNormalizer
public:
DataNormalizer(std::size_t inputDimCount);
DataNormalizer(const std::string& data);
void computeNormalizationFactors(const std::vector<InputVector>& dataSamples);
@@ -34,7 +34,15 @@ struct Coords
bool operator<(const Coords& other) const
{
return x < other.x && y < other.y;
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;
}
};
@@ -43,6 +51,8 @@ class Matrix
{
public:
Matrix() = default;
Matrix(std::size_t width, std::size_t height)
: _width(width),
_height(height)
@@ -71,7 +81,6 @@ class Matrix
{
assert(coords.x < _width);
assert(coords.y < _height);
return _values[coords.x + _width*coords.y];
}
@@ -88,6 +97,8 @@ class Matrix
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);
@@ -96,8 +107,8 @@ class Matrix
private:
std::size_t _width;
std::size_t _height;
std::size_t _width = 0;
std::size_t _height = 0;
std::vector<T> _values;
};
@@ -45,11 +45,11 @@ checkSameDimensions(const InputVector& a, std::size_t inputDimCount)
}
static InputVector::value_type
defaultLearningFactor(Network::Progress progress)
defaultLearningFactor(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type initialValue = 1;
return initialValue * exp(-((progress.idIteration + 1) / static_cast<InputVector::value_type>(progress.iterationCount)));
return initialValue * exp(-((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static InputVector::value_type
@@ -70,18 +70,18 @@ euclidianSquareDistance(const InputVector& a, const InputVector& b, const InputV
static
InputVector::value_type
sigmaFunc(Network::Progress progress)
sigmaFunc(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type sigma0 = 1;
return sigma0 * exp(- ((progress.idIteration + 1) / static_cast<InputVector::value_type>(progress.iterationCount)));
return sigma0 * exp(- ((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static
InputVector::value_type
defaultNeighborhoodFunc(InputVector::value_type norm, Network::Progress progress)
defaultNeighbourhoodFunc(InputVector::value_type norm, Network::CurrentIteration iteration)
{
auto sigma = sigmaFunc(progress);
auto sigma = sigmaFunc(iteration);
return exp(-norm / (2 * sigma * sigma));
}
@@ -168,7 +168,7 @@ _weights(inputDimCount, static_cast<InputVector::value_type>(1)),
_refVectors(width, height),
_distanceFunc(euclidianSquareDistance),
_learningFactorFunc(defaultLearningFactor),
_neighborhoodFunc(defaultNeighborhoodFunc)
_neighbourhoodFunc(defaultNeighbourhoodFunc)
{
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
@@ -188,15 +188,6 @@ _neighborhoodFunc(defaultNeighborhoodFunc)
}
}
Network::Network(const std::string& data)
: _refVectors(0, 0),
_distanceFunc(euclidianSquareDistance),
_learningFactorFunc(defaultLearningFactor),
_neighborhoodFunc(defaultNeighborhoodFunc)
{
serializeFrom(data);
}
void
Network::setDataWeights(const InputVector& weights)
{
@@ -205,6 +196,50 @@ Network::setDataWeights(const InputVector& weights)
_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
{
@@ -223,7 +258,7 @@ Network::dump(std::ostream& os) const
}
Coords
Network::getClosestRefVector(const InputVector& data) const
Network::getClosestRefVectorCoords(const InputVector& data) const
{
return _refVectors.getCoordsMinElement([&](const auto& a, const auto& b)
{
@@ -231,48 +266,77 @@ Network::getClosestRefVector(const InputVector& data) const
});
}
Coords
Network::classify(const InputVector& data) const
boost::optional<Coords>
Network::getClosestRefVectorCoords(const InputVector& data, double maxDistance) const
{
return getClosestRefVector(data);
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;
}
std::vector<Coords>
Network::classify(const InputVector& data, std::size_t size) const
boost::optional<Coords>
Network::getClosestRefVectorCoords(const std::set<Coords>& refVectorsCoords, double maxDistance) const
{
struct Entry
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;
InputVector refVector;
double distance;
};
std::vector<Entry> sortedEntries;
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
std::vector<NeighbourInfo> neighboursInfo;
for (const Coords& neighbourCoords : neighboursCoords)
{
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
sortedEntries.push_back( Entry{{x, y}, _refVectors.get({x, y})} );
}
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});
}
const InputVector& closestRefVector = _refVectors.get(getClosestRefVector(data));
if (neighboursInfo.empty())
return boost::none;
std::sort(sortedEntries.begin(), sortedEntries.end(),
[&](const Entry& a, const Entry& b)
auto min = std::min_element(neighboursInfo.begin(), neighboursInfo.end(),
[&](const auto& a, const auto& b)
{
return _distanceFunc(a.refVector, closestRefVector, _weights) < _distanceFunc(b.refVector, closestRefVector, _weights);
return a.distance < b.distance;
});
std::vector<Coords> res;
for (const Entry& entry : sortedEntries)
{
res.push_back(entry.coords);
if (res.size() == size)
break;
}
return res;
return min->coords;
}
static InputVector::value_type
@@ -286,7 +350,7 @@ computeCoordsNorm(Coords c1, Coords c2)
void
Network::updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, Progress progress)
Network::updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, CurrentIteration iteration)
{
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
@@ -298,7 +362,7 @@ Network::updateRefVectors(Coords closestRefVectorCoords, const InputVector& inpu
auto n = computeCoordsNorm({x, y}, closestRefVectorCoords);
auto oldRefVector = refVector;
refVector = refVector + delta * (_learningFactorFunc(progress) * _neighborhoodFunc(n, progress));
refVector = refVector + delta * (_learningFactorFunc(iteration) * _neighbourhoodFunc(n, iteration));
}
}
}
@@ -324,74 +388,13 @@ Network::train(const std::vector<InputVector>& inputData, std::size_t nbIteratio
for (auto input : inputDataShuffled)
{
Coords closestRefVectorCoords = getClosestRefVector(*input);
Coords closestRefVectorCoords = getClosestRefVectorCoords(*input);
updateRefVectors(closestRefVectorCoords, *input, {i, nbIterations});
}
}
}
std::string
Network::serializeTo() const
{
std::ostringstream oss;
oss << _inputDimCount << " ";
for (auto weight : _weights)
oss << weight << " ";
// Matrix
oss << _refVectors.getWidth() << " " << _refVectors.getHeight() << " ";
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
for (auto val : _refVectors.get({x,y}))
oss << val << " ";
}
}
return oss.str();
}
void
Network::serializeFrom(const std::string& data)
{
std::istringstream iss(data);
LMS_LOG(SIMILARITY, DEBUG) << "data = '" << data << "'";
iss >> _inputDimCount;
LMS_LOG(SIMILARITY, DEBUG) << "Input dim count = " << _inputDimCount;
for (std::size_t i = 0; i < _inputDimCount; ++i)
{
InputVector::value_type val;
iss >> val;
_weights.push_back(val);
}
LMS_LOG(SIMILARITY, DEBUG) << "Reading matrix...";
std::size_t width, height;
iss >> width >> height;
_refVectors = Matrix<SOM::InputVector>(width, height);
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
{
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
{
InputVector refVector;
refVector.reserve(_inputDimCount);
for (std::size_t i = 0; i < _inputDimCount; ++i)
{
InputVector::value_type val;
iss >> val;
refVector.push_back(val);
}
_refVectors.get({x, y}) = refVector;
}
}
}
} // namespace SOM
@@ -20,9 +20,12 @@
#pragma once
#include <vector>
#include <set>
#include <ostream>
#include <functional>
#include <boost/optional.hpp>
#include "Matrix.hpp"
#include "utils/Exception.hpp"
@@ -58,45 +61,43 @@ class Network
// Set weight for each dimension (default is 1 for each weight)
void setDataWeights(const InputVector& weights);
// data must be normalized
// <!> data must be normalized
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations);
// data must be normalized
Coords classify(const InputVector& data) const;
Coords getClosestRefVectorCoords(const InputVector& data) const;
boost::optional<Coords> getClosestRefVectorCoords(const InputVector& data, double maxDistance) const;
// ordered from closest to farthest
std::vector<Coords> classify(const InputVector& data, std::size_t size) 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) * NeighborhoodFunc(i) * (MatchingRefVector - refVector)
// 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 Progress
struct CurrentIteration
{
std::size_t idIteration;
std::size_t iterationCount;
};
using LearningFactorFunc = std::function<InputVector::value_type(Progress)>;
using LearningFactorFunc = std::function<InputVector::value_type(CurrentIteration)>;
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
using NeighborhoodFunc = std::function<InputVector::value_type(InputVector::value_type /* norm(Coords - CoordMatchingRefVector) */, Progress)>;
void setNeighborhoodFunc(NeighborhoodFunc neighborhoodFunc);
std::string serializeTo() const;
using NeighbourhoodFunc = std::function<InputVector::value_type(InputVector::value_type /* norm(Coords - CoordMatchingRefVector) */, CurrentIteration)>;
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
private:
void serializeFrom(const std::string& data);
Coords getClosestRefVector(const InputVector& data) const;
void updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, Progress progress);
void updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, CurrentIteration iteration);
std::size_t _inputDimCount;
InputVector _weights; // weight for each dimension
@@ -104,7 +105,7 @@ class Network
DistanceFunc _distanceFunc;
LearningFactorFunc _learningFactorFunc;
NeighborhoodFunc _neighborhoodFunc;
NeighbourhoodFunc _neighbourhoodFunc;
};
} // namespace SOM
@@ -1,272 +0,0 @@
/*
* 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 "SimilaritySOMScannerAddon.hpp"
#include <cmath>
#include "database/Track.hpp"
#include "database/SimilaritySettings.hpp"
#include "database/TrackFeature.hpp"
#include "utils/Logger.hpp"
#include "AcousticBrainzUtils.hpp"
#include "DataNormalizer.hpp"
#include "Network.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;
}
std::vector<Database::TrackFeatureType::pointer>
getTrackFeatureTypes(Wt::Dbo::Session& session, const std::set<std::string>& featureNames)
{
std::vector<Database::TrackFeatureType::pointer> res;
for (const auto& featureName : featureNames)
{
auto trackFeatureType = Database::TrackFeatureType::getByName(session, featureName);
if (!trackFeatureType)
{
LMS_LOG(DBUPDATER, ERROR) << "Missing feature type '" << featureName << "'";
res.clear();
return res;
}
res.push_back(trackFeatureType);
}
return res;
}
bool
extractFeatures(const Database::Track::pointer& track, const std::vector<Database::TrackFeatureType::pointer>& trackFeatureTypes, std::vector<double>& features)
{
features.reserve(trackFeatureTypes.size());
for (const auto& trackFeatureType : trackFeatureTypes)
{
auto feature = track->getTrackFeature(trackFeatureType);
if (!feature)
{
LMS_LOG(DBUPDATER, ERROR) << "Missing feature " << trackFeatureType->getName() << " for track '" << track->getPath().string() << "'";
return false;
}
features.emplace_back(feature->getValue());
}
return true;
}
} // namespace
SOMScannerAddon::SOMScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool)
: _db(connectionPool)
{
refreshSettings();
clusterize();
}
std::shared_ptr<Similarity::SOMSearcher>
SOMScannerAddon::getSearcher()
{
return std::atomic_load(&_finder);
}
void
SOMScannerAddon::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
SOMScannerAddon::preScanComplete()
{
auto tracksInfo = getTracksWithMBIDAndMissingFeatures(_db.getSession());
for (const auto& trackInfo : tracksInfo)
fetchFeatures(trackInfo.id, trackInfo.mbid);
LMS_LOG(DBUPDATER, INFO) << "Clustering tracks...";
clusterize();
LMS_LOG(DBUPDATER, INFO) << "Clusterization complete!";
}
void
SOMScannerAddon::clusterize()
{
Wt::Dbo::Transaction transaction(_db.getSession());
auto trackFeatureTypes = getTrackFeatureTypes(_db.getSession(), _featuresName);
LMS_LOG(DBUPDATER, DEBUG) << "Getting feature types DONE...";
LMS_LOG(DBUPDATER, DEBUG) << "Getting Tracks with features...";
auto tracks = Database::Track::getAllWithFeatures(_db.getSession());
LMS_LOG(DBUPDATER, DEBUG) << "Getting Tracks with features DONE";
std::vector<SOM::InputVector> samples;
std::vector<Database::IdType> tracksIds;
LMS_LOG(DBUPDATER, DEBUG) << "Extracting features...";
for (auto track : tracks)
{
SOM::InputVector sample;
if (!extractFeatures(track, trackFeatureTypes, sample))
continue;
samples.emplace_back(std::move(sample));
tracksIds.emplace_back(track.id());
}
LMS_LOG(DBUPDATER, DEBUG) << "Extracting features DONE";
transaction.commit();
if (tracksIds.empty())
{
LMS_LOG(DBUPDATER, INFO) << "Nothing to classify!";
std::atomic_store(&_finder, std::shared_ptr<SOMSearcher>());
return;
}
LMS_LOG(DBUPDATER, DEBUG) << "Normalizing data...";
SOM::DataNormalizer normalizer(_featuresName.size());
normalizer.computeNormalizationFactors(samples);
for (auto& sample : samples)
normalizer.normalizeData(sample);
std::size_t size = std::sqrt(samples.size()/5);
LMS_LOG(DBUPDATER, DEBUG) << "Found " << samples.size() << " tracks, Constructing a " << size << "*" << size << " network";
SOM::Network network(size, size, _featuresName.size());
LMS_LOG(DBUPDATER, DEBUG) << "Training network...";
network.train(samples, 20);
LMS_LOG(DBUPDATER, DEBUG) << "Training network DONE";
// Now classify all the tracks
LMS_LOG(DBUPDATER, DEBUG) << "Classifying tracks...";
SOM::Matrix<std::vector<Database::IdType>> tracksMap(network.getWidth(), network.getHeight());
std::map<Database::IdType, SOM::Coords> trackIdsCoords;
for (std::size_t i = 0; i < samples.size(); ++i)
{
const auto& sample = samples[i];
auto trackId = tracksIds[i];
auto coords = network.classify(sample);
tracksMap[coords].push_back(trackId);
trackIdsCoords[trackId] = coords;
}
Similarity::SOMSearcher::ConstructionParams params{std::move(network), std::move(normalizer), std::move(tracksMap), std::move(trackIdsCoords)};
auto finder = std::make_shared<Similarity::SOMSearcher>(std::move(params));
std::atomic_store(&_finder, finder);
LMS_LOG(DBUPDATER, DEBUG) << "Classifying tracks DONE";
LMS_LOG(DBUPDATER, DEBUG) << "Dumping classifier:";
std::ofstream ofs("/tmp/output");
finder->dump(_db.getSession(), ofs);
LMS_LOG(DBUPDATER, DEBUG) << "Dumping classifier DONE";
}
void
SOMScannerAddon::refreshSettings()
{
Wt::Dbo::Transaction transaction(_db.getSession());
auto settings = Database::SimilaritySettings::get(_db.getSession());
_settingsVersion = settings->getVersion();
for (auto trackFeatureType : settings->getTrackFeatureTypes())
{
_featuresName.insert(trackFeatureType->getName());
}
}
bool
SOMScannerAddon::fetchFeatures(Database::IdType trackId, const std::string& MBID)
{
std::map<std::string, double> features;
if (!AcousticBrainz::extractFeatures(MBID, _featuresName, features))
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot extract features using AcousticBrainz!";
return false;
}
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() << "'";
for (const auto& feature : features)
{
auto featureType = Database::TrackFeatureType::getByName(_db.getSession(), feature.first);
if (!featureType)
return false;
Database::TrackFeature::create(_db.getSession(), featureType, track, feature.second);
}
return true;
}
} // namespace Similarity
@@ -1,259 +0,0 @@
/*
* 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 "SimilaritySOMSearcher.hpp"
#include <random>
#include "database/Artist.hpp"
#include "database/SimilaritySettings.hpp"
#include "database/Release.hpp"
#include "database/Track.hpp"
#include "utils/Logger.hpp"
#include "utils/Utils.hpp"
namespace Similarity {
SOMSearcher::SOMSearcher(ConstructionParams params)
: _network(std::move(params.network)),
_normalizer(std::move(params.normalizer)),
_tracksMap(std::move(params.tracksMap)),
_trackIdsCoords(std::move(params.trackIdsCoords))
{
}
std::vector<Database::IdType>
SOMSearcher::getSimilarTracks(const std::vector<Database::IdType>& tracksIds, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto bestCoords = getBestMatchingCoords(tracksIds);
if (!bestCoords)
return res;
auto tracks = _tracksMap[*bestCoords];
auto now = std::chrono::system_clock::now();
std::mt19937 randGenerator(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
std::shuffle(tracks.begin(), tracks.end(), randGenerator);
if (tracks.size() > maxCount)
tracks.resize(maxCount);
return tracks;
}
std::vector<Database::IdType>
SOMSearcher::getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
Wt::Dbo::Transaction transaction(session);
auto release = Database::Release::getById(session, releaseId);
if (!release)
return res;
auto tracks = release->getTracks();
std::vector<Database::IdType> tracksIds;
for (auto track : tracks)
tracksIds.push_back(track.id());
auto matchingCoords = getMatchingCoords(tracksIds);
if (matchingCoords.empty())
return res;
auto releases = getReleases(session, matchingCoords);
uniqueAndSortedByOccurence(releases.begin(), releases.end(), std::back_inserter(res));
res.erase(std::remove_if(res.begin(), res.end(), [&](auto releaseId) { return releaseId == release.id(); }), res.end());
if (res.size() > maxCount)
res.resize(maxCount);
LMS_LOG(SIMILARITY, DEBUG) << "*** SIMILARITY RESULT *** :";
for (auto id : res)
LMS_LOG(SIMILARITY, DEBUG) << id;
return res;
}
std::vector<Database::IdType>
SOMSearcher::getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
Wt::Dbo::Transaction transaction(session);
auto artist = Database::Artist::getById(session, artistId);
if (!artist)
return res;
auto tracks = artist->getTracks();
std::vector<Database::IdType> tracksIds;
for (auto track : tracks)
tracksIds.push_back(track.id());
auto matchingCoords = getMatchingCoords(tracksIds);
if (matchingCoords.empty())
return res;
auto artists = getArtists(session, matchingCoords);
uniqueAndSortedByOccurence(artists.begin(), artists.end(), std::back_inserter(res));
res.erase(std::remove_if(res.begin(), res.end(), [&](auto artistId) { return artistId == artist.id(); }), res.end());
if (res.size() > maxCount)
res.resize(maxCount);
LMS_LOG(SIMILARITY, DEBUG) << "*** SIMILARITY RESULT *** :";
for (auto id : res)
LMS_LOG(SIMILARITY, DEBUG) << id;
return res;
}
void
SOMSearcher::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;
}
}
boost::optional<SOM::Coords>
SOMSearcher::getBestMatchingCoords(const std::vector<Database::IdType>& tracksIds) const
{
if (tracksIds.empty())
return boost::none;
std::map<SOM::Coords, std::size_t /*count*/> coordsCount;
for (auto trackId : tracksIds)
{
auto it = _trackIdsCoords.find(trackId);
if (it == _trackIdsCoords.end())
continue;
if (coordsCount.find(it->second) == coordsCount.end())
coordsCount[it->second] = 0;
coordsCount[it->second]++;
}
if (coordsCount.empty())
return boost::none;
auto bestCoords = std::max_element(std::begin(coordsCount), std::end(coordsCount),
[](const auto& a, const auto& b)
{
return a.second < b.second;
});
return bestCoords->first;
}
std::vector<SOM::Coords>
SOMSearcher::getMatchingCoords(const std::vector<Database::IdType>& tracksIds) const
{
std::vector<SOM::Coords> res;
if (tracksIds.empty())
return res;
for (auto trackId : tracksIds)
{
auto it = _trackIdsCoords.find(trackId);
if (it == _trackIdsCoords.end())
continue;
res.push_back(it->second);
}
return res;
}
std::vector<Database::IdType>
SOMSearcher::getReleases(Wt::Dbo::Session& session, const std::vector<SOM::Coords>& coords) const
{
std::vector<Database::IdType> res;
for (const auto& c : coords)
{
for (auto trackId : _tracksMap[c])
{
auto track = Database::Track::getById(session, trackId);
if (!track || !track->getRelease())
continue;
res.emplace_back(track->getRelease().id());
}
}
return res;
}
std::vector<Database::IdType>
SOMSearcher::getArtists(Wt::Dbo::Session& session, const std::vector<SOM::Coords>& coords) const
{
std::vector<Database::IdType> res;
for (const auto& c : coords)
{
for (auto trackId : _tracksMap[c])
{
auto track = Database::Track::getById(session, trackId);
if (!track || !track->getArtist())
continue;
res.emplace_back(track->getArtist().id());
}
}
return res;
}
} // ns Similarity
@@ -1,65 +0,0 @@
/*
* 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 <boost/optional.hpp>
#include "database/DatabaseHandler.hpp"
#include "database/Types.hpp"
#include "DataNormalizer.hpp"
#include "Network.hpp"
namespace Similarity {
class SOMSearcher
{
public:
struct ConstructionParams
{
SOM::Network network;
SOM::DataNormalizer normalizer;
SOM::Matrix<std::vector<Database::IdType>> tracksMap;
std::map<Database::IdType, SOM::Coords> trackIdsCoords;
};
SOMSearcher(ConstructionParams params);
std::vector<Database::IdType> getSimilarTracks(const std::vector<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount);
void dump(Wt::Dbo::Session& session, std::ostream& os) const;
private:
boost::optional<SOM::Coords> getBestMatchingCoords(const std::vector<Database::IdType>& tracksIds) const;
std::vector<SOM::Coords> getMatchingCoords(const std::vector<Database::IdType>& tracksIds) const;
std::vector<Database::IdType> getReleases(Wt::Dbo::Session& session, const std::vector<SOM::Coords>& coords) const;
std::vector<Database::IdType> getArtists(Wt::Dbo::Session& session, const std::vector<SOM::Coords>& coords) const;
SOM::Network _network;
SOM::DataNormalizer _normalizer;
SOM::Matrix<std::vector<Database::IdType>> _tracksMap;
std::map<Database::IdType, SOM::Coords> _trackIdsCoords;
};
} // ns Similarity