/* * 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 . */ #include "SimilarityFeaturesSearcher.hpp" #include #include #include "database/Artist.hpp" #include "database/Release.hpp" #include "database/Session.hpp" #include "database/Track.hpp" #include "database/TrackFeatures.hpp" #include "som/DataNormalizer.hpp" #include "utils/Logger.hpp" namespace Similarity { const FeatureSettingsMap& FeaturesSearcher::getDefaultTrainFeatureSettings() { static FeatureSettingsMap defaultTrainFeatureSettings { { "lowlevel.spectral_energyband_high.mean", {1}}, { "lowlevel.spectral_rolloff.median", {1}}, { "lowlevel.spectral_contrast_valleys.var", {1}}, { "lowlevel.erbbands.mean", {1}}, { "lowlevel.gfcc.mean", {1}}, }; return defaultTrainFeatureSettings; } static std::optional getTrackFeatureValues(FeaturesSearcher::FeaturesFetchFunc func, Database::IdType trackId, const std::unordered_set& featureNames) { return func(trackId, featureNames); } static std::optional getTrackFeatureValuesFromDb(Database::Session& session, Database::IdType trackId, const std::unordered_set& featureNames) { auto func = [&](Database::IdType trackId, const std::unordered_set& featureNames) { std::optional res; auto transaction {session.createSharedTransaction()}; Database::Track::pointer track {Database::Track::getById(session, trackId)}; if (!track) return res; res = track->getTrackFeatures()->getFeatureValuesMap(featureNames); if (res->empty()) res.reset(); return res; }; return getTrackFeatureValues(func, trackId, featureNames); } static std::optional convertFeatureValuesMapToInputVector(const FeatureValuesMap& featureValuesMap, std::size_t nbDimensions) { std::size_t i {}; std::optional res {SOM::InputVector {nbDimensions}}; for (const auto& [featureName, values] : featureValuesMap) { if (values.size() != getFeatureDef(featureName).nbDimensions) { LMS_LOG(SIMILARITY, WARNING) << "Dimension mismatch for feature '" << featureName << "'. Expected " << getFeatureDef(featureName).nbDimensions << ", got " << values.size(); res.reset(); break; } for (double val : values) (*res)[i++] = val; } return res; } static SOM::InputVector getInputVectorWeights(const FeatureSettingsMap& featureSettingsMap, std::size_t nbDimensions) { SOM::InputVector weights {nbDimensions}; std::size_t index {}; for (const auto& [featureName, featureSettings] : featureSettingsMap) { const std::size_t featureNbDimensions {getFeatureDef(featureName).nbDimensions}; for (std::size_t i {}; i < featureNbDimensions; ++i) weights[index++] = (1. / featureNbDimensions * featureSettings.weight); } assert(index == nbDimensions); return weights; } FeaturesSearcher::FeaturesSearcher(Database::Session& session, const TrainSettings& trainSettings, StopRequestedFunction stopRequested) { LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher..."; std::unordered_set featureNames; std::transform(std::cbegin(trainSettings.featureSettingsMap), std::cend(trainSettings.featureSettingsMap), std::inserter(featureNames, std::begin(featureNames)), [](const auto& itFeatureSetting) { return itFeatureSetting.first; }); const std::size_t nbDimensions {std::accumulate(std::cbegin(featureNames), std::cend(featureNames), std::size_t {0}, [](std::size_t sum, const FeatureName& featureName) { return sum + getFeatureDef(featureName).nbDimensions; })}; LMS_LOG(SIMILARITY, DEBUG) << "Features dimension = " << nbDimensions; std::vector trackIds; { auto transaction {session.createSharedTransaction()}; LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features..."; trackIds = Database::Track::getAllIdsWithFeatures(session); LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features DONE (found " << trackIds.size() << " tracks)"; } std::vector samples; std::vector samplesTrackIds; samples.reserve(trackIds.size()); samplesTrackIds.reserve(trackIds.size()); LMS_LOG(SIMILARITY, DEBUG) << "Extracting features..."; for (Database::IdType trackId : trackIds) { if (stopRequested && stopRequested()) return; std::optional featureValuesMap; if (_featuresFetchFunc) featureValuesMap = getTrackFeatureValues(_featuresFetchFunc, trackId, featureNames); else featureValuesMap = getTrackFeatureValuesFromDb(session, trackId, featureNames); if (!featureValuesMap) continue; std::optional inputVector {convertFeatureValuesMapToInputVector(*featureValuesMap, nbDimensions)}; if (!inputVector) continue; samples.emplace_back(std::move(*inputVector)); samplesTrackIds.emplace_back(trackId); } LMS_LOG(SIMILARITY, DEBUG) << "Extracting features DONE"; if (samples.empty()) { LMS_LOG(SIMILARITY, INFO) << "Nothing to classify!"; return; } LMS_LOG(SIMILARITY, DEBUG) << "Normalizing data..."; SOM::DataNormalizer dataNormalizer {nbDimensions}; dataNormalizer.computeNormalizationFactors(samples); for (auto& sample : samples) dataNormalizer.normalizeData(sample); SOM::Coordinate size {static_cast(std::sqrt(samples.size() / trainSettings.sampleCountPerNeuron))}; LMS_LOG(SIMILARITY, INFO) << "Found " << samples.size() << " tracks, constructing a " << size << "*" << size << " network"; SOM::Network network {size, size, nbDimensions}; SOM::InputVector weights {getInputVectorWeights(trainSettings.featureSettingsMap, nbDimensions)}; network.setDataWeights(weights); auto progressIndicator{[](const auto& iter) { LMS_LOG(SIMILARITY, DEBUG) << "Current pass = " << iter.idIteration << " / " << iter.iterationCount; }}; LMS_LOG(SIMILARITY, DEBUG) << "Training network..."; network.train(samples, trainSettings.iterationCount, progressIndicator, stopRequested); LMS_LOG(SIMILARITY, DEBUG) << "Training network DONE"; if (stopRequested && stopRequested()) return; LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks..."; std::map> trackPositions; for (std::size_t i {}; i < samples.size(); ++i) { if (stopRequested && stopRequested()) return; const SOM::Position position {network.getClosestRefVectorPosition(samples[i])}; trackPositions[samplesTrackIds[i]].insert(position); } LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks DONE"; init(session, std::move(network), std::move(trackPositions), stopRequested); LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher"; } FeaturesSearcher::FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested) { LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher from cache..."; init(session, std::move(cache._network), std::move(cache._trackPositions), stopRequested); LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher from cache"; } bool FeaturesSearcher::isValid() const { return _network.get() != nullptr; } bool FeaturesSearcher::isTrackClassified(Database::IdType trackId) const { return (_trackPositions.find(trackId) != _trackPositions.end()); } bool FeaturesSearcher::isReleaseClassified(Database::IdType releaseId) const { return (_releasePositions.find(releaseId) != _releasePositions.end()); } bool FeaturesSearcher::isArtistClassified(Database::IdType artistId) const { return (_artistPositions.find(artistId) != _artistPositions.end()); } std::vector FeaturesSearcher::getSimilarTracks(const std::set& tracksIds, std::size_t maxCount) const { return getSimilarObjects(tracksIds, _tracksMap, _trackPositions, maxCount); } std::vector FeaturesSearcher::getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const { return getSimilarObjects({releaseId}, _releasesMap, _releasePositions, maxCount); } std::vector FeaturesSearcher::getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const { return getSimilarObjects({artistId}, _artistsMap, _artistPositions, maxCount); } void FeaturesSearcher::dump(Database::Session& session, std::ostream& os) const { if (!isValid()) { os << "Invalid searcher" << std::endl; return; } os << "Number of tracks classified: " << _trackPositions.size() << std::endl; os << "Network size: " << _network->getWidth() << " * " << _network->getHeight() << std::endl; os << "Ref vectors median distance = " << _networkRefVectorsDistanceMedian << std::endl; auto transaction {session.createSharedTransaction()}; for (SOM::Coordinate y {}; y < _network->getHeight(); ++y) { for (SOM::Coordinate x {}; x < _network->getWidth(); ++x) { const auto& trackIds {_tracksMap[{x, y}]}; os << "{" << x << ", " << y << "}"; if (y > 0) os << " - {" << x << ", " << y - 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y - 1}); if (x > 0) os << " - {" << x - 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x - 1, y}); if (y != _network->getHeight() - 1) os << " - {" << x << ", " << y + 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y + 1}); if (x != _network->getWidth() - 1) os << " - {" << x + 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x + 1, y}); os << std::endl; for (Database::IdType trackId : trackIds) { auto track {Database::Track::getById(session, trackId)}; if (!track) continue; os << "\t"; for (auto artist : track->getArtists()) os << artist->getName() << " - "; if (track->getRelease()) os << track->getRelease()->getName() << " - "; os << track->getName() << std::endl; } } os << std::endl; } } FeaturesCache FeaturesSearcher::toCache() const { return FeaturesCache{*_network, _trackPositions}; } void FeaturesSearcher::init(Database::Session& session, SOM::Network network, std::map> tracksPosition, std::function stopRequested) { _networkRefVectorsDistanceMedian = network.computeRefVectorsDistanceMedian(); LMS_LOG(SIMILARITY, DEBUG) << "Median distance betweend ref vectors = " << _networkRefVectorsDistanceMedian; SOM::Coordinate width {network.getWidth()}; SOM::Coordinate height {network.getHeight()}; _artistsMap = SOM::Matrix>{width, height}; _releasesMap = SOM::Matrix>{width, height}; _tracksMap = SOM::Matrix>{width, height}; LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps..."; for (auto itTrackCoord : tracksPosition) { if (stopRequested && stopRequested()) return; auto transaction {session.createSharedTransaction()}; Database::IdType trackId {itTrackCoord.first}; const std::set& positionSet {itTrackCoord.second}; const Database::Track::pointer track {Database::Track::getById(session, trackId)}; if (!track) continue; for (const SOM::Position& position : positionSet) { _tracksMap[position].insert(trackId); _trackPositions[trackId].insert(position); if (track->getRelease()) { _releasePositions[track->getRelease().id()].insert(position); _releasesMap[position].insert(track->getRelease().id()); } for (const auto& artist : track->getArtists()) { _artistPositions[artist.id()].insert(position); _artistsMap[position].insert(artist.id()); } } } _network = std::make_unique(std::move(network)); LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps... DONE"; } static std::set getMatchingRefVectorsPosition(const std::set& ids, const std::map>& objectPosition) { std::set res; if (ids.empty()) return res; for (auto id : ids) { auto it = objectPosition.find(id); if (it == objectPosition.end()) continue; for (const auto& position : it->second) res.insert(position); } return res; } static std::set getObjectsIds(const std::set& positionSet, const SOM::Matrix>& objectsMap ) { std::set res; for (const auto& position : positionSet) { for (auto id : objectsMap.get(position)) res.insert(id); } return res; } std::vector FeaturesSearcher::getSimilarObjects(const std::set& ids, const SOM::Matrix>& objectsMap, const std::map>& objectPosition, std::size_t maxCount) const { std::vector res; if (!isValid()) return res; auto now {std::chrono::system_clock::now()}; std::mt19937 randGenerator{static_cast(std::chrono::duration_cast(now.time_since_epoch()).count())}; std::set searchedRefVectorsPosition {getMatchingRefVectorsPosition(ids, objectPosition)}; if (searchedRefVectorsPosition.empty()) return res; while (1) { std::set closestObjectIds {getObjectsIds(searchedRefVectorsPosition, objectsMap)}; // Remove objects that are already in input or already reported for (auto id : ids) closestObjectIds.erase(id); for (auto id : res) closestObjectIds.erase(id); { std::vector 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 std::optional closestRefVectorPosition {_network->getClosestRefVectorPosition(searchedRefVectorsPosition, _networkRefVectorsDistanceMedian * 0.75)}; if (!closestRefVectorPosition) break; searchedRefVectorsPosition.insert(*closestRefVectorPosition); } return res; } } // ns Similarity