{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-21T12:24:52.309051Z","iopub.execute_input":"2021-12-21T12:24:52.309442Z","iopub.status.idle":"2021-12-21T12:25:11.974381Z","shell.execute_reply.started":"2021-12-21T12:24:52.309312Z","shell.execute_reply":"2021-12-21T12:25:11.973212Z"},"_kg_hide-input":true,"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport librosa\n\nimport scipy\nfrom scipy.stats import skew\nfrom tqdm import tqdm, tqdm_pandas\n\ntqdm.pandas()\n\n# Preprocessing\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-12-21T12:25:59.690220Z","iopub.execute_input":"2021-12-21T12:25:59.690602Z","iopub.status.idle":"2021-12-21T12:26:02.286791Z","shell.execute_reply.started":"2021-12-21T12:25:59.690559Z","shell.execute_reply":"2021-12-21T12:26:02.285674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/freesound-audio-tagging/train.csv')\nsample_submission = pd.read_csv('../input/freesound-audio-tagging/sample_submission.csv')\n\n# Path to files\ntrain_path = '../input/freesound-audio-tagging/audio_train/'\ntest_path = '../input/freesound-audio-tagging/audio_test/'\n\ntrain_audio = os.listdir(train_path)\ntest_audio = os.listdir(test_path)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T12:26:57.223023Z","iopub.execute_input":"2021-12-21T12:26:57.223387Z","iopub.status.idle":"2021-12-21T12:26:57.292715Z","shell.execute_reply.started":"2021-12-21T12:26:57.223340Z","shell.execute_reply":"2021-12-21T12:26:57.291506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data preprocessing\nGenerate features with mean and standard deviation as the value input to the model.","metadata":{}},{"cell_type":"code","source":"SAMPLE_RATE = 44100\n\ndef clean_filename(fname, string):   \n    file_name = fname.split('/')[1]\n    if file_name[:2] == '__':        \n        file_name = string + file_name\n    return file_name\n\n# Generate features with mean and standard deviation\ndef get_mfcc(name, path):\n    data, _ = librosa.core.load(path + name, sr = SAMPLE_RATE)\n    assert _ == SAMPLE_RATE\n    try:\n        mfcc = librosa.feature.mfcc(data, sr = SAMPLE_RATE, n_mfcc=30)\n        rms = librosa.feature.rms(data)[0]\n        chroma_stft = librosa.feature.chroma_stft(data)[0]\n        zcr = librosa.feature.zero_crossing_rate(data)[0]\n        spectral_rolloff = librosa.feature.spectral_rolloff(data)[0]\n        spectral_centroid = librosa.feature.spectral_centroid(data)[0]\n        spectral_contrast = librosa.feature.spectral_contrast(data)[0]\n        spectral_bandwidth = librosa.feature.spectral_bandwidth(data)[0]\n        spectral_flatness = librosa.feature.spectral_flatness(data)[0]                                  \n        mfcc_trunc = np.hstack((np.mean(mfcc, axis=1), np.std(mfcc, axis=1), skew(mfcc, axis = 1), np.max(mfcc, axis = 1), np.median(mfcc, axis = 1), np.min(mfcc, axis = 1)))\n        rms_trunc = np.hstack((np.mean(rms), np.std(rms), skew(rms), np.max(rms), np.median(rms), np.min(rms)))\n        chroma_stft_trunc = np.hstack((np.mean(chroma_stft), np.std(chroma_stft), skew(chroma_stft), np.max(chroma_stft), np.median(chroma_stft), np.min(chroma_stft)))\n        zcr_trunc = np.hstack((np.mean(zcr), np.std(zcr), skew(zcr), np.max(zcr), np.median(zcr), np.min(zcr)))\n        spectral_rolloff_trunc = np.hstack((np.mean(spectral_rolloff), np.std(spectral_rolloff), skew(spectral_rolloff), np.max(spectral_rolloff), np.median(spectral_rolloff), np.min(spectral_rolloff)))\n        spectral_centroid_trunc = np.hstack((np.mean(spectral_centroid), np.std(spectral_centroid), skew(spectral_centroid), np.max(spectral_centroid), np.median(spectral_centroid), np.min(spectral_centroid)))\n        spectral_contrast_trunc = np.hstack((np.mean(spectral_contrast), np.std(spectral_contrast), skew(spectral_contrast), np.max(spectral_contrast), np.median(spectral_contrast), np.min(spectral_contrast)))\n        spectral_bandwidth_trunc = np.hstack((np.mean(spectral_bandwidth), np.std(spectral_bandwidth), skew(spectral_bandwidth), np.max(spectral_bandwidth), np.median(spectral_bandwidth), np.min(spectral_bandwidth)))\n        spectral_flatness_trunc = np.hstack((np.mean(spectral_flatness), np.std(spectral_flatness), skew(spectral_flatness), np.max(spectral_flatness), np.median(spectral_flatness), np.min(spectral_flatness)))\n        \n        return pd.Series(np.hstack((mfcc_trunc, rms_trunc, chroma_stft_trunc, zcr_trunc, spectral_rolloff_trunc, spectral_centroid_trunc, spectral_contrast_trunc, spectral_bandwidth_trunc, spectral_flatness_trunc)))\n    except:\n        print('bad file')\n        return pd.Series([0]*210)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T12:27:07.332964Z","iopub.execute_input":"2021-12-21T12:27:07.333305Z","iopub.status.idle":"2021-12-21T12:27:07.358122Z","shell.execute_reply.started":"2021-12-21T12:27:07.333271Z","shell.execute_reply":"2021-12-21T12:27:07.356791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.DataFrame()\ntrain_data['fname'] = train['fname']\ntrain_data = train_data['fname'].progress_apply(get_mfcc, path='../input/freesound-audio-tagging/audio_train/')\nprint('done loading train mfcc')","metadata":{"execution":{"iopub.status.busy":"2021-12-21T12:27:30.974044Z","iopub.execute_input":"2021-12-21T12:27:30.974580Z","iopub.status.idle":"2021-12-21T13:17:52.029319Z","shell.execute_reply.started":"2021-12-21T12:27:30.974533Z","shell.execute_reply":"2021-12-21T13:17:52.028405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['fname'] = train['fname']\ntrain_data['label'] = train['label']\ntrain_data.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.to_csv('../input/testdata/kaggle_train.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train Data","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv('../input/testdata/kaggle_train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:17:56.326739Z","iopub.execute_input":"2021-12-21T13:17:56.327115Z","iopub.status.idle":"2021-12-21T13:17:57.667712Z","shell.execute_reply.started":"2021-12-21T13:17:56.327075Z","shell.execute_reply":"2021-12-21T13:17:57.666694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dropping unneccesary columns\nX_train = train_data.drop(['fname','label'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:17:59.709064Z","iopub.execute_input":"2021-12-21T13:17:59.710149Z","iopub.status.idle":"2021-12-21T13:17:59.728451Z","shell.execute_reply.started":"2021-12-21T13:17:59.710081Z","shell.execute_reply":"2021-12-21T13:17:59.727439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genre_list = train_data.iloc[:, -1]\nencoder = LabelEncoder()\ny = encoder.fit_transform(genre_list)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:18:01.981148Z","iopub.execute_input":"2021-12-21T13:18:01.981769Z","iopub.status.idle":"2021-12-21T13:18:01.993117Z","shell.execute_reply.started":"2021-12-21T13:18:01.981720Z","shell.execute_reply":"2021-12-21T13:18:01.992060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX = scaler.fit_transform(np.array(X_train.iloc[:, :-1], dtype = float))","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:18:03.565845Z","iopub.execute_input":"2021-12-21T13:18:03.566209Z","iopub.status.idle":"2021-12-21T13:18:03.644127Z","shell.execute_reply.started":"2021-12-21T13:18:03.566171Z","shell.execute_reply":"2021-12-21T13:18:03.643132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test Data","metadata":{}},{"cell_type":"code","source":"test_data = pd.DataFrame()\ntest_data['fname'] = test_audio\ntest_data = test_data['fname'].progress_apply(get_mfcc, path='../input/freesound-audio-tagging/audio_test/')\nprint('done loading test mfcc')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['fname'] = test_audio\ntest_data['label'] = np.zeros((len(test_audio)))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.to_csv('../input/testdata/kaggle_test.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv('../input/testdata/kaggle_test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:18:21.489339Z","iopub.execute_input":"2021-12-21T13:18:21.489693Z","iopub.status.idle":"2021-12-21T13:18:22.677295Z","shell.execute_reply.started":"2021-12-21T13:18:21.489659Z","shell.execute_reply":"2021-12-21T13:18:22.676561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data.drop(['fname','label'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:19:09.228955Z","iopub.execute_input":"2021-12-21T13:19:09.230115Z","iopub.status.idle":"2021-12-21T13:19:09.240707Z","shell.execute_reply.started":"2021-12-21T13:19:09.230054Z","shell.execute_reply":"2021-12-21T13:19:09.239819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(np.array(X_train.iloc[:, :-1], dtype = float))\nX_test_scaled = scaler.fit_transform(np.array(X_test.iloc[:, :-1], dtype = float))","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:22:45.434328Z","iopub.execute_input":"2021-12-21T13:22:45.435575Z","iopub.status.idle":"2021-12-21T13:22:45.555522Z","shell.execute_reply.started":"2021-12-21T13:22:45.435529Z","shell.execute_reply":"2021-12-21T13:22:45.554613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split the data into training and test sets","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train_scaled, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:22:47.597853Z","iopub.execute_input":"2021-12-21T13:22:47.598421Z","iopub.status.idle":"2021-12-21T13:22:47.624773Z","shell.execute_reply.started":"2021-12-21T13:22:47.598376Z","shell.execute_reply":"2021-12-21T13:22:47.623765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:22:49.277983Z","iopub.execute_input":"2021-12-21T13:22:49.278284Z","iopub.status.idle":"2021-12-21T13:22:49.287172Z","shell.execute_reply.started":"2021-12-21T13:22:49.278244Z","shell.execute_reply":"2021-12-21T13:22:49.286225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_val)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:22:51.044800Z","iopub.execute_input":"2021-12-21T13:22:51.045066Z","iopub.status.idle":"2021-12-21T13:22:51.050775Z","shell.execute_reply.started":"2021-12-21T13:22:51.045036Z","shell.execute_reply":"2021-12-21T13:22:51.050053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"partial_x_train, x_test, partial_y_train, y_test = train_test_split(X_train, y_train, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:22:54.592884Z","iopub.execute_input":"2021-12-21T13:22:54.593373Z","iopub.status.idle":"2021-12-21T13:22:54.608641Z","shell.execute_reply.started":"2021-12-21T13:22:54.593322Z","shell.execute_reply":"2021-12-21T13:22:54.607773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create model","metadata":{}},{"cell_type":"code","source":"from keras import models\nfrom keras import optimizers\nfrom keras.layers import Dense, Dropout\n\nmodel = models.Sequential()\nmodel.add(Dense(512, activation='relu', input_shape=(partial_x_train.shape[1],)))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(41, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:27:30.933005Z","iopub.execute_input":"2021-12-21T13:27:30.933387Z","iopub.status.idle":"2021-12-21T13:27:37.614768Z","shell.execute_reply.started":"2021-12-21T13:27:30.933337Z","shell.execute_reply":"2021-12-21T13:27:37.612780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:27:45.976448Z","iopub.execute_input":"2021-12-21T13:27:45.976771Z","iopub.status.idle":"2021-12-21T13:27:45.993064Z","shell.execute_reply.started":"2021-12-21T13:27:45.976736Z","shell.execute_reply":"2021-12-21T13:27:45.992066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(partial_x_train,\n          partial_y_train,\n          epochs=35,\n          batch_size=256,\n          validation_data=(X_val, y_val))\nresults = model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:27:50.037725Z","iopub.execute_input":"2021-12-21T13:27:50.038393Z","iopub.status.idle":"2021-12-21T13:28:11.374735Z","shell.execute_reply.started":"2021-12-21T13:27:50.038314Z","shell.execute_reply":"2021-12-21T13:28:11.373593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Result of training","metadata":{}},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:28:11.376675Z","iopub.execute_input":"2021-12-21T13:28:11.376909Z","iopub.status.idle":"2021-12-21T13:28:11.384248Z","shell.execute_reply.started":"2021-12-21T13:28:11.376883Z","shell.execute_reply":"2021-12-21T13:28:11.383262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict Test Data","metadata":{}},{"cell_type":"code","source":"predictions=model.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:28:22.149143Z","iopub.execute_input":"2021-12-21T13:28:22.149636Z","iopub.status.idle":"2021-12-21T13:28:22.698132Z","shell.execute_reply.started":"2021-12-21T13:28:22.149594Z","shell.execute_reply":"2021-12-21T13:28:22.697434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('kaggle_sound_2.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = np.max(predictions,axis=1)\nscore= score.tolist()","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:28:28.278713Z","iopub.execute_input":"2021-12-21T13:28:28.279168Z","iopub.status.idle":"2021-12-21T13:28:28.285362Z","shell.execute_reply.started":"2021-12-21T13:28:28.279119Z","shell.execute_reply":"2021-12-21T13:28:28.284749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = np.argmax(predictions, axis=1)\noutput","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:28:30.412194Z","iopub.execute_input":"2021-12-21T13:28:30.412782Z","iopub.status.idle":"2021-12-21T13:28:30.420339Z","shell.execute_reply.started":"2021-12-21T13:28:30.412732Z","shell.execute_reply":"2021-12-21T13:28:30.419519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = np.argmax(predictions, axis=1)\nsubm = pd.DataFrame()\nsubm['fname'] = test_audio\nsubm['label'] = encoder.inverse_transform(output)\nsubm.to_csv('./submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-21T13:31:04.693289Z","iopub.execute_input":"2021-12-21T13:31:04.693608Z","iopub.status.idle":"2021-12-21T13:31:04.733318Z","shell.execute_reply.started":"2021-12-21T13:31:04.693574Z","shell.execute_reply":"2021-12-21T13:31:04.732470Z"},"trusted":true},"execution_count":null,"outputs":[]}]}