[WIP] Added Artist/ReleaseInfo
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 "SimilaritySearcher.hpp"
|
||||
|
||||
namespace Similarity {
|
||||
|
||||
Searcher::Searcher(SOMScannerAddon& somAddon)
|
||||
: _somAddon(somAddon)
|
||||
{}
|
||||
|
||||
std::vector<Database::IdType>
|
||||
Searcher::getSimilarTracks(const std::vector<Database::IdType>& tracksId, std::size_t maxCount)
|
||||
{
|
||||
auto somSearcher = _somAddon.getSearcher();
|
||||
if (!somSearcher)
|
||||
return {};
|
||||
|
||||
return somSearcher->getSimilarTracks(tracksId, maxCount);
|
||||
}
|
||||
|
||||
std::vector<Database::IdType>
|
||||
Searcher::getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
|
||||
{
|
||||
auto somSearcher = _somAddon.getSearcher();
|
||||
if (!somSearcher)
|
||||
return {};
|
||||
|
||||
return somSearcher->getSimilarReleases(session, releaseId, maxCount);
|
||||
}
|
||||
|
||||
std::vector<Database::IdType>
|
||||
Searcher::getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
|
||||
{
|
||||
auto somSearcher = _somAddon.getSearcher();
|
||||
if (!somSearcher)
|
||||
return {};
|
||||
|
||||
return somSearcher->getSimilarArtists(session, artistId, maxCount);
|
||||
}
|
||||
|
||||
} // ns Similarity
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/Dbo/Session.h>
|
||||
|
||||
#include "database/Types.hpp"
|
||||
#include "som/SimilaritySOMScannerAddon.hpp"
|
||||
|
||||
namespace Similarity {
|
||||
|
||||
class Searcher
|
||||
{
|
||||
public:
|
||||
Searcher(SOMScannerAddon& somAddon);
|
||||
|
||||
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);
|
||||
|
||||
private:
|
||||
|
||||
SOMScannerAddon& _somAddon;
|
||||
};
|
||||
|
||||
} // ns Similarity
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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 "SimilarityClusterSearcher.hpp"
|
||||
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Track.hpp"
|
||||
#include "utils/Utils.hpp"
|
||||
|
||||
namespace Similarity {
|
||||
|
||||
std::vector<Database::IdType>
|
||||
ClusterSearcher::getSimilarTracks(Wt::Dbo::Session& session, const std::vector<Database::IdType>& tracksId, std::size_t maxCount)
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(session);
|
||||
|
||||
std::vector<Database::IdType> clusterIds;
|
||||
for (auto trackId : tracksId)
|
||||
{
|
||||
auto track = Database::Track::getById(session, trackId);
|
||||
if (!track)
|
||||
continue;
|
||||
|
||||
auto clusters = track->getClusters();
|
||||
if (clusters.empty())
|
||||
continue;
|
||||
|
||||
for (const auto& cluster : clusters)
|
||||
clusterIds.push_back(cluster.id());
|
||||
}
|
||||
|
||||
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;
|
||||
{
|
||||
auto ids = tracklist->getTrackIds();
|
||||
trackIds = std::set<Database::IdType>(ids.begin(), ids.end());
|
||||
}
|
||||
|
||||
// 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;
|
||||
std::set_difference(clusterTrackIds.begin(), clusterTrackIds.end(),
|
||||
trackIds.begin(), trackIds.end(),
|
||||
std::inserter(candidateTrackIds, candidateTrackIds.end()));
|
||||
|
||||
if (candidateTrackIds.empty())
|
||||
continue;
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, candidateTrackIds.size() - 1);
|
||||
|
||||
auto trackToAdd = Database::Track::getById(LmsApp->getDboSession(), *std::next(candidateTrackIds.begin(), dist(randGenerator)));
|
||||
enqueueTrack(trackToAdd);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LMS_LOG(UI, INFO) << "No more track to be added!";
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<Database::IdType>
|
||||
ClusterSearcher::getSimilarReleases(Wt::Dbo::Session& session, Database::IdType releaseId, std::size_t maxCount)
|
||||
{
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<Database::IdType>
|
||||
ClusterSearcher::getSimilarArtists(Wt::Dbo::Session& session, Database::IdType artistId, std::size_t maxCount)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Similarity
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 "database/Types.hpp"
|
||||
|
||||
namespace Similarity {
|
||||
|
||||
class 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);
|
||||
};
|
||||
|
||||
} // namespace Similarity
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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 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)
|
||||
{
|
||||
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(DBUPDATER, 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(DBUPDATER, ERROR) << "CURL perform failed: " << curl_easy_strerror(res);
|
||||
return data;
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
data = std::move(oss.str());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
bool
|
||||
extractFeatures(const std::string& mbid, const std::set<std::string>& featuresName, std::map<std::string, double>& features)
|
||||
{
|
||||
return getFeaturesFromJsonData(getJsonData(mbid), featuresName, features);
|
||||
}
|
||||
|
||||
} // namespace Scanner::AcousticBrainz
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
|
||||
bool extractFeatures(const std::string& MBID, const std::set<std::string>& featuresName, std::map<std::string, double>& features);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
}
|
||||
|
||||
DataNormalizer::DataNormalizer(const std::string& data)
|
||||
{
|
||||
serializeFrom(data);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
for (std::size_t i = 0; i < _inputDimCount; ++i)
|
||||
os << "(" << _minmax[i].min << ", " << _minmax[i].max << ")";
|
||||
}
|
||||
|
||||
} // namespace SOM
|
||||
@@ -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 <vector>
|
||||
#include <ostream>
|
||||
|
||||
#include "Network.hpp"
|
||||
|
||||
namespace SOM
|
||||
{
|
||||
|
||||
class DataNormalizer
|
||||
{
|
||||
public:
|
||||
|
||||
DataNormalizer(std::size_t inputDimCount);
|
||||
DataNormalizer(const std::string& data);
|
||||
|
||||
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
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
return x < other.x && y < other.y;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Matrix
|
||||
{
|
||||
public:
|
||||
|
||||
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
|
||||
{
|
||||
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;
|
||||
std::size_t _height;
|
||||
std::vector<T> _values;
|
||||
};
|
||||
|
||||
} // ns SOM
|
||||
@@ -0,0 +1,398 @@
|
||||
/*
|
||||
* 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::Progress progress)
|
||||
{
|
||||
constexpr InputVector::value_type initialValue = 1;
|
||||
|
||||
return initialValue * exp(-((progress.idIteration + 1) / static_cast<InputVector::value_type>(progress.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::Progress progress)
|
||||
{
|
||||
constexpr InputVector::value_type sigma0 = 1;
|
||||
|
||||
return sigma0 * exp(- ((progress.idIteration + 1) / static_cast<InputVector::value_type>(progress.iterationCount)));
|
||||
}
|
||||
|
||||
static
|
||||
InputVector::value_type
|
||||
defaultNeighborhoodFunc(InputVector::value_type norm, Network::Progress progress)
|
||||
{
|
||||
auto sigma = sigmaFunc(progress);
|
||||
|
||||
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),
|
||||
_neighborhoodFunc(defaultNeighborhoodFunc)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Network::Network(const std::string& data)
|
||||
: _refVectors(0, 0),
|
||||
_distanceFunc(euclidianSquareDistance),
|
||||
_learningFactorFunc(defaultLearningFactor),
|
||||
_neighborhoodFunc(defaultNeighborhoodFunc)
|
||||
{
|
||||
serializeFrom(data);
|
||||
}
|
||||
|
||||
void
|
||||
Network::setDataWeights(const InputVector& weights)
|
||||
{
|
||||
checkSameDimensions(weights, _inputDimCount);
|
||||
|
||||
_weights = weights;
|
||||
}
|
||||
|
||||
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::getClosestRefVector(const InputVector& data) const
|
||||
{
|
||||
return _refVectors.getCoordsMinElement([&](const auto& a, const auto& b)
|
||||
{
|
||||
return (_distanceFunc(a, data, _weights) < _distanceFunc(b, data, _weights));
|
||||
});
|
||||
}
|
||||
|
||||
Coords
|
||||
Network::classify(const InputVector& data) const
|
||||
{
|
||||
return getClosestRefVector(data);
|
||||
}
|
||||
|
||||
std::vector<Coords>
|
||||
Network::classify(const InputVector& data, std::size_t size) const
|
||||
{
|
||||
struct Entry
|
||||
{
|
||||
Coords coords;
|
||||
InputVector refVector;
|
||||
};
|
||||
std::vector<Entry> sortedEntries;
|
||||
|
||||
for (std::size_t x = 0; x < _refVectors.getWidth(); ++x)
|
||||
{
|
||||
for (std::size_t y = 0; y < _refVectors.getHeight(); ++y)
|
||||
{
|
||||
sortedEntries.push_back( Entry{{x, y}, _refVectors.get({x, y})} );
|
||||
}
|
||||
}
|
||||
|
||||
const InputVector& closestRefVector = _refVectors.get(getClosestRefVector(data));
|
||||
|
||||
std::sort(sortedEntries.begin(), sortedEntries.end(),
|
||||
[&](const Entry& a, const Entry& b)
|
||||
{
|
||||
return _distanceFunc(a.refVector, closestRefVector, _weights) < _distanceFunc(b.refVector, closestRefVector, _weights);
|
||||
});
|
||||
|
||||
std::vector<Coords> res;
|
||||
for (const Entry& entry : sortedEntries)
|
||||
{
|
||||
res.push_back(entry.coords);
|
||||
|
||||
if (res.size() == size)
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
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, Progress progress)
|
||||
{
|
||||
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(progress) * _neighborhoodFunc(n, progress));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 = getClosestRefVector(*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
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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 <functional>
|
||||
|
||||
#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);
|
||||
|
||||
// data must be normalized
|
||||
Coords classify(const InputVector& data) const;
|
||||
|
||||
// ordered from closest to farthest
|
||||
std::vector<Coords> classify(const InputVector& data, std::size_t size) 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)
|
||||
|
||||
using DistanceFunc = std::function<InputVector::value_type(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
|
||||
void setDistanceFunc(DistanceFunc distanceFunc);
|
||||
|
||||
struct Progress
|
||||
{
|
||||
std::size_t idIteration;
|
||||
std::size_t iterationCount;
|
||||
};
|
||||
|
||||
using LearningFactorFunc = std::function<InputVector::value_type(Progress)>;
|
||||
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;
|
||||
|
||||
private:
|
||||
|
||||
void serializeFrom(const std::string& data);
|
||||
|
||||
Coords getClosestRefVector(const InputVector& data) const;
|
||||
|
||||
void updateRefVectors(Coords closestRefVectorCoords, const InputVector& input, Progress progress);
|
||||
|
||||
std::size_t _inputDimCount;
|
||||
InputVector _weights; // weight for each dimension
|
||||
Matrix<InputVector> _refVectors;
|
||||
|
||||
DistanceFunc _distanceFunc;
|
||||
LearningFactorFunc _learningFactorFunc;
|
||||
NeighborhoodFunc _neighborhoodFunc;
|
||||
};
|
||||
|
||||
} // namespace SOM
|
||||
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 "SimilaritySOMSearcher.hpp"
|
||||
|
||||
namespace Similarity {
|
||||
|
||||
class SOMScannerAddon : public Scanner::MediaScannerAddon
|
||||
{
|
||||
public:
|
||||
|
||||
SOMScannerAddon(Wt::Dbo::SqlConnectionPool& connectionPool);
|
||||
|
||||
std::shared_ptr<SOMSearcher> 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 clusterize();
|
||||
|
||||
std::size_t _settingsVersion;
|
||||
std::set<std::string> _featuresName;
|
||||
Database::Handler _db;
|
||||
|
||||
std::shared_ptr<SOMSearcher> _finder;
|
||||
};
|
||||
|
||||
SOMScannerAddon* setSOMScannerAddon(SOMScannerAddon addon);
|
||||
SOMScannerAddon* getSOMScannerAddon();
|
||||
|
||||
} // namespace Similarity
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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
|
||||
Reference in New Issue
Block a user