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, :]
def get_random_windows(self, window_params, num_windows): "Return at most num_windows random windows generated according to params." windows = self.get_windows(window_params) return windows[skutil.random_subset_up_to_N(windows.shape[0], num_windows), :]