{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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"}],"dockerImageVersionId":30918,"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,"execution":{"iopub.status.busy":"2025-03-24T06:38:02.763127Z","iopub.execute_input":"2025-03-24T06:38:02.763505Z","iopub.status.idle":"2025-03-24T06:38:08.989574Z","shell.execute_reply.started":"2025-03-24T06:38:02.763466Z","shell.execute_reply":"2025-03-24T06:38:08.969033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport time\nimport shutil\nimport random\n\nimport numpy as np\nimport pandas as pd\n\nimport torch\nimport torchvision\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor, FasterRCNN\nfrom torchvision.models.detection.backbone_utils import resnet_fpn_backbone\nfrom tqdm.notebook import tqdm\n\nimport albumentations\nfrom albumentations import *\nfrom albumentations.pytorch.transforms import ToTensorV2\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib import patches\nimport seaborn as sns\n%matplotlib inline\nprint('Ready...')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:40:30.820116Z","iopub.execute_input":"2025-03-24T06:40:30.820487Z","iopub.status.idle":"2025-03-24T06:40:41.67818Z","shell.execute_reply.started":"2025-03-24T06:40:30.820454Z","shell.execute_reply":"2025-03-24T06:40:41.677046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#loading data and Simple EDA\nDATA_DIR  = '../input/global-wheat-detection/train/'\nTEST_DIR  = '../input/global-wheat-detection/test/'\ntrain_df_path = '../input/global-wheat-detection/train.csv'\ntest_df_path = '../input/global-wheat-detection/sample_submission.csv'\nList_Data_dir = os.listdir(DATA_DIR)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:41:11.793775Z","iopub.execute_input":"2025-03-24T06:41:11.794352Z","iopub.status.idle":"2025-03-24T06:41:11.801971Z","shell.execute_reply.started":"2025-03-24T06:41:11.794323Z","shell.execute_reply":"2025-03-24T06:41:11.801004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw = pd.read_csv(train_df_path)\nraw","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:41:26.532117Z","iopub.execute_input":"2025-03-24T06:41:26.532457Z","iopub.status.idle":"2025-03-24T06:41:26.880593Z","shell.execute_reply.started":"2025-03-24T06:41:26.532432Z","shell.execute_reply":"2025-03-24T06:41:26.879633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# check if there is empty data\nraw.isnull().any().any()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:41:58.278576Z","iopub.execute_input":"2025-03-24T06:41:58.279014Z","iopub.status.idle":"2025-03-24T06:41:58.310383Z","shell.execute_reply.started":"2025-03-24T06:41:58.278982Z","shell.execute_reply":"2025-03-24T06:41:58.309368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:42:08.93457Z","iopub.execute_input":"2025-03-24T06:42:08.934992Z","iopub.status.idle":"2025-03-24T06:42:08.985133Z","shell.execute_reply.started":"2025-03-24T06:42:08.934963Z","shell.execute_reply":"2025-03-24T06:42:08.983926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:42:25.550193Z","iopub.execute_input":"2025-03-24T06:42:25.550526Z","iopub.status.idle":"2025-03-24T06:42:25.575409Z","shell.execute_reply.started":"2025-03-24T06:42:25.5505Z","shell.execute_reply":"2025-03-24T06:42:25.574256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f'Total number of train images: {raw.image_id.nunique()}')\nprint(f'Total number of test images: {len(os.listdir(TEST_DIR))}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:42:41.960938Z","iopub.execute_input":"2025-03-24T06:42:41.961279Z","iopub.status.idle":"2025-03-24T06:42:41.979191Z","shell.execute_reply.started":"2025-03-24T06:42:41.961254Z","shell.execute_reply":"2025-03-24T06:42:41.977935Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#visualize wheat distribution from source column\n\nplt.figure(figsize=(15,8))\nplt.title('Wheat Distribution', fontsize= 20)\nsns.countplot(x=\"source\", data=raw)\n\n# based on the chart, there are seven types of wheat from images data, with the most types 'ethz_1' and the least is 'inrae_1'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:43:28.367996Z","iopub.execute_input":"2025-03-24T06:43:28.368316Z","iopub.status.idle":"2025-03-24T06:43:29.01244Z","shell.execute_reply.started":"2025-03-24T06:43:28.368292Z","shell.execute_reply":"2025-03-24T06:43:29.011228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# visualize somw images we have\n\nfig, ax = plt.subplots(1, 2, figsize = (24, 24))\nax = ax.flatten()\nimg_path = os.path.join(DATA_DIR, 'b6ab77fd7' + '.jpg')\nimg_path2 = os.path.join(DATA_DIR, '5e0747034' + '.jpg')\n\nimage = cv2.imread(img_path, cv2.IMREAD_COLOR)\nimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\nimage /= 255.0\n    \nax[0].set_title('Image')\nax[0].imshow(image)\n    \nimage2 = cv2.imread(img_path2, cv2.IMREAD_COLOR)\nimage2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB).astype(np.float32)\nimage2 /= 255.0\n\nax[1].set_title('Image')\nax[1].imshow(image2)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:44:05.342745Z","iopub.execute_input":"2025-03-24T06:44:05.343202Z","iopub.status.idle":"2025-03-24T06:44:07.419065Z","shell.execute_reply.started":"2025-03-24T06:44:05.34317Z","shell.execute_reply":"2025-03-24T06:44:07.417437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extract bbox column to xmin, ymin, width, height, then create xmax, ymax, and area columns\n\nraw[['xmin','ymin','w','h']] = pd.DataFrame(raw.bbox.str.strip('[]').str.split(',').tolist()).astype(float)\nraw['xmax'], raw['ymax'], raw['area'] = raw['xmin'] + raw['w'], raw['ymin'] + raw['h'], raw['w'] * raw['h']\nraw.drop(['bbox'], axis=1, inplace= True)\nraw","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:44:23.995538Z","iopub.execute_input":"2025-03-24T06:44:23.996005Z","iopub.status.idle":"2025-03-24T06:44:24.627854Z","shell.execute_reply.started":"2025-03-24T06:44:23.99597Z","shell.execute_reply":"2025-03-24T06:44:24.626761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# In object detection, we usually use a boundary box to describe the target location. The boundary box is a rectangular box that can be determined by the x and y axis coordinates in the upper-left corner and the x and y axis coordinates in the lower-right corner of the rectangle.","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:44:43.604757Z","iopub.execute_input":"2025-03-24T06:44:43.605168Z","iopub.status.idle":"2025-03-24T06:44:43.610105Z","shell.execute_reply.started":"2025-03-24T06:44:43.605139Z","shell.execute_reply":"2025-03-24T06:44:43.608686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_image(image_id):\n    \n    fig, ax = plt.subplots(1, 2, figsize = (24, 24))\n    ax = ax.flatten()\n    \n    bbox = raw[raw['image_id'] == image_id ]\n    img_path = os.path.join(DATA_DIR, image_id + '.jpg')\n    \n    image = cv2.imread(img_path, cv2.IMREAD_COLOR)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n    image /= 255.0\n    image2 = image\n    \n    ax[0].set_title('Original Image')\n    ax[0].imshow(image)\n    \n    for idx, row in bbox.iterrows():\n        x1 = row['xmin']\n        y1 = row['ymin']\n        x2 = row['xmax']\n        y2 = row['ymax']\n        label = row['source']\n        \n        cv2.rectangle(image2, (int(x1),int(y1)), (int(x2),int(y2)), (255,255,255), 2)\n        font = cv2.FONT_HERSHEY_SIMPLEX\n        cv2.putText(image2, label, (int(x1),int(y1-10)), font, 1, (255,255,255), 2)\n    \n    ax[1].set_title('Image with Bondary Box')\n    ax[1].imshow(image2)\n\n\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:45:32.118108Z","iopub.execute_input":"2025-03-24T06:45:32.118432Z","iopub.status.idle":"2025-03-24T06:45:32.126837Z","shell.execute_reply.started":"2025-03-24T06:45:32.118408Z","shell.execute_reply":"2025-03-24T06:45:32.125462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_image(raw.image_id.unique()[91])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:45:52.36399Z","iopub.execute_input":"2025-03-24T06:45:52.364356Z","iopub.status.idle":"2025-03-24T06:45:54.018605Z","shell.execute_reply.started":"2025-03-24T06:45:52.364329Z","shell.execute_reply":"2025-03-24T06:45:54.016989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_image(raw.image_id.unique()[1231])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:46:07.127723Z","iopub.execute_input":"2025-03-24T06:46:07.128138Z","iopub.status.idle":"2025-03-24T06:46:08.674948Z","shell.execute_reply.started":"2025-03-24T06:46:07.12811Z","shell.execute_reply":"2025-03-24T06:46:08.672462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_image(raw.image_id.unique()[3121])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:46:17.612405Z","iopub.execute_input":"2025-03-24T06:46:17.6128Z","iopub.status.idle":"2025-03-24T06:46:19.026858Z","shell.execute_reply.started":"2025-03-24T06:46:17.612766Z","shell.execute_reply":"2025-03-24T06:46:19.025736Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Augmentations¶\nDa# ****ta augmentation is a strategy that enables practitioners to significantly increase the diversity of data available for training models, without actually collecting new data. In image data, augmentation can range from basic image manipulation like color variation, Fliping, resize, or rotate image, and data augmentation can also reduce overfitting****","metadata":{}},{"cell_type":"code","source":"def get_bboxes(bboxes, col, bbox_format = 'pascal_voc', color='white'):\n    for i in range(len(bboxes)):\n        x_min = bboxes[i][0]\n        y_min = bboxes[i][1]\n        x_max = bboxes[i][2]\n        y_max = bboxes[i][3]\n        width = x_max - x_min\n        height = y_max - y_min\n        rect = patches.Rectangle((x_min, y_min), \n                                 width, height, \n                                 linewidth=2, \n                                 edgecolor=color, \n                                 facecolor='none')\n        col.add_patch(rect)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:47:09.473599Z","iopub.execute_input":"2025-03-24T06:47:09.474042Z","iopub.status.idle":"2025-03-24T06:47:09.480281Z","shell.execute_reply.started":"2025-03-24T06:47:09.474011Z","shell.execute_reply":"2025-03-24T06:47:09.478956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def augmented_images(image, augment):\n    \n    fig, ax = plt.subplots(1, 2, figsize = (24, 24))\n    ax = ax.flatten()\n    \n    image_data = raw[raw['image_id'] == image]\n    bbox = image_data[['xmin', 'ymin', 'xmax', 'ymax']].astype(np.int32).values\n    labels = np.ones((len(bbox), ))\n\n    image = cv2.imread(os.path.join(DATA_DIR + '/{}.jpg').format(image), cv2.IMREAD_COLOR)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n    image /= 255.0\n    \n    get_bboxes(bbox, ax[0], color='white')\n    \n    ax[0].set_title('Original Image with Bounding Boxes')\n    ax[0].imshow(image)\n    \n    aug = albumentations.Compose([augment], \n                         bbox_params={'format': 'pascal_voc', 'label_fields':['labels']})\n\n    \n    aug_result = aug(image=image, bboxes=bbox, labels=labels)\n\n    aug_image = aug_result['image']\n    get_bboxes(aug_result['bboxes'], ax[1], color='red')\n    ax[1].set_title('Augmented Image with Bounding Boxes')\n    ax[1].imshow(aug_image)\n    \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:47:47.912235Z","iopub.execute_input":"2025-03-24T06:47:47.912587Z","iopub.status.idle":"2025-03-24T06:47:47.922218Z","shell.execute_reply.started":"2025-03-24T06:47:47.91256Z","shell.execute_reply":"2025-03-24T06:47:47.920855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# HorizontalFlip Augmentation\naugmented_images(raw.image_id.unique()[1230], albumentations.HorizontalFlip(p=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:48:02.225772Z","iopub.execute_input":"2025-03-24T06:48:02.226239Z","iopub.status.idle":"2025-03-24T06:48:04.144846Z","shell.execute_reply.started":"2025-03-24T06:48:02.226204Z","shell.execute_reply":"2025-03-24T06:48:04.143086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# VerticalFlip Augmentation\naugmented_images(raw.image_id.unique()[2110], albumentations.VerticalFlip(p=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:48:17.932099Z","iopub.execute_input":"2025-03-24T06:48:17.932442Z","iopub.status.idle":"2025-03-24T06:48:19.884508Z","shell.execute_reply.started":"2025-03-24T06:48:17.932415Z","shell.execute_reply":"2025-03-24T06:48:19.882514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Change Color to gray\naugmented_images(raw.image_id.unique()[1212], albumentations.ToGray(p=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:48:40.116039Z","iopub.execute_input":"2025-03-24T06:48:40.116451Z","iopub.status.idle":"2025-03-24T06:48:41.969494Z","shell.execute_reply.started":"2025-03-24T06:48:40.116417Z","shell.execute_reply":"2025-03-24T06:48:41.967757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Random Change Brightness Contrast\naugmented_images(raw.image_id.unique()[1230], albumentations.RandomBrightnessContrast(p=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:48:49.479985Z","iopub.execute_input":"2025-03-24T06:48:49.480316Z","iopub.status.idle":"2025-03-24T06:48:51.402343Z","shell.execute_reply.started":"2025-03-24T06:48:49.480291Z","shell.execute_reply":"2025-03-24T06:48:51.400791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import albumentations as A\nimport random\n\n# Define the CoarseDropout (Cutout) transformation\ncutout_transform = A.CoarseDropout(\n    max_holes=random.randint(10, 20),  # Number of cutout regions\n    max_height=64,                     # Max height of each cutout\n    max_width=64,                      # Max width of each cutout\n    p=1                                 # Always apply\n)\n\n# Apply the transformation to an image\nimage_id = raw.image_id.unique()[230]  # Select an image ID\naugmented_images(image_id, cutout_transform)  # Apply augmentation\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:54:23.773209Z","iopub.execute_input":"2025-03-24T06:54:23.773655Z","iopub.status.idle":"2025-03-24T06:54:25.37543Z","shell.execute_reply.started":"2025-03-24T06:54:23.773544Z","shell.execute_reply":"2025-03-24T06:54:25.374303Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Data preprocessing(Train Data)","metadata":{}},{"cell_type":"code","source":"class wheatdataset_train(Dataset):\n       \n    def __init__(self, dataframe, data_dir, transforms=None):\n        super().__init__()\n        self.df = dataframe \n        self.image_list = list(self.df['image_id'].unique())\n        self.image_dir = data_dir\n        self.transforms = transforms\n    \n    def __len__(self):\n        return len(self.image_list)\n        \n    def __getitem__(self, idx):\n        \n        image_id = self.image_list[idx]\n        image_data = self.df.loc[self.df['image_id'] == image_id]\n        boxes = torch.as_tensor(np.array(image_data[['xmin','ymin','xmax','ymax']]), \n                                dtype=torch.float32)\n        area = torch.tensor(np.array(image_data['area']), dtype=torch.int64) \n        labels = torch.ones((image_data.shape[0],), dtype=torch.int64)\n        iscrowd = torch.zeros((image_data.shape[0],), dtype=torch.uint8)\n         \n        target = {}\n        target['boxes'] = boxes\n        target['area'] = area\n        target['labels'] = labels\n        target['iscrowd'] = iscrowd\n\n        image = cv2.imread((self.image_dir + '/' + image_id + '.jpg'), cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        image /= 255.0\n        \n        if self.transforms:\n            \n            image_transforms = {\n                                'image': image,\n                                'bboxes': target['boxes'],\n                                'labels': labels\n                                 }\n            \n            image_transforms = self.transforms(**image_transforms)\n            image = image_transforms['image']\n            \n            target['boxes'] = torch.as_tensor(image_transforms['bboxes'], dtype=torch.float32)\n                 \n        return image, target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:51:56.270428Z","iopub.execute_input":"2025-03-24T06:51:56.27082Z","iopub.status.idle":"2025-03-24T06:51:56.280467Z","shell.execute_reply.started":"2025-03-24T06:51:56.270765Z","shell.execute_reply":"2025-03-24T06:51:56.279317Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Albumentations\n\ndef get_train_transform():\n    return albumentations.Compose([\n        #albumentations.Resize(p=1, height=512, width=512),\n        albumentations.ToGray(p=0.5),\n        albumentations.Flip(p=0.5),\n        albumentations.RandomBrightnessContrast(p=0.5),\n        albumentations.Cutout(num_holes= random.randint(10, 20), max_h_size= 64, max_w_size =64, p = 0.40),\n        ToTensorV2(p=1.0)\n    ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})\n\n\ndef get_test_transform():\n    return albumentations.Compose([\n        ToTensorV2(p=1.0)\n    ])\n\n\ndef collate_fn(batch):\n    return tuple(zip(*batch))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:52:11.937054Z","iopub.execute_input":"2025-03-24T06:52:11.937503Z","iopub.status.idle":"2025-03-24T06:52:11.946469Z","shell.execute_reply.started":"2025-03-24T06:52:11.937465Z","shell.execute_reply":"2025-03-24T06:52:11.945193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import albumentations as A\nfrom albumentations.pytorch import ToTensorV2\nimport random\n\ndef get_train_transform():\n    return A.Compose([\n        A.HorizontalFlip(p=0.5),  # Replacing deprecated Flip\n        A.RandomBrightnessContrast(p=0.5),\n        A.CoarseDropout(\n            max_holes=random.randint(10, 20),  # Number of cutout regions\n            max_height=64, \n            max_width=64, \n            p=0.4  # Probability of applying this augmentation\n        ),\n        ToTensorV2(p=1.0)  # Convert to tensor\n    ], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['labels']))\n\n# Apply transformation in dataset\ntrain_data = wheatdataset_train(raw, DATA_DIR, get_train_transform())\ntrain_dataloader = DataLoader(train_data, batch_size=16, shuffle=True, num_workers=4, collate_fn=collate_fn)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:56:03.191427Z","iopub.execute_input":"2025-03-24T06:56:03.191826Z","iopub.status.idle":"2025-03-24T06:56:03.209342Z","shell.execute_reply.started":"2025-03-24T06:56:03.191777Z","shell.execute_reply":"2025-03-24T06:56:03.208308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:56:17.218131Z","iopub.execute_input":"2025-03-24T06:56:17.218482Z","iopub.status.idle":"2025-03-24T06:56:17.225111Z","shell.execute_reply.started":"2025-03-24T06:56:17.218455Z","shell.execute_reply":"2025-03-24T06:56:17.223892Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"creating a model and trainings\n","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\ntorch.cuda.empty_cache()\nprint(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:56:53.714043Z","iopub.execute_input":"2025-03-24T06:56:53.714381Z","iopub.status.idle":"2025-03-24T06:56:53.725295Z","shell.execute_reply.started":"2025-03-24T06:56:53.714354Z","shell.execute_reply":"2025-03-24T06:56:53.724227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_model():\n    \n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)\n    num_classes = 2\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:57:09.667436Z","iopub.execute_input":"2025-03-24T06:57:09.667782Z","iopub.status.idle":"2025-03-24T06:57:09.673224Z","shell.execute_reply.started":"2025-03-24T06:57:09.667756Z","shell.execute_reply":"2025-03-24T06:57:09.671873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train(data_loader, epoch):\n        \n    model = train_model()\n    model.train()\n    model.to(device)\n    params = [p for p in model.parameters() if p.requires_grad]\n    optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005)\n    model.parameters\n\n\n    total_train_loss = []\n    itr = 1\n\n    for epoch in tqdm(range(epoch)):\n        \n        print(f'Epoch :{epoch + 1}')\n        start_time = time.time()\n        train_loss = []\n        \n        for images, targets in tqdm(data_loader):\n            \n            images = list(image.to(device) for image in images)\n            targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n\n            loss_dict = model(images, targets)\n\n            losses = sum(loss for loss in loss_dict.values())\n\n            loss_value = losses.item()\n            \n            train_loss.append(losses.item())\n            optimizer.zero_grad()\n            losses.backward()\n            optimizer.step()\n            \n            if itr % 50 == 0:\n                print(f\"Iteration #{itr} loss: {loss_value:.4f}\")\n\n            itr += 1\n    \n        \n        epoch_train_loss = np.mean(train_loss)\n        total_train_loss.append(epoch_train_loss)\n        print(f'Epoch train loss is {epoch_train_loss:.4f}')\n        time_elapsed = time.time() - start_time\n        print('{:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))\n\n    torch.save(model.state_dict(), 'resnet50_GWD.pth')\n\n    #visualize\n    plt.figure(figsize=(12,6))\n    plt.title('Train Loss', fontsize= 20)\n    plt.plot(total_train_loss)\n    plt.xlabel('epochs')\n    plt.ylabel('loss')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:57:48.724261Z","iopub.execute_input":"2025-03-24T06:57:48.724688Z","iopub.status.idle":"2025-03-24T06:57:48.73513Z","shell.execute_reply.started":"2025-03-24T06:57:48.724641Z","shell.execute_reply":"2025-03-24T06:57:48.733984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#inference and Compare Model\ntest_df = pd.read_csv(test_df_path)\ntest_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T06:58:27.339107Z","iopub.execute_input":"2025-03-24T06:58:27.339486Z","iopub.status.idle":"2025-03-24T06:58:27.359357Z","shell.execute_reply.started":"2025-03-24T06:58:27.339454Z","shell.execute_reply":"2025-03-24T06:58:27.358052Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Data preprocessing(Test Data)","metadata":{}},{"cell_type":"code","source":"class wheatdataset_test(Dataset):\n       \n    def __init__(self, dataframe, data_dir, transforms=None):\n        super().__init__()\n        self.df = dataframe \n        self.image_list = list(self.df['image_id'].unique())\n        self.image_dir = data_dir\n        self.transforms = transforms\n    \n    def __len__(self):\n        return len(self.image_list)\n        \n    def __getitem__(self, idx):\n        \n        image_id = self.image_list[idx]\n        image_data = self.df.loc[self.df['image_id'] == image_id]  \n        image = cv2.imread((self.image_dir + '/' + image_id + '.jpg'), cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        image /= 255.0\n        \n        if self.transforms:\n            \n            image_transforms = {\n                                'image': image\n                                 }\n            \n            image_transforms = self.transforms(**image_transforms)\n\n            image = image_transforms['image']\n                             \n        return image, image_id","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:00:07.189412Z","iopub.execute_input":"2025-03-24T07:00:07.189774Z","iopub.status.idle":"2025-03-24T07:00:07.197993Z","shell.execute_reply.started":"2025-03-24T07:00:07.189743Z","shell.execute_reply":"2025-03-24T07:00:07.196587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dataset = wheatdataset_test(test_df, TEST_DIR, get_test_transform())\n\ntest_data_loader = DataLoader(\n    test_dataset,\n    batch_size=4,\n    shuffle=False,\n    num_workers=4,\n    drop_last=False,\n    collate_fn=collate_fn\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:00:24.896541Z","iopub.execute_input":"2025-03-24T07:00:24.897132Z","iopub.status.idle":"2025-03-24T07:00:24.903642Z","shell.execute_reply.started":"2025-03-24T07:00:24.896982Z","shell.execute_reply":"2025-03-24T07:00:24.90237Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# inferences","metadata":{}},{"cell_type":"code","source":"detection_threshold = 0.3\nresults = []\ncolor = (255,255,255)\nthickness = 2\nfont = cv2.FONT_HERSHEY_SIMPLEX   \n\ndef save_model(model_number):\n\n    MODEL_USE = model_number\n    if MODEL_USE == 1:\n        MODEL_NAME = 'Faster_RCNN_1'\n        WEIGHT_PATH = '../input/global-wheat-detection-model/Faster_RCNN/resnet50_GWD_3.pth'\n    elif MODEL_USE == 2:\n        MODEL_NAME = 'Faster_RCNN_2'\n        WEIGHT_PATH = '../input/global-wheat-detection-model/Faster_RCNN/resnet50_GWD_4.pth'\n    elif MODEL_USE == 3:\n        MODEL_NAME = 'Faster_RCNN_3'\n        WEIGHT_PATH = '../input/global-wheat-detection-model/Faster_RCNN/resnet50_GWD_6.pth'\n    elif MODEL_USE == 4:\n        MODEL_NAME = 'Faster_RCNN_4'\n        WEIGHT_PATH = '../input/global-wheat-detection-model/Faster_RCNN/resnet50_GWD_5.pth'\n    elif MODEL_USE == 5:\n        MODEL_NAME = 'Faster_RCNN_5'\n        WEIGHT_PATH = '../input/global-wheat-detection-model/Faster_RCNN_v2/resnet50_GWD_7.pth'\n    elif MODEL_USE == 6:\n        MODEL_NAME = 'Faster_RCNN_6'\n        WEIGHT_PATH = '../input/gwd-model/resnet50_GWD_9.pth' \n    return MODEL_NAME, WEIGHT_PATH\n\ndef test_model():\n    \n    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False,\n                                                                 pretrained_backbone=False)\n    num_classes = 2\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n    return model\n\ndef format_prediction_string(boxes, scores):\n    pred_strings = []\n    for j in zip(scores, boxes):\n        pred_strings.append(\"{0:.4f} {1} {2} {3} {4}\".format(j[0], j[1][0], j[1][1], \n                                                             j[1][2], j[1][3]))\n\n    return \" \".join(pred_strings)\n\ndef show_result(data_loader, model_number, color, subs = False):\n\n\n    MODEL_NAME, WEIGHT_PATH = save_model(model_number)\n    model = test_model()\n    model.load_state_dict(torch.load(WEIGHT_PATH))\n    model.to(device)\n    model.eval()\n    print(f'Model Name : {MODEL_NAME}')\n\n    for images, image_ids in data_loader:\n\n        images = list(image.to(device) for image in images)\n        outputs = model(images)\n\n        for i, image in enumerate(images):\n\n            boxes = outputs[i]['boxes'].data.cpu().numpy()\n            scores = outputs[i]['scores'].data.cpu().numpy()\n                \n            boxes = boxes[scores >= detection_threshold].astype(np.int32)\n            scores = scores[scores >= detection_threshold]\n            image_id = image_ids[i]\n                \n            boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n            boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n\n            if subs is True:\n                result = {\n                          'image_id': image_id,\n                          'PredictionString': format_prediction_string(boxes, scores)\n                          }\n                results.append(result)\n\n            im = cv2.imread('{}/{}.jpg'.format(TEST_DIR, image_id))\n            im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB).astype(np.float32)\n            im /= 255.0\n\n            for b,s in zip(boxes,scores):\n                cv2.rectangle(im, (b[0],b[1]), (b[0]+b[2],b[1]+b[3]), color, thickness)\n                cv2.putText(im, '{:.2}'.format(s), (b[0],b[1]), font, 1, color, thickness)\n                        \n            plt.figure(figsize=(12,12))\n            plt.imshow(im)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:03:23.520917Z","iopub.execute_input":"2025-03-24T07:03:23.521354Z","iopub.status.idle":"2025-03-24T07:03:23.537269Z","shell.execute_reply.started":"2025-03-24T07:03:23.521322Z","shell.execute_reply":"2025-03-24T07:03:23.535788Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"#Comapre model","metadata":{}},{"cell_type":"code","source":"import os\nimport torch\n\n# Define correct model path\nWEIGHT_PATH = \"/path/to/your/model.pth\"  # Change this to the actual path\n\n# Check if file exists before loading\nif not os.path.exists(WEIGHT_PATH):\n    raise FileNotFoundError(f\"Model weights not found at {WEIGHT_PATH}. Check the file path!\")\n\n# Load model\nmodel = test_model()  # Ensure `test_model()` initializes the model correctly\nmodel.load_state_dict(torch.load(WEIGHT_PATH, map_location=torch.device('cuda' if torch.cuda.is_available() else 'cpu')))\nmodel.to(torch.device('cuda' if torch.cuda.is_available() else 'cpu'))\nmodel.eval()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:06:45.671888Z","iopub.execute_input":"2025-03-24T07:06:45.672474Z","iopub.status.idle":"2025-03-24T07:06:45.697767Z","shell.execute_reply.started":"2025-03-24T07:06:45.67242Z","shell.execute_reply":"2025-03-24T07:06:45.696236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_result(test_data_loader, model_number= 2, color = (255,255,0), subs = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T07:07:04.950947Z","iopub.execute_input":"2025-03-24T07:07:04.951396Z","iopub.status.idle":"2025-03-24T07:07:05.727293Z","shell.execute_reply.started":"2025-03-24T07:07:04.951359Z","shell.execute_reply":"2025-03-24T07:07:05.725493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}