示例#1
0
    def pull_item(self, index):
        """
        Returns image at index in torch tensor form (RGB),
        corresponding normalized annotation in 2d array [[xmin, ymin, xmax, ymax, label_ind],
                                                          ... ],
        height and width of image
        """
        img_path = list(self.annotation.keys())[index]
        img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        assert img is not None
        height, width, _ = img.shape

        boxes = np.asarray(
            [anno['bbox'] for anno in self.annotation[img_path]])
        labels = np.asarray(
            [anno['label_idx'] for anno in self.annotation[img_path]])

        if self.scale_bboxes:
            boxes /= np.array([width, height, width, height])

        if not boxes.size:
            logger.error("error: no annotation on image")
            sys.exit(-1)

        if self.target_transform is not None:
            annotation = self.target_transform(self.annotation, width, height)

        if self.transform is not None:
            img, boxes, labels = self.transform(img, boxes, labels)
            if self.rgb:
                img = img[:, :, (2, 1, 0)]
            annotation = np.hstack((boxes, np.expand_dims(labels, axis=1)))
        return torch.from_numpy(img).permute(2, 0,
                                             1), annotation, height, width
示例#2
0
 def load_weights(self, base_file):
     _, ext = os.path.splitext(base_file)
     if ext == '.pkl' or '.pth':
         logger.debug('Loading weights into state dict...')
         self.load_state_dict(torch.load(base_file,
                                         map_location=lambda storage, loc: storage))
         logger.debug('Finished!')
     else:
         logger.error('Sorry only .pth and .pkl files supported.')
示例#3
0
 def load_weights(self, base_file):
     _, ext = os.path.splitext(base_file)
     if ext == '.pkl' or '.pth':
         logger.debug('Loading weights into state dict...')
         #
         # ** WARNING: torch.load functionality uses Python's pickling facilities that
         # may be used to perform arbitrary code execution during unpickling. Only load the data you
         # trust.
         #
         self.load_state_dict(
             torch.load(base_file,
                        map_location=lambda storage, loc: storage))
         logger.debug('Finished!')
     else:
         logger.error('Sorry only .pth and .pkl files supported.')