{"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":"import os\nimport shutil\n\nimport IPython\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom tqdm import tqdm_notebook\nfrom sklearn.preprocessing import LabelEncoder\nimport librosa\nimport numpy as np\nimport scipy\nimport glob\nfrom tqdm.notebook import tqdm\nfrom sklearn.model_selection import train_test_split\nimport pickle\nfrom scipy.stats import skew, kurtosis\nimport lightgbm as lgb\nimport soundfile as sf\nfrom pydub import AudioSegment\nimport pydub.silence as silence\nfrom pydub.exceptions import CouldntDecodeError\nimport signal\nimport scipy.signal\nfrom sklearn.model_selection import StratifiedKFold \nimport torchvision.models as models\n\n%matplotlib inline\nmatplotlib.style.use('ggplot')\n\n\nfrom sklearn.metrics import accuracy_score, f1_score, classification_report\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport random\n\nrandom.seed(42)\nnp.random.seed(42)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:06:33.078121Z","iopub.execute_input":"2022-06-23T18:06:33.078610Z","iopub.status.idle":"2022-06-23T18:06:38.248555Z","shell.execute_reply.started":"2022-06-23T18:06:33.078512Z","shell.execute_reply":"2022-06-23T18:06:38.247393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Парсим пути и применяем LabelEncoder","metadata":{}},{"cell_type":"code","source":"path_train = '../input/itmo-acoustic-event-detection-2022/audio_train/train'\nfilepath_train = glob.glob(path_train + \"/*\")\nname_wav = [x.split('/')[-1] for x in filepath_train]\ndf_label = pd.read_csv('../input/itmo-acoustic-event-detection-2022/train.csv')\ndf_label.set_index('fname', inplace=True)\ndf_label.loc[name_wav, 'filepath'] = filepath_train\ny_train = df_label.label\nle = LabelEncoder()\ny_train = le.fit_transform(y_train)\nfilepath_train = np.array(df_label.filepath)\n\npath_test = '../input/itmo-acoustic-event-detection-2022/audio_test/test'\nfilepath_test = glob.glob(path_test + \"/*\")\nname_wav_test = [x.split('/')[-1] for x in filepath_test]\ndf_test = pd.DataFrame({'filepath_test': filepath_test, 'fname': name_wav_test})\ndf_test.set_index('fname', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:06:41.442329Z","iopub.execute_input":"2022-06-23T18:06:41.443527Z","iopub.status.idle":"2022-06-23T18:06:42.231479Z","shell.execute_reply.started":"2022-06-23T18:06:41.443467Z","shell.execute_reply":"2022-06-23T18:06:42.230273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Features","metadata":{}},{"cell_type":"markdown","source":"Просто комбинация различных признаков на вавках с обрезанной тишиной в начале и конце (trim), и в середине (split)","metadata":{}},{"cell_type":"code","source":"def compute_features_simple(filepath, n_mfcc=20):\n    features = []\n    for x in tqdm(filepath):\n        data, fs = librosa.core.load(x, sr=None)\n        length = len(data)\n        data, _ = librosa.effects.trim(data, top_db=40)\n        length_int = len(data)\n        ratio_int = length_int/length\n        splits = librosa.effects.split(data, top_db=40)\n        if len(splits) > 1:\n            data = np.concatenate([data[x[0]:x[1]] for x in splits])    \n        length_final = len(data)\n        ratio_final = length_final/length_int  \n        \n#         feature_names = []\n#         for i in ['mean', 'std', 'min', 'max', 'skew', 'kurt']:\n#             for j in range(n_mfcc):\n#                 feature_names.append('mfcc_{}_{}'.format(j, i))\n#             feature_names.append('centroid_{}'.format(i))\n#             feature_names.append('bandwidth_{}'.format(i))\n#             feature_names.append('contrast_{}'.format(i))\n#             feature_names.append('rolloff_{}'.format(i))\n#             feature_names.append('flatness_{}'.format(i))\n#             feature_names.append('zcr_{}'.format(i))\n\n        spectral_features = [\n            librosa.feature.spectral_centroid,\n            librosa.feature.spectral_bandwidth,\n            librosa.feature.spectral_contrast,\n            librosa.feature.spectral_rolloff,\n            librosa.feature.spectral_flatness,\n            librosa.feature.zero_crossing_rate]\n        M = librosa.feature.mfcc(data, sr=fs, n_mfcc=n_mfcc)\n        data_row = np.hstack((np.mean(M, axis=1), np.std(M, axis=1), np.min(M, axis=1), np.max(M, axis=1), skew(M, axis=1), kurtosis(M, axis=1)))\n        for feat in spectral_features:\n            S = feat(data)[0]\n            data_row = np.hstack((data_row, np.mean(S), np.std(S), np.min(S), np.max(S), skew(S), kurtosis(S)))\n        features.append(data_row)\n    return features\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:06:47.642462Z","iopub.execute_input":"2022-06-23T18:06:47.643180Z","iopub.status.idle":"2022-06-23T18:06:47.658192Z","shell.execute_reply.started":"2022-06-23T18:06:47.643131Z","shell.execute_reply":"2022-06-23T18:06:47.657296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_train = compute_features_simple(filepath_train)\nfeatures_val = compute_features_simple(filepath_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T20:52:31.713191Z","iopub.execute_input":"2022-06-21T20:52:31.713577Z","iopub.status.idle":"2022-06-21T21:13:09.870306Z","shell.execute_reply.started":"2022-06-21T20:52:31.713546Z","shell.execute_reply":"2022-06-21T21:13:09.868709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f'features_simple_train_comp_kaggle.pkl', 'wb') as f:\n    pickle.dump(features_train, f)\nwith open(f'features_simple_test_comp_kaggle.pkl', 'wb') as f:\n    pickle.dump(features_val, f)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T21:14:14.833077Z","iopub.execute_input":"2022-06-21T21:14:14.833534Z","iopub.status.idle":"2022-06-21T21:14:14.930746Z","shell.execute_reply.started":"2022-06-21T21:14:14.833498Z","shell.execute_reply":"2022-06-21T21:14:14.929559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CatBoost with simple features","metadata":{}},{"cell_type":"code","source":"from catboost import CatBoostClassifier, Pool, metrics, cv","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:07:06.071003Z","iopub.execute_input":"2022-06-23T18:07:06.071448Z","iopub.status.idle":"2022-06-23T18:07:06.434730Z","shell.execute_reply.started":"2022-06-23T18:07:06.071395Z","shell.execute_reply":"2022-06-23T18:07:06.433181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install hyperopt","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:37:40.646546Z","iopub.execute_input":"2022-06-23T16:37:40.647256Z","iopub.status.idle":"2022-06-23T16:37:51.914025Z","shell.execute_reply.started":"2022-06-23T16:37:40.647216Z","shell.execute_reply":"2022-06-23T16:37:51.912866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"../input/itmoaae2022/features_simple_train_comp_kaggle.pkl\", 'rb') as f:\n    X_train = pickle.load(f)\nwith open(\"../input/itmoaae2022/features_simple_test_comp_kaggle.pkl\", 'rb') as f:\n    X_test = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:07:09.926849Z","iopub.execute_input":"2022-06-23T18:07:09.927287Z","iopub.status.idle":"2022-06-23T18:07:10.105251Z","shell.execute_reply.started":"2022-06-23T18:07:09.927253Z","shell.execute_reply":"2022-06-23T18:07:10.103983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_, X_validation, y_train_, y_validation = train_test_split(X_train, y_train, train_size=0.8, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:07:13.146559Z","iopub.execute_input":"2022-06-23T18:07:13.146955Z","iopub.status.idle":"2022-06-23T18:07:13.158092Z","shell.execute_reply.started":"2022-06-23T18:07:13.146923Z","shell.execute_reply":"2022-06-23T18:07:13.157155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Попробуем пообучать вместе с оптимизатором","metadata":{}},{"cell_type":"code","source":"full_pool = Pool(X_train, y_train)\ntrain_pool = Pool(X_train_, y_train_)\nvalidate_pool = Pool(X_validation, y_validation)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:07:20.587013Z","iopub.execute_input":"2022-06-23T18:07:20.587478Z","iopub.status.idle":"2022-06-23T18:07:21.497086Z","shell.execute_reply.started":"2022-06-23T18:07:20.587410Z","shell.execute_reply":"2022-06-23T18:07:21.495796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import hyperopt\n\ndef hyperopt_objective(params):\n    model = CatBoostClassifier(\n        l2_leaf_reg=int(params['l2_leaf_reg']),\n        learning_rate=params['learning_rate'],\n        custom_loss=[metrics.Accuracy()],\n        random_seed=42,\n        classes_count=41,\n        iterations= 1300,\n        metric_period=100,\n        od_type= 'Iter',\n        od_wait= 40,\n#         task_type = 'GPU',\n        loss_function='MultiClass',\n        max_ctr_complexity=1, \n        )\n    cv_data = cv(\n        full_pool,\n        model.get_params(),\n        fold_count=5,\n        logging_level='Silent',\n        stratified=True,\n    )\n    best_accuracy = np.max(cv_data['test-Accuracy-mean'])\n    return 1 - best_accuracy # as hyperopt minimises","metadata":{"execution":{"iopub.status.busy":"2022-06-23T18:07:23.956683Z","iopub.execute_input":"2022-06-23T18:07:23.957058Z","iopub.status.idle":"2022-06-23T18:07:24.178088Z","shell.execute_reply.started":"2022-06-23T18:07:23.957028Z","shell.execute_reply":"2022-06-23T18:07:24.176981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Он требует до 9 часов нон-стопом, это долго","metadata":{}},{"cell_type":"code","source":"from numpy.random import RandomState\n\nparams_space = {\n    'l2_leaf_reg': hyperopt.hp.qloguniform('l2_leaf_reg', 0, 2, 1),\n    'learning_rate': hyperopt.hp.uniform('learning_rate', 1e-3, 5e-1),\n}\n\ntrials = hyperopt.Trials()\n\nbest = hyperopt.fmin(\n    hyperopt_objective,\n    space=params_space,\n    algo=hyperopt.tpe.suggest,\n    max_evals=10,\n    trials=trials,\n    rstate=np.random.seed(123)\n)\n\nprint(best)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CatBoostClassifier(\n    l2_leaf_reg=int(best['l2_leaf_reg']),\n    learning_rate=best['learning_rate'],\n    random_seed=42,\n    classes_count=41,\n    iterations= 1300,\n    metric_period=100,\n    od_type= 'Iter',\n    od_wait= 40,\n    loss_function='MultiClass',\n    task_type = 'GPU', \n    plot=\"True\"\n)\n# cv_data = cv(Pool(X_train, y_train), model.get_params())","metadata":{"execution":{"iopub.status.busy":"2022-06-21T21:21:24.329179Z","iopub.execute_input":"2022-06-21T21:21:24.329667Z","iopub.status.idle":"2022-06-21T21:21:24.404516Z","shell.execute_reply.started":"2022-06-21T21:21:24.329629Z","shell.execute_reply":"2022-06-21T21:21:24.403022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Конечная конфигурация модели:","metadata":{}},{"cell_type":"code","source":"model = CatBoostClassifier(\n    random_seed=42,\n    custom_loss=[metrics.Accuracy()],\n    classes_count=41,\n    learning_rate=0.1,\n    iterations= 3000,\n    metric_period=100,\n    od_type= 'Iter',\n    od_wait= 40,\n    loss_function='MultiClass',\n    task_type = 'GPU', \n    save_snapshot=False,\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:45:09.211552Z","iopub.execute_input":"2022-06-23T16:45:09.212237Z","iopub.status.idle":"2022-06-23T16:45:09.217919Z","shell.execute_reply.started":"2022-06-23T16:45:09.212199Z","shell.execute_reply":"2022-06-23T16:45:09.217011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(Pool(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:45:14.048493Z","iopub.execute_input":"2022-06-23T16:45:14.049193Z","iopub.status.idle":"2022-06-23T16:46:33.070725Z","shell.execute_reply.started":"2022-06-23T16:45:14.049154Z","shell.execute_reply":"2022-06-23T16:46:33.069570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:48:45.307607Z","iopub.execute_input":"2022-06-23T16:48:45.308308Z","iopub.status.idle":"2022-06-23T16:48:45.740171Z","shell.execute_reply.started":"2022-06-23T16:48:45.308268Z","shell.execute_reply":"2022-06-23T16:48:45.739184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_predict = pd.DataFrame({'label': le.inverse_transform(y_test), 'fname': name_wav_test})\ndf_predict.set_index('fname', inplace=True)\ndf_predict.to_csv('./sample_submission_1941.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-23T16:48:48.824835Z","iopub.execute_input":"2022-06-23T16:48:48.825180Z","iopub.status.idle":"2022-06-23T16:48:48.846114Z","shell.execute_reply.started":"2022-06-23T16:48:48.825151Z","shell.execute_reply":"2022-06-23T16:48:48.845042Z"},"trusted":true},"execution_count":null,"outputs":[]}]}