{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":3860.402315,"end_time":"2023-10-19T19:49:28.518484","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-10-19T18:45:08.116169","version":"2.4.0"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"datasetVersion","sourceId":928025,"datasetId":500970,"databundleVersionId":955383},{"sourceType":"datasetVersion","sourceId":7415566,"datasetId":4296413,"databundleVersionId":7506879}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install torch-summary\nimport 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\nfrom torchsummary import summary\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, models\nimport torch.nn as nn\nfrom tqdm.notebook import tqdm\nfrom IPython.display import FileLink","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":5.355432,"end_time":"2023-10-19T18:45:16.404255","exception":false,"start_time":"2023-10-19T18:45:11.048823","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:05:17.408789Z","iopub.execute_input":"2024-01-20T10:05:17.409135Z","iopub.status.idle":"2024-01-20T10:05:35.060046Z","shell.execute_reply.started":"2024-01-20T10:05:17.409107Z","shell.execute_reply":"2024-01-20T10:05:35.059045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda:0' if torch.cuda.is_available() else 'cpu'\nprint(device)","metadata":{"papermill":{"duration":0.079543,"end_time":"2023-10-19T18:45:16.487004","exception":false,"start_time":"2023-10-19T18:45:16.407461","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:05:35.061586Z","iopub.execute_input":"2024-01-20T10:05:35.062031Z","iopub.status.idle":"2024-01-20T10:05:35.089308Z","shell.execute_reply.started":"2024-01-20T10:05:35.062002Z","shell.execute_reply":"2024-01-20T10:05:35.088371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainPath = '/kaggle/input/urbansound8k/'\ntrainData = pd.read_csv('/kaggle/input/urbansound8k/UrbanSound8K.csv')\ntrainData.head()","metadata":{"papermill":{"duration":0.042534,"end_time":"2023-10-19T18:45:16.532179","exception":false,"start_time":"2023-10-19T18:45:16.489645","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:05:35.090617Z","iopub.execute_input":"2024-01-20T10:05:35.090887Z","iopub.status.idle":"2024-01-20T10:05:35.171185Z","shell.execute_reply.started":"2024-01-20T10:05:35.090864Z","shell.execute_reply":"2024-01-20T10:05:35.170297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(Dataset):\n    def __init__(self, dataframe, val=False, test=False):\n        self.val = val\n        if val==False and test==False:\n            self.dataframe = dataframe[~dataframe['fold'].isin([2, 10])]\n        elif val==True:\n            self.dataframe = dataframe[dataframe['fold'] == 10]\n        elif test == True:\n            self.dataframe = dataframe[dataframe['fold'] == 2]\n    def __getitem__(self, index):\n        path_to_file = self.get_path_to_file(index)\n        signal = self.preprocess_signal(path_to_file)\n\n        x = np.stack([cv2.resize(signal, (224, 224)) for _ in range(3)])\n\n        y = self.dataframe.classID.values[index]\n        return torch.tensor(x, dtype=torch.float), y\n\n    def get_path_to_file(self, index):\n        return f'/kaggle/input/urbansound8k/fold{self.dataframe.fold.values[index]}/{self.dataframe.slice_file_name.values[index]}'\n    def preprocess_signal(self, path_to_file):\n        signal, _ = librosa.load(path_to_file)\n        signal = librosa.feature.melspectrogram(y=signal)\n        return librosa.power_to_db(signal, ref=np.max)\n\n    def __len__(self):\n        return self.dataframe.shape[0]","metadata":{"papermill":{"duration":0.011647,"end_time":"2023-10-19T18:45:16.579946","exception":false,"start_time":"2023-10-19T18:45:16.568299","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:05:35.173814Z","iopub.execute_input":"2024-01-20T10:05:35.174208Z","iopub.status.idle":"2024-01-20T10:05:35.184100Z","shell.execute_reply.started":"2024-01-20T10:05:35.174174Z","shell.execute_reply":"2024-01-20T10:05:35.183162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\n\ntrainSet = Dataset(trainData)\nvalSet = Dataset(trainData, val=True)\ntestSet = Dataset(trainData, test=True)\ntrainLoader = DataLoader(trainSet, batch_size=batch_size, shuffle=True)\nvalLoader = DataLoader(valSet , batch_size=batch_size)\ntestLoader = DataLoader(testSet , batch_size=batch_size)\n\n\nprint('Training set: {}, Validation set: {}, Test set: {}'.format(len(trainSet), len(valSet), len(testSet)))","metadata":{"papermill":{"duration":0.015898,"end_time":"2023-10-19T18:45:16.598531","exception":false,"start_time":"2023-10-19T18:45:16.582633","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:05:35.185287Z","iopub.execute_input":"2024-01-20T10:05:35.185556Z","iopub.status.idle":"2024-01-20T10:05:35.205239Z","shell.execute_reply.started":"2024-01-20T10:05:35.185533Z","shell.execute_reply":"2024-01-20T10:05:35.204275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(trainSet):\n    print(i[0].shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-01-20T10:05:35.206467Z","iopub.execute_input":"2024-01-20T10:05:35.206808Z","iopub.status.idle":"2024-01-20T10:05:45.004001Z","shell.execute_reply.started":"2024-01-20T10:05:35.206774Z","shell.execute_reply":"2024-01-20T10:05:45.000679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":19.112322,"end_time":"2023-10-19T18:45:35.713571","exception":false,"start_time":"2023-10-19T18:45:16.601249","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torchvision import models\n\nclass CustomEffNet(nn.Module):\n    def __init__(self, num_classes=10):\n        super(CustomEffNet, self).__init__()\n        # Load a pre-trained EfficientNet\n        self.effnet = models.efficientnet_b0(pretrained=True)\n        # Freeze all layers in EfficientNet\n        for param in self.effnet.parameters():\n            param.requires_grad = True #Set true to unfreeze\n\n        # Get the input features of the original classifier\n        in_features = self.effnet.classifier[1].in_features\n\n        # Replace the classifier with custom layers\n        self.effnet.classifier = nn.Sequential(\n            nn.Linear(in_features, 512),\n            nn.ReLU(),\n            nn.Dropout(0.5),\n            nn.Linear(512, 256),\n            nn.ReLU(),\n            nn.Dropout(0.5),\n            nn.Linear(256, num_classes)\n        )\n\n        # Unfreeze the classifier layers\n        for param in self.effnet.classifier.parameters():\n            param.requires_grad = True\n\n    def forward(self, x):\n        return self.effnet(x)\n\n# Create the model\nmodel = CustomEffNet(num_classes=10)\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-20T10:05:45.005967Z","iopub.execute_input":"2024-01-20T10:05:45.006806Z","iopub.status.idle":"2024-01-20T10:05:45.686845Z","shell.execute_reply.started":"2024-01-20T10:05:45.006756Z","shell.execute_reply":"2024-01-20T10:05:45.685874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model, (3, 224, 224))","metadata":{"execution":{"iopub.status.busy":"2024-01-20T10:05:45.687956Z","iopub.execute_input":"2024-01-20T10:05:45.688218Z","iopub.status.idle":"2024-01-20T10:05:46.461662Z","shell.execute_reply.started":"2024-01-20T10:05:45.688196Z","shell.execute_reply":"2024-01-20T10:05:46.460697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from time import time \nstart_time = time()\n\nepochs = 50\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\ncost = torch.nn.CrossEntropyLoss()\nbest_val_accuracy = 0.0\ncheckpoint_path = 'model_checkpoint.pth'\n\nif os.path.isfile(checkpoint_path):\n    checkpoint = torch.load(checkpoint_path)\n    model.load_state_dict(checkpoint['model_state'])\n    optimizer.load_state_dict(checkpoint['optimizer_state'])\n    best_val_accuracy = checkpoint['best_val_accuracy']\n    start_epoch = checkpoint['epoch'] + 1\nelse:\n    start_epoch = 0\n    \nfor epoch in range(start_epoch, epochs):\n    train_loss = 0\n    val_loss = 0\n    train_correct = 0\n    val_correct = 0\n    model.train()\n    for x, y in tqdm(trainLoader):\n        optimizer.zero_grad()\n        x,y = x.to(device),y.to(device)\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        loss.backward()\n        optimizer.step()\n\n    model.eval()\n    with torch.no_grad():\n        for x, y in tqdm(valLoader):\n            x,y = x.to(device),y.to(device)\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    train_loss = train_loss/len(trainLoader)\n    val_loss = val_loss/len(valLoader)\n    train_accuracy = train_correct / len(trainData)\n    val_accuracy = val_correct / len(valSet)\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))\n    if val_accuracy > best_val_accuracy:\n        print(f\"Validation Accuracy improved from {best_val_accuracy:.5f} to {val_accuracy:.5f}. Saving checkpoint.\")\n        best_val_accuracy = val_accuracy\n        torch.save({\n            'epoch': epoch,\n            'model_state': model.state_dict(),\n            'optimizer_state': optimizer.state_dict(),\n            'best_val_accuracy': best_val_accuracy\n        }, \"efficientnet_baseline_bestmodel_epoch\"+str(epoch)+\".pth\")\n        FileLink(f'efficientnet_baseline_bestmodel_unfrozen_epoch\"+{epoch}+\".pth')\n        \nend_time = time()\ntotal_time = end_time - start_time\nprint(f'Total Training Time: {total_time:.2f} seconds')","metadata":{"papermill":{"duration":3408.179976,"end_time":"2023-10-19T19:42:23.897372","exception":false,"start_time":"2023-10-19T18:45:35.717396","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T10:06:10.172191Z","iopub.execute_input":"2024-01-20T10:06:10.172536Z","iopub.status.idle":"2024-01-20T14:48:13.327309Z","shell.execute_reply.started":"2024-01-20T10:06:10.172511Z","shell.execute_reply":"2024-01-20T14:48:13.326090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Miscellaneous codes below","metadata":{}},{"cell_type":"code","source":"checkpoint_path = './efficientnet_baseline_bestmodel_epoch27.pth'\ncheckpoint = torch.load(checkpoint_path)\nmodel.load_state_dict(checkpoint['model_state'])\nmodel.eval()\ntest_correct = 0\nfor x, y in tqdm(testLoader):\n    x,y = x.to(device),y.to(device)\n    pred = model(x)\n    test_correct += (pred.argmax(1) == y).type(torch.float).sum().item()\ntest_accuracy = test_correct / len(testSet)\ntest_accuracy","metadata":{"papermill":{"duration":421.238514,"end_time":"2023-10-19T19:49:25.147701","exception":false,"start_time":"2023-10-19T19:42:23.909187","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-20T14:48:45.720308Z","iopub.execute_input":"2024-01-20T14:48:45.720680Z","iopub.status.idle":"2024-01-20T14:49:37.658528Z","shell.execute_reply.started":"2024-01-20T14:48:45.720652Z","shell.execute_reply":"2024-01-20T14:49:37.657659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict, f'efficientnet_b0_urbansound8k_10epochs_{round(test_accuracy*100)}_percent.pth')","metadata":{"execution":{"iopub.status.busy":"2024-01-14T16:19:27.678959Z","iopub.execute_input":"2024-01-14T16:19:27.679305Z","iopub.status.idle":"2024-01-14T16:19:27.802569Z","shell.execute_reply.started":"2024-01-14T16:19:27.679274Z","shell.execute_reply":"2024-01-14T16:19:27.799835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}