{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"},{"sourceId":194117,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":165525,"modelId":187864}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","trusted":true,"_kg_hide-output":true,"_kg_hide-input":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:25.849750Z","iopub.execute_input":"2024-12-26T01:03:25.850230Z","iopub.status.idle":"2024-12-26T01:03:26.086396Z","shell.execute_reply.started":"2024-12-26T01:03:25.850194Z","shell.execute_reply":"2024-12-26T01:03:26.085251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir('/kaggle/input/global-wheat-detection/train'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:26.088173Z","iopub.execute_input":"2024-12-26T01:03:26.088643Z","iopub.status.idle":"2024-12-26T01:03:26.099099Z","shell.execute_reply.started":"2024-12-26T01:03:26.088593Z","shell.execute_reply":"2024-12-26T01:03:26.097866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for file in os.listdir('/kaggle/input/global-wheat-detection/train'):\n    image = os.path.join('/kaggle/input/global-wheat-detection/train',file)\n    print(file)\n    img = cv2.imread(image)\n    plt.imshow(img)\n    break\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:26.101861Z","iopub.execute_input":"2024-12-26T01:03:26.102359Z","iopub.status.idle":"2024-12-26T01:03:26.697429Z","shell.execute_reply.started":"2024-12-26T01:03:26.102309Z","shell.execute_reply":"2024-12-26T01:03:26.696330Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:26.698944Z","iopub.execute_input":"2024-12-26T01:03:26.699345Z","iopub.status.idle":"2024-12-26T01:03:27.012998Z","shell.execute_reply.started":"2024-12-26T01:03:26.699301Z","shell.execute_reply":"2024-12-26T01:03:27.011888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:27.014274Z","iopub.execute_input":"2024-12-26T01:03:27.014627Z","iopub.status.idle":"2024-12-26T01:03:27.034020Z","shell.execute_reply.started":"2024-12-26T01:03:27.014577Z","shell.execute_reply":"2024-12-26T01:03:27.032962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['image_id'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:27.036766Z","iopub.execute_input":"2024-12-26T01:03:27.037249Z","iopub.status.idle":"2024-12-26T01:03:27.059837Z","shell.execute_reply.started":"2024-12-26T01:03:27.037197Z","shell.execute_reply":"2024-12-26T01:03:27.058757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(cv2.imread('/kaggle/input/global-wheat-detection/train/b6ab77fd7.jpg'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T01:03:29.749912Z","iopub.execute_input":"2024-12-26T01:03:29.750305Z","iopub.status.idle":"2024-12-26T01:03:30.260447Z","shell.execute_reply.started":"2024-12-26T01:03:29.750269Z","shell.execute_reply":"2024-12-26T01:03:30.259276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/global-wheat-detection/train/b6ab77fd7.jpg')\nx_min = 834.0\ny_min = 222.0\nx_max = (834.0+56.0)\ny_max = (222.0+36.0)\n\ncv2.rectangle(image,(int(x_min),int(y_min),int(x_max),int(y_max)),(0,255,0),2)\nx_min = 226.0\ny_min = 548.0\nx_max = (226.0+130.0)\ny_max = (548.0+58.0)\n\ncv2.rectangle(image,(int(x_min),int(y_min),int(x_max),int(y_max)),(0,255,0),2)\nx_min = 834.0\ny_min = 95.0\nx_max = (834.0+109.0)\ny_max = (95.0+107.0)\n\ncv2.rectangle(image,(int(x_min),int(y_min),int(x_max),int(y_max)),(0,255,0),2)\n\nplt.imshow(image)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:01.304169Z","iopub.execute_input":"2024-12-10T03:41:01.304560Z","iopub.status.idle":"2024-12-10T03:41:01.750627Z","shell.execute_reply.started":"2024-12-10T03:41:01.304526Z","shell.execute_reply":"2024-12-10T03:41:01.749337Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Let's visualize one image with all the bboxes for that image","metadata":{}},{"cell_type":"code","source":"one_image_info = df[df['image_id']=='b53afdf5c']\none_image_info.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:06.925540Z","iopub.execute_input":"2024-12-10T03:41:06.925948Z","iopub.status.idle":"2024-12-10T03:41:06.952530Z","shell.execute_reply.started":"2024-12-10T03:41:06.925906Z","shell.execute_reply":"2024-12-10T03:41:06.951264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(one_image_info['bbox'].iloc[0][0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:08.571689Z","iopub.execute_input":"2024-12-10T03:41:08.572066Z","iopub.status.idle":"2024-12-10T03:41:08.578969Z","shell.execute_reply.started":"2024-12-10T03:41:08.572031Z","shell.execute_reply":"2024-12-10T03:41:08.578016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"one_image_info['X_min'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[0]))\none_image_info['y_min'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[1]))\none_image_info['X_max'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[2])+float(x.strip('[]').split(',')[0]))\none_image_info['y_max'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[3])+float(x.strip('[]').split(',')[1]))\none_image_info['W'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[2]))\none_image_info['H'] = one_image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[3]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:09.862191Z","iopub.execute_input":"2024-12-10T03:41:09.862629Z","iopub.status.idle":"2024-12-10T03:41:09.875333Z","shell.execute_reply.started":"2024-12-10T03:41:09.862593Z","shell.execute_reply":"2024-12-10T03:41:09.874238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"one_image_info","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:16.825405Z","iopub.execute_input":"2024-12-10T03:41:16.826353Z","iopub.status.idle":"2024-12-10T03:41:16.883543Z","shell.execute_reply.started":"2024-12-10T03:41:16.826300Z","shell.execute_reply":"2024-12-10T03:41:16.882386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bboxes = one_image_info[['X_min','y_min','X_max','y_max']].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:20.445967Z","iopub.execute_input":"2024-12-10T03:41:20.446742Z","iopub.status.idle":"2024-12-10T03:41:20.453494Z","shell.execute_reply.started":"2024-12-10T03:41:20.446679Z","shell.execute_reply":"2024-12-10T03:41:20.452400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for bbox in bboxes:\n    # print(bbox)\n    x1,y1,x2,y2 = bbox\n    # print(x1,y1,x2,y2)\n    cv2.rectangle(image,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:20.984249Z","iopub.execute_input":"2024-12-10T03:41:20.984599Z","iopub.status.idle":"2024-12-10T03:41:20.990840Z","shell.execute_reply.started":"2024-12-10T03:41:20.984568Z","shell.execute_reply":"2024-12-10T03:41:20.989898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/global-wheat-detection/train/b53afdf5c.jpg')\nfor bbox in bboxes:\n    x1,y1,x2,y2 = bbox\n    cv2.rectangle(image,(int(x1),int(y1)),(int(x2),int(y2)),(255,0,0),4)\n    # break\nplt.imshow(image)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:27.750099Z","iopub.execute_input":"2024-12-10T03:41:27.750486Z","iopub.status.idle":"2024-12-10T03:41:28.210932Z","shell.execute_reply.started":"2024-12-10T03:41:27.750453Z","shell.execute_reply":"2024-12-10T03:41:28.209943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:29.800896Z","iopub.execute_input":"2024-12-10T03:41:29.801310Z","iopub.status.idle":"2024-12-10T03:41:29.806313Z","shell.execute_reply.started":"2024-12-10T03:41:29.801275Z","shell.execute_reply":"2024-12-10T03:41:29.805190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Let's do this for a random picture\n\nrandom.choice(df['image_id'].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:32.407318Z","iopub.execute_input":"2024-12-10T03:41:32.407667Z","iopub.status.idle":"2024-12-10T03:41:32.423874Z","shell.execute_reply.started":"2024-12-10T03:41:32.407636Z","shell.execute_reply":"2024-12-10T03:41:32.422658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#function for creating the image with boxes\nrandom_image_id = random.choice(df['image_id'].unique())\nimage_info = df[df['image_id']==random_image_id]\nimage_info['X_min'] = image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[0]));\nimage_info['y_min'] = image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[1]));\nimage_info['X_max'] = image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[2])+float(x.strip('[]').split(',')[0]));\nimage_info['y_max'] = image_info['bbox'].apply(lambda x: float(x.strip('[]').split(',')[3])+float(x.strip('[]').split(',')[1]));\nbboxes = image_info[['X_min','y_min','X_max','y_max']].values\nimage = cv2.imread(f'/kaggle/input/global-wheat-detection/train/{random_image_id}.jpg')\n# image/=255\nfor bbox in bboxes:\n    x1,y1,x2,y2 = bbox\n    cv2.rectangle(image,(int(x1),int(y1)),(int(x2),int(y2)),(255,0,0),4)\n    # break\nplt.imshow(image)\n    \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:33.037947Z","iopub.execute_input":"2024-12-10T03:41:33.038315Z","iopub.status.idle":"2024-12-10T03:41:33.507675Z","shell.execute_reply.started":"2024-12-10T03:41:33.038284Z","shell.execute_reply":"2024-12-10T03:41:33.506666Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Visualizing the picture is done.","metadata":{}},{"cell_type":"code","source":"len(os.listdir('/kaggle/input/global-wheat-detection/test'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:38.969633Z","iopub.execute_input":"2024-12-10T03:41:38.970032Z","iopub.status.idle":"2024-12-10T03:41:38.977002Z","shell.execute_reply.started":"2024-12-10T03:41:38.969987Z","shell.execute_reply":"2024-12-10T03:41:38.975877Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Next steps:\n1. Create a custom dataset\n2. Create a NN model\n3. Train and validate","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torchvision import transforms\nfrom torch.utils.data import Dataset,DataLoader\nfrom torchvision.transforms import functional as F","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:41.750932Z","iopub.execute_input":"2024-12-10T03:41:41.751842Z","iopub.status.idle":"2024-12-10T03:41:46.400226Z","shell.execute_reply.started":"2024-12-10T03:41:41.751802Z","shell.execute_reply":"2024-12-10T03:41:46.399223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:41:46.401853Z","iopub.execute_input":"2024-12-10T03:41:46.402340Z","iopub.status.idle":"2024-12-10T03:41:46.412806Z","shell.execute_reply.started":"2024-12-10T03:41:46.402305Z","shell.execute_reply":"2024-12-10T03:41:46.411615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import ast","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:42:07.501011Z","iopub.execute_input":"2024-12-10T03:42:07.501763Z","iopub.status.idle":"2024-12-10T03:42:07.505758Z","shell.execute_reply.started":"2024-12-10T03:42:07.501707Z","shell.execute_reply":"2024-12-10T03:42:07.504819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(df['bbox'].iloc[10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:10.781981Z","iopub.execute_input":"2024-12-09T02:56:10.782600Z","iopub.status.idle":"2024-12-09T02:56:10.793228Z","shell.execute_reply.started":"2024-12-09T02:56:10.782530Z","shell.execute_reply":"2024-12-09T02:56:10.792543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"box = df['bbox'].apply(ast.literal_eval).iloc[10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:10.794301Z","iopub.execute_input":"2024-12-09T02:56:10.795074Z","iopub.status.idle":"2024-12-09T02:56:12.623402Z","shell.execute_reply.started":"2024-12-09T02:56:10.795035Z","shell.execute_reply":"2024-12-09T02:56:12.622715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:12.624318Z","iopub.execute_input":"2024-12-09T02:56:12.624579Z","iopub.status.idle":"2024-12-09T02:56:12.628550Z","shell.execute_reply.started":"2024-12-09T02:56:12.624552Z","shell.execute_reply":"2024-12-09T02:56:12.627645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#pre-process the df to make it easier to create bbox\ndef preprocess(df_any):\n    #convert the bboxes str -> list\n    df_any['bbox'] = df_any['bbox'].apply(ast.literal_eval)\n    df_any[['x1','y1','width','height']] = pd.DataFrame(df_any['bbox'].tolist(),index = df.index)\n    df_any['x2'] = df_any['x1']+df_any['width']\n    df_any['y2'] = df_any['y1']+df_any['height']\n    return df_any\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:42:11.106533Z","iopub.execute_input":"2024-12-10T03:42:11.106922Z","iopub.status.idle":"2024-12-10T03:42:11.112686Z","shell.execute_reply.started":"2024-12-10T03:42:11.106887Z","shell.execute_reply":"2024-12-10T03:42:11.111566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"processed_df = preprocess(df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:42:12.289241Z","iopub.execute_input":"2024-12-10T03:42:12.290342Z","iopub.status.idle":"2024-12-10T03:42:14.613103Z","shell.execute_reply.started":"2024-12-10T03:42:12.290297Z","shell.execute_reply":"2024-12-10T03:42:14.612243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"processed_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:42:55.426266Z","iopub.execute_input":"2024-12-10T03:42:55.427004Z","iopub.status.idle":"2024-12-10T03:42:55.442296Z","shell.execute_reply.started":"2024-12-10T03:42:55.426965Z","shell.execute_reply":"2024-12-10T03:42:55.441183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"b = processed_df[processed_df['image_id']=='b6ab77fd7'][['x1','y1','x2','y2']].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:00.672516Z","iopub.execute_input":"2024-12-10T03:43:00.672886Z","iopub.status.idle":"2024-12-10T03:43:00.694276Z","shell.execute_reply.started":"2024-12-10T03:43:00.672854Z","shell.execute_reply":"2024-12-10T03:43:00.692991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:01.356584Z","iopub.execute_input":"2024-12-10T03:43:01.356986Z","iopub.status.idle":"2024-12-10T03:43:01.365272Z","shell.execute_reply.started":"2024-12-10T03:43:01.356923Z","shell.execute_reply":"2024-12-10T03:43:01.364107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.ones(df.shape[0]) == torch.ones(df.shape[0],)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:02.959809Z","iopub.execute_input":"2024-12-10T03:43:02.960662Z","iopub.status.idle":"2024-12-10T03:43:03.037969Z","shell.execute_reply.started":"2024-12-10T03:43:02.960618Z","shell.execute_reply":"2024-12-10T03:43:03.037016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"b1 = torch.as_tensor(b,dtype = torch.float32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:06.902379Z","iopub.execute_input":"2024-12-10T03:43:06.902742Z","iopub.status.idle":"2024-12-10T03:43:06.915494Z","shell.execute_reply.started":"2024-12-10T03:43:06.902712Z","shell.execute_reply":"2024-12-10T03:43:06.914391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"-b1[:,1]+b1[:,3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:07.919743Z","iopub.execute_input":"2024-12-10T03:43:07.920109Z","iopub.status.idle":"2024-12-10T03:43:07.956682Z","shell.execute_reply.started":"2024-12-10T03:43:07.920076Z","shell.execute_reply":"2024-12-10T03:43:07.955716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class WheatDataset(Dataset):\n    def __init__(self,df,image_dir,transforms=None):\n        self.df = df\n        self.image_dir = image_dir\n        self.transforms = transforms\n\n    def __len__(self):\n        return self.df['image_id'].nunique()\n\n    def __getitem__(self,idx):\n        image_id = self.df['image_id'].unique()[idx]\n\n        #load image\n        image_path = os.path.join(self.image_dir,f\"{image_id}.jpg\")\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n\n\n        #Getting bounding box co-ordinates for the image\n        image_df = self.df[self.df['image_id']==image_id]\n        boxes = image_df[['x1','y1','x2','y2']].values\n\n        target = {\n            'boxes':torch.as_tensor(boxes,dtype = torch.float32),\n            'labels':torch.ones((image_df.shape[0],),dtype = torch.int64),\n            'image_id':torch.tensor([idx],dtype = torch.int64),\n            'area': torch.as_tensor((boxes[:,2]-boxes[:,0])*(boxes[:,3]-boxes[:,1]),dtype = torch.float32),\n            'iscrowd':torch.zeros(image_df.shape[0],dtype = torch.int64) #as all are single objects\n        }\n\n        if self.transforms:\n            augmented = self.transforms(image = image,bboxes = target['boxes'].tolist(),labels = target['labels'].tolist())\n            image = augmented['image']\n            target['boxes'] = torch.tensor(augmented['bboxes'])\n        # if self.transforms:\n        #     sample = {\n        #         'image':image,\n        #         'bboxes': target['boxes'].tolist(),\n        #         'labels': target['labels'].tolist()}\n        #     print(sample)\n        #     sample = self.transforms(**sample)\n        #     print(sample)\n        #     image = sample['image']\n        #     # target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)\n        #     target['boxes'] = torch.tensor(sample['bboxes'],dtype = torch.float32)\n        #     target['labels'] = torch.tensor(sample['labels'],dtype = torch.int64)\n\n        # # Ensure image is converted to PyTorch tensor\n        #     if not isinstance(image, torch.Tensor):\n        #         image = F.to_tensor(image)\n        # image = F.to_tensor(image) #This is also converting the image within 0..1\n        return image,target\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:43:12.195148Z","iopub.execute_input":"2024-12-10T03:43:12.195543Z","iopub.status.idle":"2024-12-10T03:43:12.206331Z","shell.execute_reply.started":"2024-12-10T03:43:12.195511Z","shell.execute_reply":"2024-12-10T03:43:12.205243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import albumentations as A\n# from albumentations.pytorch.transforms import ToTensorV2\nfrom albumentations.pytorch import ToTensorV2\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:45:40.919946Z","iopub.execute_input":"2024-12-10T03:45:40.920417Z","iopub.status.idle":"2024-12-10T03:45:40.925857Z","shell.execute_reply.started":"2024-12-10T03:45:40.920380Z","shell.execute_reply":"2024-12-10T03:45:40.924714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform_func = A.Compose([\n    A.RandomCrop(width=450,height=450,p=0.25),\n    A.Resize(height=512,width=512,p=1.0),\n    ToTensorV2(p=1.0)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:45:42.755025Z","iopub.execute_input":"2024-12-10T03:45:42.755917Z","iopub.status.idle":"2024-12-10T03:45:42.764326Z","shell.execute_reply.started":"2024-12-10T03:45:42.755869Z","shell.execute_reply":"2024-12-10T03:45:42.763303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_transform(train):\n    if train:\n        return A.Compose([\n            # A.Flip(0.5),\n            A.Resize(height=512,width=512,p=1.0),\n            A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),\n            ToTensorV2(p=1.0),\n        ],bbox_params=A.BboxParams(format='pascal_voc',label_fields=['labels']))\n\n    else:\n        return A.Compose([\n            # A.Flip(0.5),\n            A.Resize(height=512,width=512,p=1.0),\n            A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),\n            ToTensorV2(p=1.0)\n        ],bbox_params=A.BboxParams(format='pascal_voc',label_fields=['labels']))\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:45:50.925984Z","iopub.execute_input":"2024-12-10T03:45:50.926389Z","iopub.status.idle":"2024-12-10T03:45:50.933998Z","shell.execute_reply.started":"2024-12-10T03:45:50.926354Z","shell.execute_reply":"2024-12-10T03:45:50.932785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = '/kaggle/input/global-wheat-detection/train'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:45:51.886946Z","iopub.execute_input":"2024-12-10T03:45:51.887705Z","iopub.status.idle":"2024-12-10T03:45:51.892150Z","shell.execute_reply.started":"2024-12-10T03:45:51.887665Z","shell.execute_reply":"2024-12-10T03:45:51.891048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = WheatDataset(processed_df,train_dir,transforms=get_transform(train=True)) #=get_transform(train=True)\nvalid_data = WheatDataset(processed_df,train_dir,transforms=get_transform(train=False))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:45:52.483840Z","iopub.execute_input":"2024-12-10T03:45:52.484278Z","iopub.status.idle":"2024-12-10T03:45:52.494034Z","shell.execute_reply.started":"2024-12-10T03:45:52.484235Z","shell.execute_reply":"2024-12-10T03:45:52.493162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"indices = np.arange(len(train_data))\nnp.random.shuffle(indices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:04.067911Z","iopub.execute_input":"2024-12-10T03:46:04.069019Z","iopub.status.idle":"2024-12-10T03:46:04.082615Z","shell.execute_reply.started":"2024-12-10T03:46:04.068975Z","shell.execute_reply":"2024-12-10T03:46:04.081506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"split = int(0.8*len(indices))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:05.269375Z","iopub.execute_input":"2024-12-10T03:46:05.270455Z","iopub.status.idle":"2024-12-10T03:46:05.275143Z","shell.execute_reply.started":"2024-12-10T03:46:05.270401Z","shell.execute_reply":"2024-12-10T03:46:05.274153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_indices = indices[:split]\nvalid_indices = indices[split:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:05.911140Z","iopub.execute_input":"2024-12-10T03:46:05.912059Z","iopub.status.idle":"2024-12-10T03:46:05.916189Z","shell.execute_reply.started":"2024-12-10T03:46:05.912020Z","shell.execute_reply":"2024-12-10T03:46:05.915137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import Subset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:08.544110Z","iopub.execute_input":"2024-12-10T03:46:08.544533Z","iopub.status.idle":"2024-12-10T03:46:08.549405Z","shell.execute_reply.started":"2024-12-10T03:46:08.544500Z","shell.execute_reply":"2024-12-10T03:46:08.548257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_subset = Subset(train_data,train_indices)\nvalid_subset = Subset(valid_data,valid_indices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:09.134686Z","iopub.execute_input":"2024-12-10T03:46:09.135076Z","iopub.status.idle":"2024-12-10T03:46:09.139565Z","shell.execute_reply.started":"2024-12-10T03:46:09.135041Z","shell.execute_reply":"2024-12-10T03:46:09.138598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader = DataLoader(train_subset,batch_size=8,shuffle= True,num_workers=4,collate_fn = lambda x: tuple(zip(*x))) #very important to use collate_fn\nvalid_loader = DataLoader(valid_subset,batch_size=8,shuffle = False,num_workers=4,collate_fn = lambda x: tuple(zip(*x)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:09.681879Z","iopub.execute_input":"2024-12-10T03:46:09.682314Z","iopub.status.idle":"2024-12-10T03:46:09.688698Z","shell.execute_reply.started":"2024-12-10T03:46:09.682278Z","shell.execute_reply":"2024-12-10T03:46:09.687641Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Why Use collate_fn?**\n\n\nObject detection datasets often return images and targets of varying shapes. A custom collate_fn like tuple(zip(*x)) ensures the DataLoader batches them correctly.","metadata":{}},{"cell_type":"code","source":"from torchvision.models.detection import fasterrcnn_resnet50_fpn\n# ,ssd300_vgg16\n# from torchvision.models.detection.backbone_utils import BackboneWithFPN, mobilenet_backbone","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:46:12.564069Z","iopub.execute_input":"2024-12-10T03:46:12.564988Z","iopub.status.idle":"2024-12-10T03:46:12.568959Z","shell.execute_reply.started":"2024-12-10T03:46:12.564945Z","shell.execute_reply":"2024-12-10T03:46:12.568001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = fasterrcnn_resnet50_fpn(pretrained = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:01:31.820174Z","iopub.execute_input":"2024-12-10T04:01:31.821308Z","iopub.status.idle":"2024-12-10T04:01:32.605164Z","shell.execute_reply.started":"2024-12-10T04:01:31.821266Z","shell.execute_reply":"2024-12-10T04:01:32.604057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes = 2\nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = torchvision.models.detection.faster_rcnn.FastRCNNPredictor(in_features,num_classes)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:01:34.225707Z","iopub.execute_input":"2024-12-10T04:01:34.226082Z","iopub.status.idle":"2024-12-10T04:01:34.232365Z","shell.execute_reply.started":"2024-12-10T04:01:34.226050Z","shell.execute_reply":"2024-12-10T04:01:34.231152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nmodel = model.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:01:34.643520Z","iopub.execute_input":"2024-12-10T04:01:34.643884Z","iopub.status.idle":"2024-12-10T04:01:34.652525Z","shell.execute_reply.started":"2024-12-10T04:01:34.643852Z","shell.execute_reply":"2024-12-10T04:01:34.651485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch import optim","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:17.795326Z","iopub.execute_input":"2024-12-09T02:56:17.795599Z","iopub.status.idle":"2024-12-09T02:56:17.799461Z","shell.execute_reply.started":"2024-12-09T02:56:17.795571Z","shell.execute_reply":"2024-12-09T02:56:17.798669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#hyperparameters\noptimizer = optim.SGD(model.parameters(),lr=1e-03)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:17.800294Z","iopub.execute_input":"2024-12-09T02:56:17.800501Z","iopub.status.idle":"2024-12-09T02:56:17.816051Z","shell.execute_reply.started":"2024-12-09T02:56:17.800478Z","shell.execute_reply":"2024-12-09T02:56:17.815274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"next(iter(train_loader))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Training loop\ntrain_losses,test_losses = [],[]\nfor epoch in range(5):\n    model.train()\n    epoch_loss,test_loss = 0,0\n\n    for images,labels in train_loader:\n        # print(f\"Batch size (number of images): {len(images)}\")\n        images = [image.to(device) for image in images]\n        labels = [{k:v.to(device) for k,v in t.items()} for t in labels]\n\n        loss_dict = model(images,labels)\n        losses = sum(loss for loss in loss_dict.values())\n        epoch_loss += losses.item()\n        losses.backward()\n        optimizer.step()\n        optimizer.zero_grad()\n        torch.cuda.empty_cache()\n    avg_train_loss = epoch_loss/len(train_loader)\n    print(f'Epoch: {epoch} | Loss: {avg_train_loss}')\n\n    model.eval()\n    with torch.no_grad():\n        for images,labels in valid_loader:\n            images = [image.to(device) for image in images]\n            labels = [{k:v.to(device) for k,v in t.items()} for t in labels]\n            model.train()\n            loss_dict = model(images,labels)\n            model.eval()\n            losses = sum(loss for loss in loss_dict.values())\n            test_loss += losses\n        avg_test_loss = test_loss/len(valid_loader)\n        print(f'Epoch: {epoch} | Loss: {avg_train_loss}')\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T02:56:18.975054Z","iopub.execute_input":"2024-12-09T02:56:18.975313Z","iopub.status.idle":"2024-12-09T03:27:30.464103Z","shell.execute_reply.started":"2024-12-09T02:56:18.975288Z","shell.execute_reply":"2024-12-09T03:27:30.463001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_save_path = 'gwd.pth'\ntorch.save(model.state_dict(), model_save_path)\nprint(f\"Model saved to {model_save_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:27:30.465792Z","iopub.execute_input":"2024-12-09T03:27:30.466224Z","iopub.status.idle":"2024-12-09T03:27:30.740722Z","shell.execute_reply.started":"2024-12-09T03:27:30.466181Z","shell.execute_reply":"2024-12-09T03:27:30.739886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_path = '/kaggle/input/gwd_f_rcnn/pytorch/default/1'\nfiles = os.listdir(base_path)\n\n# model.load_state_dict(torch.load(model_save_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T03:55:02.048945Z","iopub.execute_input":"2024-12-10T03:55:02.050033Z","iopub.status.idle":"2024-12-10T03:55:02.055954Z","shell.execute_reply.started":"2024-12-10T03:55:02.049989Z","shell.execute_reply":"2024-12-10T03:55:02.054719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"checkpoint_path = '/kaggle/input/gwd_f_rcnn/pytorch/default/1/gwd.pth'\n# model = torch.load(checkpoint_path,map_location=torch.device('cpu'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:01:47.170185Z","iopub.execute_input":"2024-12-10T04:01:47.170606Z","iopub.status.idle":"2024-12-10T04:01:47.175338Z","shell.execute_reply.started":"2024-12-10T04:01:47.170572Z","shell.execute_reply":"2024-12-10T04:01:47.174278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(checkpoint_path, map_location=torch.device('cpu')))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:01:55.853271Z","iopub.execute_input":"2024-12-10T04:01:55.854465Z","iopub.status.idle":"2024-12-10T04:01:56.041172Z","shell.execute_reply.started":"2024-12-10T04:01:55.854421Z","shell.execute_reply":"2024-12-10T04:01:56.040104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get one batch using iter()\nvalid_iter = iter(valid_loader)\nimages, targets = next(valid_iter)  # Get the first batch\n\n# Move images to the correct device\nimages = [image.to(device) for image in images]\n\n# Perform inference\nmodel.eval()\nwith torch.no_grad():\n    predictions = model(images)\n\n# Process predictions\nfor i, (image, prediction) in enumerate(zip(images, predictions)):\n    predicted_boxes = prediction['boxes'].cpu().numpy()\n    predicted_labels = prediction['labels'].cpu().numpy()\n\n    # Convert image tensor to numpy for visualization (optional)\n    image_np = image.permute(1, 2, 0).cpu().numpy()  # HWC format\n    print(f\"Image {i} - Predicted Boxes: {predicted_boxes}, Labels: {predicted_labels}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:15:07.653132Z","iopub.execute_input":"2024-12-10T04:15:07.653545Z","iopub.status.idle":"2024-12-10T04:15:40.261765Z","shell.execute_reply.started":"2024-12-10T04:15:07.653511Z","shell.execute_reply":"2024-12-10T04:15:40.260257Z"},"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2\n\n# Example for the first image in the batch\nimage_np = images[1].permute(1, 2, 0).cpu().numpy()  # HWC format\nboxes = predictions[1]['boxes'].cpu().numpy()\n\n# Plot the image\nplt.figure(figsize=(10, 10))\nfor box in boxes:\n    x1, y1, x2, y2 = box\n    cv2.rectangle(image_np, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)  # Draw box\nplt.imshow(image_np)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:20:10.856673Z","iopub.execute_input":"2024-12-10T04:20:10.857127Z","iopub.status.idle":"2024-12-10T04:20:11.441524Z","shell.execute_reply.started":"2024-12-10T04:20:10.857093Z","shell.execute_reply":"2024-12-10T04:20:11.440391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model\nmodel.eval()\nwith torch.no_grad():\n    for images,targets in valid_loader:\n        images = [image.to(device) for image in images]\n        predictions = model(images)\n\n        images_np = [(image) for image in images]\n        predicted_boxes = [pred['boxes'].cpu().numpy() for pred in predictions]\n        predicted_labels = [pred['labels'].cpu().numpy() for pred in predictions]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T04:02:00.783026Z","iopub.execute_input":"2024-12-10T04:02:00.783959Z","iopub.status.idle":"2024-12-10T04:13:28.900349Z","shell.execute_reply.started":"2024-12-10T04:02:00.783914Z","shell.execute_reply":"2024-12-10T04:13:28.898383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for pred in predictions:\n    print (pred)\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:28:05.735548Z","iopub.execute_input":"2024-12-09T03:28:05.735914Z","iopub.status.idle":"2024-12-09T03:28:05.784670Z","shell.execute_reply.started":"2024-12-09T03:28:05.735882Z","shell.execute_reply":"2024-12-09T03:28:05.783873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-09T03:28:05.785830Z","iopub.execute_input":"2024-12-09T03:28:05.786438Z","iopub.status.idle":"2024-12-09T03:28:05.794253Z","shell.execute_reply.started":"2024-12-09T03:28:05.786399Z","shell.execute_reply":"2024-12-09T03:28:05.793416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}