{"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"},{"sourceId":653195,"sourceType":"datasetVersion","datasetId":325566},{"sourceId":3394382,"sourceType":"datasetVersion","datasetId":2046290},{"sourceId":8870981,"sourceType":"datasetVersion","datasetId":5338451},{"sourceId":9206846,"sourceType":"datasetVersion","datasetId":5566662},{"sourceId":9238847,"sourceType":"datasetVersion","datasetId":5588470},{"sourceId":9239083,"sourceType":"datasetVersion","datasetId":5588635},{"sourceId":9539870,"sourceType":"datasetVersion","datasetId":5565803},{"sourceId":9621868,"sourceType":"datasetVersion","datasetId":5297443},{"sourceId":9745212,"sourceType":"datasetVersion","datasetId":5965723}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport math\nimport os\nimport cv2\nimport IPython.display as ipd \nimport librosa \nimport librosa.display\nimport torch\nimport numpy as np\nimport torch.nn.functional as F\nimport torchvision\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom torch.utils.data.dataset import Dataset\nfrom torch.utils.data import DataLoader\nfrom torchvision import transforms","metadata":{"_uuid":"43d96169-73e7-4b08-9e92-8af125d53e43","_cell_guid":"c15a369b-0bc6-4661-91cd-04f25e8e6a7e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-30T05:11:35.321393Z","iopub.execute_input":"2024-10-30T05:11:35.321723Z","iopub.status.idle":"2024-10-30T05:11:41.868996Z","shell.execute_reply.started":"2024-10-30T05:11:35.321687Z","shell.execute_reply":"2024-10-30T05:11:41.867964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda:0' if torch.cuda.is_available() else 'cpu'\nprint(device)","metadata":{"_uuid":"e9a099d2-108e-4233-b1fa-c7bfbaa354ad","_cell_guid":"47379fa6-07e3-41b0-aac1-48c609b1bcaa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-30T05:11:41.870921Z","iopub.execute_input":"2024-10-30T05:11:41.871498Z","iopub.status.idle":"2024-10-30T05:11:41.907543Z","shell.execute_reply.started":"2024-10-30T05:11:41.871436Z","shell.execute_reply":"2024-10-30T05:11:41.906484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport os\nimport glob\nfrom pathlib import Path\n\nimport librosa\nimport librosa.display\nimport IPython\nfrom IPython.display import Audio\nfrom scipy.io.wavfile import read, write\n\nfrom sklearn.preprocessing import StandardScaler, OneHotEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport sklearn","metadata":{"_uuid":"204ecfb7-5d86-40b8-9838-3f6d55f4a930","_cell_guid":"e96e837d-83fc-46c0-a0a7-858bd1d109aa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-30T05:11:41.908852Z","iopub.execute_input":"2024-10-30T05:11:41.909174Z","iopub.status.idle":"2024-10-30T05:11:42.267938Z","shell.execute_reply.started":"2024-10-30T05:11:41.909140Z","shell.execute_reply":"2024-10-30T05:11:42.266869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torchvision.transforms as transforms\nfrom sklearn.model_selection import train_test_split\n\nCrema = \"/kaggle/input/cremad/AudioWAV/\"\n\ncrema_directory_list = os.listdir(Crema)\n\nfile_emotion = []\nfile_path = []\n\nfor file in crema_directory_list:\n    file_path.append(Crema + file)\n    part=file.split('_')\n    if part[2] == 'SAD':\n        file_emotion.append('sad')\n    elif part[2] == 'ANG':\n        file_emotion.append('angry')\n    elif part[2] == 'DIS':\n        file_emotion.append('disgust')\n    elif part[2] == 'FEA':\n        file_emotion.append('fear')\n    elif part[2] == 'HAP':\n        file_emotion.append('happy')\n    elif part[2] == 'NEU':\n        file_emotion.append('neutral')\n    else:\n        file_emotion.append('Unknown')\n        \nemotion_df = pd.DataFrame(file_emotion, columns=['label'])\n\npath_df = pd.DataFrame(file_path, columns=['fname'])\ntrain = pd.concat([path_df, emotion_df], axis=1)\ntrain.head()","metadata":{"_uuid":"cd5e3aa4-5b24-4886-b188-40350e89a3ec","_cell_guid":"735bfdd1-ca9b-4a66-b500-0ea6a20034dc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-30T05:11:42.270552Z","iopub.execute_input":"2024-10-30T05:11:42.271339Z","iopub.status.idle":"2024-10-30T05:11:42.522546Z","shell.execute_reply.started":"2024-10-30T05:11:42.271294Z","shell.execute_reply":"2024-10-30T05:11:42.521617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train.label.unique()))\nprint(train.label.unique())\n\nlabels = np.unique(train.label.values)\nlabel_encoder = {label:i for i, label in enumerate(labels)}","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:11:42.523758Z","iopub.execute_input":"2024-10-30T05:11:42.524580Z","iopub.status.idle":"2024-10-30T05:11:42.539942Z","shell.execute_reply.started":"2024-10-30T05:11:42.524534Z","shell.execute_reply":"2024-10-30T05:11:42.538899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport numpy as np\nimport librosa\nimport cv2\nfrom torch.utils.data import Dataset, DataLoader\nimport math\n\n# Define constants\nIMG_SIZE = (128, 87)\nSPEC_PATH = '/kaggle/input/tess-melpec-128-512-2sec/CREMA-D-melpec-128-512-2sec'\n\nclass AudioDataset(Dataset):\n    def __init__(self, dataframe, is_train=True):\n        self.dataframe = dataframe\n        self.is_train = is_train\n\n    def __getitem__(self, index):\n        audio_path = self.dataframe.fname.values[index]\n        label = self.dataframe.label.values[index]\n\n        # Lấy tên file .npy từ audio_path\n        file_name = os.path.basename(audio_path).replace('.wav', '.npy')\n        spec_path = os.path.join(SPEC_PATH, file_name)\n\n        # Kiểm tra xem file .npy có tồn tại hay không\n        if not os.path.exists(spec_path):\n            print(f\"Error: File not found: {spec_path}\")\n            return None, None\n\n        try:\n            data_array = np.load(spec_path)\n        except Exception as e:\n            print(f\"Error loading {spec_path}: {e}\")\n            return None, None\n\n        resized = cv2.resize(data_array, (IMG_SIZE[1], IMG_SIZE[0]))\n        X = np.zeros(shape=(3, IMG_SIZE[0], IMG_SIZE[1]))\n        for j in range(3):\n            X[j,:,:] = resized\n\n        if self.is_train:\n            y = label_encoder[label]  # Assuming you have a label encoder defined\n            return torch.tensor(X, dtype=torch.float), y\n        else:\n            return torch.tensor(X, dtype=torch.float)\n\n    def __len__(self):\n        return self.dataframe.shape[0]\n\n\n# # Example usage:\n# train_df = # your training dataframe\n# test_df = # your testing dataframe\ntrain_dd, test_df = train_test_split(train, test_size=0.2, random_state=1, stratify=train.label)\ntrain_df, val_df = train_test_split(train_dd, test_size=0.2, random_state=1, stratify=train_dd.label)\n\ntrain_dataset = AudioDataset(train_df, is_train=True)\nval_dataset = AudioDataset(val_df, is_train=True)\ntest_dataset = AudioDataset(test_df, is_train=True)\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=True)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:11:42.541518Z","iopub.execute_input":"2024-10-30T05:11:42.541891Z","iopub.status.idle":"2024-10-30T05:11:42.582644Z","shell.execute_reply.started":"2024-10-30T05:11:42.541848Z","shell.execute_reply":"2024-10-30T05:11:42.581665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Định nghĩa hàm kiểm tra bất thường\ndef check_for_anomalies(data, target):\n    \"\"\"\n    Kiểm tra dữ liệu và nhãn cho các bất thường.\n\n    Args:\n        data (torch.Tensor): Dữ liệu đầu vào.\n        target (torch.Tensor): Nhãn của dữ liệu.\n\n    Returns:\n        bool: True nếu có bất thường, False nếu không.\n    \"\"\"\n\n    # Kiểm tra NaN (Not a Number)\n    if torch.isnan(data).any() or torch.isnan(target).any():\n        print(\"Phát hiện NaN trong dữ liệu hoặc nhãn!\")\n        return True\n\n    # Kiểm tra Inf (Infinity)\n    if torch.isinf(data).any() or torch.isinf(target).any():\n        print(\"Phát hiện Inf trong dữ liệu hoặc nhãn!\")\n        return True\n\n    # Kiểm tra giá trị vượt quá ngưỡng\n#     data_min, data_max = data.min(), data.max()\n#     target_min, target_max = target.min(), target.max()\n#     if data_min < -1e6 or data_max > 1e6:\n#         print(f\"Giá trị dữ liệu nằm ngoài ngưỡng cho phép: [{data_min}, {data_max}]\")\n#         return True\n#     if target_min < 0 or target_max >= 10:  # Ví dụ: target phải nằm trong khoảng [0, 10)\n#         print(f\"Giá trị nhãn nằm ngoài ngưỡng cho phép: [{target_min}, {target_max}]\")\n#         return True\n\n    # Thêm các kiểm tra khác tùy thuộc vào đặc thù dữ liệu của bạn\n\n    return False","metadata":{"_uuid":"7c1c8d4b-5ffe-4367-9fa0-1eb59c95b5e9","_cell_guid":"c2dff015-ec57-48bb-bb86-be718c8e15ec","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-30T05:11:42.583984Z","iopub.execute_input":"2024-10-30T05:11:42.584394Z","iopub.status.idle":"2024-10-30T05:11:42.592520Z","shell.execute_reply.started":"2024-10-30T05:11:42.584350Z","shell.execute_reply":"2024-10-30T05:11:42.591424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Iterate through the data loaders:\nfor batch_idx, (data, target) in enumerate(train_loader):\n     # Kiểm tra bất thường\n    if check_for_anomalies(data, target):\n        print(f\"Bỏ qua batch {batch_idx} do có bất thường!\")\n        continue  # Bỏ qua batch hiện tại và chuyển sang batch tiếp theo\n\n    # Do your training logic here\n#     print(f\"Batch {batch_idx}: data shape {data.shape}, target shape {target.shape}\")\n\nfor batch_idx, (data, target) in enumerate(val_loader):\n     # Kiểm tra bất thường\n    if check_for_anomalies(data, target):\n        print(f\"Bỏ qua batch {batch_idx} do có bất thường!\")\n        continue  # Bỏ qua batch hiện tại và chuyển sang batch tiếp theo\n\n    # Do your testing logic here\n#     print(f\"Batch {batch_idx}: data shape {data.shape}, target shape {target.shape}\")\n\nfor batch_idx, (data, target) in enumerate(test_loader):\n     # Kiểm tra bất thường\n    if check_for_anomalies(data, target):\n        print(f\"Bỏ qua batch {batch_idx} do có bất thường!\")\n        continue  # Bỏ qua batch hiện tại và chuyển sang batch tiếp theo\n\n    # Do your testing logic here\n#     print(f\"Batch {batch_idx}: data shape {data.shape}, target shape {target.shape}\")    ","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:11:42.594246Z","iopub.execute_input":"2024-10-30T05:11:42.594600Z","iopub.status.idle":"2024-10-30T05:12:59.536154Z","shell.execute_reply.started":"2024-10-30T05:11:42.594561Z","shell.execute_reply":"2024-10-30T05:12:59.535162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to get label counts from a DataLoader\ndef get_label_counts(dataloader):\n    label_counts = {}\n    for batch_data, batch_labels in dataloader:\n        for label in batch_labels:\n            label_int = label.item()  # Convert to integer\n            if label_int not in label_counts:\n                label_counts[label_int] = 0\n            label_counts[label_int] += 1\n    return label_counts\n\n# Get label counts for each DataLoader\ntrain_counts = get_label_counts(train_loader)\nval_counts = get_label_counts(val_loader)\ntest_counts = get_label_counts(test_loader)\n\n# Plotting\nlabels = list(train_counts.keys())  # Assuming labels are the same across datasets\nx = np.arange(len(labels))  # x-axis positions\n\nwidth = 0.25  # Width of bars\n\nfig, ax = plt.subplots()\nrects1 = ax.bar(x - width, train_counts.values(), width, label='Train')\nrects2 = ax.bar(x, val_counts.values(), width, label='Validation')\nrects3 = ax.bar(x + width, test_counts.values(), width, label='Test')\n\nax.set_ylabel('Number of Samples')\nax.set_title('Label Distribution')\nax.set_xticks(x)\nax.set_xticklabels(labels)\nax.legend()\n\nfig.tight_layout()\nplt.show()\n# Use code with caution.","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:12:59.537455Z","iopub.execute_input":"2024-10-30T05:12:59.537828Z","iopub.status.idle":"2024-10-30T05:13:09.883209Z","shell.execute_reply.started":"2024-10-30T05:12:59.537786Z","shell.execute_reply":"2024-10-30T05:13:09.882232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{"_uuid":"44591fbc-2918-4194-a6a7-0ba79f67c5cb","_cell_guid":"7feee079-024b-46a8-b511-e5c317e99dd4","trusted":true}},{"cell_type":"code","source":"model = torchvision.models.mobilenet_v2(pretrained=True)\n# Define the number of output classes\nnum_classes = 6\n\n# Get the input features of the existing classifier\nin_features = model.classifier[1].in_features\n\n# Define the new classifier with two additional FC layers\nnew_classifier = torch.nn.Sequential(\n    torch.nn.Dropout(p=0.2),\n    torch.nn.Linear(in_features, 256),\n    torch.nn.ReLU(),\n    torch.nn.Dropout(p=0.2),\n    torch.nn.Linear(256, 128),\n    torch.nn.ReLU(),\n    torch.nn.Dropout(p=0.2),\n    torch.nn.Linear(128, num_classes)\n)\n\n# Replace the original classifier with the new classifier\nmodel.classifier = new_classifier\ndevice = 'cuda:0' if torch.cuda.is_available() else 'cpu'\nmodel.to(device);","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:13:09.885745Z","iopub.execute_input":"2024-10-30T05:13:09.886049Z","iopub.status.idle":"2024-10-30T05:13:10.450594Z","shell.execute_reply.started":"2024-10-30T05:13:09.886016Z","shell.execute_reply":"2024-10-30T05:13:10.449729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import precision_score, recall_score, f1_score\n\n# Assuming `model` is an instance of EfficientNet-B0\n\n# Function to get all convolutional layers\ndef get_conv_layers(model):\n    conv_layers = []\n    for name, layer in model.named_modules():\n        if isinstance(layer, nn.Conv2d):\n            conv_layers.append((name, layer))\n    return conv_layers\n\n# Group filters into a specified number of groups\ndef group_filters(conv_layer, num_groups):\n    num_filters = conv_layer.weight.size(0)\n    group_size = num_filters // num_groups\n    groups = [list(range(i * group_size, (i + 1) * group_size)) for i in range(num_groups)]\n    if num_filters % num_groups != 0:\n        groups[-1].extend(range(num_groups * group_size, num_filters))\n    return groups\n\n# Convert layer groups to one-hot numpy array\ndef layer_groups_to_one_hot(num_layers, num_groups, num_groups_to_unfreeze):\n    total_groups = num_layers * num_groups\n    one_hot_array = np.zeros(total_groups, dtype=int)\n    if num_groups_to_unfreeze > 0:\n        one_hot_array[-num_groups_to_unfreeze:] = 1\n    return one_hot_array\n\n# Convert one-hot index to layer and group\ndef one_hot_index_to_layer_group(index, num_groups):\n    layer = index // num_groups\n    group = index % num_groups\n    return layer, group\n\n# Freeze the entire model except fully connected layers\ndef freeze_model_except_fc(model):\n    for name, module in model.named_modules():\n        if isinstance(module, nn.Linear):\n            for param in module.parameters():\n                param.requires_grad = True\n        else:\n            for param in module.parameters():\n                param.requires_grad = True\n\n# Unfreeze specific groups of filters based on the one-hot array\ndef unfreeze_groups(model, one_hot_array, num_layers, num_groups):\n    conv_layers = get_conv_layers(model)\n    conv_layer_groups = [group_filters(layer[1], num_groups) for layer in conv_layers]\n\n    for index in range(len(one_hot_array)):\n        if one_hot_array[index] == 0:\n            layer_index, group_index = one_hot_index_to_layer_group(index, num_groups)\n            layer_name, layer = conv_layers[layer_index]\n            for i in conv_layer_groups[layer_index][group_index]:\n                layer.weight.grad[i] = 0\n                if layer.bias is not None:\n                    layer.bias.grad[i] = 0\n                    \n# Count total parameters for selected groups\ndef count_parameters_for_groups(model, one_hot_array, num_groups_per_layer):\n    total_params = 0\n    conv_layers = get_conv_layers(model)\n    for index in range(len(one_hot_array)):\n        if one_hot_array[index] == 1:\n            layer_index, group_index = one_hot_index_to_layer_group(index, num_groups_per_layer)\n            layer_name, layer = conv_layers[layer_index]\n            group_filters_indices = group_filters(layer, num_groups_per_layer)[group_index]\n            for i in group_filters_indices:\n                total_params += layer.weight[i].numel()\n                if layer.bias is not None:\n                    total_params += layer.bias[i].numel()\n    return total_params\n\ndef count_parameters(model, num_groups, num_groups_to_unfreeze):\n    num_layers = len(get_conv_layers(model))\n    one_hot_array = layer_groups_to_one_hot(num_layers, num_groups, num_groups_to_unfreeze)\n    total_params = count_parameters_for_groups(model, one_hot_array, num_groups)\n    return total_params\n\n# Training function with validation and testing\ndef train_model(model, train_loader, val_loader, num_groups_to_unfreeze, num_layers, num_groups, num_epochs=10, learning_rate=0.01):\n    one_hot_array = layer_groups_to_one_hot(num_layers, num_groups, num_groups_to_unfreeze)\n    \n    num_parameters = count_parameters(model, num_groups, num_groups_to_unfreeze)\n    \n    criterion = nn.CrossEntropyLoss()\n    optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=learning_rate, momentum=0.9)\n\n    train_losses = []\n    val_losses = []\n    train_accuracies = []\n    val_accuracies = []\n\n    for epoch in range(num_epochs):\n        model.train()\n        running_loss = 0.0\n        correct = 0\n        total = 0\n\n        for images, labels in train_loader:\n            images = images.to(device)\n            labels = labels.to(device)\n\n            optimizer.zero_grad()\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            loss.backward()\n\n            unfreeze_groups(model, one_hot_array, num_layers, num_groups)\n            optimizer.step()\n            running_loss += loss.item()\n\n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n        train_loss = running_loss / len(train_loader)\n        train_accuracy = 100 * correct / total\n        train_losses.append(train_loss)\n        train_accuracies.append(train_accuracy)\n\n        model.eval()\n        val_running_loss = 0.0\n        val_correct = 0\n        val_total = 0\n\n        with torch.no_grad():\n            for val_images, val_labels in val_loader:\n                val_images = val_images.to(device)\n                val_labels = val_labels.to(device)\n\n                val_outputs = model(val_images)\n                val_loss = criterion(val_outputs, val_labels)\n                val_running_loss += val_loss.item()\n\n                _, val_predicted = torch.max(val_outputs.data, 1)\n                val_total += val_labels.size(0)\n                val_correct += (val_predicted == val_labels).sum().item()\n\n        val_loss = val_running_loss / len(val_loader)\n        val_accuracy = 100 * val_correct / val_total\n        val_losses.append(val_loss)\n        val_accuracies.append(val_accuracy)\n\n        print(f'Total parameters: {num_parameters}, Train Loss: {train_loss:.4f}, Train Accuracy: {train_accuracy:.2f}%, Val Loss: {val_loss:.4f}, Val Accuracy: {val_accuracy:.2f}%')\n\n    return train_losses, train_accuracies, val_losses, val_accuracies\n\n# Function to evaluate the model on the test set\ndef evaluate_model(model, test_loader):\n    model.eval()\n    test_running_loss = 0.0\n    test_correct = 0\n    test_total = 0\n    criterion = nn.CrossEntropyLoss()\n\n    all_labels = []\n    all_predictions = []\n\n    with torch.no_grad():\n        for test_images, test_labels in test_loader:\n            test_images = test_images.to(device)\n            test_labels = test_labels.to(device)\n\n            test_outputs = model(test_images)\n            test_loss = criterion(test_outputs, test_labels)\n            test_running_loss += test_loss.item()\n\n            _, test_predicted = torch.max(test_outputs.data, 1)\n            test_total += test_labels.size(0)\n            test_correct += (test_predicted == test_labels).sum().item()\n\n            all_labels.extend(test_labels.cpu().numpy())\n            all_predictions.extend(test_predicted.cpu().numpy())\n\n    test_loss = test_running_loss / len(test_loader)\n    test_accuracy = 100 * test_correct / test_total\n\n    # Calculate precision, recall, and f1 score\n    precision = precision_score(all_labels, all_predictions, average='weighted')\n    recall = recall_score(all_labels, all_predictions, average='weighted')\n    f1 = f1_score(all_labels, all_predictions, average='weighted')\n\n    print(f'Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%')\n    print(f'Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}')\n\n    return test_loss, test_accuracy, precision, recall, f1","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:36:16.414351Z","iopub.execute_input":"2024-10-30T05:36:16.415050Z","iopub.status.idle":"2024-10-30T05:36:16.447908Z","shell.execute_reply.started":"2024-10-30T05:36:16.415007Z","shell.execute_reply":"2024-10-30T05:36:16.446859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradual Unfreeze","metadata":{"_uuid":"67053e08-d1ee-49a0-9ba2-cb3f65f79eda","_cell_guid":"f53bd560-508a-4b7a-86b8-78e5d229cf21","trusted":true}},{"cell_type":"markdown","source":"# 2 - 7","metadata":{}},{"cell_type":"code","source":"action_hist = [0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0]","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:36:17.716214Z","iopub.execute_input":"2024-10-30T05:36:17.716608Z","iopub.status.idle":"2024-10-30T05:36:17.721330Z","shell.execute_reply.started":"2024-10-30T05:36:17.716568Z","shell.execute_reply":"2024-10-30T05:36:17.720367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tìm vị trí của các số 1\npositions = [index for index, value in enumerate(action_hist) if value == 1]\n\nprint(\"Vị trí của các số 1:\", positions)","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:36:17.900328Z","iopub.execute_input":"2024-10-30T05:36:17.900715Z","iopub.status.idle":"2024-10-30T05:36:17.906161Z","shell.execute_reply.started":"2024-10-30T05:36:17.900674Z","shell.execute_reply":"2024-10-30T05:36:17.905206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 15%","metadata":{}},{"cell_type":"code","source":"train_dd, test_df = train_test_split(train, test_size=0.2, random_state=1, stratify=train.label)\ntrain_df, val_df = train_test_split(train_dd, test_size=0.2, random_state=1, stratify=train_dd.label)\ntrain_df, _ = train_test_split(train_df, test_size=0.85, random_state=1, stratify=train_df.label)\n\ntrain_dataset = AudioDataset(train_df, is_train=True)\nval_dataset = AudioDataset(val_df, is_train=True)\ntest_dataset = AudioDataset(test_df, is_train=True)\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=32, shuffle=True)\ntest_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)\n\n# Get label counts for each DataLoader\ntrain_counts = get_label_counts(train_loader)\nval_counts = get_label_counts(val_loader)\ntest_counts = get_label_counts(test_loader)\n\n# Plotting\nlabels = list(train_counts.keys())  # Assuming labels are the same across datasets\nx = np.arange(len(labels))  # x-axis positions\n\nwidth = 0.25  # Width of bars\n\nfig, ax = plt.subplots()\nrects1 = ax.bar(x - width, train_counts.values(), width, label='Train')\nrects2 = ax.bar(x, val_counts.values(), width, label='Validation')\nrects3 = ax.bar(x + width, test_counts.values(), width, label='Test')\n\nax.set_ylabel('Number of Samples')\nax.set_title('Label Distribution')\nax.set_xticks(x)\nax.set_xticklabels(labels)\nax.legend()\n\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:44:08.850189Z","iopub.execute_input":"2024-10-30T05:44:08.850597Z","iopub.status.idle":"2024-10-30T05:44:14.219633Z","shell.execute_reply.started":"2024-10-30T05:44:08.850560Z","shell.execute_reply":"2024-10-30T05:44:14.218689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2, 7","metadata":{}},{"cell_type":"code","source":"trial_time = 0","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:44:17.377056Z","iopub.execute_input":"2024-10-30T05:44:17.377474Z","iopub.status.idle":"2024-10-30T05:44:17.382026Z","shell.execute_reply.started":"2024-10-30T05:44:17.377428Z","shell.execute_reply":"2024-10-30T05:44:17.381051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example usage\nmodel.load_state_dict(torch.load('/kaggle/input/mbv2-sleep-default-temp/model_weights_cremad_default.pth'))\nnum_layers = len(get_conv_layers(model))\nnum_groups = 2\nfreeze_model_except_fc(model)\nadd_groups_freeze = 31\n\n# unfreeze_epochs = [1, 4, 5, 6, 7, 9, 10, 11, 14, 15, 22, 25, 28, 31, 32, 33, 34, 35, 37, 38, 39]\nunfreeze_epochs = [index for index, value in enumerate(action_hist) if value == 1]\n\nepochs = 15\none_hot_array = layer_groups_to_one_hot(num_layers, num_groups, 0)\nnum_layers_unfreeze = 0\nprint(f'Training... with total group layers {len(one_hot_array)}, divided by groups of {num_groups} per layer')\n\ntrain_losses = []\ntrain_accuracies = []\nval_losses = []\nval_accuracies = []\nbest_val_accuracy = 0\n\ntrial_time += 1\nsave_path = 'best_model_15_' + str(trial_time) + '.pth'\n\nfor epoch in range(epochs):\n    print(f'--- group layers unfreezed: {num_layers_unfreeze} ')\n    print(f'Epoch [{epoch+1}/{epochs}], ', end='')\n    tl, ta, vl, va = train_model(model, train_loader, val_loader, num_groups_to_unfreeze=num_layers_unfreeze, num_layers=num_layers, num_groups=num_groups, num_epochs=1)\n    # Save the model if validation accuracy is the best we've seen so far\n    if va[-1] > best_val_accuracy:\n        best_val_accuracy = va[-1]\n        best_train_acc = ta[-1]\n        best_val_loss = vl[-1]\n        best_train_loss = tl[-1]\n        torch.save(model.state_dict(), save_path)\n\n    if epoch in unfreeze_epochs:\n        num_layers_unfreeze += add_groups_freeze\n    train_losses.extend(tl)\n    train_accuracies.extend(ta)\n    val_losses.extend(vl)\n    val_accuracies.extend(va)\n\nprint(f'------ Best model with train_loss {best_train_loss:.4f}, train accuracy {best_train_acc:.2f}%, val_loss {best_val_loss:.4f}, val accuracy: {best_val_accuracy:.2f}%')\n\n# Plotting loss and accuracy\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(range(1, epochs + 1), train_losses, label='Training Loss')\nplt.plot(range(1, epochs + 1), val_losses, label='Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss over Epochs')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(range(1, epochs + 1), train_accuracies, label='Training Accuracy')\nplt.plot(range(1, epochs + 1), val_accuracies, label='Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy (%)')\nplt.title('Training and Validation Accuracy over Epochs')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n\ntest_loss, test_accuracy, precision, recall, f1 = evaluate_model(model, test_loader)","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:44:23.005561Z","iopub.execute_input":"2024-10-30T05:44:23.006316Z","iopub.status.idle":"2024-10-30T05:45:30.664988Z","shell.execute_reply.started":"2024-10-30T05:44:23.006275Z","shell.execute_reply":"2024-10-30T05:45:30.664003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example usage\nmodel.load_state_dict(torch.load('/kaggle/input/mbv2-sleep-default-temp/model_weights_cremad_default.pth'))\nnum_layers = len(get_conv_layers(model))\nnum_groups = 2\nfreeze_model_except_fc(model)\nadd_groups_freeze = 7\n\n# unfreeze_epochs = [1, 4, 5, 6, 7, 9, 10, 11, 14, 15, 22, 25, 28, 31, 32, 33, 34, 35, 37, 38, 39]\n\nepochs = 65\none_hot_array = layer_groups_to_one_hot(num_layers, num_groups, 0)\nnum_layers_unfreeze = 0\nprint(f'Training... with total group layers {len(one_hot_array)}, divided by groups of {num_groups} per layer')\n\ntrain_losses = []\ntrain_accuracies = []\nval_losses = []\nval_accuracies = []\nbest_val_accuracy = 0\n\ntrial_time += 1\nsave_path = 'best_model_15_' + str(trial_time) + '.pth'\n\nfor epoch in range(epochs):\n    print(f'--- group layers unfreezed: {num_layers_unfreeze} ')\n    print(f'Epoch [{epoch+1}/{epochs}], ', end='')\n    tl, ta, vl, va = train_model(model, train_loader, val_loader, num_groups_to_unfreeze=num_layers_unfreeze, num_layers=num_layers, num_groups=num_groups, num_epochs=1)\n    # Save the model if validation accuracy is the best we've seen so far\n    if va[-1] > best_val_accuracy:\n        best_val_accuracy = va[-1]\n        best_train_acc = ta[-1]\n        best_val_loss = vl[-1]\n        best_train_loss = tl[-1]\n        torch.save(model.state_dict(), save_path)\n\n#     if epoch in unfreeze_epochs:\n    num_layers_unfreeze += add_groups_freeze\n    train_losses.extend(tl)\n    train_accuracies.extend(ta)\n    val_losses.extend(vl)\n    val_accuracies.extend(va)\n\nprint(f'------ Best model with train_loss {best_train_loss:.4f}, train accuracy {best_train_acc:.2f}%, val_loss {best_val_loss:.4f}, val accuracy: {best_val_accuracy:.2f}%')\n\n# Plotting loss and accuracy\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(range(1, epochs + 1), train_losses, label='Training Loss')\nplt.plot(range(1, epochs + 1), val_losses, label='Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss over Epochs')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(range(1, epochs + 1), train_accuracies, label='Training Accuracy')\nplt.plot(range(1, epochs + 1), val_accuracies, label='Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy (%)')\nplt.title('Training and Validation Accuracy over Epochs')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n\ntest_loss, test_accuracy, precision, recall, f1 = evaluate_model(model, test_loader)","metadata":{"execution":{"iopub.status.busy":"2024-10-30T05:45:30.666650Z","iopub.execute_input":"2024-10-30T05:45:30.666987Z","iopub.status.idle":"2024-10-30T05:49:10.715250Z","shell.execute_reply.started":"2024-10-30T05:45:30.666951Z","shell.execute_reply":"2024-10-30T05:49:10.714053Z"},"trusted":true},"execution_count":null,"outputs":[]}]}