{"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":"! pip install librosa\nimport pandas as pd\nimport os\nimport librosa\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport sklearn\nimport tensorflow as tf\nimport scipy\nimport gc\n\nfrom scipy.io import wavfile\nfrom scipy.stats import skew\nfrom tqdm import tqdm, tqdm_pandas\n\ntqdm.pandas()\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom sklearn.model_selection import GridSearchCV\n\nfrom warnings import simplefilter","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-26T19:08:02.126859Z","iopub.execute_input":"2022-05-26T19:08:02.127349Z","iopub.status.idle":"2022-05-26T19:08:19.639324Z","shell.execute_reply.started":"2022-05-26T19:08:02.127240Z","shell.execute_reply":"2022-05-26T19:08:19.638438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.641112Z","iopub.execute_input":"2022-05-26T19:08:19.641723Z","iopub.status.idle":"2022-05-26T19:08:19.647957Z","shell.execute_reply.started":"2022-05-26T19:08:19.641692Z","shell.execute_reply":"2022-05-26T19:08:19.647209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/freesound-audio-tagging/train.csv\")\ntest = pd.read_csv(\"../input/freesound-audio-tagging/sample_submission.csv\")\n\ntrain_path = '../input/freesound-audio-tagging/audio_train/'\ntest_path = '../input/freesound-audio-tagging/audio_test/'","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.649180Z","iopub.execute_input":"2022-05-26T19:08:19.651005Z","iopub.status.idle":"2022-05-26T19:08:19.697858Z","shell.execute_reply.started":"2022-05-26T19:08:19.650950Z","shell.execute_reply":"2022-05-26T19:08:19.697052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.701494Z","iopub.execute_input":"2022-05-26T19:08:19.701805Z","iopub.status.idle":"2022-05-26T19:08:19.722608Z","shell.execute_reply.started":"2022-05-26T19:08:19.701777Z","shell.execute_reply":"2022-05-26T19:08:19.721892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.723940Z","iopub.execute_input":"2022-05-26T19:08:19.724307Z","iopub.status.idle":"2022-05-26T19:08:19.749500Z","shell.execute_reply.started":"2022-05-26T19:08:19.724270Z","shell.execute_reply":"2022-05-26T19:08:19.748486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.750969Z","iopub.execute_input":"2022-05-26T19:08:19.751916Z","iopub.status.idle":"2022-05-26T19:08:19.767358Z","shell.execute_reply.started":"2022-05-26T19:08:19.751874Z","shell.execute_reply":"2022-05-26T19:08:19.766270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[:7000]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.768684Z","iopub.execute_input":"2022-05-26T19:08:19.769131Z","iopub.status.idle":"2022-05-26T19:08:19.773628Z","shell.execute_reply.started":"2022-05-26T19:08:19.769091Z","shell.execute_reply":"2022-05-26T19:08:19.772553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"def audio_magic(fileName, path):\n    data, _ = librosa.core.load(path + fileName, sr = 44100)\n    try:\n        mfcc = librosa.feature.mfcc(data, sr = 44100, n_mfcc=20)\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        rolloff = librosa.feature.spectral_rolloff(data)[0]\n        spectral_centroid = librosa.feature.spectral_centroid(data)[0]\n        spectral_bandwidth = librosa.feature.spectral_bandwidth(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        return pd.Series(np.hstack((mfcc_trunc, rms, chroma_stft, zcr, rolloff, spectral_centroid, spectral_bandwidth)))\n    except:\n        print('Error with audio')\n        return pd.Series([0]*20)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:26:29.045423Z","iopub.execute_input":"2022-05-26T19:26:29.046064Z","iopub.status.idle":"2022-05-26T19:26:29.054997Z","shell.execute_reply.started":"2022-05-26T19:26:29.046028Z","shell.execute_reply":"2022-05-26T19:26:29.054244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame()\ntrain_df['fname'] = train['fname']\ntrain_df = train_df['fname'].progress_apply(audio_magic, path=train_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:08:19.803952Z","iopub.execute_input":"2022-05-26T19:08:19.804404Z","iopub.status.idle":"2022-05-26T19:11:46.428708Z","shell.execute_reply.started":"2022-05-26T19:08:19.804364Z","shell.execute_reply":"2022-05-26T19:11:46.427820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:11:46.429985Z","iopub.execute_input":"2022-05-26T19:11:46.430620Z","iopub.status.idle":"2022-05-26T19:11:46.453235Z","shell.execute_reply.started":"2022-05-26T19:11:46.430581Z","shell.execute_reply":"2022-05-26T19:11:46.452417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.iloc[:, 0:20]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:10.060316Z","iopub.execute_input":"2022-05-26T19:20:10.060688Z","iopub.status.idle":"2022-05-26T19:20:10.065220Z","shell.execute_reply.started":"2022-05-26T19:20:10.060656Z","shell.execute_reply":"2022-05-26T19:20:10.064384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['fname'] = train['fname']\ntrain_df['label'] = train['label']\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:21.814748Z","iopub.execute_input":"2022-05-26T19:20:21.815648Z","iopub.status.idle":"2022-05-26T19:20:21.841899Z","shell.execute_reply.started":"2022-05-26T19:20:21.815601Z","shell.execute_reply":"2022-05-26T19:20:21.841153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = train_df.drop(['fname', 'label'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:28.999581Z","iopub.execute_input":"2022-05-26T19:20:29.000022Z","iopub.status.idle":"2022-05-26T19:20:29.006738Z","shell.execute_reply.started":"2022-05-26T19:20:28.999980Z","shell.execute_reply":"2022-05-26T19:20:29.005830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genre_list = train_df.iloc[:, -1]\nencoder = LabelEncoder()\ny = encoder.fit_transform(genre_list)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:31.735741Z","iopub.execute_input":"2022-05-26T19:20:31.736096Z","iopub.status.idle":"2022-05-26T19:20:31.741086Z","shell.execute_reply.started":"2022-05-26T19:20:31.736068Z","shell.execute_reply":"2022-05-26T19:20:31.740317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX = scaler.fit_transform(np.array(X_data.iloc[:, :-1], dtype = float))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:33.087296Z","iopub.execute_input":"2022-05-26T19:20:33.087774Z","iopub.status.idle":"2022-05-26T19:20:33.098045Z","shell.execute_reply.started":"2022-05-26T19:20:33.087731Z","shell.execute_reply":"2022-05-26T19:20:33.097186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:20:35.284588Z","iopub.execute_input":"2022-05-26T19:20:35.285320Z","iopub.status.idle":"2022-05-26T19:20:35.290669Z","shell.execute_reply.started":"2022-05-26T19:20:35.285281Z","shell.execute_reply":"2022-05-26T19:20:35.289516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model building","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n  tf.keras.layers.Dense(512, activation='relu', input_shape=(X_train.shape[1],)),\n  tf.keras.layers.Dropout(0.2),\n  tf.keras.layers.Dense(256, activation='relu'),\n  tf.keras.layers.Dropout(0.2),\n  tf.keras.layers.Dense(41, activation='softmax')\n])\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\nmodel.fit(\n    X_train,\n    y_train,\n    epochs=50,\n    batch_size=256,\n    validation_data=(X_val, y_val)\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:21:15.095362Z","iopub.execute_input":"2022-05-26T19:21:15.096191Z","iopub.status.idle":"2022-05-26T19:21:22.432153Z","shell.execute_reply.started":"2022-05-26T19:21:15.096152Z","shell.execute_reply":"2022-05-26T19:21:22.431393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test data","metadata":{}},{"cell_type":"code","source":"test_df = pd.DataFrame()\ntest_df['fname'] = test['fname']\ntest_df = test_df['fname'].progress_apply(audio_magic, path=test_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:26:35.584642Z","iopub.execute_input":"2022-05-26T19:26:35.585259Z","iopub.status.idle":"2022-05-26T19:27:52.728249Z","shell.execute_reply.started":"2022-05-26T19:26:35.585222Z","shell.execute_reply":"2022-05-26T19:27:52.727463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.iloc[:, 0:20]","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:27:56.366462Z","iopub.execute_input":"2022-05-26T19:27:56.367130Z","iopub.status.idle":"2022-05-26T19:27:56.372012Z","shell.execute_reply.started":"2022-05-26T19:27:56.367091Z","shell.execute_reply":"2022-05-26T19:27:56.370830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['fname'] = test['fname']\ntest_df['label'] = np.zeros((len(test)))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:27:59.726056Z","iopub.execute_input":"2022-05-26T19:27:59.726422Z","iopub.status.idle":"2022-05-26T19:27:59.732344Z","shell.execute_reply.started":"2022-05-26T19:27:59.726392Z","shell.execute_reply":"2022-05-26T19:27:59.731651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:01.085203Z","iopub.execute_input":"2022-05-26T19:28:01.085785Z","iopub.status.idle":"2022-05-26T19:28:01.124354Z","shell.execute_reply.started":"2022-05-26T19:28:01.085742Z","shell.execute_reply":"2022-05-26T19:28:01.123590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:04.739016Z","iopub.execute_input":"2022-05-26T19:28:04.739569Z","iopub.status.idle":"2022-05-26T19:28:04.751682Z","shell.execute_reply.started":"2022-05-26T19:28:04.739515Z","shell.execute_reply":"2022-05-26T19:28:04.750908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_data = test_df.drop(['fname','label'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:15.196366Z","iopub.execute_input":"2022-05-26T19:28:15.197308Z","iopub.status.idle":"2022-05-26T19:28:15.203748Z","shell.execute_reply.started":"2022-05-26T19:28:15.197262Z","shell.execute_reply":"2022-05-26T19:28:15.203007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_s = scaler.fit_transform(np.array(X_test_data.iloc[:, :-1], dtype = float))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:18.030128Z","iopub.execute_input":"2022-05-26T19:28:18.030501Z","iopub.status.idle":"2022-05-26T19:28:18.038133Z","shell.execute_reply.started":"2022-05-26T19:28:18.030471Z","shell.execute_reply":"2022-05-26T19:28:18.037420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"preds = model.predict(X_test_s)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:19.514082Z","iopub.execute_input":"2022-05-26T19:28:19.514433Z","iopub.status.idle":"2022-05-26T19:28:19.670335Z","shell.execute_reply.started":"2022-05-26T19:28:19.514404Z","shell.execute_reply":"2022-05-26T19:28:19.669572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('sound.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:20.646653Z","iopub.execute_input":"2022-05-26T19:28:20.647406Z","iopub.status.idle":"2022-05-26T19:28:20.680007Z","shell.execute_reply.started":"2022-05-26T19:28:20.647366Z","shell.execute_reply":"2022-05-26T19:28:20.679273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = np.max(preds,axis=1)\nscore = score.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:24.654985Z","iopub.execute_input":"2022-05-26T19:28:24.655329Z","iopub.status.idle":"2022-05-26T19:28:24.659960Z","shell.execute_reply.started":"2022-05-26T19:28:24.655301Z","shell.execute_reply":"2022-05-26T19:28:24.658858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = np.argmax(preds, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:26.129280Z","iopub.execute_input":"2022-05-26T19:28:26.129836Z","iopub.status.idle":"2022-05-26T19:28:26.138486Z","shell.execute_reply.started":"2022-05-26T19:28:26.129791Z","shell.execute_reply":"2022-05-26T19:28:26.137780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = np.argmax(preds, axis=1)\nsubmission = pd.DataFrame()\nsubmission['fname'] = test['fname']\nsubmission['label'] = encoder.inverse_transform(output)\nsubmission.to_csv('./submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T19:28:42.388325Z","iopub.execute_input":"2022-05-26T19:28:42.388701Z","iopub.status.idle":"2022-05-26T19:28:42.400319Z","shell.execute_reply.started":"2022-05-26T19:28:42.388673Z","shell.execute_reply":"2022-05-26T19:28:42.399470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{},"execution_count":null,"outputs":[]}]}