{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":8900,"databundleVersionId":862232,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Импорт библиотек\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport librosa\nimport librosa.display\nimport soundfile\nimport os\nimport glob\nimport fnmatch   \nimport copy\n\nfrom sklearn.model_selection import train_test_split\n\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'\nimport tensorflow as tf\n\nimport warnings; warnings.filterwarnings('ignore')\n\n# Для центрирования графиков matplotlib\nfrom IPython.core.display import HTML \nHTML(\"\"\"\n<style>\n.output_png {\n    display: table-cell;\n    text-align: center;\n    vertical-align: middle;\n}\n</style>\n\"\"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:01.251424Z","iopub.execute_input":"2024-12-18T20:27:01.252313Z","iopub.status.idle":"2024-12-18T20:27:04.645653Z","shell.execute_reply.started":"2024-12-18T20:27:01.252276Z","shell.execute_reply":"2024-12-18T20:27:04.644789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def log_progress(sequence, every=None, size=None, name='Items'):\n    from ipywidgets import IntProgress, HTML, VBox\n    from IPython.display import display\n\n    is_iterator = False\n    if size is None:\n        try:\n            size = len(sequence)\n        except TypeError:\n            is_iterator = True\n    if size is not None:\n        if every is None:\n            if size <= 200:\n                every = 1\n            else:\n                every = int(size / 200)     # every 0.5%\n    else:\n        assert every is not None, 'sequence is iterator, set every'\n\n    if is_iterator:\n        progress = IntProgress(min=0, max=1, value=1)\n        progress.bar_style = 'info'\n    else:\n        progress = IntProgress(min=0, max=size, value=0)\n    label = HTML()\n    box = VBox(children=[label, progress])\n    display(box)\n\n    index = 0\n    try:\n        for index, record in enumerate(sequence, 1):\n            if index == 1 or index % every == 0:\n                if is_iterator:\n                    label.value = '{name}: {index} / ?'.format(\n                        name=name,\n                        index=index\n                    )\n                else:\n                    progress.value = index\n                    label.value = u'{name}: {index} / {size}'.format(\n                        name=name,\n                        index=index,\n                        size=size\n                    )\n            yield record\n    except:\n        progress.bar_style = 'danger'\n        raise\n    else:\n        progress.bar_style = 'success'\n        progress.value = index\n        label.value = \"{name}: {index}\".format(\n            name=name,\n            index=str(index or '?')\n        )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:04.647940Z","iopub.execute_input":"2024-12-18T20:27:04.648930Z","iopub.status.idle":"2024-12-18T20:27:04.657382Z","shell.execute_reply.started":"2024-12-18T20:27:04.648885Z","shell.execute_reply":"2024-12-18T20:27:04.656458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_feature(waveform, sample_rate):\n    feature = librosa.feature.melspectrogram(y=waveform, sr=sample_rate, n_fft=2048)\n    return feature\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":false,"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:04.658559Z","iopub.execute_input":"2024-12-18T20:27:04.658998Z","iopub.status.idle":"2024-12-18T20:27:04.670954Z","shell.execute_reply.started":"2024-12-18T20:27:04.658924Z","shell.execute_reply":"2024-12-18T20:27:04.670203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = pd.read_csv(\"/kaggle/input/freesound-audio-tagging/train.csv\")\nprint(train_csv)\n\nunique_labels = set(train_csv[\"label\"].values)\nunique_labels_count = len(unique_labels)\nprint(unique_labels_count)\ncount = 0\nfile_name_index = 0\nlabel_index = 1\nmanually_verified_flag_index = 2","metadata":{"execution":{"iopub.status.busy":"2024-12-18T20:27:04.671937Z","iopub.execute_input":"2024-12-18T20:27:04.672197Z","iopub.status.idle":"2024-12-18T20:27:04.696823Z","shell.execute_reply.started":"2024-12-18T20:27:04.672163Z","shell.execute_reply":"2024-12-18T20:27:04.695876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_data(csv_mapping, path = 'Audio/FSDKaggle2018.audio_train/', pred = None):\n    result = []\n\n    count = 0\n    \n    folder = path    \n\n    label_dict = {}\n    next_label_id = 0\n    \n    for record in csv_mapping.values:\n        if pred != None:\n            if pred(record):\n                continue\n        sound_file = folder + record[file_name_index]    \n        waveform, sample_rate = librosa.load(sound_file)\n        label=record[label_index]\n\n        new_rec = {}        \n        new_rec[\"soundfile\"] = (waveform, sample_rate)\n        new_rec[\"label\"] = label\n        result.append(new_rec)\n        res_id = label_dict.setdefault(label, next_label_id)\n        if res_id == next_label_id:\n            next_label_id += 1\n        count += 1        \n        \n        print('\\r' + f' Loaded {count}/{len(csv_mapping)} audio samples',end='')\n    \n    return result, label_dict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:04.700057Z","iopub.execute_input":"2024-12-18T20:27:04.700830Z","iopub.status.idle":"2024-12-18T20:27:04.706886Z","shell.execute_reply.started":"2024-12-18T20:27:04.700745Z","shell.execute_reply":"2024-12-18T20:27:04.705919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n# Загружаем набор данных и вычисляем характеристики каждого аудиофайла:\ndef calc_features(record, data, lbl = []):\n    count = 0\n    x = data['X']\n    waveform, sample_rate = record[\"soundfile\"]\n    features = get_feature(waveform, sample_rate)\n    features = librosa.power_to_db(features, ref=np.max)    \n    features = cv2.resize(features, (128, 128), interpolation=cv2.INTER_AREA)\n    x.append(tf.constant(features))\n    \n\n    if 'Y' in data.keys():\n        y = data['Y']\n        y.append(tf.constant(label_dict[record[\"label\"]]))\n    lbl.append(record[\"label\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:04.707926Z","iopub.execute_input":"2024-12-18T20:27:04.708208Z","iopub.status.idle":"2024-12-18T20:27:04.743814Z","shell.execute_reply.started":"2024-12-18T20:27:04.708182Z","shell.execute_reply":"2024-12-18T20:27:04.743156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loaded_data, label_dict = load_data(train_csv, '/kaggle/input/freesound-audio-tagging/audio_train/', lambda x: False)# x[manually_verified_flag_index] == 0)\n\nprint (label_dict)\nreverse_dict = {}\nfor k, v in label_dict.items():\n    reverse_dict[v] = k\nreverse_dict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:27:04.744695Z","iopub.execute_input":"2024-12-18T20:27:04.744974Z","iopub.status.idle":"2024-12-18T20:29:14.132753Z","shell.execute_reply.started":"2024-12-18T20:27:04.744949Z","shell.execute_reply":"2024-12-18T20:29:14.131702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = {'X': [], 'Y' : []}\nlabels = []\n\nfor record in log_progress(loaded_data): \n    calc_features(record, data, labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:29:14.134075Z","iopub.execute_input":"2024-12-18T20:29:14.134701Z","iopub.status.idle":"2024-12-18T20:32:03.188891Z","shell.execute_reply.started":"2024-12-18T20:29:14.134670Z","shell.execute_reply":"2024-12-18T20:32:03.187205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del loaded_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:03.190472Z","iopub.execute_input":"2024-12-18T20:32:03.191224Z","iopub.status.idle":"2024-12-18T20:32:03.215166Z","shell.execute_reply.started":"2024-12-18T20:32:03.191182Z","shell.execute_reply":"2024-12-18T20:32:03.213854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['X'] = np.array(data['X'])\ndata['Y'] = np.array(data['Y'])\nlabels = np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:03.224877Z","iopub.execute_input":"2024-12-18T20:32:03.225500Z","iopub.status.idle":"2024-12-18T20:32:04.190315Z","shell.execute_reply.started":"2024-12-18T20:32:03.225444Z","shell.execute_reply":"2024-12-18T20:32:04.189356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"signals_count = data['X'].shape[0]\nprint(f'Сигналов: {signals_count}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:04.191861Z","iopub.execute_input":"2024-12-18T20:32:04.192805Z","iopub.status.idle":"2024-12-18T20:32:04.197198Z","shell.execute_reply.started":"2024-12-18T20:32:04.192769Z","shell.execute_reply":"2024-12-18T20:32:04.196352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(15,4))\nlabel_list, count = np.unique(labels, return_counts=True)\nplt.bar(x=range(unique_labels_count), height=count)\nplt.xticks(ticks=range(unique_labels_count), labels = [key for key in label_dict.keys()],fontsize=10)\nplt.xticks(rotation=90)\nplt.xlabel('Звук')\nplt.tick_params(labelsize=16)\nplt.ylabel('Число элементов')\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:04.198677Z","iopub.execute_input":"2024-12-18T20:32:04.199049Z","iopub.status.idle":"2024-12-18T20:32:04.632629Z","shell.execute_reply.started":"2024-12-18T20:32:04.199009Z","shell.execute_reply":"2024-12-18T20:32:04.631787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scale_factor = tf.subtract(\n      tf.reduce_max(data['X']), \n      tf.reduce_min(data['X'])\n   )\n\ndata['X_std'] = np.divide(\n   tf.subtract(\n      data['X'], \n      tf.reduce_min(data['X'])\n   ), \n    scale_factor\n)\ndel data['X']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:04.633994Z","iopub.execute_input":"2024-12-18T20:32:04.634676Z","iopub.status.idle":"2024-12-18T20:32:11.071495Z","shell.execute_reply.started":"2024-12-18T20:32:04.634636Z","shell.execute_reply":"2024-12-18T20:32:11.070745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del scale_factor\ndata['X_std'][0].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:11.074183Z","iopub.execute_input":"2024-12-18T20:32:11.074476Z","iopub.status.idle":"2024-12-18T20:32:11.080290Z","shell.execute_reply.started":"2024-12-18T20:32:11.074447Z","shell.execute_reply":"2024-12-18T20:32:11.079396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(shape=(data['X_std'][0].shape[0], data['X_std'][0].shape[0], 1)),\n    tf.keras.layers.Conv2D(data['X_std'][0].shape[0], (2, 2), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(data['X_std'][0].shape[0], (2, 2), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(data['X_std'][0].shape[0], (2, 2), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(data['X_std'][0].shape[0], (2, 2), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Flatten(input_shape=data['X_std'][0].shape),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dense(unique_labels_count, activation='softmax'),\n])\n\nmodel.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['acc'])\n\nmodel.fit(data['X_std'], data['Y'], epochs=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:32:11.081399Z","iopub.execute_input":"2024-12-18T20:32:11.081800Z","iopub.status.idle":"2024-12-18T20:33:20.512437Z","shell.execute_reply.started":"2024-12-18T20:32:11.081769Z","shell.execute_reply":"2024-12-18T20:33:20.511441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probability_model = model\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:33:20.513555Z","iopub.execute_input":"2024-12-18T20:33:20.513896Z","iopub.status.idle":"2024-12-18T20:33:20.517970Z","shell.execute_reply.started":"2024-12-18T20:33:20.513867Z","shell.execute_reply":"2024-12-18T20:33:20.517001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_csv = pd.read_csv(\"/kaggle/input/freesound-audio-tagging/sample_submission.csv\")\nprint(submission_csv)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:33:20.519003Z","iopub.execute_input":"2024-12-18T20:33:20.519217Z","iopub.status.idle":"2024-12-18T20:33:20.542125Z","shell.execute_reply.started":"2024-12-18T20:33:20.519195Z","shell.execute_reply":"2024-12-18T20:33:20.541188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loaded_data = load_data(submission_csv, '/kaggle/input/freesound-audio-tagging/audio_test/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:33:20.543173Z","iopub.execute_input":"2024-12-18T20:33:20.543444Z","iopub.status.idle":"2024-12-18T20:34:03.934127Z","shell.execute_reply.started":"2024-12-18T20:33:20.543418Z","shell.execute_reply":"2024-12-18T20:34:03.933192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = {'X': []}\nlabels = []\n\nfor record in log_progress(loaded_data[0]):\n    calc_features(record, data)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:34:03.937430Z","iopub.execute_input":"2024-12-18T20:34:03.937997Z","iopub.status.idle":"2024-12-18T20:36:31.269674Z","shell.execute_reply.started":"2024-12-18T20:34:03.937966Z","shell.execute_reply":"2024-12-18T20:36:31.268533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del loaded_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:36:31.271066Z","iopub.execute_input":"2024-12-18T20:36:31.271528Z","iopub.status.idle":"2024-12-18T20:36:31.293115Z","shell.execute_reply.started":"2024-12-18T20:36:31.271476Z","shell.execute_reply":"2024-12-18T20:36:31.292024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scale_factor = tf.subtract(\n      tf.reduce_max(data['X']), \n      tf.reduce_min(data['X'])\n   )\n\ndata['X_std'] = np.divide(\n   tf.subtract(\n      data['X'], \n      tf.reduce_min(data['X'])\n   ), \n    scale_factor\n)\ndel data['X']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:36:31.328021Z","iopub.execute_input":"2024-12-18T20:36:31.328483Z","iopub.status.idle":"2024-12-18T20:36:32.740638Z","shell.execute_reply.started":"2024-12-18T20:36:31.328431Z","shell.execute_reply":"2024-12-18T20:36:32.739908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['X_std'].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:36:32.745333Z","iopub.execute_input":"2024-12-18T20:36:32.745584Z","iopub.status.idle":"2024-12-18T20:36:32.751152Z","shell.execute_reply.started":"2024-12-18T20:36:32.745560Z","shell.execute_reply":"2024-12-18T20:36:32.750318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame()\n\nsubmission['fname'] = submission_csv['fname']\n\npredictions = model.predict(data['X_std'])\n\n\nsubmission['label'] = np.array([reverse_dict[key] for key in np.argmax(predictions, axis=1)])\nsubmission.to_csv('./submission.csv', index=False)\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-18T20:36:32.752735Z","iopub.execute_input":"2024-12-18T20:36:32.753078Z","iopub.status.idle":"2024-12-18T20:36:36.763774Z","shell.execute_reply.started":"2024-12-18T20:36:32.753040Z","shell.execute_reply":"2024-12-18T20:36:36.762785Z"}},"outputs":[],"execution_count":null}]}