{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from PIL import Image\nimport torch\nimport os\nimport cv2\nfrom tqdm import tqdm\nimport copy\nimport torch.optim as optim\nimport numpy as np\nimport optuna\nfrom sklearn.metrics import f1_score\nfrom optuna.exceptions import TrialPruned\nimport random\nimport torch.nn as nn\nfrom torchvision import datasets, models, transforms\nfrom torch.utils.data import DataLoader, Dataset\nimport pandas as pd\nfrom skimage import io\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport glob\nfrom sklearn.preprocessing import LabelEncoder\nimport keras\nfrom sklearn.metrics import confusion_matrix\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:50:51.346463Z","iopub.execute_input":"2024-03-29T01:50:51.347177Z","iopub.status.idle":"2024-03-29T01:50:51.354034Z","shell.execute_reply.started":"2024-03-29T01:50:51.347146Z","shell.execute_reply":"2024-03-29T01:50:51.353042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Dataset","metadata":{}},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:50:59.186528Z","iopub.execute_input":"2024-03-29T01:50:59.187120Z","iopub.status.idle":"2024-03-29T01:50:59.244943Z","shell.execute_reply.started":"2024-03-29T01:50:59.187090Z","shell.execute_reply":"2024-03-29T01:50:59.244051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"byClass = data.groupby('classname')\nbyClass.size().plot.bar(title='Number of images per class')\nfor index, value in enumerate(byClass.size()):\n    plt.text(index, value, str(value), ha = 'center',rotation=90)    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:51:01.823869Z","iopub.execute_input":"2024-03-29T01:51:01.824278Z","iopub.status.idle":"2024-03-29T01:51:02.190184Z","shell.execute_reply.started":"2024-03-29T01:51:01.824250Z","shell.execute_reply":"2024-03-29T01:51:02.189201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We Have 10 classes with 1900 in the smallest class and 2489 in the largest class <br>\nSo The Data is said to be `balanced`","metadata":{}},{"cell_type":"code","source":"bySubject = data.groupby('subject')\nbySubject.size().plot.bar(title='Number of images per subject',figsize=(10,5))\nfor index, value in enumerate(bySubject.size()):\n    plt.text(index, value, str(value), ha = 'center', rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:51:04.576658Z","iopub.execute_input":"2024-03-29T01:51:04.577341Z","iopub.status.idle":"2024-03-29T01:51:05.019043Z","shell.execute_reply.started":"2024-03-29T01:51:04.577312Z","shell.execute_reply":"2024-03-29T01:51:05.018130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<pre>There is 16 sunbject particibated in this experiment with different contribution in each class\nthe further investigation of which subject contributed to which class will help to explain why would\nmodel behave bad but this is to be done in after the modeling part if the model is not performing well</pre>","metadata":{}},{"cell_type":"code","source":"classes={'c0': 'safe driving'\n,'c1': 'texting - right'\n,'c2': 'talking on the phone - right'\n,'c3': 'texting - left'\n,'c4': 'talking on the phone - left'\n,'c5': 'operating the radio'\n,'c6': 'drinking'\n,'c7': 'reaching behind'\n,'c8': 'hair and makeup'\n,'c9': 'talking to passenger'\n}\n\n\nimgRow=224\nimgCol=224\n#BGRA to RGB\ncolorType=1","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:51:07.474727Z","iopub.execute_input":"2024-03-29T01:51:07.475372Z","iopub.status.idle":"2024-03-29T01:51:07.480279Z","shell.execute_reply.started":"2024-03-29T01:51:07.475342Z","shell.execute_reply":"2024-03-29T01:51:07.479472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pa='/kaggle/input/state-farm-distracted-driver-detection/imgs/train'\ntest_path='/kaggle/input/state-farm-distracted-driver-detection/imgs/test'","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:51:30.128320Z","iopub.execute_input":"2024-03-29T01:51:30.129099Z","iopub.status.idle":"2024-03-29T01:51:30.134559Z","shell.execute_reply.started":"2024-03-29T01:51:30.129067Z","shell.execute_reply":"2024-03-29T01:51:30.133298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getCV2img(path,h,w,colorType=1):\n    img=cv2.imread(path)\n    img=cv2.resize(img,(h,w))\n    if colorType==1:\n        img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n    else:\n        img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    return img","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:51:53.157066Z","iopub.execute_input":"2024-03-29T01:51:53.157414Z","iopub.status.idle":"2024-03-29T01:51:53.163106Z","shell.execute_reply.started":"2024-03-29T01:51:53.157389Z","shell.execute_reply":"2024-03-29T01:51:53.162134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=train_pa+'/c0/img_34.jpg'\nx","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:54:55.715988Z","iopub.execute_input":"2024-03-29T01:54:55.716329Z","iopub.status.idle":"2024-03-29T01:54:55.722070Z","shell.execute_reply.started":"2024-03-29T01:54:55.716306Z","shell.execute_reply":"2024-03-29T01:54:55.721214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst=getCV2img(x,224,224)\nplt.imshow(tst)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:54:58.619625Z","iopub.execute_input":"2024-03-29T01:54:58.620022Z","iopub.status.idle":"2024-03-29T01:54:58.886225Z","shell.execute_reply.started":"2024-03-29T01:54:58.619992Z","shell.execute_reply":"2024-03-29T01:54:58.885283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loadTrain(imgR,imgC,colorType=1):\n    trainX=[]\n    trainY=[]\n    for i in range(data.shape[0]):\n        imgPath=os.path.join(train_pa,data['classname'][i],data['img'][i])\n        img=getCV2img(imgPath,imgR,imgC,colorType)\n        trainX.append(img)\n        trainY.append(data['classname'][i])\n    return np.array(trainX),np.array(trainY)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:55:01.728670Z","iopub.execute_input":"2024-03-29T01:55:01.729326Z","iopub.status.idle":"2024-03-29T01:55:01.734938Z","shell.execute_reply.started":"2024-03-29T01:55:01.729295Z","shell.execute_reply":"2024-03-29T01:55:01.734149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loadTest(imgR,imgC,colorType=1):\n    testX=[]\n    idTest=[]\n    path=os.path.join(test_path)\n    files=sorted(glob.glob(path))\n    files=os.listdir(test_path)\n    fsz=len(files)\n    for i in range(fsz):\n        imgPath=os.path.join(test_path,files[i])\n        img=getCV2img(imgPath,imgR,imgC,colorType)\n        testX.append(img)\n        idTest.append(files[i])\n    return np.array(testX),np.array(idTest)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:55:18.458337Z","iopub.execute_input":"2024-03-29T01:55:18.458953Z","iopub.status.idle":"2024-03-29T01:55:18.465914Z","shell.execute_reply.started":"2024-03-29T01:55:18.458891Z","shell.execute_reply":"2024-03-29T01:55:18.464889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testData,testId=loadTest(imgRow,imgCol,colorType)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T01:55:19.527510Z","iopub.execute_input":"2024-03-29T01:55:19.527856Z","iopub.status.idle":"2024-03-29T02:24:21.005949Z","shell.execute_reply.started":"2024-03-29T01:55:19.527829Z","shell.execute_reply":"2024-03-29T02:24:21.004928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Unnormalized Data\nDataX,DataY=loadTrain(imgRow,imgCol)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:24:21.007585Z","iopub.execute_input":"2024-03-29T02:24:21.007976Z","iopub.status.idle":"2024-03-29T02:31:41.101147Z","shell.execute_reply.started":"2024-03-29T02:24:21.007942Z","shell.execute_reply":"2024-03-29T02:31:41.100313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(DataX[0])\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:26.929715Z","iopub.execute_input":"2024-03-29T02:43:26.930336Z","iopub.status.idle":"2024-03-29T02:43:27.129502Z","shell.execute_reply.started":"2024-03-29T02:43:26.930306Z","shell.execute_reply":"2024-03-29T02:43:27.128615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DataY=DataY.reshape(-1)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:29.206185Z","iopub.execute_input":"2024-03-29T02:43:29.206825Z","iopub.status.idle":"2024-03-29T02:43:29.210521Z","shell.execute_reply.started":"2024-03-29T02:43:29.206798Z","shell.execute_reply":"2024-03-29T02:43:29.209725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nDataY = label_encoder.fit_transform(DataY)\nDataY = keras.utils.to_categorical(DataY, 10)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:29.714973Z","iopub.execute_input":"2024-03-29T02:43:29.715250Z","iopub.status.idle":"2024-03-29T02:43:29.721426Z","shell.execute_reply.started":"2024-03-29T02:43:29.715227Z","shell.execute_reply":"2024-03-29T02:43:29.720677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DataX.shape,DataY.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:30.557562Z","iopub.execute_input":"2024-03-29T02:43:30.557923Z","iopub.status.idle":"2024-03-29T02:43:30.563772Z","shell.execute_reply.started":"2024-03-29T02:43:30.557877Z","shell.execute_reply":"2024-03-29T02:43:30.563015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainX,testX,trainY,testY=train_test_split(DataX,DataY,test_size=0.2,random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:31.248049Z","iopub.execute_input":"2024-03-29T02:43:31.248338Z","iopub.status.idle":"2024-03-29T02:43:31.952867Z","shell.execute_reply.started":"2024-03-29T02:43:31.248315Z","shell.execute_reply":"2024-03-29T02:43:31.952095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainX.shape,trainY.shape,testX.shape,testY.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:32.154096Z","iopub.execute_input":"2024-03-29T02:43:32.154381Z","iopub.status.idle":"2024-03-29T02:43:32.161346Z","shell.execute_reply.started":"2024-03-29T02:43:32.154357Z","shell.execute_reply":"2024-03-29T02:43:32.160485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Base Model","metadata":{}},{"cell_type":"markdown","source":"The Base Model is trained only on Normalized Data with 10 classes and 16 subjects","metadata":{}},{"cell_type":"code","source":"resnet=models.resnet50(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:34.331362Z","iopub.execute_input":"2024-03-29T02:43:34.332289Z","iopub.status.idle":"2024-03-29T02:43:36.006536Z","shell.execute_reply.started":"2024-03-29T02:43:34.332249Z","shell.execute_reply":"2024-03-29T02:43:36.005615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyDataset:\n    def __init__(self,X,Y,transform=None):\n        self.X=X\n        self.Y=Y\n        self.transform=transform\n        self.idx=-1\n    def __len__(self):\n        return len(self.X)\n    def __getitem__(self,idx):\n        self.idx=idx\n        img=self.X[idx]\n        label=self.Y[idx]\n        if self.transform:\n            img=self.transform(img)\n        return img,label\n    def __next_data__(self):\n        return self.__getitem__(self.idx+1)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:55:29.245749Z","iopub.execute_input":"2024-03-29T02:55:29.246432Z","iopub.status.idle":"2024-03-29T02:55:29.255319Z","shell.execute_reply.started":"2024-03-29T02:55:29.246400Z","shell.execute_reply":"2024-03-29T02:55:29.254440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Device configuration\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Assuming you have your own trainX and trainY data\n# Example transforms\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Resize((224, 224)),  # Resize images to 224x224\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # Normalize with ImageNet stats\n])\n\n# Assuming you have your own trainX and trainY data\n# Example dataset and dataloader\ntrainset = MyDataset(trainX, trainY, transform=transform)  # Fix: Change DataX to trainX\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)\n\n# Load pre-trained ResNet model\nmodel = models.resnet18(weights=True)\n\n# Replace the output layer with a new fully connected layer for 10 classes\nnum_ftrs = model.fc.in_features\nmodel.fc = nn.Linear(num_ftrs, 10)  # Assuming you have 10 classes\nmodel = model.to(device)\n\n# Loss and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)\n\n# Training loop\nnum_epochs = 5  # Example number of epochs\ntotal_step = len(trainloader)\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(trainloader):\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Forward pass\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        # Backward and optimize\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        if (i+1) % 100 == 0:\n            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'\n                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))\n\nprint('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:43:37.906469Z","iopub.execute_input":"2024-03-29T02:43:37.906812Z","iopub.status.idle":"2024-03-29T02:48:06.118964Z","shell.execute_reply.started":"2024-03-29T02:43:37.906784Z","shell.execute_reply":"2024-03-29T02:48:06.117975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()  # Set model to evaluation mode","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:48:06.121157Z","iopub.execute_input":"2024-03-29T02:48:06.121636Z","iopub.status.idle":"2024-03-29T02:48:06.130074Z","shell.execute_reply.started":"2024-03-29T02:48:06.121600Z","shell.execute_reply":"2024-03-29T02:48:06.129165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(model, testloader):\n    with torch.no_grad():\n        correct = 0\n        total = 0\n        for images, labels in testloader:\n            images = images.to(device)\n            labels = labels.to(device)\n            outputs = model(images)\n            _, predicted = torch.max(outputs.data, 1)\n            total += labels.size(0)\n            labels = labels.argmax(dim=1)\n            correct += (predicted == labels).sum().item()\n        print('Accuracy of the network on the test images: {} %'.format(100 * correct / total))","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:48:06.131381Z","iopub.execute_input":"2024-03-29T02:48:06.131773Z","iopub.status.idle":"2024-03-29T02:48:06.169084Z","shell.execute_reply.started":"2024-03-29T02:48:06.131739Z","shell.execute_reply":"2024-03-29T02:48:06.168347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntestset = MyDataset(testX, testY, transform=transform)  # Fix: Change DataX to testX\ntestloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)\nprint('Evaluating model on test data')\nevaluate(model, testloader)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:55:36.715534Z","iopub.execute_input":"2024-03-29T02:55:36.715868Z","iopub.status.idle":"2024-03-29T02:55:42.823104Z","shell.execute_reply.started":"2024-03-29T02:55:36.715842Z","shell.execute_reply":"2024-03-29T02:55:42.822245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save base model\ntorch.save(model.state_dict(), 'BaseModel.pth')","metadata":{"execution":{"iopub.status.busy":"2024-03-29T02:48:12.466447Z","iopub.execute_input":"2024-03-29T02:48:12.466747Z","iopub.status.idle":"2024-03-29T02:48:12.557439Z","shell.execute_reply.started":"2024-03-29T02:48:12.466722Z","shell.execute_reply":"2024-03-29T02:48:12.556324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class mytestDataset:\n    def __init__(self,X,transform=None):\n        self.X=X\n        self.transform=transform\n        self.idx=-1\n    def __len__(self):\n        return len(self.X)\n    def __getitem__(self,idx):\n        self.idx=idx\n        img=self.X[idx]\n        if self.transform:\n            img=self.transform(img)\n        return img\n    def __next_data__(self):\n        return self.__getitem__(self.idx+1)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T03:05:10.360460Z","iopub.execute_input":"2024-03-29T03:05:10.360848Z","iopub.status.idle":"2024-03-29T03:05:10.367254Z","shell.execute_reply.started":"2024-03-29T03:05:10.360820Z","shell.execute_reply":"2024-03-29T03:05:10.366360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if testData.size == 0:\n    print(\"Error: Input image size is empty.\")\nelse:\n    testset = mytestDataset(testData, transform=transform)\n    testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)\n    print(type(testloader))\n    y_pred=[]\n    for images in testloader:\n        images = images.to(device)\n        outputs = model(images)\n        _, predicted = torch.max(outputs.data, 1)\n        y_pred.append(predicted.cpu().numpy())\n    y_pred = np.concatenate(y_pred).reshape(-1, 1)\n    y_pred = label_encoder.inverse_transform(y_pred)\n    submission = pd.DataFrame({'img': testId, 'classname': y_pred})\n    submission.to_csv('submission.csv', index=False)\n    print(\"Submission file created successfully.\")\n    ","metadata":{"execution":{"iopub.status.busy":"2024-03-29T03:09:55.095267Z","iopub.execute_input":"2024-03-29T03:09:55.095998Z","iopub.status.idle":"2024-03-29T03:11:42.558758Z","shell.execute_reply.started":"2024-03-29T03:09:55.095967Z","shell.execute_reply":"2024-03-29T03:11:42.557787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}