{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"papermill":{"default_parameters":{},"duration":11941.014877,"end_time":"2023-07-31T07:28:46.965525","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-07-31T04:09:45.950648","version":"2.4.0"},"colab":{"provenance":[{"file_id":"1l_XnSYjAZOFE8f5fDI2BhrteH-MikUeH","timestamp":1733151575945}],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":85240,"databundleVersionId":9622164,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imports","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-07-30T11:34:23.871202Z","iopub.status.busy":"2023-07-30T11:34:23.870814Z","iopub.status.idle":"2023-07-30T11:34:24.499667Z","shell.execute_reply":"2023-07-30T11:34:24.498626Z","shell.execute_reply.started":"2023-07-30T11:34:23.87117Z"},"papermill":{"duration":0.00922,"end_time":"2023-07-31T04:09:56.161922","exception":false,"start_time":"2023-07-31T04:09:56.152702","status":"completed"},"tags":[],"id":"0a002225"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport copy\nimport os\nimport cv2\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.model_selection import ShuffleSplit\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch.utils.data import Subset\nimport torchvision.transforms.functional as tf\nimport torch.optim as optim\nimport torchvision\nfrom torchvision.models.detection import fasterrcnn_mobilenet_v3_large_fpn\nfrom torchvision.models.detection import FasterRCNN_MobileNet_V3_Large_FPN_Weights\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom tqdm import tqdm\n%matplotlib inline","metadata":{"papermill":{"duration":5.411068,"end_time":"2023-07-31T04:10:01.581861","exception":false,"start_time":"2023-07-31T04:09:56.170793","status":"completed"},"tags":[],"id":"6a7dc8a9","executionInfo":{"status":"ok","timestamp":1733156796009,"user_tz":-330,"elapsed":6273,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T05:41:24.066047Z","iopub.execute_input":"2024-12-03T05:41:24.066995Z","iopub.status.idle":"2024-12-03T05:41:24.081101Z","shell.execute_reply.started":"2024-12-03T05:41:24.066899Z","shell.execute_reply":"2024-12-03T05:41:24.079222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nnp.random.seed(42)\ntorch.manual_seed(42)","metadata":{"papermill":{"duration":0.024147,"end_time":"2023-07-31T04:10:01.615325","exception":false,"start_time":"2023-07-31T04:10:01.591178","status":"completed"},"tags":[],"id":"59742826","outputId":"39bd0538-02c9-4e80-f12d-c2e56615c2ee","executionInfo":{"status":"ok","timestamp":1733156796009,"user_tz":-330,"elapsed":4,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T05:02:02.063707Z","iopub.execute_input":"2024-12-03T05:02:02.064571Z","iopub.status.idle":"2024-12-03T05:02:02.086696Z","shell.execute_reply.started":"2024-12-03T05:02:02.064505Z","shell.execute_reply":"2024-12-03T05:02:02.085103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/train'\ntest_dir = '/kaggle/input/dlp-object-detection/final_dlp_data/final_dlp_data/test'\nimage_dir=os.path.join(train_dir, 'images')","metadata":{"id":"gzDbtcfG8cDU","executionInfo":{"status":"ok","timestamp":1733156797151,"user_tz":-330,"elapsed":570,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T05:02:02.0887Z","iopub.execute_input":"2024-12-03T05:02:02.089232Z","iopub.status.idle":"2024-12-03T05:02:02.096502Z","shell.execute_reply.started":"2024-12-03T05:02:02.089177Z","shell.execute_reply":"2024-12-03T05:02:02.094839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ndata = pd.DataFrame([np.loadtxt(os.path.join(train_dir, 'labels', filename)) for filename in tqdm(os.listdir(os.path.join(train_dir, 'labels')))])\ndata.columns = ['label', 'x_centre', 'y_centre', 'w', 'h']\n\ndata['image_id'] = [os.path.splitext(filename)[0] for filename in os.listdir(os.path.join(train_dir, 'labels'))]\ndata.label = data.label.astype('int64')\n\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:12:22.817458Z","iopub.execute_input":"2024-12-03T06:12:22.817948Z","iopub.status.idle":"2024-12-03T06:12:46.026185Z","shell.execute_reply.started":"2024-12-03T06:12:22.817903Z","shell.execute_reply":"2024-12-03T06:12:46.024808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install imagesize\nimport imagesize\n\ndims = pd.DataFrame([imagesize.get(os.path.join(image_dir,image_id+'.jpeg')) for image_id in tqdm(data['image_id'])], columns=['W', 'H'])\ndata = pd.concat((data, dims), axis=1)\n\ndata['w'] = data['w'] * data['W']\ndata['h'] = data['h'] * data['H']\n\ndata['x0'] = data['W'] * data['x_centre'] - data['w']/2\ndata['y0'] = data['H'] * data['y_centre'] - data['h']/2\n\ndata = data[['image_id', 'label', 'x0', 'y0', 'w', 'h']]\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:12:46.027988Z","iopub.execute_input":"2024-12-03T06:12:46.028337Z","iopub.status.idle":"2024-12-03T06:13:04.254141Z","shell.execute_reply.started":"2024-12-03T06:12:46.028305Z","shell.execute_reply":"2024-12-03T06:13:04.252478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes = data['label'].nunique()\n\ndata['label'].unique()","metadata":{"papermill":{"duration":0.034894,"end_time":"2023-07-31T04:10:03.631587","exception":false,"start_time":"2023-07-31T04:10:03.596693","status":"completed"},"tags":[],"id":"7a0b8080","outputId":"cbbd8dba-101d-408f-bf01-e01f03df9639","executionInfo":{"status":"ok","timestamp":1733156805207,"user_tz":-330,"elapsed":9,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:13:04.256583Z","iopub.execute_input":"2024-12-03T06:13:04.257013Z","iopub.status.idle":"2024-12-03T06:13:04.267622Z","shell.execute_reply.started":"2024-12-03T06:13:04.256969Z","shell.execute_reply":"2024-12-03T06:13:04.266272Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Configuration","metadata":{"papermill":{"duration":0.009154,"end_time":"2023-07-31T04:10:03.650189","exception":false,"start_time":"2023-07-31T04:10:03.641035","status":"completed"},"tags":[],"id":"3046f551"}},{"cell_type":"code","source":"dataframe=data\nmodel_weights_file=\"model_v3.pth\"\ndevice=\"cuda\" if torch.cuda.is_available() else \"cpu\"\nbatch_size=4\nlearning_rate=3e-5\nepochs=50\nthreshold=0.5\niou_threshold=0.8\n\nprint(device)","metadata":{"papermill":{"duration":0.085493,"end_time":"2023-07-31T04:10:03.745043","exception":false,"start_time":"2023-07-31T04:10:03.65955","status":"completed"},"tags":[],"id":"9246965d","executionInfo":{"status":"ok","timestamp":1733156904694,"user_tz":-330,"elapsed":417,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"outputId":"8f206653-a2d5-4bce-b3f8-ce1e479e034b","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:10.59396Z","iopub.execute_input":"2024-12-03T06:14:10.594409Z","iopub.status.idle":"2024-12-03T06:14:10.602467Z","shell.execute_reply.started":"2024-12-03T06:14:10.594368Z","shell.execute_reply":"2024-12-03T06:14:10.601113Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Custom Dataset","metadata":{"papermill":{"duration":0.009219,"end_time":"2023-07-31T04:10:03.763925","exception":false,"start_time":"2023-07-31T04:10:03.754706","status":"completed"},"tags":[],"id":"2872ca77"}},{"cell_type":"code","source":"\"\"\"The input to the model is expected to be a list of tensors, each of shape [C, H, W],\n\n   one for each image, and should be in 0-1 range. Different images can have different\n\n   sizes.The behavior of the model changes depending if it is in training or evaluation\n\n   mode.\"\"\"\n\n\n\nclass MosquitoDataset(Dataset):\n\n    def __init__(self,dataframe,image_dir):\n\n        super().__init__()\n\n        self.dataframe=dataframe\n\n        self.img_list=sorted(self.dataframe[\"image_id\"].unique())\n\n        self.img_dir=image_dir\n\n\n\n    def __len__(self):\n\n        return len(self.img_list)\n\n\n\n    def __getitem__(self,idx):\n\n        img_name=self.img_list[idx]+\".jpeg\"\n\n        img_path=os.path.join(self.img_dir,img_name)\n\n        img=cv2.imread(img_path)\n\n        img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n\n        img=tf.to_tensor(img)\n\n\n\n        inter=self.dataframe[self.dataframe[\"image_id\"]==self.img_list[idx]]\n\n        boxes=inter[[\"x0\",\"y0\",\"w\",\"h\"]].values\n\n        area=boxes[:,2]*boxes[:,3]\n\n\n\n        # converting bounding box from x0y0wh format to x0y0x1y1 format\n\n        boxes[:,2]=boxes[:,0]+boxes[:,2]\n\n        boxes[:,3]=boxes[:,1]+boxes[:,3]\n\n\n\n        labels=torch.tensor(inter.label.values, dtype=torch.int64)\n\n        iscrowd=torch.zeros((boxes.shape[0]),dtype=torch.uint8)\n\n\n\n        target={}\n\n        target[\"boxes\"]=torch.as_tensor(boxes,dtype=torch.float32)\n\n        target[\"area\"]=torch.as_tensor(area,dtype=torch.float32)\n\n        target[\"labels\"]=labels\n\n        target[\"iscrowd\"]=iscrowd\n\n        target[\"id\"]=torch.tensor(idx)\n\n\n\n        return img,target","metadata":{"papermill":{"duration":0.024381,"end_time":"2023-07-31T04:10:03.797712","exception":false,"start_time":"2023-07-31T04:10:03.773331","status":"completed"},"tags":[],"id":"087000e6","executionInfo":{"status":"ok","timestamp":1733156811268,"user_tz":-330,"elapsed":457,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:16.46138Z","iopub.execute_input":"2024-12-03T06:14:16.461826Z","iopub.status.idle":"2024-12-03T06:14:16.473695Z","shell.execute_reply.started":"2024-12-03T06:14:16.461785Z","shell.execute_reply":"2024-12-03T06:14:16.472542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds=MosquitoDataset(dataframe,image_dir)\n\nval_ds=MosquitoDataset(dataframe,image_dir)","metadata":{"papermill":{"duration":0.045186,"end_time":"2023-07-31T04:10:03.852269","exception":false,"start_time":"2023-07-31T04:10:03.807083","status":"completed"},"tags":[],"id":"404ffe11","executionInfo":{"status":"ok","timestamp":1733156813548,"user_tz":-330,"elapsed":409,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:19.177553Z","iopub.execute_input":"2024-12-03T06:14:19.177963Z","iopub.status.idle":"2024-12-03T06:14:19.190922Z","shell.execute_reply.started":"2024-12-03T06:14:19.177927Z","shell.execute_reply":"2024-12-03T06:14:19.189553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ss=ShuffleSplit(n_splits=1,test_size=0.2,random_state=1)\n\n\n\nindexs=range(len(train_ds))\n\nfor train_idx,val_idx in ss.split(indexs):\n\n    print(f\"Train dataset length: {len(train_idx)}\")\n\n    print(f\"Validation dataset length: {len(val_idx)}\")","metadata":{"papermill":{"duration":0.019921,"end_time":"2023-07-31T04:10:03.881495","exception":false,"start_time":"2023-07-31T04:10:03.861574","status":"completed"},"tags":[],"id":"0bbb9df3","outputId":"813d097b-c2f8-4fd5-f3a0-2c6dd52bf36c","executionInfo":{"status":"ok","timestamp":1733156815098,"user_tz":-330,"elapsed":411,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:20.302038Z","iopub.execute_input":"2024-12-03T06:14:20.30254Z","iopub.status.idle":"2024-12-03T06:14:20.311445Z","shell.execute_reply.started":"2024-12-03T06:14:20.302497Z","shell.execute_reply":"2024-12-03T06:14:20.31004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds=Subset(train_ds,train_idx)\n\nval_ds=Subset(val_ds,val_idx)","metadata":{"papermill":{"duration":0.016242,"end_time":"2023-07-31T04:10:03.907366","exception":false,"start_time":"2023-07-31T04:10:03.891124","status":"completed"},"tags":[],"id":"003da8d4","executionInfo":{"status":"ok","timestamp":1733156816369,"user_tz":-330,"elapsed":331,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:22.245789Z","iopub.execute_input":"2024-12-03T06:14:22.246266Z","iopub.status.idle":"2024-12-03T06:14:22.252453Z","shell.execute_reply.started":"2024-12-03T06:14:22.246225Z","shell.execute_reply":"2024-12-03T06:14:22.250924Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Visualization","metadata":{"papermill":{"duration":0.009542,"end_time":"2023-07-31T04:10:03.926635","exception":false,"start_time":"2023-07-31T04:10:03.917093","status":"completed"},"tags":[],"id":"b67153f2"}},{"cell_type":"code","source":"def show(img,boxes, color=(220, 0, 0)):\n\n    boxes=(boxes.detach().numpy()).astype(np.int32)\n\n    sample=img.permute(1,2,0).numpy().copy()\n\n    for box in boxes:\n\n        cv2.rectangle(sample,(box[0], box[1]),(box[2], box[3]),color, 3)\n\n\n\n    plt.axis(\"off\");\n\n    plt.imshow(sample);","metadata":{"papermill":{"duration":0.018565,"end_time":"2023-07-31T04:10:03.955296","exception":false,"start_time":"2023-07-31T04:10:03.936731","status":"completed"},"tags":[],"id":"fbf3e01d","executionInfo":{"status":"ok","timestamp":1733156816926,"user_tz":-330,"elapsed":2,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:23.770247Z","iopub.execute_input":"2024-12-03T06:14:23.770689Z","iopub.status.idle":"2024-12-03T06:14:23.778371Z","shell.execute_reply.started":"2024-12-03T06:14:23.770643Z","shell.execute_reply":"2024-12-03T06:14:23.776798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\n\n\n\nimg,target=next(iter(train_ds))\n\nshow(img,target[\"boxes\"])\n\nplt.savefig(\"1.png\")","metadata":{"papermill":{"duration":1.098031,"end_time":"2023-07-31T04:10:05.063082","exception":false,"start_time":"2023-07-31T04:10:03.965051","status":"completed"},"tags":[],"id":"5f553053","outputId":"c3c2a709-8a35-4aef-b621-5a6264c2b2c3","executionInfo":{"status":"ok","timestamp":1733156821808,"user_tz":-330,"elapsed":2117,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:25.686023Z","iopub.execute_input":"2024-12-03T06:14:25.686483Z","iopub.status.idle":"2024-12-03T06:14:26.646253Z","shell.execute_reply.started":"2024-12-03T06:14:25.686442Z","shell.execute_reply":"2024-12-03T06:14:26.644944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\n\n\n\nimg,target=next(iter(val_ds))\n\nshow(img,target[\"boxes\"])\n\nplt.savefig(\"2.png\")","metadata":{"papermill":{"duration":0.972157,"end_time":"2023-07-31T04:10:06.055876","exception":false,"start_time":"2023-07-31T04:10:05.083719","status":"completed"},"tags":[],"id":"4442fbec","outputId":"2e31fd19-eca6-486a-a989-35c12a2ef9e9","executionInfo":{"status":"ok","timestamp":1733155656822,"user_tz":-330,"elapsed":6320,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:30.261086Z","iopub.execute_input":"2024-12-03T06:14:30.26154Z","iopub.status.idle":"2024-12-03T06:14:36.566429Z","shell.execute_reply.started":"2024-12-03T06:14:30.261503Z","shell.execute_reply":"2024-12-03T06:14:36.564747Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# DataLoader","metadata":{"papermill":{"duration":0.034261,"end_time":"2023-07-31T04:10:06.123226","exception":false,"start_time":"2023-07-31T04:10:06.088965","status":"completed"},"tags":[],"id":"5c428a8c"}},{"cell_type":"code","source":"def collate_fn(batch):\n\n    return tuple(zip(*batch))","metadata":{"papermill":{"duration":0.040519,"end_time":"2023-07-31T04:10:06.195656","exception":false,"start_time":"2023-07-31T04:10:06.155137","status":"completed"},"tags":[],"id":"4744b6da","executionInfo":{"status":"ok","timestamp":1733156845516,"user_tz":-330,"elapsed":327,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:36.568739Z","iopub.execute_input":"2024-12-03T06:14:36.569263Z","iopub.status.idle":"2024-12-03T06:14:36.576105Z","shell.execute_reply.started":"2024-12-03T06:14:36.569211Z","shell.execute_reply":"2024-12-03T06:14:36.574647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dl=DataLoader(train_ds,batch_size=batch_size,shuffle=True,num_workers=2,\n\n                    pin_memory=True if torch.cuda.is_available else False,\n\n                    collate_fn=collate_fn)\n\nval_dl=DataLoader(val_ds,batch_size=batch_size,shuffle=False,num_workers=2,\n\n                  pin_memory=True if torch.cuda.is_available else False,\n\n                  collate_fn=collate_fn)","metadata":{"papermill":{"duration":0.042935,"end_time":"2023-07-31T04:10:06.269314","exception":false,"start_time":"2023-07-31T04:10:06.226379","status":"completed"},"tags":[],"id":"aa75b109","executionInfo":{"status":"ok","timestamp":1733156914604,"user_tz":-330,"elapsed":401,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:14:37.896428Z","iopub.execute_input":"2024-12-03T06:14:37.896852Z","iopub.status.idle":"2024-12-03T06:14:37.904515Z","shell.execute_reply.started":"2024-12-03T06:14:37.896817Z","shell.execute_reply":"2024-12-03T06:14:37.903052Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model","metadata":{"papermill":{"duration":0.032277,"end_time":"2023-07-31T04:10:06.333066","exception":false,"start_time":"2023-07-31T04:10:06.300789","status":"completed"},"tags":[],"id":"0b2ecdb0"}},{"cell_type":"code","source":"# load a model pre-trained on COCO\n\nweights=FasterRCNN_MobileNet_V3_Large_FPN_Weights.DEFAULT\n\nmodel=fasterrcnn_mobilenet_v3_large_fpn(weights=weights)\n\n\n\n# replace the classifier with a new one, that has\n\n# num_classes which is user-defined\n\n# mosquito_classes + background\n\nnum_classes += 1\n\n\n\n# get number of input features for the classifier\n\nin_features=model.roi_heads.box_predictor.cls_score.in_features\n\n\n\n\n\n# replace the pre-trained head with a new one\n\nmodel.roi_heads.box_predictor=FastRCNNPredictor(in_channels=in_features,\n\n                                                num_classes=num_classes)\n\n\n\nmodel.to(device)","metadata":{"_kg_hide-output":true,"papermill":{"duration":4.97508,"end_time":"2023-07-31T04:10:11.340502","exception":false,"start_time":"2023-07-31T04:10:06.365422","status":"completed"},"tags":[],"id":"1d6cb4ed","outputId":"59a788bf-82b4-4a54-ca98-fbc607b27d16","executionInfo":{"status":"ok","timestamp":1733156849305,"user_tz":-330,"elapsed":1311,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:15:09.287737Z","iopub.execute_input":"2024-12-03T06:15:09.288982Z","iopub.status.idle":"2024-12-03T06:15:11.254996Z","shell.execute_reply.started":"2024-12-03T06:15:09.288878Z","shell.execute_reply":"2024-12-03T06:15:11.253691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"\"\"ConvNet as fixed feature extractor: Here, we will freeze the weights for the backbone of\n\n   the network (resnet50 with feature pyramid network). The Regional Proposal network and\n\n   Region of Interest heads will be fine tuned using transfer learning.\"\"\"\n\n\n\nclassification_head=list(model.children())[-2:]\n\n\n\nfor children in list(model.children())[:-2]:\n\n    for params in children.parameters():\n\n        params.requires_grad=False\n\n\n\nparameters=[]\n\nfor heads in classification_head:\n\n    for params in heads.parameters():\n\n        parameters.append(params)\n\n\n\n\n\noptimizer=optim.Adam(parameters,lr=learning_rate)\n\nlr_scheduler=optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1,\n\n                                                  patience=8, threshold=0.0001)\n\n\n\n\n\nprint(classification_head)","metadata":{"papermill":{"duration":0.051123,"end_time":"2023-07-31T04:10:11.51992","exception":false,"start_time":"2023-07-31T04:10:11.468797","status":"completed"},"tags":[],"id":"6725cb02","outputId":"1173ae3c-bfd9-4845-c219-73a814bfeffa","executionInfo":{"status":"ok","timestamp":1733156851292,"user_tz":-330,"elapsed":332,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:15:16.222086Z","iopub.execute_input":"2024-12-03T06:15:16.222533Z","iopub.status.idle":"2024-12-03T06:15:16.23335Z","shell.execute_reply.started":"2024-12-03T06:15:16.222496Z","shell.execute_reply":"2024-12-03T06:15:16.232026Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training","metadata":{"papermill":{"duration":0.036887,"end_time":"2023-07-31T04:10:11.594305","exception":false,"start_time":"2023-07-31T04:10:11.557418","status":"completed"},"tags":[],"id":"16816706"}},{"cell_type":"code","source":"def get_lr(optimizer):\n\n    for params in optimizer.param_groups:\n\n        return params[\"lr\"]","metadata":{"execution":{"iopub.status.busy":"2024-12-03T06:15:03.214143Z","iopub.execute_input":"2024-12-03T06:15:03.214637Z","iopub.status.idle":"2024-12-03T06:15:03.221406Z","shell.execute_reply.started":"2024-12-03T06:15:03.214598Z","shell.execute_reply":"2024-12-03T06:15:03.219604Z"},"papermill":{"duration":0.046012,"end_time":"2023-07-31T04:10:11.677464","exception":false,"start_time":"2023-07-31T04:10:11.631452","status":"completed"},"tags":[],"id":"af529585","executionInfo":{"status":"ok","timestamp":1733156853075,"user_tz":-330,"elapsed":609,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm\n\n\n\nepochs = 10\n\nloss_history={\"training_loss\":[],\n\n              \"validation_loss\":[]}\n\n\n\ntrain_len=len(train_dl.dataset)\n\nval_len=len(val_dl.dataset)\n\n\n\nbest_validation_loss=np.inf\n\nbest_weights=copy.deepcopy(model.state_dict())\n\n\n\nfor epoch in range(epochs):\n\n\n\n    training_loss=0.0\n\n    validation_loss=0.0\n\n    current_lr=get_lr(optimizer)\n\n\n\n    #During training, the model expects both the input tensors, as well as a targets\n\n    model.train()\n\n    for imgs,targets in tqdm(train_dl):\n\n        imgs=[img.to(device) for img in imgs]\n\n        targets=[{k:v.to(device) for (k,v) in d.items()} for d in targets]\n\n\n\n        \"\"\"The model returns a Dict[Tensor] during training, containing the classification\n\n           and regression losses for both the RPN and the R-CNN.\"\"\"\n\n\n\n        loss_dict=model(imgs,targets)\n\n        losses=sum(loss for loss in loss_dict.values())\n\n        training_loss+=losses.item()\n\n\n\n        optimizer.zero_grad()\n\n        losses.backward()\n\n        optimizer.step()\n\n\n\n    with torch.no_grad():\n\n        for imgs,targets in val_dl:\n\n            imgs=[img.to(device) for img in imgs]\n\n            targets=[{k:v.to(device) for (k,v) in d.items()} for d in targets]\n\n\n\n            \"\"\"The model returns a Dict[Tensor] during training, containing the classification\n\n               and regression losses for both the RPN and the R-CNN.\"\"\"\n\n\n\n            loss_dict=model(imgs,targets)\n\n            losses=sum(loss for loss in loss_dict.values())\n\n            validation_loss+=losses.item()\n\n\n\n\n\n    lr_scheduler.step(validation_loss)\n\n    if current_lr!=get_lr(optimizer):\n\n        print(\"Loading best Model weights\")\n\n        model.load_state_dict(best_weights)\n\n\n\n    if validation_loss<best_validation_loss:\n\n        best_validation_loss=validation_loss\n\n        best_weights=copy.deepcopy(model.state_dict())\n\n        print(\"Updating Best Model weights\")\n\n\n\n\n\n    loss_history[\"training_loss\"].append(training_loss/train_len)\n\n    loss_history[\"validation_loss\"].append(validation_loss/val_len)\n\n\n\n    print(f\"\\n{epoch+1}/{epochs}\")\n\n    print(f\"Training Loss: {training_loss/train_len}\")\n\n    print(f\"Validation_loss: {validation_loss/val_len}\")\n\n    print(\"\\n\"+\"*\"*50)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-12-02T16:48:20.236125Z","iopub.execute_input":"2024-12-02T16:48:20.23639Z","iopub.status.idle":"2024-12-02T16:56:03.157929Z","shell.execute_reply.started":"2024-12-02T16:48:20.236354Z","shell.execute_reply":"2024-12-02T16:56:03.156702Z"},"papermill":{"duration":11906.705989,"end_time":"2023-07-31T07:28:38.42025","exception":false,"start_time":"2023-07-31T04:10:11.714261","status":"completed"},"tags":[],"id":"d96a9fa8","outputId":"36f1a349-87f8-48f0-8403-a5c4298a7281","executionInfo":{"status":"error","timestamp":1733157016721,"user_tz":-330,"elapsed":74895,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.lineplot(x=range(epochs),y=loss_history[\"training_loss\"],label=\"Train Losses\");\n\nsns.lineplot(x=range(epochs),y=loss_history[\"validation_loss\"],label=\"Validation Losses\");\n\nplt.title(\"Training Validation Datasets Losses Plot\");\n\nplt.legend();\n\nplt.savefig(\"3.jpg\")","metadata":{"execution":{"iopub.status.busy":"2024-12-02T16:56:03.159218Z","iopub.status.idle":"2024-12-02T16:56:03.159726Z","shell.execute_reply.started":"2024-12-02T16:56:03.159475Z","shell.execute_reply":"2024-12-02T16:56:03.159498Z"},"papermill":{"duration":0.469639,"end_time":"2023-07-31T07:28:38.93066","exception":false,"start_time":"2023-07-31T07:28:38.461021","status":"completed"},"tags":[],"id":"7a526614","executionInfo":{"status":"aborted","timestamp":1733156718502,"user_tz":-330,"elapsed":9,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Saving Model State","metadata":{"papermill":{"duration":0.041318,"end_time":"2023-07-31T07:28:39.017684","exception":false,"start_time":"2023-07-31T07:28:38.976366","status":"completed"},"tags":[],"id":"8adf2969"}},{"cell_type":"code","source":"torch.save(best_weights,model_weights_file)","metadata":{"execution":{"iopub.status.busy":"2024-12-02T16:56:53.240824Z","iopub.execute_input":"2024-12-02T16:56:53.241653Z","iopub.status.idle":"2024-12-02T16:56:53.51635Z","shell.execute_reply.started":"2024-12-02T16:56:53.241615Z","shell.execute_reply":"2024-12-02T16:56:53.515613Z"},"papermill":{"duration":0.34448,"end_time":"2023-07-31T07:28:39.403304","exception":false,"start_time":"2023-07-31T07:28:39.058824","status":"completed"},"tags":[],"id":"fe63fa4e","executionInfo":{"status":"aborted","timestamp":1733157016722,"user_tz":-330,"elapsed":10,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Inference","metadata":{"papermill":{"duration":0.040749,"end_time":"2023-07-31T07:28:39.486712","exception":false,"start_time":"2023-07-31T07:28:39.445963","status":"completed"},"tags":[],"id":"3d131e6c"}},{"cell_type":"code","source":"model.load_state_dict(torch.load(model_weights_file))\n\nmodel.to(device)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-12-02T16:56:03.162484Z","iopub.status.idle":"2024-12-02T16:56:03.162775Z","shell.execute_reply.started":"2024-12-02T16:56:03.162641Z","shell.execute_reply":"2024-12-02T16:56:03.162655Z"},"papermill":{"duration":0.205263,"end_time":"2023-07-31T07:28:39.73308","exception":false,"start_time":"2023-07-31T07:28:39.527817","status":"completed"},"tags":[],"id":"284a966c","executionInfo":{"status":"aborted","timestamp":1733156718503,"user_tz":-330,"elapsed":9,"user":{"displayName":"Vivek Sivaramakrishnan","userId":"13013544173607732074"}},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MosquitoTestDataset(Dataset):\n\n    def __init__(self,dataframe,image_dir):\n\n        super().__init__()\n\n        self.dataframe=dataframe\n\n        self.img_list=sorted(self.dataframe[\"ImageID\"].unique())\n\n        self.img_dir=image_dir\n\n\n\n    def __len__(self):\n\n        return len(self.img_list)\n\n\n\n    def __getitem__(self,idx):\n\n        img_name=self.img_list[idx]\n\n        img_path=os.path.join(self.img_dir,img_name)\n\n        img=cv2.imread(img_path)\n\n        img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n\n        img=tf.to_tensor(img)\n\n\n        return img_name, img\n\n\ndef collate_fn(batch):\n\n    return tuple(zip(*batch))\n\ntest_ds = MosquitoTestDataset(test_data, image_dir)\n\ntest_loader = DataLoader(test_ds, batch_size=4,shuffle=False,num_workers=4,\n\n                    pin_memory=True if torch.cuda.is_available else False,\n\n                    collate_fn=collate_fn)\n\nprint(device)\nmodel.eval()\n\nid_to_label = [\"aegypti\",\"albopictus\",\"anopheles\",\"culex\",\"culiseta\",\"japonicus/koreicus\"]\n\nfinal_submission = []","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Predictions on Test set","metadata":{}},{"cell_type":"code","source":"for imgnames, imgs in tqdm(test_loader):\n\n    output = model([img.to(device) for img in imgs])\n    for imgid,(i,idx) in zip(imgnames,enumerate(range(len(imgs)))):\n    \n        img=imgs[idx]\n        predictions=output[idx]\n        \n        W, H = test_data[test_data['ImageID']==imgid][['W', 'H']].values[0]\n        conf = predictions['scores'].detach().cpu().numpy()[0]\n        x0, y0, x1, y1 = predictions[\"boxes\"][0].detach().cpu().numpy()\n        xc, yc = (x0+x1)/2, (y0+y1)/2\n        w, h = abs(x1-x0), abs(y1-y0)\n        label = id_to_label[predictions['labels'][0].detach().cpu().numpy()]\n    \n        xc /= W\n        w /= W\n        yc /= H\n        h /= H\n        \n        final_submission.append([imgid, label, conf, xc, yc, w, h])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_submission = pd.DataFrame(final_submission, columns=['ImageID','LabelName','Conf','xcenter','ycenter','bbx_width','bbx_height'])\nfinal_submission.index.name = 'id'\nfinal_submission.to_csv('21F2000045.csv')\nfinal_submission.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}