{
  "id": 221619,
  "title": "Need help in creating dataset class for multilabel object detection in a single image",
  "url": "/competitions/vinbigdata-chest-xray-abnormalities-detection/discussion/221619",
  "author_name": "IamSparky",
  "post_date": "2021-02-23T13:31:08.295000",
  "votes": 1,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Can some please help me in modifying this single class object detection dataset class to multiple class object detection dataset in PYTORCH</p>\n<pre><code>import os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\nclass object_detection_dataset_class(Dataset):\n    def __init__(self, csv_file_path):\n        self.train_df = pd.read_csv(csv_file_path)\n        self.image_ids = self.train_df['image_id'].unique()\n\n    def __len__(self):\n        return len(self.image_ids.shape[0])\n\n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        bboxes = self.train_df[self.train_df['image_id'] == image_id]\n\n        image = np.array(Image.open('__image_file_path__'+ self.image_id[index] +'.jpg')) \n        image = Image.fromarray(image).convert('RGB')\n        image /= 255.0\n\n        boxes = bboxes[['xmin', 'ymin', 'xmax', 'ymax']]\n        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])\n\n        boxes = torch.as_tensor(boxes, dtype = torch.float32)\n        area = torch.as_tensor(area, dtype = torch.float32)\n\n        labels = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n        is_crowd = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n\n        target = {}\n        target[\"boxes\"] = boxes\n        target[\"labels\"] = labels\n        target[\"masks\"] = masks\n        target[\"image_id\"] = image_id\n        target[\"area\"] = area\n        target[\"iscrowd\"] = iscrowd\n\n        image = torchvision.transforms.ToTensor()(image)\n        return image, target\n</code></pre>",
  "messages": [
    {
      "id": 1215246,
      "postDate": "2021-02-23T13:31:08.297Z",
      "content": "<p>Can some please help me in modifying this single class object detection dataset class to multiple class object detection dataset in PYTORCH</p>\n<pre><code>import os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\nclass object_detection_dataset_class(Dataset):\n    def __init__(self, csv_file_path):\n        self.train_df = pd.read_csv(csv_file_path)\n        self.image_ids = self.train_df['image_id'].unique()\n\n    def __len__(self):\n        return len(self.image_ids.shape[0])\n\n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        bboxes = self.train_df[self.train_df['image_id'] == image_id]\n\n        image = np.array(Image.open('__image_file_path__'+ self.image_id[index] +'.jpg')) \n        image = Image.fromarray(image).convert('RGB')\n        image /= 255.0\n\n        boxes = bboxes[['xmin', 'ymin', 'xmax', 'ymax']]\n        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])\n\n        boxes = torch.as_tensor(boxes, dtype = torch.float32)\n        area = torch.as_tensor(area, dtype = torch.float32)\n\n        labels = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n        is_crowd = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n\n        target = {}\n        target[\"boxes\"] = boxes\n        target[\"labels\"] = labels\n        target[\"masks\"] = masks\n        target[\"image_id\"] = image_id\n        target[\"area\"] = area\n        target[\"iscrowd\"] = iscrowd\n\n        image = torchvision.transforms.ToTensor()(image)\n        return image, target\n</code></pre>",
      "rawMarkdown": "Can some please help me in modifying this single class object detection dataset class to multiple class object detection dataset in PYTORCH\n\n```\nimport os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\nclass object_detection_dataset_class(Dataset):\n    def __init__(self, csv_file_path):\n        self.train_df = pd.read_csv(csv_file_path)\n        self.image_ids = self.train_df['image_id'].unique()\n        \n    def __len__(self):\n        return len(self.image_ids.shape[0])\n    \n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        bboxes = self.train_df[self.train_df['image_id'] == image_id]\n        \n        image = np.array(Image.open('__image_file_path__'+ self.image_id[index] +'.jpg')) \n        image = Image.fromarray(image).convert('RGB')\n        image /= 255.0\n       \n        boxes = bboxes[['xmin', 'ymin', 'xmax', 'ymax']]\n        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])\n\n        boxes = torch.as_tensor(boxes, dtype = torch.float32)\n        area = torch.as_tensor(area, dtype = torch.float32)\n\n        labels = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n        is_crowd = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n\n        target = {}\n        target[\"boxes\"] = boxes\n        target[\"labels\"] = labels\n        target[\"masks\"] = masks\n        target[\"image_id\"] = image_id\n        target[\"area\"] = area\n        target[\"iscrowd\"] = iscrowd\n\n        image = torchvision.transforms.ToTensor()(image)\n        return image, target\n```",
      "votes": 1
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1215246": "Can some please help me in modifying this single class object detection dataset class to multiple class object detection dataset in PYTORCH\n\n```\nimport os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\nclass object_detection_dataset_class(Dataset):\n    def __init__(self, csv_file_path):\n        self.train_df = pd.read_csv(csv_file_path)\n        self.image_ids = self.train_df['image_id'].unique()\n        \n    def __len__(self):\n        return len(self.image_ids.shape[0])\n    \n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        bboxes = self.train_df[self.train_df['image_id'] == image_id]\n        \n        image = np.array(Image.open('__image_file_path__'+ self.image_id[index] +'.jpg')) \n        image = Image.fromarray(image).convert('RGB')\n        image /= 255.0\n       \n        boxes = bboxes[['xmin', 'ymin', 'xmax', 'ymax']]\n        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])\n\n        boxes = torch.as_tensor(boxes, dtype = torch.float32)\n        area = torch.as_tensor(area, dtype = torch.float32)\n\n        labels = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n        is_crowd = torch.ones((bboxes.shape[0], ), dtype = torch.int64)\n\n        target = {}\n        target[\"boxes\"] = boxes\n        target[\"labels\"] = labels\n        target[\"masks\"] = masks\n        target[\"image_id\"] = image_id\n        target[\"area\"] = area\n        target[\"iscrowd\"] = iscrowd\n\n        image = torchvision.transforms.ToTensor()(image)\n        return image, target\n```"
  }
}