Exemplo n.º 1
0
    def get_neg_windows(self, num, cls=None, window_params=None, max_overlap=0,
                        max_num_images=250):
        """
        Return array of num windows that can be generated with window_params
        that do not overlap with ground truth by more than max_overlap.
        * If cls is not given, returns ground truth for all classes.
        * If max_num_images is given, samples from at most that many images.
        """
        sw = SlidingWindows(self, self)
        if not window_params:
            window_params = sw.get_default_window_params(cls)
        all_windows = []
        image_inds = self.get_pos_samples_for_class(cls)

        max_num = len(image_inds)
        inds = image_inds
        if max_num_images:
            inds = skutil.random_subset(image_inds, max_num_images)
        num_per_image = round(1. * num / max_num)
        for ind in inds:
            image = self.images[ind]
            windows = image.get_windows(window_params)
            gts = image.get_ground_truth(cls)
            for gt in gts.arr:
                overlaps = BoundingBox.get_overlap(windows[:, :4], gt[:4])
                windows = windows[overlaps <= max_overlap, :]
            if windows.shape[0] == 0:
                continue
            ind_to_take = skutil.random_subset_up_to_N(
                windows.shape[0], num_per_image)
            all_windows.append(np.hstack(
                (windows[ind_to_take, :], np.tile(ind, (ind_to_take.shape[0], 1)))))
        all_windows = np.concatenate(all_windows, 0)
        return all_windows[:num, :]
Exemplo n.º 2
0
 def get_neg_samples_for_class(self, cls, number=None, with_diff=False, with_trun=True):
     """
     Return array of indices of self.images that contain no objects of this class.
     """
     if number == 0:
         return np.array([])
     pos_indices = self.get_pos_samples_for_class(cls, with_diff, with_trun)
     neg_indices = np.setdiff1d(np.arange(len(self.images)), pos_indices, assume_unique=True)
     return sorted(skutil.random_subset(neg_indices, number))