{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-25T19:21:38.1944Z","iopub.execute_input":"2024-02-25T19:21:38.195032Z","iopub.status.idle":"2024-02-25T19:21:43.821116Z","shell.execute_reply.started":"2024-02-25T19:21:38.195002Z","shell.execute_reply":"2024-02-25T19:21:43.820139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nfrom PIL import Image, ImageDraw\nfrom collections import Counter\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nimport torchvision\nfrom torchvision import transforms as T\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:43.822959Z","iopub.execute_input":"2024-02-25T19:21:43.82341Z","iopub.status.idle":"2024-02-25T19:21:51.739109Z","shell.execute_reply.started":"2024-02-25T19:21:43.823384Z","shell.execute_reply":"2024-02-25T19:21:51.738051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(r\"../input/global-wheat-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:51.740562Z","iopub.execute_input":"2024-02-25T19:21:51.743203Z","iopub.status.idle":"2024-02-25T19:21:52.051088Z","shell.execute_reply.started":"2024-02-25T19:21:51.743164Z","shell.execute_reply":"2024-02-25T19:21:52.050055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.053246Z","iopub.execute_input":"2024-02-25T19:21:52.053547Z","iopub.status.idle":"2024-02-25T19:21:52.071973Z","shell.execute_reply.started":"2024-02-25T19:21:52.053522Z","shell.execute_reply":"2024-02-25T19:21:52.07108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coords = pd.DataFrame(list(train.bbox.apply(lambda x : x[1 : -1].split(\",\")).values), columns = ['x1', 'y1', 'w', 'h'])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.07298Z","iopub.execute_input":"2024-02-25T19:21:52.073246Z","iopub.status.idle":"2024-02-25T19:21:52.465037Z","shell.execute_reply.started":"2024-02-25T19:21:52.073223Z","shell.execute_reply":"2024-02-25T19:21:52.464042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train, coords], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.466212Z","iopub.execute_input":"2024-02-25T19:21:52.466491Z","iopub.status.idle":"2024-02-25T19:21:52.49049Z","shell.execute_reply.started":"2024-02-25T19:21:52.466466Z","shell.execute_reply":"2024-02-25T19:21:52.489399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['x1'] = pd.to_numeric(df['x1'])\ndf['y1'] = pd.to_numeric(df['y1'])\ndf['w'] = pd.to_numeric(df['w'])\ndf['h'] = pd.to_numeric(df['h'])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.494088Z","iopub.execute_input":"2024-02-25T19:21:52.49446Z","iopub.status.idle":"2024-02-25T19:21:52.892426Z","shell.execute_reply.started":"2024-02-25T19:21:52.494433Z","shell.execute_reply":"2024-02-25T19:21:52.89161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['x2'] = df['x1'] + df['w']\ndf['y2'] = df['y1'] + df['h']","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.893573Z","iopub.execute_input":"2024-02-25T19:21:52.89388Z","iopub.status.idle":"2024-02-25T19:21:52.902423Z","shell.execute_reply.started":"2024-02-25T19:21:52.893856Z","shell.execute_reply":"2024-02-25T19:21:52.901641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(['bbox', 'width', 'height', 'w', 'h', 'source'], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.903535Z","iopub.execute_input":"2024-02-25T19:21:52.903908Z","iopub.status.idle":"2024-02-25T19:21:52.924166Z","shell.execute_reply.started":"2024-02-25T19:21:52.903876Z","shell.execute_reply":"2024-02-25T19:21:52.92324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.927615Z","iopub.execute_input":"2024-02-25T19:21:52.927968Z","iopub.status.idle":"2024-02-25T19:21:52.941389Z","shell.execute_reply.started":"2024-02-25T19:21:52.927937Z","shell.execute_reply":"2024-02-25T19:21:52.940323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_imgs = df.image_id.unique()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.942649Z","iopub.execute_input":"2024-02-25T19:21:52.943044Z","iopub.status.idle":"2024-02-25T19:21:52.967386Z","shell.execute_reply.started":"2024-02-25T19:21:52.943006Z","shell.execute_reply":"2024-02-25T19:21:52.966518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_imgs","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.968565Z","iopub.execute_input":"2024-02-25T19:21:52.968867Z","iopub.status.idle":"2024-02-25T19:21:52.976156Z","shell.execute_reply.started":"2024-02-25T19:21:52.968842Z","shell.execute_reply":"2024-02-25T19:21:52.975169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustDat(torch.utils.data.Dataset):\n    def __init__(self, df, unique_imgs, indices):\n        self.df = df\n        self.unique_imgs = unique_imgs\n        self.indices = indices\n    def __len__(self):\n        return len(self.indices)\n    def __getitem__(self, idx):\n        image_name = self.unique_imgs[self.indices[idx]]\n        boxes = self.df[self.df.image_id == image_name].values[:, 1:].astype('float')\n        img = Image.open(\"../input/global-wheat-detection/train/\" + image_name + \".jpg\").convert(\"RGB\")\n        labels = torch.ones(boxes.shape[0], dtype = torch.int64)\n        target = {}\n        target['boxes'] = torch.tensor(boxes)\n        target['label'] = labels\n        \n        return T.ToTensor()(img), target","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.977681Z","iopub.execute_input":"2024-02-25T19:21:52.978007Z","iopub.status.idle":"2024-02-25T19:21:52.986779Z","shell.execute_reply.started":"2024-02-25T19:21:52.977983Z","shell.execute_reply":"2024-02-25T19:21:52.985719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_inds, val_inds = train_test_split(range(unique_imgs.shape[0]), test_size = 0.1)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:52.988053Z","iopub.execute_input":"2024-02-25T19:21:52.988326Z","iopub.status.idle":"2024-02-25T19:21:53.003971Z","shell.execute_reply.started":"2024-02-25T19:21:52.988302Z","shell.execute_reply":"2024-02-25T19:21:53.003024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_collate(data):\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:53.005201Z","iopub.execute_input":"2024-02-25T19:21:53.005591Z","iopub.status.idle":"2024-02-25T19:21:53.014054Z","shell.execute_reply.started":"2024-02-25T19:21:53.005558Z","shell.execute_reply":"2024-02-25T19:21:53.013114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = torch.utils.data.DataLoader(CustDat(df, unique_imgs, train_inds),\n                                      batch_size = 16,\n                                      shuffle = True,\n                                      collate_fn = custom_collate,\n                                      pin_memory = True if torch.cuda.is_available() else False)\n\nval_dl = torch.utils.data.DataLoader(CustDat(df, unique_imgs, val_inds),\n                                      batch_size = 16,\n                                      shuffle = True,\n                                      collate_fn = custom_collate,\n                                      pin_memory = True if torch.cuda.is_available() else False)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:53.015258Z","iopub.execute_input":"2024-02-25T19:21:53.015623Z","iopub.status.idle":"2024-02-25T19:21:53.075196Z","shell.execute_reply.started":"2024-02-25T19:21:53.015591Z","shell.execute_reply":"2024-02-25T19:21:53.074066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained = True)\nnum_classes = 2\nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:53.076579Z","iopub.execute_input":"2024-02-25T19:21:53.076976Z","iopub.status.idle":"2024-02-25T19:21:55.499887Z","shell.execute_reply.started":"2024-02-25T19:21:53.076941Z","shell.execute_reply":"2024-02-25T19:21:55.499105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:55.501219Z","iopub.execute_input":"2024-02-25T19:21:55.501942Z","iopub.status.idle":"2024-02-25T19:21:55.507597Z","shell.execute_reply.started":"2024-02-25T19:21:55.501901Z","shell.execute_reply":"2024-02-25T19:21:55.506462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:55.508958Z","iopub.execute_input":"2024-02-25T19:21:55.509257Z","iopub.status.idle":"2024-02-25T19:21:55.519967Z","shell.execute_reply.started":"2024-02-25T19:21:55.509232Z","shell.execute_reply":"2024-02-25T19:21:55.51907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.SGD(model.parameters(), lr = 0.001, momentum = 0.9, weight_decay = 0.0005)\nnum_epochs = 5","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:55.521275Z","iopub.execute_input":"2024-02-25T19:21:55.521651Z","iopub.status.idle":"2024-02-25T19:21:55.528881Z","shell.execute_reply.started":"2024-02-25T19:21:55.521617Z","shell.execute_reply":"2024-02-25T19:21:55.527985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(device)\nfor epoch in range(num_epochs):\n    epoch_loss = 0.0 \n    for data in train_dl:\n        imgs = []\n        targets = []\n        for d in data:\n            imgs.append(d[0].to(device))\n            targ = {}\n            targ['boxes'] = d[1]['boxes'].to(device)\n            targ['labels'] = d[1]['label'].to(device)\n            targets.append(targ)\n        loss_dict = model(imgs, targets)\n        loss = sum(v for v in loss_dict.values())\n        epoch_loss += loss.cpu().detach().numpy()\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    print(epoch_loss)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T19:21:55.530066Z","iopub.execute_input":"2024-02-25T19:21:55.530342Z","iopub.status.idle":"2024-02-25T19:24:18.095799Z","shell.execute_reply.started":"2024-02-25T19:21:55.530318Z","shell.execute_reply":"2024-02-25T19:24:18.094338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ndata = iter(val_dl).__next__()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:00:28.092009Z","iopub.execute_input":"2024-02-25T20:00:28.092857Z","iopub.status.idle":"2024-02-25T20:00:28.852614Z","shell.execute_reply.started":"2024-02-25T20:00:28.092816Z","shell.execute_reply":"2024-02-25T20:00:28.851724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = data[0][0]\nboxes = data[0][1]['boxes']\nlabels = data[0][1]['label']","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:01:33.805629Z","iopub.execute_input":"2024-02-25T20:01:33.806326Z","iopub.status.idle":"2024-02-25T20:01:33.811279Z","shell.execute_reply.started":"2024-02-25T20:01:33.806282Z","shell.execute_reply":"2024-02-25T20:01:33.810351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model([img.to(device)])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:01:59.206865Z","iopub.execute_input":"2024-02-25T20:01:59.2072Z","iopub.status.idle":"2024-02-25T20:01:59.42418Z","shell.execute_reply.started":"2024-02-25T20:01:59.207175Z","shell.execute_reply":"2024-02-25T20:01:59.423381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:02:01.685935Z","iopub.execute_input":"2024-02-25T20:02:01.686897Z","iopub.status.idle":"2024-02-25T20:02:01.748601Z","shell.execute_reply.started":"2024-02-25T20:02:01.686853Z","shell.execute_reply":"2024-02-25T20:02:01.747518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox = output[0]['boxes']\nout_scores = output[0]['scores']","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:03:21.323078Z","iopub.execute_input":"2024-02-25T20:03:21.323668Z","iopub.status.idle":"2024-02-25T20:03:21.327784Z","shell.execute_reply.started":"2024-02-25T20:03:21.323637Z","shell.execute_reply":"2024-02-25T20:03:21.326883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keep = torchvision.ops.nms(out_bbox, out_scores, 0.45)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:03:56.72081Z","iopub.execute_input":"2024-02-25T20:03:56.721181Z","iopub.status.idle":"2024-02-25T20:03:56.726219Z","shell.execute_reply.started":"2024-02-25T20:03:56.721153Z","shell.execute_reply":"2024-02-25T20:03:56.72523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_bbox.shape, keep.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:04:31.389244Z","iopub.execute_input":"2024-02-25T20:04:31.389641Z","iopub.status.idle":"2024-02-25T20:04:31.396232Z","shell.execute_reply.started":"2024-02-25T20:04:31.389608Z","shell.execute_reply":"2024-02-25T20:04:31.395157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = (img.permute(1,2,0).cpu().detach().numpy()*255).astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:05:41.024959Z","iopub.execute_input":"2024-02-25T20:05:41.025922Z","iopub.status.idle":"2024-02-25T20:05:41.033832Z","shell.execute_reply.started":"2024-02-25T20:05:41.025882Z","shell.execute_reply":"2024-02-25T20:05:41.033014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T20:06:09.550991Z","iopub.execute_input":"2024-02-25T20:06:09.551359Z","iopub.status.idle":"2024-02-25T20:06:10.023519Z","shell.execute_reply.started":"2024-02-25T20:06:09.551331Z","shell.execute_reply":"2024-02-25T20:06:10.022474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}