WIP, first working genetic algorithm to train the neural network
This commit is contained in:
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
* 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 <chrono>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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 <numeric>
|
||||
|
||||
#include "utils/Utils.hpp"
|
||||
#include "ParallelFor.hpp"
|
||||
|
||||
template<typename Individual>
|
||||
class GeneticAlgorithm
|
||||
{
|
||||
public:
|
||||
using Score = float;
|
||||
|
||||
using BreedFunction = std::function<Individual(const Individual&, const Individual&)>;
|
||||
using MutateFunction = std::function<void(Individual&)>;
|
||||
using ScoreFunction = std::function<Score(const Individual&)>;
|
||||
|
||||
struct Params
|
||||
{
|
||||
std::size_t nbWorkers {1};
|
||||
std::size_t nbGenerations;
|
||||
float mutationProbability {0.05};
|
||||
BreedFunction breedFunction;
|
||||
MutateFunction mutateFunction;
|
||||
ScoreFunction scoreFunction;
|
||||
};
|
||||
|
||||
GeneticAlgorithm(const Params& params);
|
||||
|
||||
// Returns the individual that has the maximum score after processing the requested generations
|
||||
Individual simulate(const std::vector<Individual>& initialPopulation);
|
||||
|
||||
private:
|
||||
|
||||
struct ScoredIndividual
|
||||
{
|
||||
Individual individual;
|
||||
std::optional<Score> score {};
|
||||
};
|
||||
|
||||
void scoreAndSortPopulation(std::vector<ScoredIndividual>& population);
|
||||
|
||||
Params _params;
|
||||
};
|
||||
|
||||
template<typename Individual>
|
||||
GeneticAlgorithm<Individual>::GeneticAlgorithm(const Params& params)
|
||||
: _params {params}
|
||||
{
|
||||
}
|
||||
|
||||
template<typename Individual>
|
||||
Individual
|
||||
GeneticAlgorithm<Individual>::simulate(const std::vector<Individual>& initialPopulation)
|
||||
{
|
||||
if (initialPopulation.size() < 10)
|
||||
throw std::runtime_error("Initial population must has at least 10 elements");
|
||||
|
||||
std::vector<ScoredIndividual> scoredPopulation;
|
||||
scoredPopulation.reserve(initialPopulation.size());
|
||||
|
||||
std::transform(std::cbegin(initialPopulation), std::cend(initialPopulation), std::back_inserter(scoredPopulation ),
|
||||
[](const Individual& individual) { return ScoredIndividual {individual};});
|
||||
|
||||
scoreAndSortPopulation(scoredPopulation);
|
||||
|
||||
for (std::size_t currentGeneration {}; currentGeneration < _params.nbGenerations; ++currentGeneration)
|
||||
{
|
||||
std::cout << "Processing generation " << currentGeneration << "..." << std::endl;
|
||||
// parent selection (elitist selection)
|
||||
scoredPopulation.resize(scoredPopulation.size() / 2);
|
||||
|
||||
// breed the remaining individuals
|
||||
std::vector<ScoredIndividual> children;
|
||||
children.reserve(initialPopulation.size() - scoredPopulation.size());
|
||||
|
||||
while (children.size() + scoredPopulation.size() < initialPopulation.size())
|
||||
{
|
||||
// Select two random parents
|
||||
const auto itParent1 {pickRandom(scoredPopulation)};
|
||||
const auto itParent2 {pickRandom(scoredPopulation)};
|
||||
|
||||
if (itParent1 == itParent2)
|
||||
continue;
|
||||
|
||||
ScoredIndividual child {_params.breedFunction(itParent1->individual, itParent2->individual)};
|
||||
|
||||
if (getRandom(0, 100) <= _params.mutationProbability * 100)
|
||||
_params.mutateFunction(child.individual);
|
||||
|
||||
children.emplace_back(std::move(child ));
|
||||
}
|
||||
|
||||
scoredPopulation.insert(std::end(scoredPopulation), std::make_move_iterator(std::begin(children)), std::make_move_iterator(std::end(children)));
|
||||
assert(scoredPopulation.size() == initialPopulation.size());
|
||||
|
||||
scoreAndSortPopulation(scoredPopulation);
|
||||
|
||||
std::cout << "Current best score = " << *scoredPopulation.front().score << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Best score = " << *scoredPopulation.front().score << std::endl;
|
||||
return scoredPopulation.front().individual;
|
||||
}
|
||||
|
||||
|
||||
template<typename Individual>
|
||||
void
|
||||
GeneticAlgorithm<Individual>::scoreAndSortPopulation(std::vector<ScoredIndividual>& scoredPopulation)
|
||||
{
|
||||
parallel_foreach(_params.nbWorkers, std::begin(scoredPopulation), std::end(scoredPopulation),
|
||||
[&](ScoredIndividual& scoredIndividual)
|
||||
{
|
||||
if (!scoredIndividual.score)
|
||||
scoredIndividual.score = _params.scoreFunction(scoredIndividual.individual);
|
||||
});
|
||||
|
||||
std::sort(std::begin(scoredPopulation), std::end(scoredPopulation), [](const ScoredIndividual& a, const ScoredIndividual& b) { return a.score > b.score; });
|
||||
}
|
||||
|
||||
@@ -1,47 +1,354 @@
|
||||
/*
|
||||
* 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 <iostream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Release.hpp"
|
||||
#include "database/SessionPool.hpp"
|
||||
#include "database/Track.hpp"
|
||||
#include "similarity/features/SimilarityFeaturesSearcher.hpp"
|
||||
#include "utils/Config.hpp"
|
||||
#include "utils/Service.hpp"
|
||||
#include "utils/StreamLogger.hpp"
|
||||
|
||||
#include "GeneticAlgorithm.hpp"
|
||||
|
||||
using namespace Similarity;
|
||||
using SimilarityScore = GeneticAlgorithm<FeatureSettingsMap>::Score;
|
||||
|
||||
// An individual is just a FeatureSettingsMap
|
||||
// The goal is to get the FeatureSettingsMap that maximize the score
|
||||
const FeatureSettingsMap featuresSettings
|
||||
{
|
||||
{ "lowlevel.average_loudness", {1}},
|
||||
{ "lowlevel.barkbands.mean", {1}},
|
||||
{ "lowlevel.barkbands.median", {1}},
|
||||
{ "lowlevel.barkbands.var", {1}},
|
||||
{ "lowlevel.barkbands_crest.mean", {1}},
|
||||
{ "lowlevel.barkbands_crest.median", {1}},
|
||||
{ "lowlevel.barkbands_crest.var", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.mean", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.median", {1}},
|
||||
{ "lowlevel.barkbands_flatness_db.var", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.mean", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.median", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.var", {1}},
|
||||
{ "lowlevel.barkbands_skewness.mean", {1}},
|
||||
{ "lowlevel.barkbands_skewness.median", {1}},
|
||||
{ "lowlevel.barkbands_skewness.var", {1}},
|
||||
{ "lowlevel.barkbands_spread.mean", {1}},
|
||||
{ "lowlevel.barkbands_spread.median", {1}},
|
||||
{ "lowlevel.barkbands_spread.var", {1}},
|
||||
{ "lowlevel.dissonance.mean", {1}},
|
||||
{ "lowlevel.dissonance.median", {1}},
|
||||
{ "lowlevel.dissonance.var", {1}},
|
||||
{ "lowlevel.dynamic_complexity", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", {1}},
|
||||
{ "lowlevel.erbbands.mean", {1}},
|
||||
{ "lowlevel.erbbands.median", {1}},
|
||||
{ "lowlevel.erbbands.var", {1}},
|
||||
{ "lowlevel.gfcc.mean", {1}},
|
||||
{ "lowlevel.hfc.mean", {1}},
|
||||
{ "lowlevel.hfc.median", {1}},
|
||||
{ "lowlevel.hfc.var", {1}},
|
||||
{ "tonal.hpcp.median", {1}},
|
||||
{ "lowlevel.melbands.median", {1}},
|
||||
{ "lowlevel.mfcc.mean", {1}},
|
||||
{ "lowlevel.pitch_salience.mean", {1}},
|
||||
{ "lowlevel.pitch_salience.median", {1}},
|
||||
{ "lowlevel.pitch_salience.var", {1}},
|
||||
{ "lowlevel.spectral_centroid.mean", {1}},
|
||||
{ "lowlevel.spectral_centroid.median", {1}},
|
||||
{ "lowlevel.spectral_centroid.var", {1}},
|
||||
{ "lowlevel.spectral_complexity.mean", {1}},
|
||||
{ "lowlevel.spectral_complexity.median", {1}},
|
||||
{ "lowlevel.spectral_complexity.var", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_coeffs.var", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.mean", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.median", {1}},
|
||||
{ "lowlevel.spectral_contrast_valleys.var", {1}},
|
||||
{ "lowlevel.spectral_decrease.mean", {1}},
|
||||
{ "lowlevel.spectral_decrease.median", {1}},
|
||||
{ "lowlevel.spectral_decrease.var", {1}},
|
||||
{ "lowlevel.spectral_energy.mean", {1}},
|
||||
{ "lowlevel.spectral_energy.median", {1}},
|
||||
{ "lowlevel.spectral_energy.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_high.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_low.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_high.var", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.mean", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.median", {1}},
|
||||
{ "lowlevel.spectral_energyband_middle_low.var", {1}},
|
||||
{ "lowlevel.spectral_entropy.mean", {1}},
|
||||
{ "lowlevel.spectral_entropy.median", {1}},
|
||||
{ "lowlevel.spectral_entropy.var", {1}},
|
||||
{ "lowlevel.spectral_flux.mean", {1}},
|
||||
{ "lowlevel.spectral_flux.median", {1}},
|
||||
{ "lowlevel.spectral_flux.var", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.mean", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.median", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.var", {1}},
|
||||
{ "lowlevel.spectral_rms.mean", {1}},
|
||||
{ "lowlevel.spectral_rms.median", {1}},
|
||||
{ "lowlevel.spectral_rms.var", {1}},
|
||||
{ "lowlevel.spectral_rolloff.mean", {1}},
|
||||
{ "lowlevel.spectral_rolloff.median", {1}},
|
||||
{ "lowlevel.spectral_rolloff.var", {1}},
|
||||
{ "lowlevel.spectral_skewness.mean", {1}},
|
||||
{ "lowlevel.spectral_skewness.median", {1}},
|
||||
{ "lowlevel.spectral_skewness.var", {1}},
|
||||
{ "lowlevel.spectral_spread.mean", {1}},
|
||||
{ "lowlevel.spectral_spread.median", {1}},
|
||||
{ "lowlevel.spectral_spread.var", {1}},
|
||||
{ "lowlevel.zerocrossingrate.mean", {1}},
|
||||
{ "lowlevel.zerocrossingrate.median", {1}},
|
||||
{ "lowlevel.zerocrossingrate.var", {1}},
|
||||
};
|
||||
|
||||
|
||||
static
|
||||
void
|
||||
printFeatureSettingsMap(const FeatureSettingsMap& featureSettings)
|
||||
{
|
||||
std::cout << "FeatureSettingsMap: (" << featureSettings.size() << " features)" << std::endl;
|
||||
for (const auto& [name, settings] : featureSettings)
|
||||
std::cout << "\t" << name << std::endl;
|
||||
}
|
||||
|
||||
static
|
||||
std::string
|
||||
trackToString(Database::Session& session, Database::IdType trackId)
|
||||
{
|
||||
std::string res;
|
||||
auto transaction {session.createSharedTransaction()};
|
||||
Database::Track::pointer track {Database::Track::getById(session, trackId)};
|
||||
|
||||
res += track->getName();
|
||||
if (track->getRelease())
|
||||
res += " [" + track->getRelease()->getName() + "]";
|
||||
for (auto artist : track->getArtists())
|
||||
res += " - " + artist->getName();
|
||||
for (auto cluster : track->getClusters())
|
||||
res += " {" + cluster->getType()->getName() + "-"+ cluster->getName() + "}";
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static
|
||||
SimilarityScore
|
||||
computeTrackScore(Database::Session& session, Database::IdType track1Id, Database::IdType track2Id)
|
||||
{
|
||||
SimilarityScore score {};
|
||||
|
||||
auto transaction {session.createSharedTransaction()};
|
||||
|
||||
auto track1 {Database::Track::getById(session, track1Id)};
|
||||
auto track2 {Database::Track::getById(session, track2Id)};
|
||||
|
||||
if (track1->getRelease() == track2->getRelease())
|
||||
score += 1;
|
||||
|
||||
// Artists in common
|
||||
{
|
||||
auto track1ArtistIds {track1->getArtistIds()};
|
||||
auto track2ArtistIds {track2->getArtistIds()};
|
||||
|
||||
std::vector<Database::IdType> commonArtistIds;
|
||||
std::set_intersection(std::cbegin(track1ArtistIds), std::cend(track1ArtistIds),
|
||||
std::cbegin(track2ArtistIds), std::cend(track2ArtistIds),
|
||||
std::back_inserter(commonArtistIds));
|
||||
|
||||
score += commonArtistIds.size();
|
||||
}
|
||||
|
||||
// Clusters in common
|
||||
{
|
||||
auto track1ClusterIds {track1->getClusterIds()};
|
||||
auto track2ClusterIds {track2->getClusterIds()};
|
||||
|
||||
std::vector<Database::IdType> commonClusterIds;
|
||||
std::set_intersection(std::cbegin(track1ClusterIds), std::cend(track1ClusterIds),
|
||||
std::cbegin(track2ClusterIds), std::cend(track2ClusterIds),
|
||||
std::back_inserter(commonClusterIds));
|
||||
|
||||
score += commonClusterIds.size();
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
static
|
||||
SimilarityScore
|
||||
computeSimilarityScore(Database::Session& session, const FeatureSettingsMap& featuresSettings)
|
||||
{
|
||||
std::cout << "Compute score of: ";
|
||||
printFeatureSettingsMap(featuresSettings);
|
||||
std::cout << std::endl;
|
||||
|
||||
FeaturesSearcher::TrainSettings trainSettings;
|
||||
trainSettings.nbIterations = 10;
|
||||
trainSettings.featureSettingsMap = featuresSettings;
|
||||
FeaturesSearcher searcher {session, trainSettings};
|
||||
|
||||
const std::vector<Database::IdType> trackIds = std::invoke([&]()
|
||||
{
|
||||
auto transaction {session.createSharedTransaction()};
|
||||
return Database::Track::getAllIds(session);
|
||||
});
|
||||
|
||||
SimilarityScore score {};
|
||||
for (Database::IdType trackId : trackIds)
|
||||
{
|
||||
// std::cout << "Processing track '" << trackToString(session, trackId) << "'" << std::endl;
|
||||
SimilarityScore factor {1};
|
||||
for (Database::IdType similarTrackId : searcher.getSimilarTracks({trackId}, 3))
|
||||
{
|
||||
SimilarityScore trackScore {computeTrackScore(session, trackId, similarTrackId)};
|
||||
// std::cout << "\tScore = " << trackScore << " (*" << factor << ") with track '" << trackToString(session, similarTrackId) << "'" << std::endl;
|
||||
trackScore *= factor;
|
||||
score += trackScore;
|
||||
|
||||
factor -= (SimilarityScore {1}/3);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Total score = " << score << std::endl;
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
static
|
||||
FeatureSettingsMap
|
||||
breedFeatureSettingsMap(const FeatureSettingsMap& a, const FeatureSettingsMap& b)
|
||||
{
|
||||
FeatureSettingsMap res;
|
||||
|
||||
res.insert(std::cbegin(a), std::cend(a));
|
||||
res.insert(std::cbegin(b), std::cend(b));
|
||||
|
||||
// just kill random elements until size is good
|
||||
while (res.size() > a.size())
|
||||
{
|
||||
const auto itFeature {pickRandom(res)};
|
||||
res.erase(itFeature);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mutateFeatureSettingsMap(FeatureSettingsMap& a)
|
||||
{
|
||||
const std::size_t size {a.size()};
|
||||
// Replace one of the feature with another one, random
|
||||
a.erase(pickRandom(a));
|
||||
|
||||
while (a.size() != size)
|
||||
{
|
||||
const auto itFeatureSetting {pickRandom(featuresSettings)};
|
||||
a.emplace(itFeatureSetting->first, itFeatureSetting->second);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
// log to stdout
|
||||
ServiceProvider<Logger>::create<StreamLogger>(std::cout);
|
||||
|
||||
std::filesystem::path configFilePath {"/etc/lms.conf"};
|
||||
if (argc >= 2)
|
||||
configFilePath = std::string(argv[1], 0, 256);
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "usage: <lms_conf_file> <nb_workers>" << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const std::filesystem::path configFilePath {std::string(argv[1], 0, 256)};
|
||||
const std::size_t nbWorkers = atoi(argv[2]);
|
||||
|
||||
ServiceProvider<Config>::create(configFilePath);
|
||||
|
||||
Database::Db db {ServiceProvider<Config>::get()->getPath("working-dir") / "lms.db"};
|
||||
Database::Session session {db};
|
||||
Database::SessionPool sessionPool {db, nbWorkers};
|
||||
|
||||
/* const FeatureSettings
|
||||
// Create some random settings (i.e random population)
|
||||
std::vector<FeatureSettingsMap> initialPopulation;
|
||||
|
||||
constexpr std::size_t populationSize {100};
|
||||
constexpr std::size_t nbFeatures {5};
|
||||
|
||||
for (std::size_t i {}; i < populationSize; ++i)
|
||||
{
|
||||
{ "lowlevel.average_loudness", 1 },
|
||||
{ "lowlevel.dynamic_complexity", 1 },
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", 6 },
|
||||
{ "lowlevel.erbbands.median", 40 },
|
||||
{ "tonal.hpcp.median", 36 },
|
||||
{ "lowlevel.melbands.median", 40 },
|
||||
{ "lowlevel.barkbands.median", 27 },
|
||||
{ "lowlevel.mfcc.mean", 13 },
|
||||
{ "lowlevel.gfcc.mean", 13 },
|
||||
};
|
||||
FeatureSettingsMap settings;
|
||||
|
||||
const TrackFeaturesMap trackFeaturesMap {getAllTrackFeatures(*session)};
|
||||
while (settings.size() < nbFeatures)
|
||||
{
|
||||
const auto itFeatureSetting {pickRandom(featuresSettings)};
|
||||
settings.emplace(itFeatureSetting->first, itFeatureSetting->second);
|
||||
}
|
||||
|
||||
std::cout << "Found " << trackFeaturesMap.size() << " tracks with features!" << std::endl;*/
|
||||
initialPopulation.emplace_back(std::move(settings));
|
||||
}
|
||||
|
||||
GeneticAlgorithm<FeatureSettingsMap>::Params params;
|
||||
params.nbWorkers = nbWorkers;
|
||||
params.nbGenerations = 300;
|
||||
params.mutationProbability = 0.2;
|
||||
params.breedFunction = breedFeatureSettingsMap;
|
||||
params.mutateFunction = mutateFeatureSettingsMap;
|
||||
params.scoreFunction =
|
||||
[&](const FeatureSettingsMap& settings)
|
||||
{
|
||||
Database::SessionPool::ScopedSession scopedSession {sessionPool};
|
||||
return computeSimilarityScore(scopedSession.get(), settings);
|
||||
};
|
||||
|
||||
GeneticAlgorithm<FeatureSettingsMap> geneticAlgorithm {params};
|
||||
|
||||
std::cout << "Parameters:\n"
|
||||
<< "\tnb generations = " << params.nbGenerations << "\n"
|
||||
<< "\tpopulationSize = " << populationSize << "\n"
|
||||
<< "\tnbFeatures = " << nbFeatures << "\n"
|
||||
<< "\tmutationProbability = " << params.mutationProbability << "\n"
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "Starting simulation..." << std::endl;
|
||||
const FeatureSettingsMap selectedSettings {geneticAlgorithm.simulate(initialPopulation)};
|
||||
std::cout << "Simulation complete! Best result:" << std::endl;
|
||||
printFeatureSettingsMap(selectedSettings);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
|
||||
@@ -10,12 +10,16 @@ lms_similarity_parameters_SOURCES = \
|
||||
$(top_srcdir)/src/database/Release.cpp \
|
||||
$(top_srcdir)/src/database/ScanSettings.cpp \
|
||||
$(top_srcdir)/src/database/Session.cpp \
|
||||
$(top_srcdir)/src/database/SessionPool.cpp \
|
||||
$(top_srcdir)/src/database/SimilaritySettings.cpp \
|
||||
$(top_srcdir)/src/database/SqlQuery.cpp \
|
||||
$(top_srcdir)/src/database/Track.cpp \
|
||||
$(top_srcdir)/src/database/User.cpp \
|
||||
$(top_srcdir)/src/similarity/features/som/DataNormalizer.cpp \
|
||||
$(top_srcdir)/src/similarity/features/som/Network.cpp \
|
||||
$(top_srcdir)/src/similarity/features/SimilarityFeaturesCache.cpp \
|
||||
$(top_srcdir)/src/similarity/features/SimilarityFeaturesSearcher.cpp \
|
||||
$(top_srcdir)/src/similarity/features/SimilarityFeaturesDefs.cpp \
|
||||
$(top_srcdir)/src/utils/Config.cpp \
|
||||
$(top_srcdir)/src/utils/Logger.cpp \
|
||||
$(top_srcdir)/src/utils/StreamLogger.cpp \
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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 <functional>
|
||||
#include <thread>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
|
||||
template <typename It, typename Func>
|
||||
void parallel_foreach(std::size_t nbWorkers, It begin, It end, Func&& func)
|
||||
{
|
||||
if (nbWorkers == 0)
|
||||
throw std::runtime_error("Invalid worker count");
|
||||
|
||||
boost::asio::io_context ioContext;
|
||||
|
||||
for (It it {begin}; it != end; ++it)
|
||||
{
|
||||
auto refValue {std::ref<typename It::value_type>(*it)};
|
||||
ioContext.post([refValue, &func]() { std::cout << "EXEC FROM WORKER" << std::endl; func(refValue); std::cout << "END EXEC FROM WORKER" << std::endl; });
|
||||
}
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
for (std::size_t i {}; i < nbWorkers - 1; ++i)
|
||||
threads.emplace_back([&]() { ioContext.run(); });
|
||||
|
||||
ioContext.run();
|
||||
|
||||
for (std::thread& t : threads)
|
||||
t.join();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
* 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 <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
@@ -25,8 +44,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
const FeatureSettingsMap featuresSettings
|
||||
{
|
||||
// { "lowlevel.average_loudness", 1 },
|
||||
// { "lowlevel.dynamic_complexity", 1 },
|
||||
/* { "lowlevel.average_loudness", 1 },
|
||||
{ "lowlevel.dynamic_complexity", 1 },
|
||||
{ "lowlevel.spectral_contrast_coeffs.median", {1} },
|
||||
{ "lowlevel.erbbands.median", {1} },
|
||||
{ "tonal.hpcp.median", {1} },
|
||||
@@ -34,6 +53,41 @@ int main(int argc, char *argv[])
|
||||
{ "lowlevel.barkbands.median", {1} },
|
||||
{ "lowlevel.mfcc.mean", {1} },
|
||||
{ "lowlevel.gfcc.mean", {1} },
|
||||
*/
|
||||
{ "lowlevel.spectral_kurtosis.median", {1}},
|
||||
{ "lowlevel.spectral_kurtosis.mean", {1}},
|
||||
{ "lowlevel.spectral_complexity.var", {1}},
|
||||
{ "lowlevel.barkbands.median", {1}},
|
||||
{ "lowlevel.barkbands_kurtosis.mean", {1}},
|
||||
/*
|
||||
{"lowlevel.spectral_centroid.dvar2", {1} },
|
||||
{"lowlevel.barkbands.median", {1} },
|
||||
{ "lowlevel.barkbands.dvar", {1} },
|
||||
{ "lowlevel.spectral_complexity.min", {1} },
|
||||
{ "lowlevel.pitch_salience.dmean2", {1} },
|
||||
{ "lowlevel.spectral_contrast_valleys.dmean", {1} },
|
||||
{ "lowlevel.pitch_salience.max", {1} },
|
||||
{ "lowlevel.barkbands.mean", {1} },
|
||||
{ "lowlevel.spectral_complexity.mean", {1} },
|
||||
{ "lowlevel.dissonance.dvar", {1} },
|
||||
*/
|
||||
/*
|
||||
{ "lowlevel.spectral_energy.dvar", {1} },
|
||||
{ "lowlevel.barkbands.min", {1} },
|
||||
{ "lowlevel.spectral_centroid.median", {1} },
|
||||
{"lowlevel.barkbands_kurtosis.median", {1} },
|
||||
{"lowlevel.spectral_energy.median", {1} },
|
||||
{"lowlevel.barkbands.max", {1} },
|
||||
{"lowlevel.barkbands_spread.var", {1} },
|
||||
{"lowlevel.spectral_decrease.var", {1} },
|
||||
{"lowlevel.spectral_contrast_valleys.dmean", {1} },
|
||||
{"lowlevel.barkbands_crest.mean", {1} },
|
||||
{"lowlevel.spectral_entropy.var", {1} },
|
||||
{"lowlevel.barkbands_crest.max", {1} },
|
||||
{"lowlevel.hfc.dvar", {1} },
|
||||
{"lowlevel.barkbands_skewness.dvar2", {1} },
|
||||
{"lowlevel.spectral_centroid.max", {1} },
|
||||
*/
|
||||
};
|
||||
|
||||
std::filesystem::path configFilePath {"/etc/lms.conf"};
|
||||
@@ -47,7 +101,10 @@ int main(int argc, char *argv[])
|
||||
|
||||
std::cout << "Classifying tracks..." << std::endl;
|
||||
// may be long...
|
||||
FeaturesSearcher searcher {session, featuresSettings};
|
||||
struct FeaturesSearcher::TrainSettings trainSettings;
|
||||
trainSettings.nbIterations = 10;
|
||||
trainSettings.featureSettingsMap = featuresSettings;
|
||||
FeaturesSearcher searcher {session, trainSettings};
|
||||
std::cout << "Classifying tracks DONE" << std::endl;
|
||||
|
||||
const std::vector<Database::IdType> trackIds = std::invoke([&]()
|
||||
|
||||
Reference in New Issue
Block a user