{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nimport time\nfrom tqdm import tqdm\nimport random\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd\nimport PIL.Image\nfrom IPython.display import Image\nfrom sklearn.metrics import confusion_matrix\n\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision import models,transforms,datasets","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:28.202138Z","iopub.execute_input":"2022-08-19T06:35:28.202706Z","iopub.status.idle":"2022-08-19T06:35:31.622623Z","shell.execute_reply.started":"2022-08-19T06:35:28.202599Z","shell.execute_reply":"2022-08-19T06:35:31.621429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing and preparing data","metadata":{}},{"cell_type":"code","source":"path_train = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\"\nclasses = [c for c in os.listdir(path_train) if not c.startswith(\".\")]\nclasses.sort()\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:36.227082Z","iopub.execute_input":"2022-08-19T06:35:36.227600Z","iopub.status.idle":"2022-08-19T06:35:36.243725Z","shell.execute_reply.started":"2022-08-19T06:35:36.227566Z","shell.execute_reply":"2022-08-19T06:35:36.242746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_dict = {0 : \"safe driving\",\n              1 : \"texting - right\",\n              2 : \"talking on the phone - right\",\n              3 : \"texting - left\",\n              4 : \"talking on the phone - left\",\n              5 : \"operating the radio\",\n              6 : \"drinking\",\n              7 : \"reaching behind\",\n              8 : \"hair and makeup\",\n              9 : \"talking to passenger\"}","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:39.392680Z","iopub.execute_input":"2022-08-19T06:35:39.393798Z","iopub.status.idle":"2022-08-19T06:35:39.398939Z","shell.execute_reply.started":"2022-08-19T06:35:39.393750Z","shell.execute_reply":"2022-08-19T06:35:39.398041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = {\"img\" : [], \"class\" : []}\nfor c in classes:\n    imgs = [img for img in os.listdir(os.path.join(path_train,c)) if not img.startswith(\".\")]\n    for img in imgs:\n        d[\"img\"].append(img)\n        d[\"class\"].append(c)\ndf = pd.DataFrame(d)\nax = sns.countplot(data=df,x=\"class\")\nax.set(title=\"Classes distribution\")\nprint(\"Total number of training data :\",len(df))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:41.087702Z","iopub.execute_input":"2022-08-19T06:35:41.088211Z","iopub.status.idle":"2022-08-19T06:35:46.241646Z","shell.execute_reply.started":"2022-08-19T06:35:41.088163Z","shell.execute_reply":"2022-08-19T06:35:46.240734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([transforms.Resize((400, 400)),\n                                 transforms.RandomRotation(10),\n                                 transforms.ToTensor()])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:48.656700Z","iopub.execute_input":"2022-08-19T06:35:48.657604Z","iopub.status.idle":"2022-08-19T06:35:48.662099Z","shell.execute_reply.started":"2022-08-19T06:35:48.657568Z","shell.execute_reply":"2022-08-19T06:35:48.661121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = datasets.ImageFolder(root = path_train, transform = transform)\n\ntotal_len = len(data)\ntraining_len = int(0.8*total_len)\ntesting_len = total_len - training_len\n\ntraining_data,testing_data = torch.utils.data.random_split(data,(training_len,testing_len))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:35:50.135672Z","iopub.execute_input":"2022-08-19T06:35:50.136183Z","iopub.status.idle":"2022-08-19T06:36:15.238907Z","shell.execute_reply.started":"2022-08-19T06:35:50.136139Z","shell.execute_reply":"2022-08-19T06:36:15.238058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(dataset=training_data,\n                                           batch_size=64,\n                                           shuffle=True,\n                                           drop_last=False)\ntest_loader = torch.utils.data.DataLoader(dataset=testing_data,\n                                          batch_size=64,\n                                          shuffle=False,\n                                          drop_last=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:26.966226Z","iopub.execute_input":"2022-08-19T06:36:26.966592Z","iopub.status.idle":"2022-08-19T06:36:26.972397Z","shell.execute_reply.started":"2022-08-19T06:36:26.966561Z","shell.execute_reply":"2022-08-19T06:36:26.971169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img,c = data[0]\nprint(img.shape)\nprint(\"Label:\", classes[c], f\"({class_dict[c]})\")\nplt.imshow(img.permute(1,2,0))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:28.600375Z","iopub.execute_input":"2022-08-19T06:36:28.600759Z","iopub.status.idle":"2022-08-19T06:36:28.865127Z","shell.execute_reply.started":"2022-08-19T06:36:28.600726Z","shell.execute_reply":"2022-08-19T06:36:28.864329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader,labels = next(iter(train_loader))\nprint(loader.shape)\nprint(labels.view(8,8))\nplt.figure(figsize=(16,16))\nplt.imshow(torchvision.utils.make_grid(loader,nrow=8).permute((1,2,0)))\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:32.696570Z","iopub.execute_input":"2022-08-19T06:36:32.696968Z","iopub.status.idle":"2022-08-19T06:36:36.631871Z","shell.execute_reply.started":"2022-08-19T06:36:32.696935Z","shell.execute_reply":"2022-08-19T06:36:36.630939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating and training the model","metadata":{}},{"cell_type":"code","source":"device = torch.device(\"cuda:0\")\nprint(device)\nprint(torch.cuda.get_device_name(device))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:36.894910Z","iopub.execute_input":"2022-08-19T06:36:36.895292Z","iopub.status.idle":"2022-08-19T06:36:36.968205Z","shell.execute_reply.started":"2022-08-19T06:36:36.895259Z","shell.execute_reply":"2022-08-19T06:36:36.967325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The model works better with 'normalized' data.","metadata":{}},{"cell_type":"code","source":"transform = transforms.Compose([transforms.Resize((400, 400)),\n                           transforms.RandomRotation(10),\n                           transforms.ToTensor(),\n                           transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n                          ])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:40.090172Z","iopub.execute_input":"2022-08-19T06:36:40.090538Z","iopub.status.idle":"2022-08-19T06:36:40.095925Z","shell.execute_reply.started":"2022-08-19T06:36:40.090506Z","shell.execute_reply":"2022-08-19T06:36:40.094823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = datasets.ImageFolder(root = path_train, transform = transform)\n\ntotal_len = len(data)\ntraining_len = int(0.8*total_len)\ntesting_len = total_len - training_len\n\ntraining_data,testing_data = torch.utils.data.random_split(data,(training_len,testing_len))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:42.825746Z","iopub.execute_input":"2022-08-19T06:36:42.826421Z","iopub.status.idle":"2022-08-19T06:36:46.268868Z","shell.execute_reply.started":"2022-08-19T06:36:42.826383Z","shell.execute_reply":"2022-08-19T06:36:46.267966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(dataset=training_data,\n                                           batch_size=32,\n                                           shuffle=True,\n                                           drop_last=False,\n                                           num_workers=2)\ntest_loader = torch.utils.data.DataLoader(dataset=testing_data,\n                                          batch_size=32,\n                                          shuffle=False,\n                                          drop_last=False,\n                                          num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:50.197054Z","iopub.execute_input":"2022-08-19T06:36:50.197903Z","iopub.status.idle":"2022-08-19T06:36:50.204871Z","shell.execute_reply.started":"2022-08-19T06:36:50.197866Z","shell.execute_reply":"2022-08-19T06:36:50.204093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model, criterion, optimizer, scheduler, n_epochs = 5):\n    \n    losses = []\n    accuracies = []\n    test_accuracies = []\n    # set the model to train mode initially\n    model.train()\n    for epoch in tqdm(range(n_epochs)):\n        since = time.time()\n        running_loss = 0.0\n        running_correct = 0.0\n        for data in train_loader:\n\n            # get the inputs and assign them to cuda\n            inputs, labels = data\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n            optimizer.zero_grad()\n            \n            # forward + backward + optimize\n            outputs = model(inputs)\n            _, predicted = torch.max(outputs.data, 1)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            \n            # calculate the loss/acc later\n            running_loss += loss.item()\n            running_correct += (labels==predicted).sum().item()\n\n        epoch_duration = time.time()-since\n        epoch_loss = running_loss/len(train_loader)\n        epoch_acc = 100/32*running_correct/len(train_loader)\n\n        print(\"Epoch %s, duration: %d s, loss: %.4f, acc: %.4f\" % (epoch+1, epoch_duration, epoch_loss, epoch_acc))\n        \n        losses.append(epoch_loss)\n        accuracies.append(epoch_acc)\n        \n        # switch the model to eval mode to evaluate on test data\n        model.eval()\n        test_acc = eval_model(model)\n        test_accuracies.append(test_acc)\n        \n        # re-set the model to train mode after validating\n        model.train()\n        scheduler.step(test_acc)\n        since = time.time()\n    print('Finished Training')\n    return model, losses, accuracies, test_accuracies","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:55.330292Z","iopub.execute_input":"2022-08-19T06:36:55.330688Z","iopub.status.idle":"2022-08-19T06:36:55.341116Z","shell.execute_reply.started":"2022-08-19T06:36:55.330634Z","shell.execute_reply":"2022-08-19T06:36:55.340041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval_model(model):\n    correct = 0.0\n    total = 0.0\n    with torch.no_grad():\n        for i, data in enumerate(test_loader, 0):\n            images, labels = data\n            images = images.to(device)\n            labels = labels.to(device)\n            \n            outputs = model_ft(images)\n            _, predicted = torch.max(outputs.data, 1)\n            \n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n    test_acc = 100.0 * correct / total\n    print('Accuracy of the network on the test images: %d %%' % (\n        test_acc))\n    return test_acc","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:36:59.015738Z","iopub.execute_input":"2022-08-19T06:36:59.016460Z","iopub.status.idle":"2022-08-19T06:36:59.023604Z","shell.execute_reply.started":"2022-08-19T06:36:59.016424Z","shell.execute_reply":"2022-08-19T06:36:59.022490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft = models.resnet50(pretrained=True)\nnum_ftrs = model_ft.fc.in_features\n\nmodel_ft.fc = nn.Linear(num_ftrs, 10) #No. of classes = 10\nmodel_ft = model_ft.to(device)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model_ft.parameters(), lr=0.01, momentum=0.9)\nlrscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=3, threshold = 0.9)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:37:02.599354Z","iopub.execute_input":"2022-08-19T06:37:02.599735Z","iopub.status.idle":"2022-08-19T06:37:17.101695Z","shell.execute_reply.started":"2022-08-19T06:37:02.599703Z","shell.execute_reply":"2022-08-19T06:37:17.100884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# takes around 5-6 minutes per epoch with GPU\nmodel_ft, training_losses, training_accs, test_accs = train_model(model_ft, criterion, optimizer, lrscheduler, n_epochs=3)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:37:17.103189Z","iopub.execute_input":"2022-08-19T06:37:17.103563Z","iopub.status.idle":"2022-08-19T06:56:14.799223Z","shell.execute_reply.started":"2022-08-19T06:37:17.103529Z","shell.execute_reply":"2022-08-19T06:56:14.798166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Training losses')\nplt.plot(training_losses)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:33.459362Z","iopub.execute_input":"2022-08-19T06:58:33.460164Z","iopub.status.idle":"2022-08-19T06:58:33.655278Z","shell.execute_reply.started":"2022-08-19T06:58:33.460116Z","shell.execute_reply":"2022-08-19T06:58:33.654496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Training Accuracy')\nplt.plot(training_accs)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:38.743096Z","iopub.execute_input":"2022-08-19T06:58:38.744014Z","iopub.status.idle":"2022-08-19T06:58:38.937764Z","shell.execute_reply.started":"2022-08-19T06:58:38.743968Z","shell.execute_reply":"2022-08-19T06:58:38.936986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Test Accuracy')\nplt.plot(test_accs)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:40.668174Z","iopub.execute_input":"2022-08-19T06:58:40.668787Z","iopub.status.idle":"2022-08-19T06:58:40.922339Z","shell.execute_reply.started":"2022-08-19T06:58:40.668746Z","shell.execute_reply":"2022-08-19T06:58:40.921495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model_ft,\"/kaggle/working/model-best.hd5\")","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:56:58.936075Z","iopub.execute_input":"2022-08-19T07:56:58.936828Z","iopub.status.idle":"2022-08-19T07:56:59.143350Z","shell.execute_reply.started":"2022-08-19T07:56:58.936789Z","shell.execute_reply":"2022-08-19T07:56:59.142433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model_ft.state_dict(), \"/kaggle/working/model-driver\")","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:44.615340Z","iopub.execute_input":"2022-08-19T06:58:44.615725Z","iopub.status.idle":"2022-08-19T06:58:44.808065Z","shell.execute_reply.started":"2022-08-19T06:58:44.615690Z","shell.execute_reply":"2022-08-19T06:58:44.807225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing the model and submitting csv","metadata":{}},{"cell_type":"code","source":"model = models.resnet50()\nnum_ftrs = model.fc.in_features\nmodel.fc = nn.Linear(num_ftrs, 10)\nmodel.load_state_dict(torch.load(\"/kaggle/working/model-driver\"))\nmodel.eval()\nmodel.cuda()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:51.994448Z","iopub.execute_input":"2022-08-19T06:58:51.994857Z","iopub.status.idle":"2022-08-19T06:58:52.731366Z","shell.execute_reply.started":"2022-08-19T06:58:51.994823Z","shell.execute_reply":"2022-08-19T06:58:52.730533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_test = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/test\"\nlist_img_test = [img for img in os.listdir(path_test) if not img.startswith(\".\")]\nlist_img_test.sort()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:58:58.342125Z","iopub.execute_input":"2022-08-19T06:58:58.342530Z","iopub.status.idle":"2022-08-19T06:59:00.617274Z","shell.execute_reply.started":"2022-08-19T06:58:58.342495Z","shell.execute_reply":"2022-08-19T06:59:00.616453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:40:47.266402Z","iopub.execute_input":"2022-08-19T07:40:47.266881Z","iopub.status.idle":"2022-08-19T07:40:47.309894Z","shell.execute_reply.started":"2022-08-19T07:40:47.266841Z","shell.execute_reply":"2022-08-19T07:40:47.309067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:40:38.042938Z","iopub.execute_input":"2022-08-19T07:40:38.043307Z","iopub.status.idle":"2022-08-19T07:40:38.087320Z","shell.execute_reply.started":"2022-08-19T07:40:38.043275Z","shell.execute_reply":"2022-08-19T07:40:38.086358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:40:25.653041Z","iopub.execute_input":"2022-08-19T07:40:25.653627Z","iopub.status.idle":"2022-08-19T07:40:25.696914Z","shell.execute_reply.started":"2022-08-19T07:40:25.653593Z","shell.execute_reply":"2022-08-19T07:40:25.695957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:59:01.005378Z","iopub.execute_input":"2022-08-19T06:59:01.006113Z","iopub.status.idle":"2022-08-19T06:59:01.074476Z","shell.execute_reply.started":"2022-08-19T06:59:01.006074Z","shell.execute_reply":"2022-08-19T06:59:01.073352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:02:05.531647Z","iopub.execute_input":"2022-08-19T07:02:05.532105Z","iopub.status.idle":"2022-08-19T07:02:05.589767Z","shell.execute_reply.started":"2022-08-19T07:02:05.532067Z","shell.execute_reply":"2022-08-19T07:02:05.588901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:01:58.715348Z","iopub.execute_input":"2022-08-19T07:01:58.716146Z","iopub.status.idle":"2022-08-19T07:01:58.759029Z","shell.execute_reply.started":"2022-08-19T07:01:58.716098Z","shell.execute_reply":"2022-08-19T07:01:58.758199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:01:50.657382Z","iopub.execute_input":"2022-08-19T07:01:50.658030Z","iopub.status.idle":"2022-08-19T07:01:50.700894Z","shell.execute_reply.started":"2022-08-19T07:01:50.657993Z","shell.execute_reply":"2022-08-19T07:01:50.700063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:38:46.584768Z","iopub.execute_input":"2022-08-19T07:38:46.585373Z","iopub.status.idle":"2022-08-19T07:38:46.629962Z","shell.execute_reply.started":"2022-08-19T07:38:46.585338Z","shell.execute_reply":"2022-08-19T07:38:46.629132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = random.choice(list_img_test)\nim_path = os.path.join(path_test,file)\ndisplay(Image(filename=im_path))\nwith PIL.Image.open(im_path) as im:\n    im = transform(im)\n    im = im.unsqueeze(0)\n    output = model(im.cuda())\n    proba = nn.Softmax(dim=1)(output)\n    proba = [round(float(elem),4) for elem in proba[0]]\n    print(proba)\n    print(\"Predicted class:\",class_dict[proba.index(max(proba))])\n    print(\"Confidence:\",max(proba))\n    proba2 = proba.copy()\n    proba2[proba2.index(max(proba2))] = 0.\n    print(\"2nd answer:\",class_dict[proba2.index(max(proba2))])\n    print(\"Confidence:\",max(proba2))","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:01:41.315976Z","iopub.execute_input":"2022-08-19T07:01:41.316558Z","iopub.status.idle":"2022-08-19T07:01:41.368411Z","shell.execute_reply.started":"2022-08-19T07:01:41.316520Z","shell.execute_reply":"2022-08-19T07:01:41.367573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def true_pred(test_data,model):\n    y_true = []\n    y_pred = []\n    n = len(test_data)\n    sum = 0\n    with torch.no_grad():\n        for x,y in tqdm(test_data):\n            x = x.to(device)\n            pred = torch.argmax(model(x),dim=1)\n            y_true.extend(list(np.array(y)))\n            y_pred.extend(list(np.array(pred.cpu())))\n    return y_true,y_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:59:14.113017Z","iopub.execute_input":"2022-08-19T06:59:14.115886Z","iopub.status.idle":"2022-08-19T06:59:14.126379Z","shell.execute_reply.started":"2022-08-19T06:59:14.115831Z","shell.execute_reply":"2022-08-19T06:59:14.125598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true,y_pred = true_pred(test_loader,model)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T06:59:14.429905Z","iopub.execute_input":"2022-08-19T06:59:14.430271Z","iopub.status.idle":"2022-08-19T07:00:12.040250Z","shell.execute_reply.started":"2022-08-19T06:59:14.430241Z","shell.execute_reply":"2022-08-19T07:00:12.038953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = confusion_matrix(y_true, y_pred)\nm  = m.astype('float') / m.sum(axis=1)[:, np.newaxis]","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:01:02.408134Z","iopub.execute_input":"2022-08-19T07:01:02.408944Z","iopub.status.idle":"2022-08-19T07:01:02.417619Z","shell.execute_reply.started":"2022-08-19T07:01:02.408904Z","shell.execute_reply":"2022-08-19T07:01:02.416622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(m)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T07:00:12.054866Z","iopub.execute_input":"2022-08-19T07:00:12.055354Z","iopub.status.idle":"2022-08-19T07:00:12.327551Z","shell.execute_reply.started":"2022-08-19T07:00:12.055252Z","shell.execute_reply":"2022-08-19T07:00:12.326800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need to create a '/test/test' so that we can use `datasets.ImageFolder` and use a loader which is faster than iterating one by one (40 minutes) through all imgs/test files.","metadata":{}},{"cell_type":"code","source":"os.mkdir(\"/kaggle/working/test\")","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:05:29.335898Z","iopub.execute_input":"2022-06-17T12:05:29.336497Z","iopub.status.idle":"2022-06-17T12:05:29.342393Z","shell.execute_reply.started":"2022-06-17T12:05:29.336449Z","shell.execute_reply":"2022-06-17T12:05:29.341078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in tqdm(list_img_test):\n    os.mkdir(\"/kaggle/working/test/\"+img[:-4])\n    source = path_test+\"/\"+img\n    destination = \"/kaggle/working/test/\"+img[:-4]+\"/\"+img\n    shutil.copy(source, destination)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:05:29.344409Z","iopub.execute_input":"2022-06-17T12:05:29.345245Z","iopub.status.idle":"2022-06-17T12:18:33.379366Z","shell.execute_reply.started":"2022-06-17T12:05:29.345197Z","shell.execute_reply":"2022-06-17T12:18:33.378075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform_test = transforms.Compose([transforms.Resize((400, 400)),\n                                     #transforms.RandomRotation(10),\n                                     transforms.ToTensor(),\n                                     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n                               ])","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:33.381144Z","iopub.execute_input":"2022-06-17T12:18:33.381629Z","iopub.status.idle":"2022-06-17T12:18:33.388356Z","shell.execute_reply.started":"2022-06-17T12:18:33.381571Z","shell.execute_reply":"2022-06-17T12:18:33.387075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datatest = datasets.ImageFolder(root = \"/kaggle/working/test\",\n                                transform = transform_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:33.390122Z","iopub.execute_input":"2022-06-17T12:18:33.390552Z","iopub.status.idle":"2022-06-17T12:18:36.720838Z","shell.execute_reply.started":"2022-06-17T12:18:33.390509Z","shell.execute_reply":"2022-06-17T12:18:36.719637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader = torch.utils.data.DataLoader(dataset=datatest,\n                                     batch_size=16,\n                                     shuffle=False,\n                                     drop_last=False,\n                                     num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:36.722381Z","iopub.execute_input":"2022-06-17T12:18:36.722837Z","iopub.status.idle":"2022-06-17T12:18:36.73719Z","shell.execute_reply.started":"2022-06-17T12:18:36.722794Z","shell.execute_reply":"2022-06-17T12:18:36.735101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(iter(loader))","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:36.739111Z","iopub.execute_input":"2022-06-17T12:18:36.739572Z","iopub.status.idle":"2022-06-17T12:18:37.870731Z","shell.execute_reply.started":"2022-06-17T12:18:36.739527Z","shell.execute_reply":"2022-06-17T12:18:37.869243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y)\nplt.figure(figsize=(16,16))\nplt.imshow(torchvision.utils.make_grid(x,nrow=8).permute((1,2,0)))\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:37.87901Z","iopub.execute_input":"2022-06-17T12:18:37.881837Z","iopub.status.idle":"2022-06-17T12:18:38.673341Z","shell.execute_reply.started":"2022-06-17T12:18:37.881787Z","shell.execute_reply":"2022-06-17T12:18:38.671498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/state-farm-distracted-driver-detection/sample_submission.csv\",index_col = 0)","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:38.675299Z","iopub.execute_input":"2022-06-17T12:18:38.676028Z","iopub.status.idle":"2022-06-17T12:18:38.84742Z","shell.execute_reply.started":"2022-06-17T12:18:38.675986Z","shell.execute_reply":"2022-06-17T12:18:38.846367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"line = 0\nfor x,y in tqdm(loader,total = len(loader)) :\n    output = model_ft(x.cuda())\n    output = nn.Softmax(dim=1)(output)\n    for i in range(len(output)) :\n        proba = [float(elem) for elem in output[i]]\n        df.iloc[line][:]=proba\n        line += 1","metadata":{"execution":{"iopub.status.busy":"2022-06-17T12:18:38.849267Z","iopub.execute_input":"2022-06-17T12:18:38.849837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img in tqdm(list_img_test):\n    os.remove(\"/kaggle/working/test/\"+img[:-4]+\"/\"+img)\n    os.rmdir(\"/kaggle/working/test/\"+img[:-4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.rmdir(\"/kaggle/working/test\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"/kaggle/working/submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}