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_pos_windows(self, cls=None, window_params=None, min_overlap=0.7): """ Return array of all ground truth windows for the class, plus windows that can be generated with window_params that overlap with it by more than min_overlap. * If cls not given, return positive windows for all classes. * If window_params not given, use default for the class. * Adjust min_overlap to fetch fewer windows. """ sw = SlidingWindows(self, self) if not window_params: window_params = sw.get_default_window_params(cls) overlapping_windows = [] image_inds = self.get_pos_samples_for_class(cls) times = [] window_nums = [] for i in image_inds: image = self.images[i] gts = image.get_ground_truth(cls) if gts.arr.shape[0] > 0: overlap_wins = gts.arr[:, :4] overlap_wins = np.hstack( (overlap_wins, np.tile(i, (overlap_wins.shape[0], 1)))) overlapping_windows.append(overlap_wins.astype(int)) windows, time_elapsed = image.get_windows( window_params, with_time=True) window_nums.append(windows.shape[0]) times.append(time_elapsed) for gt in gts.arr: overlaps = BoundingBox.get_overlap(windows[:, :4], gt[:4]) overlap_wins = windows[overlaps >= min_overlap, :] overlap_wins = np.hstack(( overlap_wins, np.tile(i, (overlap_wins.shape[0], 1)))) overlapping_windows.append(overlap_wins.astype(int)) windows = windows[overlaps < min_overlap, :] overlapping_windows = np.concatenate(overlapping_windows, 0) print("Windows generated per image: %d +/- %.3f, in %.3f +/- %.3f sec" % ( np.mean(window_nums), np.std(window_nums), np.mean(times), np.std(times))) return overlapping_windows
def get_windows(self, window_params, with_time=False): "Return all windows that can be generated with given params." return SlidingWindows.get_windows(self, None, window_params, with_time)