{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Finding Similar Songs - Part 1: Distance Based Search\n", "\n", "The first part of this tutorial series demonstrates a distance based similarity search approach, based on extracted content descriptors and distance metrics. \n", "\n", "## Part 1 - Overview\n", "\n", "1. Introductions & Requirements\n", "2. Loading data\n", "3. Preprocess data\n", "4. Define the Similarity Model\n", "5. Optimize the Model\n", "\n", "# Short Introduction to Music Similarity Retrieval\n", "\n", "The objective of Music Similarity estimation or retrieval is to estimate the notion of similarity between two given tracks. A central part of such an approaches is the definition of a measure for similarity which is further affected by the approach taken to extract the relevant information. One approach is to analyze contextual data such as user generated listening behaviour data (e.g. play/skip-counts, user-tags, ratings, etc.). The approach followed by this tutorial is based on the music content itself and largely focuses on the notion of *acoustic similarity*. Music features are extracted from the audio content. The resulting music descriptors are high-dimensional numeric vectors and the accumulation of all feature vectors of a collection forms a vector-space. The general principle of content based similarity estimations is based on the assumption that numerical differences are an expression of perceptual dissimilarity. Different metrics such as the Manhattan (L1) or the Euclidean Distance (L2) or non-metric similarity functions such as the Kullback-Leibler divergence are used to estimate the numerical similarity of the feature vectors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Requirements\n", "\n", "Please follow the instructions on the tutorial's Github page to install the following dependencies to run this tutorial:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2017-08-24T10:20:32.488000Z", "start_time": "2017-08-24T10:20:32.483000Z" } }, "outputs": [], "source": [ "# visualization\n", "%matplotlib inline\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "matplotlib.style.use('ggplot')\n", "\n", "# numeric and scientific processing\n", "import numpy as np\n", "import pandas as pd\n", "\n", "# misc\n", "import os\n", "import progressbar\n", "\n", "from IPython.display import HTML, display\n", "pd.set_option('display.max_colwidth', -1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loading Data\n", "\n", "Before we can train our models we first have to get some data." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "DATASET_PATH = \"D:/Research/Data/MIR/MagnaTagATune/ISMIR2018\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Feature Data\n", "\n", "load feature data from numpy pickle" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "with np.load(\"%s/ISMIR2018_tut_Magnagtagatune_rp_features.npz\" % DATASET_PATH) as npz:\n", " features_rp = npz[\"rp\"]\n", " features_ssd = npz[\"ssd\"]\n", " clip_id = npz[\"clip_id\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "prepare feature-metadata for alignment with dataset meta-data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "feature_metadata = pd.DataFrame({\"featurespace_id\": np.arange(features_rp.shape[0]), \n", " \"clip_id\" : clip_id})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Metadata\n", "\n", "load meta-data from csv-file." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
clip_idmp3_pathtrack_numbertitleartistalbumurlsegmentStartsegmentEndoriginal_url
1920042150D:/Research/Data/MIR/MagnaTagATune/mp3_full/f/professor_armchair-too_much_mustard-10-bethena-117-146.mp310BethenaProfessor ArmchairToo Much Mustardhttp://www.magnatune.com/artists/albums/armchair-octopants/117146http://he3.magnatune.com/all/10-Bethena-Professor%20Armchair.mp3
774516994D:/Research/Data/MIR/MagnaTagATune/mp3_full/c/liquid_zen-seventythree-04-close-262-291.mp34CloseLiquid ZenSeventythreehttp://www.magnatune.com/artists/albums/liquid-seventythree/262291http://he3.magnatune.com/all/04-Close%20-%20Liquid%20Zen.mp3
2541657455D:/Research/Data/MIR/MagnaTagATune/mp3_full/6/doc_rossi-demarzi6_sonatas_for_cetra_o_kitara-23-sonata_vi_allegro-59-88.mp323Sonata VI AllegroDoc RossiDemarzi-6 Sonatas for Cetra o Kitarahttp://www.magnatune.com/artists/albums/rossi-demarzi/5988http://he3.magnatune.com/all/23-Sonata%20VI%20Allegro-Doc%20Rossi.mp3
6131473D:/Research/Data/MIR/MagnaTagATune/mp3_full/b/philharmonia_baroque-beethoven_symphonies_no_3_eroica_and_no_8-01-eroica_1st-117-146.mp31Eroica 1stPhilharmonia BaroqueBeethoven Symphonies No 3 Eroica and No 8http://www.magnatune.com/artists/albums/pb-eroica/117146http://he3.magnatune.com/all/01-Eroica%201st-Philharmonia%20Baroque.mp3
1509533011D:/Research/Data/MIR/MagnaTagATune/mp3_full/0/barbara_leoni-human_needs-07-ring_around_the_rosey-88-117.mp37Ring around the roseyBarbara LeoniHuman Needshttp://www.magnatune.com/artists/albums/leoni-human/88117http://he3.magnatune.com/all/07-Ring%20around%20the%20rosey-Barbara%20Leoni.mp3
\n", "
" ], "text/plain": [ " clip_id \\\n", "19200 42150 \n", "7745 16994 \n", "25416 57455 \n", "613 1473 \n", "15095 33011 \n", "\n", " mp3_path \\\n", "19200 D:/Research/Data/MIR/MagnaTagATune/mp3_full/f/professor_armchair-too_much_mustard-10-bethena-117-146.mp3 \n", "7745 D:/Research/Data/MIR/MagnaTagATune/mp3_full/c/liquid_zen-seventythree-04-close-262-291.mp3 \n", "25416 D:/Research/Data/MIR/MagnaTagATune/mp3_full/6/doc_rossi-demarzi6_sonatas_for_cetra_o_kitara-23-sonata_vi_allegro-59-88.mp3 \n", "613 D:/Research/Data/MIR/MagnaTagATune/mp3_full/b/philharmonia_baroque-beethoven_symphonies_no_3_eroica_and_no_8-01-eroica_1st-117-146.mp3 \n", "15095 D:/Research/Data/MIR/MagnaTagATune/mp3_full/0/barbara_leoni-human_needs-07-ring_around_the_rosey-88-117.mp3 \n", "\n", " track_number title artist \\\n", "19200 10 Bethena Professor Armchair \n", "7745 4 Close Liquid Zen \n", "25416 23 Sonata VI Allegro Doc Rossi \n", "613 1 Eroica 1st Philharmonia Baroque \n", "15095 7 Ring around the rosey Barbara Leoni \n", "\n", " album \\\n", "19200 Too Much Mustard \n", "7745 Seventythree \n", "25416 Demarzi-6 Sonatas for Cetra o Kitara \n", "613 Beethoven Symphonies No 3 Eroica and No 8 \n", "15095 Human Needs \n", "\n", " url \\\n", "19200 http://www.magnatune.com/artists/albums/armchair-octopants/ \n", "7745 http://www.magnatune.com/artists/albums/liquid-seventythree/ \n", "25416 http://www.magnatune.com/artists/albums/rossi-demarzi/ \n", "613 http://www.magnatune.com/artists/albums/pb-eroica/ \n", "15095 http://www.magnatune.com/artists/albums/leoni-human/ \n", "\n", " segmentStart segmentEnd \\\n", "19200 117 146 \n", "7745 262 291 \n", "25416 59 88 \n", "613 117 146 \n", "15095 88 117 \n", "\n", " original_url \n", "19200 http://he3.magnatune.com/all/10-Bethena-Professor%20Armchair.mp3 \n", "7745 http://he3.magnatune.com/all/04-Close%20-%20Liquid%20Zen.mp3 \n", "25416 http://he3.magnatune.com/all/23-Sonata%20VI%20Allegro-Doc%20Rossi.mp3 \n", "613 http://he3.magnatune.com/all/01-Eroica%201st-Philharmonia%20Baroque.mp3 \n", "15095 http://he3.magnatune.com/all/07-Ring%20around%20the%20rosey-Barbara%20Leoni.mp3 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metadata = pd.read_csv(\"./metadata/ismir2018_tut_part_3_similartiy_metadata.csv\", index_col=0)\n", "metadata.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Align featuredata with metadata" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "metadata = metadata.reset_index()\n", "metadata = metadata.merge(feature_metadata, left_on=\"clip_id\", right_on=\"clip_id\", how=\"inner\", left_index=True, right_index=False)\n", "metadata = metadata.set_index(\"index\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Add Media-Player to Metadata**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "tmp = metadata.mp3_path.str.split(\"/\", expand=True)\n", "metadata[\"player\"] = '