{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":8900,"databundleVersionId":862232,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Импорт библиотек**","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\n\nimport numpy as np\nimport pandas as pd\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.dataset import Dataset\nfrom torchvision.models import efficientnet_b0, EfficientNet_B0_Weights","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:25.953397Z","iopub.execute_input":"2025-10-19T20:07:25.954161Z","iopub.status.idle":"2025-10-19T20:07:25.958544Z","shell.execute_reply.started":"2025-10-19T20:07:25.954137Z","shell.execute_reply":"2025-10-19T20:07:25.957755Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Устройство для вычислений**","metadata":{}},{"cell_type":"code","source":"device = 'cuda:0' if torch.cuda.is_available() else 'cpu'\nprint('Доступное устройство: {}'.format(device))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:31.236801Z","iopub.execute_input":"2025-10-19T20:07:31.237522Z","iopub.status.idle":"2025-10-19T20:07:31.241700Z","shell.execute_reply.started":"2025-10-19T20:07:31.237470Z","shell.execute_reply":"2025-10-19T20:07:31.240741Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Датасет**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/freesound-audio-tagging/train.csv\")\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:33.433962Z","iopub.execute_input":"2025-10-19T20:07:33.434433Z","iopub.status.idle":"2025-10-19T20:07:33.452972Z","shell.execute_reply.started":"2025-10-19T20:07:33.434414Z","shell.execute_reply":"2025-10-19T20:07:33.452316Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Сортировка и кодирование классов**","metadata":{}},{"cell_type":"code","source":"labels = np.unique(train.label.values)\nprint('Всего классов: {}'.format(len(labels)))\nlabels_encoder = {label:i for i, label in enumerate(labels)}\nprint(labels_encoder)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:36.229667Z","iopub.execute_input":"2025-10-19T20:07:36.230116Z","iopub.status.idle":"2025-10-19T20:07:36.239114Z","shell.execute_reply.started":"2025-10-19T20:07:36.230098Z","shell.execute_reply":"2025-10-19T20:07:36.238206Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Класс для работы с датасетом**","metadata":{}},{"cell_type":"code","source":"trainPath = '../input/freesound-audio-tagging/audio_train/'\ntestPath = '../input/freesound-audio-tagging/audio_test/'\n\nclass Lab1Dataset(Dataset):\n    def __init__(self, dataframe, test=False):\n        self.dataframe = dataframe\n        self.test = test\n        \n    def __getitem__(self, index):\n        fileName = self.dataframe.fname.values[index]\n        label = self.dataframe.label.values[index]\n        \n        path = (testPath if self.test else trainPath) + fileName\n        signal, _ = librosa.load(path)\n        signal = librosa.feature.melspectrogram(y=signal)    \n        signal = librosa.power_to_db(signal, ref=np.max) \n        \n        try:\n            resized = cv2.resize(signal, (128, 128))\n        except Exception as e:\n            print(path)\n            print(str(e))\n            resized = np.zeros(shape=(128, 128))\n        \n        x = np.stack([resized] * 3)\n        x = torch.tensor(x, dtype=torch.float32)\n\n        if self.test == False:\n            y = labels_encoder[label]\n            return x, y\n        else:\n             return x\n        \n    def __len__(self):\n        return self.dataframe.shape[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:40.934654Z","iopub.execute_input":"2025-10-19T20:07:40.935313Z","iopub.status.idle":"2025-10-19T20:07:40.941693Z","shell.execute_reply.started":"2025-10-19T20:07:40.935292Z","shell.execute_reply":"2025-10-19T20:07:40.940958Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Размер batch и количество эпох**","metadata":{}},{"cell_type":"code","source":"batch_size = 64\nepochs = 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:43.847998Z","iopub.execute_input":"2025-10-19T20:07:43.848682Z","iopub.status.idle":"2025-10-19T20:07:43.852029Z","shell.execute_reply.started":"2025-10-19T20:07:43.848660Z","shell.execute_reply":"2025-10-19T20:07:43.851285Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Разбиение выборки на тренировочную и валидационную**","metadata":{}},{"cell_type":"code","source":"train, validation = train_test_split(train, test_size=0.2, shuffle=True, random_state=5)\n\ntrain_set = Lab1Dataset(train)\nval_set = Lab1Dataset(validation)\n\ntrain_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True)\nval_loader = DataLoader(val_set , batch_size=batch_size, shuffle=True)\n\nprint('Тренировочная выборка: {}'.format(train.shape[0]))\nprint('Валидационная выборка: {}'.format(validation.shape[0]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:46.833599Z","iopub.execute_input":"2025-10-19T20:07:46.834147Z","iopub.status.idle":"2025-10-19T20:07:46.842881Z","shell.execute_reply.started":"2025-10-19T20:07:46.834127Z","shell.execute_reply":"2025-10-19T20:07:46.842027Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Использование предобученной модели**","metadata":{}},{"cell_type":"code","source":"model = efficientnet_b0(weights='EfficientNet_B0_Weights.DEFAULT')\nmodel.classifier[1] = torch.nn.Linear(1280, 41) # изменение классификатора под задачу\nmodel = model.to(device)\nmodel.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:07:49.189641Z","iopub.execute_input":"2025-10-19T20:07:49.190196Z","iopub.status.idle":"2025-10-19T20:07:49.346472Z","shell.execute_reply.started":"2025-10-19T20:07:49.190175Z","shell.execute_reply":"2025-10-19T20:07:49.345703Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Обучение**","metadata":{}},{"cell_type":"code","source":"optimizer = torch.optim.AdamW(model.parameters(), lr=0.001)\ncost = torch.nn.CrossEntropyLoss()\n\nfor epoch in range(epochs):\n    train_loss = 0\n    val_loss = 0\n    train_correct = 0\n    val_correct = 0\n    \n    # перевод модели в режим обучения\n    model.train()\n    for x, y in train_loader:\n        optimizer.zero_grad()\n        x,y = x.to(device),y.to(device)\n        \n        # вычисление предсказания и потерь\n        pred = model(x)\n        loss = cost(pred, y)\n        train_loss += cost(pred, y).item()\n        train_correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n        \n        # обратное распространение ошибки\n        loss.backward()\n        optimizer.step()\n    \n    # перевод модели в режим оценивания\n    model.eval()\n    with torch.no_grad():\n        for x, y in val_loader:\n            x,y = x.to(device),y.to(device)\n            \n            pred = model(x)\n            loss = cost(pred, y)\n            val_loss += cost(pred, y).item()\n            val_correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n            \n    train_loss = train_loss/len(train_loader)\n    val_loss = val_loss/len(val_loader)\n    train_accuracy = train_correct / len(train)\n    val_accuracy = val_correct / len(validation)\n    print(\"Epoch = %d, train_loss = %.5f, val_loss = %.5f, train_accuracy = %.5f, val_accuracy = %.5f\" % (epoch, train_loss, val_loss, train_accuracy, val_accuracy))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:08:05.571311Z","iopub.execute_input":"2025-10-19T20:08:05.571609Z","iopub.status.idle":"2025-10-19T20:38:09.177649Z","shell.execute_reply.started":"2025-10-19T20:08:05.571591Z","shell.execute_reply":"2025-10-19T20:38:09.176864Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Предсказания для тестовой выборки**","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/freesound-audio-tagging/sample_submission.csv')\n\ntest_dataset = Lab1Dataset(test, test=True)\ntest_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\npredictions = torch.tensor([])\nmodel.eval()\n\nfor x in test_loader:\n    x = x.to(device)\n    with torch.no_grad():\n        y_hat = model(x)\n    predictions = torch.cat([predictions, y_hat.cpu()])\n\npredictions = torch.nn.functional.softmax(predictions, dim=1).detach().numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T20:38:35.323742Z","iopub.execute_input":"2025-10-19T20:38:35.324620Z","iopub.status.idle":"2025-10-19T20:43:06.891001Z","shell.execute_reply.started":"2025-10-19T20:38:35.324599Z","shell.execute_reply":"2025-10-19T20:43:06.890315Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Создание результата**","metadata":{}},{"cell_type":"code","source":"submission_result = test.copy()\n\nN = len(test)\nfor i in range(N):\n    p = predictions[i, :]\n    idx = np.argmax(p)\n    submission_result.label[i] = labels[idx]\n\nsubmission_result.to_csv('submission_final.csv', index=False)\n\nsubmission_result.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-19T21:12:41.560763Z","iopub.execute_input":"2025-10-19T21:12:41.561403Z","iopub.status.idle":"2025-10-19T21:12:42.421452Z","shell.execute_reply.started":"2025-10-19T21:12:41.561384Z","shell.execute_reply":"2025-10-19T21:12:42.420700Z"}},"outputs":[],"execution_count":null}]}