|
| 1 | +"""VOC Dataset Classes |
| 2 | +
|
| 3 | +Original author: Francisco Massa |
| 4 | +https://github.com/fmassa/vision/blob/voc_dataset/torchvision/datasets/voc.py |
| 5 | +
|
| 6 | +Updated by: Ellis Brown, Max deGroot |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import os.path |
| 11 | +import sys |
| 12 | +import torch |
| 13 | +import torch.utils.data as data |
| 14 | +import torchvision.transforms as transforms |
| 15 | +from PIL import Image, ImageDraw, ImageFont |
| 16 | +import cv2 |
| 17 | +import numpy as np |
| 18 | +if sys.version_info[0] == 2: |
| 19 | + import xml.etree.cElementTree as ET |
| 20 | +else: |
| 21 | + import xml.etree.ElementTree as ET |
| 22 | + |
| 23 | +VOC_CLASSES = ( # always index 0 |
| 24 | + 'aeroplane', 'bicycle', 'bird', 'boat', |
| 25 | + 'bottle', 'bus', 'car', 'cat', 'chair', |
| 26 | + 'cow', 'diningtable', 'dog', 'horse', |
| 27 | + 'motorbike', 'person', 'pottedplant', |
| 28 | + 'sheep', 'sofa', 'train', 'tvmonitor') |
| 29 | + |
| 30 | +# for making bounding boxes pretty |
| 31 | +COLORS = ((255, 0, 0, 128), (0, 255, 0, 128), (0, 0, 255, 128), |
| 32 | + (0, 255, 255, 128), (255, 0, 255, 128), (255, 255, 0, 128)) |
| 33 | + |
| 34 | + |
| 35 | +class AnnotationTransform(object): |
| 36 | + """Transforms a VOC annotation into a Tensor of bbox coords and label index |
| 37 | + Initilized with a dictionary lookup of classnames to indexes |
| 38 | +
|
| 39 | + Arguments: |
| 40 | + class_to_ind (dict, optional): dictionary lookup of classnames -> indexes |
| 41 | + (default: alphabetic indexing of VOC's 20 classes) |
| 42 | + keep_difficult (bool, optional): keep difficult instances or not |
| 43 | + (default: False) |
| 44 | + height (int): height |
| 45 | + width (int): width |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, class_to_ind=None, keep_difficult=False): |
| 49 | + self.class_to_ind = class_to_ind or dict( |
| 50 | + zip(VOC_CLASSES, range(len(VOC_CLASSES)))) |
| 51 | + self.keep_difficult = keep_difficult |
| 52 | + |
| 53 | + def __call__(self, target, width, height): |
| 54 | + """ |
| 55 | + Arguments: |
| 56 | + target (annotation) : the target annotation to be made usable |
| 57 | + will be an ET.Element |
| 58 | + Returns: |
| 59 | + a list containing lists of bounding boxes [bbox coords, class name] |
| 60 | + """ |
| 61 | + res = [] |
| 62 | + for obj in target.iter('object'): |
| 63 | + difficult = int(obj.find('difficult').text) == 1 |
| 64 | + if not self.keep_difficult and difficult: |
| 65 | + continue |
| 66 | + name = obj.find('name').text.lower().strip() |
| 67 | + bbox = obj.find('bndbox') |
| 68 | + |
| 69 | + pts = ['xmin', 'ymin', 'xmax', 'ymax'] |
| 70 | + bndbox = [] |
| 71 | + for i, pt in enumerate(pts): |
| 72 | + cur_pt = int(bbox.find(pt).text) - 1 |
| 73 | + # scale height or width |
| 74 | + cur_pt = cur_pt / width if i % 2 == 0 else cur_pt / height |
| 75 | + bndbox.append(cur_pt) |
| 76 | + label_idx = self.class_to_ind[name] |
| 77 | + bndbox.append(label_idx) |
| 78 | + res += [bndbox] # [xmin, ymin, xmax, ymax, label_ind] |
| 79 | + # img_id = target.find('filename').text[:-4] |
| 80 | + |
| 81 | + return res # [[xmin, ymin, xmax, ymax, label_ind], ... ] |
| 82 | + |
| 83 | + |
| 84 | +class VOCDetection(data.Dataset): |
| 85 | + """VOC Detection Dataset Object |
| 86 | +
|
| 87 | + input is image, target is annotation |
| 88 | +
|
| 89 | + Arguments: |
| 90 | + root (string): filepath to VOCdevkit folder. |
| 91 | + image_set (string): imageset to use (eg. 'train', 'val', 'test') |
| 92 | + transform (callable, optional): transformation to perform on the |
| 93 | + input image |
| 94 | + target_transform (callable, optional): transformation to perform on the |
| 95 | + target `annotation` |
| 96 | + (eg: take in caption string, return tensor of word indices) |
| 97 | + dataset_name (string, optional): which dataset to load |
| 98 | + (default: 'VOC2007') |
| 99 | + """ |
| 100 | + |
| 101 | + def __init__(self, root, image_sets, transform=None, target_transform=None, |
| 102 | + dataset_name='VOC0712'): |
| 103 | + self.root = root |
| 104 | + self.image_set = image_sets |
| 105 | + self.transform = transform |
| 106 | + self.target_transform = target_transform |
| 107 | + self.name = dataset_name |
| 108 | + self._annopath = os.path.join('%s', 'Annotations', '%s.xml') |
| 109 | + self._imgpath = os.path.join('%s', 'JPEGImages', '%s.jpg') |
| 110 | + self.ids = list() |
| 111 | + for (year, name) in image_sets: |
| 112 | + rootpath = os.path.join(self.root, 'VOC' + year) |
| 113 | + for line in open(os.path.join(rootpath, 'ImageSets', 'Main', name + '.txt')): |
| 114 | + self.ids.append((rootpath, line.strip())) |
| 115 | + |
| 116 | + def __getitem__(self, index): |
| 117 | + im, gt, h, w = self.pull_item(index) |
| 118 | + |
| 119 | + return im, gt |
| 120 | + |
| 121 | + def __len__(self): |
| 122 | + return len(self.ids) |
| 123 | + |
| 124 | + def pull_item(self, index): |
| 125 | + img_id = self.ids[index] |
| 126 | + |
| 127 | + target = ET.parse(self._annopath % img_id).getroot() |
| 128 | + img = cv2.imread(self._imgpath % img_id) |
| 129 | + height, width, channels = img.shape |
| 130 | + |
| 131 | + if self.target_transform is not None: |
| 132 | + target = self.target_transform(target, width, height) |
| 133 | + |
| 134 | + if self.transform is not None: |
| 135 | + target = np.array(target) |
| 136 | + img, boxes, labels = self.transform(img, target[:, :4], target[:, 4]) |
| 137 | + # to rgb |
| 138 | + img = img[:, :, (2, 1, 0)] |
| 139 | + # img = img.transpose(2, 0, 1) |
| 140 | + target = np.hstack((boxes, np.expand_dims(labels, axis=1))) |
| 141 | + return torch.from_numpy(img).permute(2, 0, 1), target, height, width |
| 142 | + # return torch.from_numpy(img), target, height, width |
| 143 | + |
| 144 | + def pull_image(self, index): |
| 145 | + '''Returns the original image object at index in PIL form |
| 146 | +
|
| 147 | + Note: not using self.__getitem__(), as any transformations passed in |
| 148 | + could mess up this functionality. |
| 149 | +
|
| 150 | + Argument: |
| 151 | + index (int): index of img to show |
| 152 | + Return: |
| 153 | + PIL img |
| 154 | + ''' |
| 155 | + img_id = self.ids[index] |
| 156 | + return cv2.imread(self._imgpath % img_id, cv2.IMREAD_COLOR) |
| 157 | + |
| 158 | + def pull_anno(self, index): |
| 159 | + '''Returns the original annotation of image at index |
| 160 | +
|
| 161 | + Note: not using self.__getitem__(), as any transformations passed in |
| 162 | + could mess up this functionality. |
| 163 | +
|
| 164 | + Argument: |
| 165 | + index (int): index of img to get annotation of |
| 166 | + Return: |
| 167 | + list: [img_id, [(label, bbox coords),...]] |
| 168 | + eg: ('001718', [('dog', (96, 13, 438, 332))]) |
| 169 | + ''' |
| 170 | + img_id = self.ids[index] |
| 171 | + anno = ET.parse(self._annopath % img_id).getroot() |
| 172 | + gt = self.target_transform(anno, 1, 1) |
| 173 | + return img_id[1], gt |
| 174 | + |
| 175 | + def pull_tensor(self, index): |
| 176 | + '''Returns the original image at an index in tensor form |
| 177 | +
|
| 178 | + Note: not using self.__getitem__(), as any transformations passed in |
| 179 | + could mess up this functionality. |
| 180 | +
|
| 181 | + Argument: |
| 182 | + index (int): index of img to show |
| 183 | + Return: |
| 184 | + tensorized version of img, squeezed |
| 185 | + ''' |
| 186 | + return torch.Tensor(self.pull_image(index)).unsqueeze_(0) |
| 187 | + |
| 188 | + |
| 189 | +def detection_collate(batch): |
| 190 | + """Custom collate fn for dealing with batches of images that have a different |
| 191 | + number of associated object annotations (bounding boxes). |
| 192 | +
|
| 193 | + Arguments: |
| 194 | + batch: (tuple) A tuple of tensor images and lists of annotations |
| 195 | +
|
| 196 | + Return: |
| 197 | + A tuple containing: |
| 198 | + 1) (tensor) batch of images stacked on their 0 dim |
| 199 | + 2) (list of tensors) annotations for a given image are stacked on 0 dim |
| 200 | + """ |
| 201 | + targets = [] |
| 202 | + imgs = [] |
| 203 | + for sample in batch: |
| 204 | + imgs.append(sample[0]) |
| 205 | + targets.append(torch.FloatTensor(sample[1])) |
| 206 | + return torch.stack(imgs, 0), targets |
0 commit comments