def make_fine(idrive, filename=None): if filename is None: filename = "drive%02d_fine.npy" % idrive video = load_stereo_video(idrive) # video = video[:3] n_disp = 128 fine_params = { 'data_weight': 0.01289, 'disc_max': 411.9, 'data_max': 2382540., 'ksize': 15, 'iters': 10 } # 2.023 # disps = [] disps = np.zeros((len(video), ) + video[0][0].shape, dtype='uint8') for i, frame in enumerate(video): tic('BP frame %d' % i) disp = fine_bp(frame, values=n_disp, **fine_params) toc() # print disp.dtype # disps.append(disp) disps[i] = disp # disps = np.array(disps) np.save(filename, disps)
def estimate_coarse_error(down_factor, iters, video, n_disp, drive, n_frames=20, **params): # TODO: use fine BP as ground truth? coarse_error = [] for i in range(n_frames): print(i) frame = video[i] tic('coarse') coarse_disp = coarse_bp(frame, down_factor=down_factor, iters=iters, **params) toc() xyd = load_disparity_points(drive, i) coarse_error.append(error_on_points(xyd, coarse_disp, n_disp, kind='close')) return np.mean(coarse_error)
def test_tic_toc(): rng = np.random.RandomState(0) n = int(1e6) tic() rng.normal(size=n) t0 = toc() tic() rng.normal(size=5 * n) t1 = toc() assert 0.001 <= t0 <= 10 assert 3 * t0 <= t1 <= 30
def test_shift_images(): # n = 5 # images = np.zeros((n, 9)) # shape = (3, 3) # images[np.arange(n), 4] = 1 # images2 = shift_images(images, shape) # for image in images2: # print image.reshape(shape) # --- timing test from hunse_tools.timing import tic, toc [train_images, _], _, _ = mnist() tic() images2 = shift_images(train_images, (28, 28)) toc()
a = nengo.Ensemble(n_hid, n_vis, eval_points=images, encoders=encoders, intercepts=[-0.5] * n_hid, max_rates=[200] * n_hid) o = nengo.Node(size_in=n_vis) op = nengo.Probe(o, synapse=0.03) ca = nengo.Connection(u, a) co = nengo.Connection(a, o) from hunse_tools.timing import tic, toc tic() sim = nengo.Simulator(model) toc() tic() sim.run(1.0) toc() x = sim.data[up].reshape(-1, 28, 28) y = sim.data[op].reshape(-1, 28, 28) plt.figure(1) plt.clf() plt.subplot(121) plt.imshow(x.mean(0), cmap='gray') plt.subplot(122) plt.imshow(y.mean(0), cmap='gray')
frame = load_stereo_frame(idrive, iframe) points = load_disparity_points(idrive, iframe) full_values = 128 frame_down_factor = 1 values = full_values / 2**frame_down_factor iters = 3 params = { 'data_weight': 0.16145115747533928, 'disc_max': 294.1504935618425, 'data_max': 32.024780646200725, 'ksize': 3} frame = (downsample(frame[0], frame_down_factor), downsample(frame[1], frame_down_factor)) tic() coarse_disp = coarse_bp(frame, values=values, down_factor=1, iters=iters, **params) coarse_disp *= 2**frame_down_factor toc() tic() fine_disp = coarse_bp(frame, values=values, down_factor=0, iters=iters, **params) fine_disp *= 2**frame_down_factor toc() # fovea_corners = (60, 360) # fovea_shapes = (80, 80) fovea_corners = [(60, 160), (60, 360)] fovea_shapes = [(80, 80), (80, 80)] fovea_corners = np.array(fovea_corners, ndmin=2)
coarse_shape = low0.shape fine_shape = np.asarray(high0.shape) filt = BryanFilter(coarse_shape, fine_shape, fovea_shape) # filt.fovea_margin = fovea_margin # filt_nofovea = BryanFilter(coarse_shape, fovea_shape) fig = plt.figure(1) fig.clf() ax_disp = plt.gca() plot_disp = ax_disp.imshow(high1, vmin=0, vmax=n_disp) for iframe, [frame, pos] in enumerate(zip(video, positions)): tic('Coarse BP') coarse_disp = coarse_bp(frame, down_factor=coarse_down_factor, iters=coarse_iters, **coarse_params) toc() ratio = np.round(float(frame.shape[1]) / float(coarse_disp.shape[0])) ij0 = np.round(np.asarray(fovea_ij) / ratio) coarse_shape = np.round(np.asarray(fovea_shape) / ratio) ij1 = ij0 + coarse_shape coarse_subwindow = coarse_disp[ij0[0]:ij1[0], ij0[1]:ij1[1]] # TODO: scaling only the edges may be faster seed = cv2.resize(coarse_subwindow, fovea_shape[::-1]) band_size = 10 seed[band_size:-band_size, band_size:-band_size] = 0 tic('Fine BP')
# plot_disp2 = ax_disp2.imshow(high1, vmin=0, vmax=n_disp) # plot_truth = ax_truth.imshow(low0, vmin=0, vmax=n_disp) # ax_cost.imshow(filt.cost) # for tight layout # # ax_frame.set_title('Left camera frame (red: current fovea, green: next fovea)') # ax_coarse.set_title('Coarse') # ax_fovea.set_title('Fovea') # ax_disp.set_title('Estimated disparity (fovea)') # ax_disp2.set_title('Estimated disparity (no fovea)') # ax_cost.set_title('Estimated cost') # ax_truth.set_title('Truth') # fig.tight_layout() for iframe, [frame, pos] in enumerate(zip(video, positions)): tic('Coarse BP') coarse_disp = coarse_bp(frame, **coarse_params) toc() ratio = np.round(float(frame.shape[1]) / float(coarse_disp.shape[0])) ij0 = np.round(np.asarray(fovea_ij) / ratio) coarse_shape = np.round(np.asarray(fovea_shape) / ratio) ij1 = ij0 + coarse_shape coarse_subwindow = coarse_disp[ij0[0]:ij1[0], ij0[1]:ij1[1]] # TODO: scaling only the edges may be faster seed = cv2.resize(coarse_subwindow, fovea_shape[::-1]) band_size = 20 seed[band_size:-band_size, band_size:-band_size] = 0 tic('Fine BP')