def run(which, expect): v = False cur_dir = os.getcwd() dest = os.path.join(cur_dir, f"out/regression-output/{which}") cwd = dtu.get_output_dir_for_test() if not os.path.exists(cwd): dtu.mkdirs_thread_safe(cwd) try: cmd = [ "rosrun", "easy_regression", "run", "--expect", expect, "--test", which, "-o", dest, "-c", "rmake", ] dtu.system_cmd_result(cwd, cmd, display_stdout=v, display_stderr=v, raise_on_error=True) finally: if False: shutil.rmtree(cwd)
def single_image1(): p = dtu.require_resource("frame0002.jpg") image_cv = dtu.image_cv_from_jpg_fn(p) line_detector_name = "baseline" image_prep_name = "baseline" lane_filter_name = "baseline" anti_instagram_name = "baseline" robot_name = dtu.DuckietownConstants.ROBOT_NAME_FOR_TESTS rcg = get_robot_camera_geometry(robot_name) res, _stats = run_pipeline( image_cv, gpg=rcg.gpg, rectifier=rcg.rectifier, line_detector_name=line_detector_name, image_prep_name=image_prep_name, lane_filter_name=lane_filter_name, anti_instagram_name=anti_instagram_name, all_details=False, ground_truth=None, ) outd = dtu.get_output_dir_for_test() dtu.write_jpgs_to_dir(res, outd)
def single_image_histograms(): p = dtu.require_resource("frame0002.jpg") image_cv = dtu.image_cv_from_jpg_fn(p) res = go(image_cv) outd = dtu.get_output_dir_for_test() dtu.write_bgr_images_as_jpgs(res, outd)
def compare_faster2(): variables = {} variables["alpha"] = dict(min=-180, max=180, description="angle", resolution=5, units="deg", units_display="deg") variables["r"] = dict(min=3, max=5, description="distance", resolution=0.2, units="m", units_display="cm") # this will fail if precision is float32 gh = GridHelper(variables, precision="float64") val_fast = gh.create_new() val_fast.fill(0) val_slow = gh.create_new() val_slow.fill(0) od = dtu.get_output_dir_for_test() F = 1 alpha0 = 177 # r0 = 4 r0 = 4.1 w0 = 1 value = dict(alpha=alpha0, r=r0) gh.add_vote(val_slow, value, w0, F) values = np.zeros((2, 1)) values[0, 0] = alpha0 values[1, 0] = r0 weights = np.zeros(1) weights[0] = w0 gh.add_vote_faster(val_fast, values, weights, F) print(f"sum slow: {np.sum(val_slow)}") print(f"sum fast: {np.sum(val_fast)}") d = grid_helper_plot(gh, val_slow) fn = os.path.join(od, "compare_faster_slow.jpg") dtu.write_data_to_file(d.get_png(), fn) d = grid_helper_plot(gh, val_fast) fn = os.path.join(od, "compare_faster_fast.jpg") dtu.write_data_to_file(d.get_png(), fn) f = lambda x: f" {x:5f}" if x else " " print((dtu.indent(array_as_string(val_fast, f), "val_fast "))) print((dtu.indent(array_as_string(val_slow, f), "val_slow "))) assert_almost_equal(val_fast, val_slow)
def run_one(cmd): v = False cwd = dtu.get_output_dir_for_test() if not os.path.exists(cwd): dtu.mkdirs_thread_safe(cwd) dtu.write_str_to_file("config echo 1", os.path.join(cwd, ".compmake.rc")) try: dtu.system_cmd_result(cwd, cmd, display_stdout=v, display_stderr=v, raise_on_error=True) finally: pass
def grid_visualization(): variables = {} variables["alpha"] = dict( min=-np.pi / 2, max=np.pi / 2, description="angle", resolution=np.deg2rad(10), units="rad", units_display="deg", ) variables["r"] = dict(min=3, max=5, description="distance", resolution=0.1, units="m", units_display="cm") gh = GridHelper(variables) val = gh.create_new() val.fill(0) val += np.random.randn(*val.shape) val[1, 2] = 0 d = grid_helper_plot(gh, val) od = dtu.get_output_dir_for_test() fn = os.path.join(od, "grid_visualization.jpg") dtu.write_data_to_file(d.get_png(), fn)
def _cwd(): cwd = dtu.get_output_dir_for_test() if not os.path.exists(cwd): dtu.mkdirs_thread_safe(cwd) return cwd
def compare_faster(): variables = {} variables["alpha"] = dict(min=-180, max=180, description="angle", resolution=5, units="deg", units_display="deg") variables["r"] = dict(min=3, max=5, description="distance", resolution=0.1, units="m", units_display="cm") # this will fail if precision is float32 gh = GridHelper(variables, precision="float64") val_fast = gh.create_new() val_fast.fill(0) val_slow = gh.create_new() val_slow.fill(0) od = dtu.get_output_dir_for_test() F = 1 alpha0 = 7 # r0 = 4 r0 = 4.1 w0 = 1.0 value = dict(alpha=alpha0, r=r0) gh.add_vote(val_slow, value, w0, F) assert_equal(np.sum(val_slow > 0), 9) values = np.zeros((2, 1)) values[0, 0] = alpha0 values[1, 0] = r0 weights = np.zeros(1) weights[0] = w0 gh.add_vote_faster(val_fast, values, weights, F) assert_equal(np.sum(val_fast > 0), 9) d = grid_helper_plot(gh, val_slow) fn = os.path.join(od, "compare_faster_slow.jpg") dtu.write_data_to_file(d.get_png(), fn) d = grid_helper_plot(gh, val_fast) fn = os.path.join(od, "compare_faster_fast.jpg") dtu.write_data_to_file(d.get_png(), fn) D = val_fast - val_slow diff = np.max(np.abs(D)) print(f"diff: {diff!r}") if diff > 1e-8: print(dtu.indent(array_as_string_sign(val_fast), "val_fast ")) print(dtu.indent(array_as_string_sign(val_slow), "val_slow ")) print(dtu.indent(array_as_string_sign(D), "Diff ")) print(f"non zero val_fast: {val_fast[val_fast > 0]}") print(f"non zero val_slow: {val_slow[val_slow > 0]}") assert_almost_equal(val_fast, val_slow)
def dirn(misc: str) -> str: outd = dtu.get_output_dir_for_test() return os.path.join(outd, misc)
def voting_kernel1(): resolution = 10.0 variables = {} variables["x"] = dict( min=100, max=500, description="x", resolution=resolution, units="cm", units_display="cm" ) variables["y"] = dict( min=100, max=500, description="y", resolution=resolution, units="cm", units_display="cm" ) gh = GridHelper(variables) votes = gh.create_new() votes.fill(0) points = [] estimated = [] estimated_weighted = [] F = 1 N = 25 errors_x = [] errors_x_w = [] for i in range(N): dx = resolution / N Dy = 70 Dx = 70 u = i / 5 v = i % 5 x = 125 + dx * i + Dx * u y = 127 + Dy * v p = dict(x=x, y=y) points.append(p) weight = 1 gh.add_vote(votes, p, weight=weight, F=F) tmp = gh.create_new() tmp.fill(0) gh.add_vote(tmp, p, weight=weight, F=F) assert_almost_equal(tmp.sum(), weight) estimate = gh.get_max(tmp) estimated.append(estimate) estimate_weigthed = gh.get_max_weighted(tmp, F=F) estimated_weighted.append(estimate_weigthed) errors_x.append(p["x"] - estimate["x"]) errors_x_w.append(p["x"] - estimate_weigthed["x"]) errors_x = np.array(errors_x) errors_x_w = np.array(errors_x_w) dtu.logger.debug(f"errors_x: {errors_x}") dtu.logger.debug(f"mean: {np.abs(errors_x).mean()}") dtu.logger.debug(f"errors_x_w: {errors_x_w}") dtu.logger.debug(f"mean: {np.abs(errors_x_w).mean()}") assert errors_x.max() <= +resolution / 2 assert errors_x.min() >= -resolution / 2 assert np.abs(errors_x_w).max() <= resolution / 10 a = dtu.CreateImageFromPylab(dpi=1000) with a as pylab: grid_helper_plot_field(gh, votes, pylab) pylab.axis("equal") grid_helper_annotate_axes(gh, pylab) for p in points: grid_helper_mark_point(gh, pylab, p, color="blue", markersize=4) for e in estimated: grid_helper_mark_point(gh, pylab, e, color="red", markersize=3) for e in estimated_weighted: grid_helper_mark_point(gh, pylab, e, color="green", markersize=3) b = dtu.CreateImageFromPylab(dpi=1000) with b as pylab: x = np.array([_["x"] for _ in points]) xe = np.array([_["x"] for _ in estimated]) xew = np.array([_["x"] for _ in estimated_weighted]) xe -= x xew -= x x = x * 0 # XXX? pylab.plot(x, ".", label="x") pylab.plot(xe, ".", label="x estimated") pylab.plot(xew, ".", label="x estimated weighted") pylab.legend() od = dtu.get_output_dir_for_test() fn = os.path.join(od, "voting_kernel1.jpg") dtu.write_data_to_file(a.get_png(), fn) fn = os.path.join(od, "errors.jpg") dtu.write_data_to_file(b.get_png(), fn)