def load_dpm_dets_for_image(cls, image, dataset, suffix='dets_all_may25_DP'): """ Loads multi-class array of detections for an image from .mat format. """ t = time.time() name = os.path.splitext(image.name)[0] # TODO: figure out how to deal with different types of detections dets_dir = '/u/vis/x1/sergeyk/rl_detection/voc-release4/2007/tmp/dets_may25_DP' filename = os.path.join(dets_dir, '%s_dets_all_may25_DP.mat' % name) if not os.path.exists(filename): dets_dir = '/u/vis/x1/sergeyk/rl_detection/voc-release4/2007/tmp/dets_jun1_DP_trainval' filename = os.path.join(dets_dir, '%s_dets_all_jun1_DP_trainval.mat' % name) if not os.path.exists(filename): filename = os.path.join(config.test_support_dir, 'dets/%s_dets_all_may25_DP.mat' % name) if not os.path.exists(filename): print("File does not exist!") return None mat = scipy.io.loadmat(filename) dets = mat['dets_mc'] times = mat['times_mc'] # feat_time = times[0,0] dets_seq = [] cols = [ 'x1', 'y1', 'x2', 'y2', 'dummy', 'dummy', 'dummy', 'dummy', 'score', 'time' ] for cls_ind, cls in enumerate(config.pascal_classes): cls_dets = dets[cls_ind][0] if cls_dets.shape[0] > 0: det_time = times[cls_ind, 1] # all detections get the final time cls_dets = ut.append_index_column(cls_dets, det_time) cls_dets = ut.append_index_column(cls_dets, cls_ind) # subtract 1 pixel and convert from corners! cls_dets[:, :4] -= 1 cls_dets[:, :4] = BoundingBox.convert_arr_from_corners( cls_dets[:, :4]) dets_seq.append(cls_dets) cols = [ 'x', 'y', 'w', 'h', 'dummy', 'dummy', 'dummy', 'dummy', 'score', 'time', 'cls_ind' ] # NMS detections per class individually dets_mc = ut.collect(dets_seq, Detector.nms_detections, {'cols': cols}) dets_mc[:, :4] = BoundingBox.clipboxes_arr( dets_mc[:, :4], (0, 0, image.size[0] - 1, image.size[1] - 1)) time_elapsed = time.time() - t print("On image %s, took %.3f s" % (image.name, time_elapsed)) return dets_mc
def load_csc_dpm_dets_for_image(cls, image, dataset): """ Loads HOS's cascaded dets. """ t = time.time() name = os.path.splitext(image.name)[0] # if uest dataset, use HOS's detections. if not, need to output my own if re.search('test', dataset.name): dirname = config.get_dets_test_wholeset_dir() filename = os.path.join( dirname, '%s_dets_all_test_original_cascade_wholeset.mat' % name) else: dirname = config.get_dets_nov19() filename = os.path.join(dirname, '%s_dets_all_nov19.mat' % name) print filename if not os.path.exists(filename): raise RuntimeError("File %s does not exist!" % filename) return None mat = scipy.io.loadmat(filename) dets = mat['dets_mc'] times = mat['times_mc'] # feat_time = times[0,0] dets_seq = [] cols = [ 'x1', 'y1', 'x2', 'y2', 'dummy', 'dummy', 'dummy', 'dummy', 'dummy', 'dummy', 'score' ] for cls_ind, cls in enumerate(dataset.classes): cls_dets = dets[cls_ind][0] if cls_dets.shape[0] > 0: good_ind = [0, 1, 2, 3, 10] cls_dets = cls_dets[:, good_ind] det_time = times[cls_ind, 1] # all detections get the final time cls_dets = ut.append_index_column(cls_dets, det_time) cls_dets = ut.append_index_column(cls_dets, cls_ind) # convert from corners! cls_dets[:, :4] = BoundingBox.convert_arr_from_corners( cls_dets[:, :4]) cls_dets[:, :4] = BoundingBox.clipboxes_arr( cls_dets[:, :4], (0, 0, image.size[0], image.size[1])) dets_seq.append(cls_dets) cols = ['x', 'y', 'w', 'h', 'score', 'time', 'cls_ind'] dets_mc = ut.collect(dets_seq, Detector.nms_detections, {'cols': cols}) time_elapsed = time.time() - t print("On image %s, took %.3f s" % (image.name, time_elapsed)) return dets_mc
def load_dpm_dets_for_image(cls, image, dataset, suffix='dets_all_may25_DP'): """ Loads multi-class array of detections for an image from .mat format. """ t = time.time() name = os.path.splitext(image.name)[0] # TODO: figure out how to deal with different types of detections dets_dir = '/u/vis/x1/sergeyk/rl_detection/voc-release4/2007/tmp/dets_may25_DP' filename = os.path.join(dets_dir, '%s_dets_all_may25_DP.mat' % name) if not os.path.exists(filename): dets_dir = '/u/vis/x1/sergeyk/rl_detection/voc-release4/2007/tmp/dets_jun1_DP_trainval' filename = os.path.join( dets_dir, '%s_dets_all_jun1_DP_trainval.mat' % name) if not os.path.exists(filename): filename = os.path.join( config.test_support_dir, 'dets/%s_dets_all_may25_DP.mat' % name) if not os.path.exists(filename): print("File does not exist!") return None mat = scipy.io.loadmat(filename) dets = mat['dets_mc'] times = mat['times_mc'] # feat_time = times[0,0] dets_seq = [] cols = ['x1', 'y1', 'x2', 'y2', 'dummy', 'dummy', 'dummy', 'dummy', 'score', 'time'] for cls_ind, cls in enumerate(config.pascal_classes): cls_dets = dets[cls_ind][0] if cls_dets.shape[0] > 0: det_time = times[cls_ind, 1] # all detections get the final time cls_dets = ut.append_index_column(cls_dets, det_time) cls_dets = ut.append_index_column(cls_dets, cls_ind) # subtract 1 pixel and convert from corners! cls_dets[:, :4] -= 1 cls_dets[:, : 4] = BoundingBox.convert_arr_from_corners(cls_dets[:, :4]) dets_seq.append(cls_dets) cols = ['x', 'y', 'w', 'h', 'dummy', 'dummy', 'dummy', 'dummy', 'score', 'time', 'cls_ind'] # NMS detections per class individually dets_mc = ut.collect(dets_seq, Detector.nms_detections, {'cols': cols}) dets_mc[:, :4] = BoundingBox.clipboxes_arr( dets_mc[:, :4], (0, 0, image.size[0] - 1, image.size[1] - 1)) time_elapsed = time.time() - t print("On image %s, took %.3f s" % (image.name, time_elapsed)) return dets_mc
def get_windows(clas, image, cls=None, window_params=None, with_time=False): """ Return all windows that can be generated with window_params. If with_time=True, return tuple of (windows, time_elapsed). """ assert cls or window_params if not window_params: window_params = self.get_default_window_params(cls) t = time.time() stride = window_params.stride min_width = window_params.min_width actual_xs = [] actual_ys = [] actual_ws = [] actual_hs = [] num_windows = 0 # we want to be able to capture objects that extend past the image # we always iterate over locations in native space, and convert to # actual image space when we record the window w_pad = int(1.0 * min_width / 2) x_min = -w_pad for scale in window_params.scales: x_max = int(image.width * scale) - w_pad if w_pad > 0: x_max += stride actual_w = int(min_width / scale) + 1 for ratio in window_params.aspect_ratios: h_pad = int(1.0 * min_width * ratio / 2) y_min = -h_pad y_max = int(image.height * scale) - h_pad if h_pad > 0: y_max += stride actual_h = int(min_width / scale * ratio) + 1 for y in range(y_min, y_max, stride): for x in range(x_min, x_max, stride): actual_ws.append(actual_w) actual_hs.append(actual_h) actual_xs.append(int(x / scale)) actual_ys.append(int(y / scale)) windows = np.array([actual_xs, actual_ys, actual_ws, actual_hs]).T windows = BoundingBox.clipboxes_arr(windows, (0, 0, image.width, image.height)) if with_time: time_elapsed = time.time() - t return (windows, time_elapsed) else: return windows
def load_csc_dpm_dets_for_image(cls, image, dataset): """ Loads HOS's cascaded dets. """ t = time.time() name = os.path.splitext(image.name)[0] # if uest dataset, use HOS's detections. if not, need to output my own if re.search('test', dataset.name): dirname = config.get_dets_test_wholeset_dir() filename = os.path.join( dirname, '%s_dets_all_test_original_cascade_wholeset.mat' % name) else: dirname = config.get_dets_nov19() filename = os.path.join(dirname, '%s_dets_all_nov19.mat' % name) print filename if not os.path.exists(filename): raise RuntimeError("File %s does not exist!" % filename) return None mat = scipy.io.loadmat(filename) dets = mat['dets_mc'] times = mat['times_mc'] # feat_time = times[0,0] dets_seq = [] cols = ['x1', 'y1', 'x2', 'y2', 'dummy', 'dummy', 'dummy', 'dummy', 'dummy', 'dummy', 'score'] for cls_ind, cls in enumerate(dataset.classes): cls_dets = dets[cls_ind][0] if cls_dets.shape[0] > 0: good_ind = [0, 1, 2, 3, 10] cls_dets = cls_dets[:, good_ind] det_time = times[cls_ind, 1] # all detections get the final time cls_dets = ut.append_index_column(cls_dets, det_time) cls_dets = ut.append_index_column(cls_dets, cls_ind) # convert from corners! cls_dets[:, : 4] = BoundingBox.convert_arr_from_corners(cls_dets[:, :4]) cls_dets[:, :4] = BoundingBox.clipboxes_arr( cls_dets[:, :4], (0, 0, image.size[0], image.size[1])) dets_seq.append(cls_dets) cols = ['x', 'y', 'w', 'h', 'score', 'time', 'cls_ind'] dets_mc = ut.collect(dets_seq, Detector.nms_detections, {'cols': cols}) time_elapsed = time.time() - t print("On image %s, took %.3f s" % (image.name, time_elapsed)) return dets_mc
def get_windows_new(self, image, cls, metaparams=None, with_time=False, at_most=200000, force=False): """ Generate windows by using ground truth window stats and metaparams. metaparams must contain keys 'samples_per_500px', 'num_scales', 'num_ratios', 'mode' metaparams['mode'] can be 'linear' or 'importance' and refers to the method of sampling intervals per window parameter. If with_time=True, return tuple of (windows, time_elapsed). """ if not metaparams: metaparams = { "samples_per_500px": 83, "num_scales": 12, "num_ratios": 6, "mode": "importance", "priority": 0, } t = time.time() x_samples = int(image.width / 500.0 * metaparams["samples_per_500px"]) y_samples = int(image.height / 500.0 * metaparams["samples_per_500px"]) # check for cached windows and return if found dirname = config.get_sliding_windows_cached_dir(self.train_name) filename = "%s_%d_%d_%s_%s_%d_%d_%d.npy" % ( cls, metaparams["samples_per_500px"], metaparams["num_scales"], metaparams["num_ratios"], metaparams["mode"], metaparams["priority"], x_samples, y_samples, ) filename = os.path.join(dirname, filename) if os.path.exists(filename) and not force: windows = np.load(filename) else: # fine, we'll figure out the windows again # load the kde for x_scaled,y_scaled,scale,log_ratio stats = self.get_stats() kde = stats["%s_kde" % cls] x_frac = kde.dataset[0, :] y_frac = kde.dataset[1, :] scale = kde.dataset[2, :] log_ratio = kde.dataset[3, :] # given the metaparameters, sample points to generate the complete list of # parameter combinations if metaparams["mode"] == "linear": x_points = np.linspace(x_frac.min(), x_frac.max(), x_samples) y_points = np.linspace(y_frac.min(), y_frac.max(), y_samples) scale_points = np.linspace(scale.min(), scale.max(), metaparams["num_scales"]) ratio_points = np.linspace(log_ratio.min(), log_ratio.max(), metaparams["num_ratios"]) elif metaparams["mode"] == "importance": x_points = ut.importance_sample(x_frac, x_samples, stats["%s_%s_kde" % (cls, "x_frac")]) y_points = ut.importance_sample(y_frac, y_samples, stats["%s_%s_kde" % (cls, "y_frac")]) scale_points = ut.importance_sample( scale, metaparams["num_scales"], stats["%s_%s_kde" % (cls, "scale")] ) ratio_points = ut.importance_sample( log_ratio, metaparams["num_ratios"], stats["%s_%s_kde" % (cls, "log_ratio")] ) else: raise RuntimeError("Invalid mode") combinations = [x for x in itertools.product(x_points, y_points, scale_points, ratio_points)] combinations = np.array(combinations).T # only take the top-scoring detections if metaparams["priority"]: t22 = time.time() scores = kde(combinations) # (so slow!) print("kde took %.3f s" % (time.time() - t22)) sorted_inds = np.argsort(-scores) max_num = min(at_most, sorted_inds.size) combinations = combinations[:, sorted_inds[:max_num]] # convert to x,y,scale,ratio,w,h scale = combinations[2, :] # x = x_frac*img_width x = combinations[0, :] * img_width # ratio = exp(log_ratio) ratio = np.exp(combinations[3, :]) # y = y_frac*img_height y = combinations[1, :] * img_height # w = scale*min_width w = scale * SlidingWindows.MIN_WIDTH # h = w*ratio h = w * ratio combinations[0, :] = x combinations[1, :] = y combinations[2, :] = w combinations[3, :] = h windows = combinations.T windows = BoundingBox.clipboxes_arr(windows, (0, 0, img_width, img_height)) np.save(filename, windows) # does not take more than 0.5 sec even for 10**6 windows time_elapsed = time.time() - t print("get_windows_new() got %d windows in %.3fs" % (windows.shape[0], time_elapsed)) if with_time: return (windows, time_elapsed) else: return windows