{"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"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os \nimport ast","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T14:05:28.609320Z","iopub.execute_input":"2024-11-02T14:05:28.609756Z","iopub.status.idle":"2024-11-02T14:05:28.615022Z","shell.execute_reply.started":"2024-11-02T14:05:28.609716Z","shell.execute_reply":"2024-11-02T14:05:28.613613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nfrom PIL import Image, ImageDraw\nfrom collections import Counter\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torchvision import io\nfrom torchvision.models.detection.faster_rcnn import FasterRCNN","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T15:10:00.449469Z","iopub.execute_input":"2024-11-02T15:10:00.450258Z","iopub.status.idle":"2024-11-02T15:10:00.456123Z","shell.execute_reply.started":"2024-11-02T15:10:00.450210Z","shell.execute_reply":"2024-11-02T15:10:00.455037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = pd.read_csv(r'../input/global-wheat-detection/train.csv')\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T15:07:49.318903Z","iopub.execute_input":"2024-11-02T15:07:49.320033Z","iopub.status.idle":"2024-11-02T15:07:49.531345Z","shell.execute_reply.started":"2024-11-02T15:07:49.319979Z","shell.execute_reply":"2024-11-02T15:07:49.530022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class WheatDataset(Dataset):\n    def __init__(self, img_dir, annotations_file, transforms=None):\n        self.img_dir = img_dir\n        self.transforms = transforms\n        self.df = pd.read_csv(annotations_file)\n        self.imgs = self.df['image_id'].unique()\n\n    def __len__(self):\n        return len(self.imgs)\n\n    def __getitem__(self, idx):\n        image_id = self.imgs[idx]\n        img_path = os.path.join(self.img_dir, f\"{image_id}.jpg\")\n        img = Image.open(img_path).convert(\"RGB\")\n        records = self.df[self.df['image_id'] == image_id]\n        boxes = [list(map(float, row['bbox'][1:-1].split(\",\"))) for _, row in records.iterrows()]\n        target = {\n            \"boxes\": torch.tensor(boxes, dtype=torch.float32),\n            \"labels\": torch.ones((len(boxes),), dtype=torch.int64)\n        }\n        target[\"boxes\"] = box_convert(target[\"boxes\"], in_fmt=\"xywh\", out_fmt=\"xyxy\")\n\n        if self.transforms:\n            augmented = self.transforms(image=np.array(img))\n            img = augmented['image']\n\n        return img, target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T15:08:39.018956Z","iopub.execute_input":"2024-11-02T15:08:39.019401Z","iopub.status.idle":"2024-11-02T15:08:39.029127Z","shell.execute_reply.started":"2024-11-02T15:08:39.019360Z","shell.execute_reply":"2024-11-02T15:08:39.027995Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def tensor_to_image(tensor):\n    return tensor.permute(1,2,0).numpy().astype(np.uint8)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T13:27:21.574060Z","iopub.execute_input":"2024-11-02T13:27:21.574490Z","iopub.status.idle":"2024-11-02T13:27:21.580548Z","shell.execute_reply.started":"2024-11-02T13:27:21.574452Z","shell.execute_reply":"2024-11-02T13:27:21.579162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((180,180)),\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T15:10:46.327530Z","iopub.execute_input":"2024-11-02T15:10:46.327969Z","iopub.status.idle":"2024-11-02T15:10:46.333682Z","shell.execute_reply.started":"2024-11-02T15:10:46.327926Z","shell.execute_reply":"2024-11-02T15:10:46.332478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = WheatDataset(images, bboxs, transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T13:27:22.522280Z","iopub.execute_input":"2024-11-02T13:27:22.522688Z","iopub.status.idle":"2024-11-02T13:27:22.528097Z","shell.execute_reply.started":"2024-11-02T13:27:22.522651Z","shell.execute_reply":"2024-11-02T13:27:22.526821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image= dataset[0][0]\n# target = dataset[0][1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T13:27:23.172025Z","iopub.execute_input":"2024-11-02T13:27:23.172455Z","iopub.status.idle":"2024-11-02T13:27:23.193962Z","shell.execute_reply.started":"2024-11-02T13:27:23.172413Z","shell.execute_reply":"2024-11-02T13:27:23.192929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(tensor_to_image(image))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T13:27:26.421877Z","iopub.execute_input":"2024-11-02T13:27:26.422356Z","iopub.status.idle":"2024-11-02T13:27:26.822458Z","shell.execute_reply.started":"2024-11-02T13:27:26.422310Z","shell.execute_reply":"2024-11-02T13:27:26.821149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T13:02:57.119230Z","iopub.execute_input":"2024-11-02T13:02:57.119707Z","iopub.status.idle":"2024-11-02T13:02:57.128345Z","shell.execute_reply.started":"2024-11-02T13:02:57.119663Z","shell.execute_reply":"2024-11-02T13:02:57.127253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_image_bbox(image, bboxes):\n    fig, ax = plt.subplots(1,1)\n\n    \n    ax.imshow(Image.open(image))\n\n    for box in bboxes:\n        x, y, w, h = box[0], box[1], box[2], box[3]\n        rect = patches.Rectangle((x,y),w,h, linewidth=2,edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T14:31:55.105150Z","iopub.execute_input":"2024-11-02T14:31:55.105589Z","iopub.status.idle":"2024-11-02T14:31:55.112232Z","shell.execute_reply.started":"2024-11-02T14:31:55.105545Z","shell.execute_reply":"2024-11-02T14:31:55.111139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplot_image_bbox(train_data[100][0], train_data[100][1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-02T14:59:13.897371Z","iopub.execute_input":"2024-11-02T14:59:13.897758Z","iopub.status.idle":"2024-11-02T14:59:14.360731Z","shell.execute_reply.started":"2024-11-02T14:59:13.897722Z","shell.execute_reply":"2024-11-02T14:59:14.359321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}