demo-ml-pennfudanped
/
coco_for_mask_r_cnn_dataset.py
82 строки · 3.0 Кб
1import torch2from PIL import Image3import numpy as np4from pycocotools.coco import COCO5
6
7class CocoForMaskRCNNDataset(torch.utils.data.Dataset):8
9def __init__(self, images_root, annotations, transforms=None, entry_type=[]):10self.imDir = images_root11self.annFile = annotations12self.entry_type = entry_type13
14self.transforms = transforms15
16self.coco=COCO(self.annFile)17self.catIds = self.coco.getCatIds(catNms=self.entry_type)18self.imgIds = self.coco.getImgIds(catIds=self.catIds)19
20def __getitem__(self, idx):21img = self.coco.loadImgs( self.imgIds[idx] )[0]22im_path = f'{self.imDir}/{img["file_name"]}'23image = Image.open(im_path).convert("RGB")24
25annIds = self.coco.getAnnIds( imgIds=img['id'], catIds=self.catIds, iscrowd=None )26anns = self.coco.loadAnns( annIds )27# anns[0]['segmentation']28# anns[0]['bbox']29# id, category_id, image_id, iscrowd, area30
31masks = []32for i in range(len(anns)):33masks.append(self.coco.annToMask(anns[i]))34
35torch_masks = torch.as_tensor(np.array(masks, dtype=np.uint8), dtype=torch.uint8)36
37num_objs = len(anns)38labels = torch.ones((num_objs,), dtype=torch.int64)39boxes = []40area = []41for ann in anns:42# COCO: "bbox" : [x,y,width,height]43boxes.append([44ann['bbox'][0],45ann['bbox'][1],46ann['bbox'][0] + ann['bbox'][2],47ann['bbox'][1] + ann['bbox'][3],48])49area.append(ann['area'])50image_id = torch.tensor([ self.imgIds[idx] ])51iscrowd = torch.zeros((num_objs,), dtype=torch.int64)52
53target = {}54
55# boxes (FloatTensor[N, 4]): the coordinates of the N bounding boxes in [x0, y0, x1, y1] format, ranging from 0 to W and 0 to H56target["boxes"] = torch.as_tensor(boxes, dtype=torch.float32)57
58# labels (Int64Tensor[N]): the label for each bounding box. 0 represents always the background class.59target["labels"] = labels60
61# (optionally) masks (UInt8Tensor[N, H, W]): The segmentation masks for each one of the objects62target["masks"] = torch_masks63
64# image_id (Int64Tensor[1]): an image identifier. It should be unique between all the images in the dataset, and is used during evaluation65target["image_id"] = image_id66
67# area (Tensor[N]): The area of the bounding box. This is used during evaluation with the COCO metric, to separate the metric scores between small, medium and large boxes.68target["area"] = torch.as_tensor(boxes, dtype=torch.float32)69
70# iscrowd (UInt8Tensor[N]): instances with iscrowd=True will be ignored during evaluation.71target["iscrowd"] = iscrowd72
73if self.transforms is not None:74image, target = self.transforms(image, target)75
76return image, target77
78def get_categories(self):79return self.coco.cats80
81def __len__(self):82return len(self.imgIds)83