{"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":25954,"databundleVersionId":2091745,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Установка и импорт зависимостей","metadata":{}},{"cell_type":"code","source":"!pip install audioread --quiet","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:49:12.271698Z","iopub.execute_input":"2024-11-18T18:49:12.272060Z","iopub.status.idle":"2024-11-18T18:49:54.152864Z","shell.execute_reply.started":"2024-11-18T18:49:12.272025Z","shell.execute_reply":"2024-11-18T18:49:54.151423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport librosa\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom librosa.feature import melspectrogram\nfrom tqdm import tqdm\nimport gc\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nimport keras\nfrom keras.callbacks import ReduceLROnPlateau,ModelCheckpoint\nfrom keras.layers import Input, Convolution2D, MaxPooling2D, Dense, Dropout, Flatten\nimport audioread","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:49:54.154710Z","iopub.execute_input":"2024-11-18T18:49:54.155117Z","iopub.status.idle":"2024-11-18T18:49:54.162462Z","shell.execute_reply.started":"2024-11-18T18:49:54.155082Z","shell.execute_reply":"2024-11-18T18:49:54.161313Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Подотовка тренировочного и валидационного датасетов\n\nЗагрузка тренировочных данных","metadata":{}},{"cell_type":"code","source":"TRAIN_METADATA_FILE = \"/kaggle/input/birdclef-2021/train_metadata.csv\"\ntrain_data_df = pd.read_csv(TRAIN_METADATA_FILE)\ntrain_data_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:53.607160Z","iopub.execute_input":"2024-11-18T17:52:53.607768Z","iopub.status.idle":"2024-11-18T17:52:54.056911Z","shell.execute_reply.started":"2024-11-18T17:52:53.607739Z","shell.execute_reply":"2024-11-18T17:52:54.056176Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Получение названий файлов для последующей загрузки","metadata":{}},{"cell_type":"code","source":"train_filename_template = \"/kaggle/input/birdclef-2021/train_short_audio/{}/{}\"\n\ntrain_data_df[\"full_filename\"] = [\n    train_filename_template.format(lbl, flname)\n    for lbl, flname in zip(train_data_df[\"primary_label\"], train_data_df[\"filename\"])\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:54.058055Z","iopub.execute_input":"2024-11-18T17:52:54.058358Z","iopub.status.idle":"2024-11-18T17:52:54.102775Z","shell.execute_reply.started":"2024-11-18T17:52:54.058331Z","shell.execute_reply":"2024-11-18T17:52:54.101843Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data_df = train_data_df[[\"primary_label\", \"full_filename\"]]\ntrain_data_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:54.105144Z","iopub.execute_input":"2024-11-18T17:52:54.105423Z","iopub.status.idle":"2024-11-18T17:52:54.194836Z","shell.execute_reply.started":"2024-11-18T17:52:54.105397Z","shell.execute_reply":"2024-11-18T17:52:54.194022Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Получение словарей для преобразования названий птиц в числа и обратно + получение количества классов","metadata":{}},{"cell_type":"code","source":"LABEL_TO_NUM = {l: n for n, l in enumerate(train_data_df.primary_label.unique())}\nNUM_TO_LABEL = {n: l for l, n in LABEL_TO_NUM.items()}\nNUM_CLASSES = len(LABEL_TO_NUM)\nNUM_CLASSES","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:54.196022Z","iopub.execute_input":"2024-11-18T17:52:54.196373Z","iopub.status.idle":"2024-11-18T17:52:54.207119Z","shell.execute_reply.started":"2024-11-18T17:52:54.196346Z","shell.execute_reply":"2024-11-18T17:52:54.206302Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Здесь вычисляется сколько данных для каждого лейбла и сколько всего останется записей, если для каждого лейбла поставить ограничение в MAX_ONE_LABEL (50) файлов","metadata":{}},{"cell_type":"code","source":"value_counts = train_data_df.primary_label.value_counts()\n\nMAX_ONE_LABEL = 50\n\nprint(value_counts.map(lambda x: x if x <= MAX_ONE_LABEL else MAX_ONE_LABEL).sum())\nvalue_counts","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:54.208286Z","iopub.execute_input":"2024-11-18T17:52:54.208598Z","iopub.status.idle":"2024-11-18T17:52:54.226609Z","shell.execute_reply.started":"2024-11-18T17:52:54.208563Z","shell.execute_reply":"2024-11-18T17:52:54.225721Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Для каждого вида птиц выбирается не более MAX_ONE_LABEL наименьших по размеру файлов ","metadata":{}},{"cell_type":"code","source":"TRAIN_SHORT_DIR = '/kaggle/input/birdclef-2021/train_short_audio/'\n\ndirs = os.listdir(TRAIN_SHORT_DIR)\nprint(f\"total dirs count = {len(dirs)}, dir name example = '{dirs[0]}'\")\nfiles_by_labels = {}\nfor dir in tqdm(dirs):\n    list_of_files = os.listdir(TRAIN_SHORT_DIR + dir)\n    sorted_by_size = sorted( list_of_files, \n                        key =  lambda x: os.stat \n                       (os.path.join(TRAIN_SHORT_DIR+dir, x)).st_size) \n    filenames = [f'{TRAIN_SHORT_DIR}{dir}/{i}' for i in sorted_by_size[:MAX_ONE_LABEL]]\n    files_by_labels[dir] = filenames\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:52:54.227660Z","iopub.execute_input":"2024-11-18T17:52:54.228470Z","iopub.status.idle":"2024-11-18T17:54:54.778439Z","shell.execute_reply.started":"2024-11-18T17:52:54.228437Z","shell.execute_reply":"2024-11-18T17:54:54.777543Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Функции для получения MFCC для файла","metadata":{}},{"cell_type":"code","source":"SAMPLE_RATE = 32000\nN_MFCC = 32\nHOP_LENGTH = 512 \n\n\ndef extract_mfcc(audio, sr=SAMPLE_RATE, n_mfcc=N_MFCC, hop_length=HOP_LENGTH):\n    mfcc = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=n_mfcc, hop_length=hop_length).astype(np.float16)\n    return mfcc\n\n# Функция для загрузки аудио и генерации MFCC\ndef load_and_convert_to_mfcc(file_path):\n    audio, _ = librosa.load(file_path, sr=SAMPLE_RATE)\n    mfcc = extract_mfcc(audio)\n    return mfcc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:54:54.779425Z","iopub.execute_input":"2024-11-18T17:54:54.779681Z","iopub.status.idle":"2024-11-18T17:54:54.784964Z","shell.execute_reply.started":"2024-11-18T17:54:54.779656Z","shell.execute_reply":"2024-11-18T17:54:54.784143Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Для каждого файла записывается MFCC и label","metadata":{}},{"cell_type":"code","source":"train_metadata = pd.read_csv(TRAIN_METADATA_FILE)\n\ntrain_data = []\nnot_in = []\nfor label, files in tqdm(files_by_labels.items()):\n    for file_path in files:\n        if os.path.exists(file_path):\n            mel_spec = load_and_convert_to_mfcc(file_path)\n            train_data.append({'mfcc': mel_spec, 'label': label})\n        else:\n            print(f\"Файл не найден: {file_path}\")\n\nprint(f\"len train data: {len(train_data)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T17:54:54.785959Z","iopub.execute_input":"2024-11-18T17:54:54.786198Z","iopub.status.idle":"2024-11-18T18:06:51.465068Z","shell.execute_reply.started":"2024-11-18T17:54:54.786174Z","shell.execute_reply":"2024-11-18T18:06:51.464107Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Здесь вычисляются \"длины\" полученных MFCC, они сортируются и строится график. Также вычисляется сколько данных имеют длину больше MAX_LEN (они будут обрезаны)","metadata":{}},{"cell_type":"code","source":"data_lens = sorted([i['mfcc'].shape[1] for i in train_data])\n\nplt.plot(data_lens)\nplt.show()\n\nprint(f\"Max length = {data_lens[-1]}, avg len = {round(sum(data_lens) / len(data_lens))}, median ~ {data_lens[len(data_lens) // 2]}\")\nMAX_LEN = 2000\ngreater_then_max_len = [i for i in data_lens if i > MAX_LEN]\namount_greater_max = len(greater_then_max_len)\n\nprint(f\"{amount_greater_max} elements with length graeter than {MAX_LEN}\")\nprint(f\"{round(amount_greater_max / len(data_lens) * 100, 2)} % will be cropped\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:51.741122Z","iopub.execute_input":"2024-11-18T18:06:51.741429Z","iopub.status.idle":"2024-11-18T18:06:51.962883Z","shell.execute_reply.started":"2024-11-18T18:06:51.741403Z","shell.execute_reply":"2024-11-18T18:06:51.962109Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Видно, что бОльшая часть данных обрезана не будет","metadata":{}},{"cell_type":"code","source":"train_data[0]['mfcc'].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:51.964072Z","iopub.execute_input":"2024-11-18T18:06:51.964432Z","iopub.status.idle":"2024-11-18T18:06:51.970043Z","shell.execute_reply.started":"2024-11-18T18:06:51.964394Z","shell.execute_reply":"2024-11-18T18:06:51.969323Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Здесь матрицы дополнются или обрезаются до длины MAX_LEN по второму измерению. Также повышается размерность матриц для корректной работы слоя Convolution2D","metadata":{}},{"cell_type":"code","source":"for datum in tqdm(train_data):\n    if datum['mfcc'].shape[1] < MAX_LEN:\n        datum['mfcc'] = np.pad(datum['mfcc'], ((0, 0), (0, MAX_LEN - datum['mfcc'].shape[1])), mode='constant')\n    else:\n        datum['mfcc'] = datum['mfcc'][:,:MAX_LEN]\n    datum['mfcc'] = datum['mfcc'][..., np.newaxis]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:51.971197Z","iopub.execute_input":"2024-11-18T18:06:51.971560Z","iopub.status.idle":"2024-11-18T18:06:53.254833Z","shell.execute_reply.started":"2024-11-18T18:06:51.971522Z","shell.execute_reply":"2024-11-18T18:06:53.253897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[0]['mfcc'].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:53.255841Z","iopub.execute_input":"2024-11-18T18:06:53.256110Z","iopub.status.idle":"2024-11-18T18:06:53.261665Z","shell.execute_reply.started":"2024-11-18T18:06:53.256084Z","shell.execute_reply":"2024-11-18T18:06:53.260845Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Названия птиц переводятся сначала в числа, а потом в вектор длины NUM_CLASSES, состоящий из NUM_CLASSES-1 нулей и одной единицы\n\nДатасет разбивается на обучающи и валидационный в соотношении 70%/30%","metadata":{}},{"cell_type":"code","source":"labels_as_nums = [LABEL_TO_NUM[i['label']] for i in train_data]\nlabels_categorical = to_categorical(labels_as_nums, num_classes=NUM_CLASSES)\nprint(f\"labels_categorical shape = {labels_categorical.shape}\")\n\nX_train, X_val, y_train, y_val = train_test_split(\n    np.array([i['mfcc'] for i in train_data]), labels_categorical, test_size=0.3, random_state=42\n)\nX_train.shape, y_train.shape, X_val.shape, y_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:53.262746Z","iopub.execute_input":"2024-11-18T18:06:53.263407Z","iopub.status.idle":"2024-11-18T18:06:54.213956Z","shell.execute_reply.started":"2024-11-18T18:06:53.263376Z","shell.execute_reply":"2024-11-18T18:06:54.213107Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Инициализация и обучение модели","metadata":{}},{"cell_type":"code","source":"kernel_size = 3\npool_size = 2\nconv_depth_1 = 32\nconv_depth_2 = 64  \ndrop_prob_1 = 0.25 \ndrop_prob_2 = 0.5 \nhidden_size = 512 \n\nmodel = Sequential(\n    [\n        Input(shape=X_train.shape[1:]),  \n        \n        Convolution2D(conv_depth_1, kernel_size, padding=\"same\", activation=\"relu\"),\n        MaxPooling2D(pool_size=pool_size),\n        Dropout(drop_prob_1),\n        \n        Convolution2D(conv_depth_2, kernel_size, padding=\"same\", activation=\"relu\"),\n        MaxPooling2D(pool_size=pool_size),\n        Dropout(drop_prob_1),\n\n        Flatten(),\n        Dense(hidden_size, activation=\"relu\"),\n        Dropout(drop_prob_2),\n        Dense(NUM_CLASSES, activation=\"softmax\"),\n    ]\n)\n\nmodel.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer='adam',\n    metrics=[\"accuracy\"],\n)\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:54.215031Z","iopub.execute_input":"2024-11-18T18:06:54.215320Z","iopub.status.idle":"2024-11-18T18:06:55.023774Z","shell.execute_reply.started":"2024-11-18T18:06:54.215293Z","shell.execute_reply":"2024-11-18T18:06:55.022969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:55.024798Z","iopub.execute_input":"2024-11-18T18:06:55.025062Z","iopub.status.idle":"2024-11-18T18:06:55.255634Z","shell.execute_reply.started":"2024-11-18T18:06:55.025036Z","shell.execute_reply":"2024-11-18T18:06:55.254573Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Обучение модели\n\nКоличество эпох обучение намеренно завышено, так как применяются коллбэки для ранней остановки (при не улучшении метрики \"val_loss\" в течение patience шагов) и коллбэк, сохраняющий веса лучшей модели (по метрике val_accuracy)","metadata":{}},{"cell_type":"code","source":"num_epochs = 50\nbatch_size = 16\n\ncheckpoint_filepath = \"checkpoint.weights.h5\"\n\nmodel_checkpoint_callback = keras.callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=True,\n    monitor=\"val_accuracy\",\n    mode=\"max\",\n    save_best_only=True,\n    verbose=1,\n)\n\nearly_stoping = keras.callbacks.EarlyStopping(monitor=\"val_loss\", patience=8, verbose=1)\n\n# Обучение модели\nhistory = model.fit(\n    X_train, y_train,\n    epochs=num_epochs,\n    batch_size=batch_size,\n    validation_data=[X_val, y_val],\n    callbacks=[model_checkpoint_callback, early_stoping],\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:06:55.256685Z","iopub.execute_input":"2024-11-18T18:06:55.256961Z","iopub.status.idle":"2024-11-18T18:12:11.067276Z","shell.execute_reply.started":"2024-11-18T18:06:55.256917Z","shell.execute_reply":"2024-11-18T18:12:11.066499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:14:01.659227Z","iopub.execute_input":"2024-11-18T18:14:01.659696Z","iopub.status.idle":"2024-11-18T18:14:01.671236Z","shell.execute_reply.started":"2024-11-18T18:14:01.659656Z","shell.execute_reply":"2024-11-18T18:14:01.670246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.evaluate(X_val, y_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:14:01.909041Z","iopub.execute_input":"2024-11-18T18:14:01.909631Z","iopub.status.idle":"2024-11-18T18:14:09.826175Z","shell.execute_reply.started":"2024-11-18T18:14:01.909599Z","shell.execute_reply":"2024-11-18T18:14:09.825434Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Загрузка весов \"лучшей модели\" и оценка на валидационом датасете","metadata":{}},{"cell_type":"code","source":"model.load_weights(checkpoint_filepath, skip_mismatch=False)\nmodel.evaluate(X_val, y_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:14:09.827434Z","iopub.execute_input":"2024-11-18T18:14:09.827698Z","iopub.status.idle":"2024-11-18T18:14:22.482040Z","shell.execute_reply.started":"2024-11-18T18:14:09.827673Z","shell.execute_reply":"2024-11-18T18:14:22.481327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = model.predict(X_train[:2])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:19:12.079014Z","iopub.execute_input":"2024-11-18T18:19:12.079855Z","iopub.status.idle":"2024-11-18T18:19:12.144792Z","shell.execute_reply.started":"2024-11-18T18:19:12.079820Z","shell.execute_reply":"2024-11-18T18:19:12.144124Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Функция для получения MFCC нужной формы по имени файла","metadata":{}},{"cell_type":"code","source":"def preprocess_data(file_path, seconds=0):\n    audio, _ = librosa.load(file_path, sr=SAMPLE_RATE, offset=seconds - 5, duration=5)\n    mfcc = extract_mfcc(audio)\n    if mfcc.shape[1] < MAX_LEN:\n        mfcc = np.pad(mfcc, ((0, 0), (0, MAX_LEN - mfcc.shape[1])), mode='constant')\n    else:\n        mfcc = mfcc[:,:MAX_LEN]\n    mfcc = mfcc[..., np.newaxis]\n    return mfcc\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:54:13.761880Z","iopub.execute_input":"2024-11-18T18:54:13.762323Z","iopub.status.idle":"2024-11-18T18:54:13.768496Z","shell.execute_reply.started":"2024-11-18T18:54:13.762285Z","shell.execute_reply":"2024-11-18T18:54:13.767550Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Получение предсказаний на тестовых данных\n\nПолучение названий файлов из test_soundscapes, их длительности и составление row_id (пока без секунд)","metadata":{}},{"cell_type":"code","source":"def get_audio_duration(filename):\n    with audioread.audio_open(filename) as f:\n        sec = f.duration\n    return sec\n\n\nTEST_DIR= '/kaggle/input/birdclef-2021/test_soundscapes/'\n\nfilenames = []\nfilenames = [f\"{TEST_DIR}{file}\" for file in os.listdir(TEST_DIR) if file.endswith('ogg')]\n\ndf = pd.DataFrame({\"filename\": filenames})\ndf['duration'] = df.filename.map(lambda x: get_audio_duration(x))\ndf['audio_id'] = df.filename.map(lambda x: x.split('/')[-1].split('_')[0])\ndf['site'] = df.filename.map(lambda x: x.split('/')[-1].split('_')[1])\ndf['row_id'] = df.audio_id.astype(str) + '_' + df.site\ndf = df[['row_id', 'duration', 'filename']]\ndf.duration = df.duration.map(lambda x: [i for i in range(5, int(x)+1, 5)]) # здесь делаю список с шагом 5 секунд\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:51:15.723205Z","iopub.execute_input":"2024-11-18T18:51:15.724175Z","iopub.status.idle":"2024-11-18T18:51:18.022244Z","shell.execute_reply.started":"2024-11-18T18:51:15.724130Z","shell.execute_reply":"2024-11-18T18:51:18.021228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"del X_train, y_train, X_val, y_val\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:44:24.345767Z","iopub.execute_input":"2024-11-18T18:44:24.346122Z","iopub.status.idle":"2024-11-18T18:44:24.374166Z","shell.execute_reply.started":"2024-11-18T18:44:24.346089Z","shell.execute_reply":"2024-11-18T18:44:24.372964Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Здесь для каждого файла идем по списку duration (шаг 5 секунд), записываем MFCC сразу нужной формы и row_id дополняем секундами","metadata":{}},{"cell_type":"code","source":"row_ids = []\nmfccs = []\nfor _, row in tqdm(df.iterrows()):\n    id = row[0]\n    filename = row[2]\n    for dur in row[1]:\n        mfcc = preprocess_data(filename, dur)\n        mfccs.append(mfcc)\n        row_ids.append(f'{id}_{dur}')\nmfccs = np.array(mfccs)\nmfccs.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T18:58:54.411871Z","iopub.execute_input":"2024-11-18T18:58:54.412282Z","iopub.status.idle":"2024-11-18T19:00:34.937171Z","shell.execute_reply.started":"2024-11-18T18:58:54.412232Z","shell.execute_reply":"2024-11-18T19:00:34.936035Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Получаем предсказания модели, которые имеют вероятность более lower_limit (0.25)","metadata":{}},{"cell_type":"code","source":"res_birds = []\nif mfccs.shape[0] > 0:\n    preds = model.predict(mfccs)\n    print(pred.shape)\n    \n    lower_limit=0.15\n    for num, pred in enumerate(preds):\n        selected_indices = np.where(pred > lower_limit)[0]\n        predicted_labels = [NUM_TO_LABEL[i] for i in selected_indices]\n        birds = ' '.join(predicted_labels) if predicted_labels else 'nocall'\n        res_birds.append(birds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T19:00:45.529154Z","iopub.execute_input":"2024-11-18T19:00:45.529524Z","iopub.status.idle":"2024-11-18T19:00:49.854469Z","shell.execute_reply.started":"2024-11-18T19:00:45.529494Z","shell.execute_reply":"2024-11-18T19:00:49.853676Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Записываем в датафрейм и сохраняем в submission.csv","metadata":{}},{"cell_type":"code","source":"result_df = pd.DataFrame({'row_id': row_ids, 'birds': res_birds})\nresult_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T19:00:49.856023Z","iopub.execute_input":"2024-11-18T19:00:49.856391Z","iopub.status.idle":"2024-11-18T19:00:49.867507Z","shell.execute_reply.started":"2024-11-18T19:00:49.856359Z","shell.execute_reply":"2024-11-18T19:00:49.866544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result_df.to_csv(\"submission.csv\", index=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T19:00:49.868815Z","iopub.execute_input":"2024-11-18T19:00:49.869099Z","iopub.status.idle":"2024-11-18T19:00:49.882148Z","shell.execute_reply.started":"2024-11-18T19:00:49.869072Z","shell.execute_reply":"2024-11-18T19:00:49.881195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}