{
  "id": 405183,
  "title": "How to handle different number of bounding boxes per image?",
  "url": "/competitions/benetech-making-graphs-accessible/discussion/405183",
  "author_name": "Harry4463",
  "post_date": "2023-04-26T12:44:34.381000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Currently making this dataset</p>\n<pre><code>class CustomDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transform=None):\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df[\"filename\"].unique())\n\n    def __getitem__(self, index):\n        image_id = self.df[\"filename\"].unique()[index]\n        image_path = os.path.join(self.image_dir, image_id)\n        image = Image.open(image_path).convert(\"RGB\")\n\n        #get the scale factor\n        scale_height = config.HEIGHT / image.size[1]\n        scale_width = config.WIDTH / image.size[0]\n\n\n        # filter the dataframe to only include rows with the current image_id\n        image_df = self.df[self.df[\"filename\"] == image_id]\n\n        boxes = []\n        labels = []\n\n        for i in range(len(image_df)):\n            # get the coordinates and label for the current box\n            x1 = image_df.iloc[i][\"x1\"] * scale_width\n            y1 = image_df.iloc[i][\"y1\"] * scale_height\n            x2 = image_df.iloc[i][\"x2\"] * scale_width\n            y2 = image_df.iloc[i][\"y2\"] * scale_height\n            x3 = image_df.iloc[i][\"x3\"] * scale_width\n            y3 = image_df.iloc[i][\"y3\"] * scale_height\n            x4 = image_df.iloc[i][\"x4\"] * scale_width\n            y4 = image_df.iloc[i][\"y4\"] * scale_height\n            label = image_df.iloc[i][\"label\"]\n\n            # add the box coordinates and label to the lists\n            boxes.append([x1, y1, x2, y2, x3, y3, x4, y4])\n            labels.append(label)\n\n        num_boxes = len(boxes)\n\n        # convert the boxes and labels to tensors\n        boxes = torch.tensor(boxes, dtype=torch.float32)\n        labels = torch.tensor(labels, dtype=torch.int64)\n\n        target = {}\n        target[\"image_id\"] = torch.tensor([index]).to(device)\n        target[\"boxes\"] = boxes.to(device)\n        target[\"labels\"] = labels.to(device)\n        target[\"num_boxes\"] = torch.tensor([num_boxes]).to(device)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, target\n</code></pre>\n<p>Any suggestions on how to handle this. I know there is such thing as padding etc.. </p>",
  "messages": [
    {
      "id": 2235947,
      "postDate": "2023-04-26T12:44:34.380Z",
      "content": "<p>Currently making this dataset</p>\n<pre><code>class CustomDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transform=None):\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df[\"filename\"].unique())\n\n    def __getitem__(self, index):\n        image_id = self.df[\"filename\"].unique()[index]\n        image_path = os.path.join(self.image_dir, image_id)\n        image = Image.open(image_path).convert(\"RGB\")\n\n        #get the scale factor\n        scale_height = config.HEIGHT / image.size[1]\n        scale_width = config.WIDTH / image.size[0]\n\n\n        # filter the dataframe to only include rows with the current image_id\n        image_df = self.df[self.df[\"filename\"] == image_id]\n\n        boxes = []\n        labels = []\n\n        for i in range(len(image_df)):\n            # get the coordinates and label for the current box\n            x1 = image_df.iloc[i][\"x1\"] * scale_width\n            y1 = image_df.iloc[i][\"y1\"] * scale_height\n            x2 = image_df.iloc[i][\"x2\"] * scale_width\n            y2 = image_df.iloc[i][\"y2\"] * scale_height\n            x3 = image_df.iloc[i][\"x3\"] * scale_width\n            y3 = image_df.iloc[i][\"y3\"] * scale_height\n            x4 = image_df.iloc[i][\"x4\"] * scale_width\n            y4 = image_df.iloc[i][\"y4\"] * scale_height\n            label = image_df.iloc[i][\"label\"]\n\n            # add the box coordinates and label to the lists\n            boxes.append([x1, y1, x2, y2, x3, y3, x4, y4])\n            labels.append(label)\n\n        num_boxes = len(boxes)\n\n        # convert the boxes and labels to tensors\n        boxes = torch.tensor(boxes, dtype=torch.float32)\n        labels = torch.tensor(labels, dtype=torch.int64)\n\n        target = {}\n        target[\"image_id\"] = torch.tensor([index]).to(device)\n        target[\"boxes\"] = boxes.to(device)\n        target[\"labels\"] = labels.to(device)\n        target[\"num_boxes\"] = torch.tensor([num_boxes]).to(device)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, target\n</code></pre>\n<p>Any suggestions on how to handle this. I know there is such thing as padding etc.. </p>",
      "rawMarkdown": "Currently making this dataset\n```\nclass CustomDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transform=None):\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df[\"filename\"].unique())\n\n    def __getitem__(self, index):\n        image_id = self.df[\"filename\"].unique()[index]\n        image_path = os.path.join(self.image_dir, image_id)\n        image = Image.open(image_path).convert(\"RGB\")\n\n        #get the scale factor\n        scale_height = config.HEIGHT / image.size[1]\n        scale_width = config.WIDTH / image.size[0]\n\n\n        # filter the dataframe to only include rows with the current image_id\n        image_df = self.df[self.df[\"filename\"] == image_id]\n\n        boxes = []\n        labels = []\n\n        for i in range(len(image_df)):\n            # get the coordinates and label for the current box\n            x1 = image_df.iloc[i][\"x1\"] * scale_width\n            y1 = image_df.iloc[i][\"y1\"] * scale_height\n            x2 = image_df.iloc[i][\"x2\"] * scale_width\n            y2 = image_df.iloc[i][\"y2\"] * scale_height\n            x3 = image_df.iloc[i][\"x3\"] * scale_width\n            y3 = image_df.iloc[i][\"y3\"] * scale_height\n            x4 = image_df.iloc[i][\"x4\"] * scale_width\n            y4 = image_df.iloc[i][\"y4\"] * scale_height\n            label = image_df.iloc[i][\"label\"]\n\n            # add the box coordinates and label to the lists\n            boxes.append([x1, y1, x2, y2, x3, y3, x4, y4])\n            labels.append(label)\n\n        num_boxes = len(boxes)\n\n        # convert the boxes and labels to tensors\n        boxes = torch.tensor(boxes, dtype=torch.float32)\n        labels = torch.tensor(labels, dtype=torch.int64)\n\n        target = {}\n        target[\"image_id\"] = torch.tensor([index]).to(device)\n        target[\"boxes\"] = boxes.to(device)\n        target[\"labels\"] = labels.to(device)\n        target[\"num_boxes\"] = torch.tensor([num_boxes]).to(device)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, target\n```\n\nAny suggestions on how to handle this. I know there is such thing as padding etc.. "
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2235947": "Currently making this dataset\n```\nclass CustomDataset(Dataset):\n    def __init__(self, dataframe, image_dir, transform=None):\n        self.df = dataframe\n        self.image_dir = image_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df[\"filename\"].unique())\n\n    def __getitem__(self, index):\n        image_id = self.df[\"filename\"].unique()[index]\n        image_path = os.path.join(self.image_dir, image_id)\n        image = Image.open(image_path).convert(\"RGB\")\n\n        #get the scale factor\n        scale_height = config.HEIGHT / image.size[1]\n        scale_width = config.WIDTH / image.size[0]\n\n\n        # filter the dataframe to only include rows with the current image_id\n        image_df = self.df[self.df[\"filename\"] == image_id]\n\n        boxes = []\n        labels = []\n\n        for i in range(len(image_df)):\n            # get the coordinates and label for the current box\n            x1 = image_df.iloc[i][\"x1\"] * scale_width\n            y1 = image_df.iloc[i][\"y1\"] * scale_height\n            x2 = image_df.iloc[i][\"x2\"] * scale_width\n            y2 = image_df.iloc[i][\"y2\"] * scale_height\n            x3 = image_df.iloc[i][\"x3\"] * scale_width\n            y3 = image_df.iloc[i][\"y3\"] * scale_height\n            x4 = image_df.iloc[i][\"x4\"] * scale_width\n            y4 = image_df.iloc[i][\"y4\"] * scale_height\n            label = image_df.iloc[i][\"label\"]\n\n            # add the box coordinates and label to the lists\n            boxes.append([x1, y1, x2, y2, x3, y3, x4, y4])\n            labels.append(label)\n\n        num_boxes = len(boxes)\n\n        # convert the boxes and labels to tensors\n        boxes = torch.tensor(boxes, dtype=torch.float32)\n        labels = torch.tensor(labels, dtype=torch.int64)\n\n        target = {}\n        target[\"image_id\"] = torch.tensor([index]).to(device)\n        target[\"boxes\"] = boxes.to(device)\n        target[\"labels\"] = labels.to(device)\n        target[\"num_boxes\"] = torch.tensor([num_boxes]).to(device)\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, target\n```\n\nAny suggestions on how to handle this. I know there is such thing as padding etc.. "
  }
}