b1k / task01_picking_up_trash /code /tests /test_visual_navigation.py
davidwdw's picture
Upload Task 00, Task 01 and Task 02 code, datasets and evaluation results (part 20)
9befad5 verified
Raw History Blame Contribute Delete
7.15 kB
import numpy as np
from types import SimpleNamespace
from gpt6_b1k.visual_navigation import ObservedFloorGrid, forward_clearance, match_features, VisualTracker
from gpt6_b1k.sensors import Odometry
def test_reset_camera_frames_are_not_used_as_visual_anchors(monkeypatch):
import io
import gpt6_b1k.visual_navigation as nav
policy = nav.VisualTrashPolicy(io.StringIO())
obs = SimpleNamespace(kin=None, p=np.zeros(61), hold=lambda: np.ones(23))
monkeypatch.setattr(nav, 'VisualSensors', lambda raw, kin: obs)
updates = []
monkeypatch.setattr(policy.tracker, 'update', lambda *args, **kw: updates.append(args[2]))
policy.generator = iter([np.zeros(23)])
for _ in range(10):
action = policy.predict({})
np.testing.assert_array_equal(action[:3], 0)
assert updates == []
policy.predict({})
assert updates == [11]
def test_active_approach_receives_refined_object_position(monkeypatch):
import io
import gpt6_b1k.policy as base
from gpt6_b1k.visual_navigation import VisualTrashPolicy
from gpt6_b1k.perception import Detection
policy = VisualTrashPolicy(io.StringIO())
policy.obs = SimpleNamespace()
policy.grid.observe = lambda *args: None
target = np.array([1., 0., .07])
policy.objects = [dict(point=target, done=False, attempts=0, seen=1)]
detection = Detection(center=np.array([1.2, 0., .07]), kind='can',
low=np.array([1.16, -.04, 0.]), high=np.array([1.24, .04, .14]),
pixels=np.zeros((100, 2)), points=np.zeros((100, 3)), score=.8)
monkeypatch.setattr(base, 'floor_objects', lambda obs: [detection])
policy.observe()
np.testing.assert_array_equal(target, detection.center)
assert policy.objects[0]['point'] is target
assert not policy.obs.strict_can_colors
def test_route_cannot_cross_unobserved_gap():
grid = ObservedFloorGrid(size=80, resolution=.05)
grid.seen_floor[10:30, 30:50] = True
grid.seen_floor[45:65, 30:50] = True
assert grid.route(grid.xy([20, 40]), grid.xy([55, 40])) is None
grid.seen_floor[25:50, 30:50] = True
grid._allowed = None
assert grid.route(grid.xy([20, 40]), grid.xy([55, 40])) is not None
def test_furniture_clearance_blocks_too_narrow_passage():
grid = ObservedFloorGrid(size=80, resolution=.05)
grid.seen_floor[:] = True
grid.occupied[:, 30] = True
grid.occupied[:, 45] = True
assert grid.route(grid.xy([20, 37]), grid.xy([60, 37])) is None
def test_observed_floor_clears_collected_can_but_keeps_table_above_it():
grid, odom = ObservedFloorGrid(), Odometry()
cloud = np.zeros((4, 4, 3))
cloud[0, 0] = [1., 0., .12]
cloud[2, 2] = [1., 1., .80]
obs = SimpleNamespace(cloud=lambda: cloud, external=lambda points: np.ones(len(points), bool))
grid.observe(obs, odom)
can, table = tuple(grid.cell([1., 0.])), tuple(grid.cell([1., 1.]))
assert grid.occupied[can] and grid.occupied[table]
cloud[0, 0, 2] = cloud[2, 2, 2] = 0.
grid.observe(obs, odom)
assert not grid.occupied[can]
assert grid.occupied[table]
def test_single_thin_leg_stops_forward_motion():
xyz = np.full((8, 8, 3), 10.)
xyz[2, 2] = [.39, .30, .10]
obs = SimpleNamespace(cloud=lambda: xyz, external=lambda points: np.ones(len(points), bool))
assert forward_clearance(obs) == .39
def test_short_sideways_alignment_checks_its_own_corridor():
xyz = np.full((8, 8, 3), 10.)
xyz[2, 2] = [.30, .39, .10]
obs = SimpleNamespace(cloud=lambda: xyz, external=lambda points: np.ones(len(points), bool))
assert forward_clearance(obs, direction=[0., 1.]) == .39
def test_depth_feature_match_recovers_observed_motion():
rng = np.random.default_rng(4)
points = rng.uniform([.6, -1., 0.], [3., 1., 1.], (80, 3))
desc = rng.integers(0, 256, (80, 32), dtype=np.uint8)
angle = .08
rot = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
current = points.copy()
current[:, :2] = current[:, :2]@rot.T + [.06, -.03]
current[-15:, :2] += 1. # Moving objects / incorrect depth must not set pose.
tf, quality = match_features((points, desc), (current, desc))
np.testing.assert_allclose(tf[:2, :2], rot, atol=.001)
np.testing.assert_allclose(tf[:2, 3], [.06, -.03], atol=.001)
assert quality['inliers'] >= 60
def test_failed_visual_match_retains_trusted_anchor(monkeypatch):
import gpt6_b1k.visual_navigation as nav
empty = (np.empty((0, 3)), None)
monkeypatch.setattr(nav, 'visual_features', lambda obs: empty)
tracker, odom = VisualTracker(), Odometry()
obs = SimpleNamespace(p=np.zeros(61))
tracker.update(obs, odom, 1)
anchor = tracker.anchor
tracker.update(obs, odom, 5)
assert tracker.anchor is anchor and tracker.last_match == 1
def test_official_camera_calibration_order_and_privileged_filter():
from gpt6_b1k.visual_navigation import VisualSensors
p = np.zeros(61)
p[23] = p[48] = 1
poses = np.array([[1., 0, 0, 0, 0, 0, 1], [2., 0, 0, 0, 0, 0, 1], [3., 0, 0, 0, 0, 0, 1]])
obs = VisualSensors({'robot::proprio': p, 'robot::cam_rel_poses': poses.ravel(),
'object_poses': {'forbidden': [99, 99, 99]}, 'q_score': 1.})
for i, camera in enumerate(('left_realsense', 'right_realsense', 'zed'), 1):
np.testing.assert_array_equal(obs.camera(camera)[:3, 3], [i, 0, 0])
assert 'object_poses' not in obs.raw and 'q_score' not in obs.raw
def test_navigation_reaches_goal_between_grid_cells():
import io
from gpt6_b1k.visual_navigation import VisualTrashPolicy
from gpt6_b1k.sensors import planar
policy = VisualTrashPolicy(io.StringIO())
policy.odom.pose = np.array([.05, .05, np.pi/4])
policy.grid.seen_floor[:] = True
cloud = np.tile([2., 0., 0.], (8, 8, 1))
policy.obs = SimpleNamespace(hold=lambda: np.zeros(23), cloud=lambda: cloud,
external=lambda points: np.ones(len(points), bool))
policy.observe = lambda: None
goal = np.array([.125, .125, np.pi/4])
# Its rounded endpoint (.10,.10) is already within the waypoint tolerance,
# although the actual goal is still outside the route completion tolerance.
for action in policy.travel(goal):
policy.steps += 1
policy.tracker.last_match = policy.steps
rotation = planar(policy.odom.pose)[:2, :2]
policy.odom.pose[:2] += rotation@action[:2]/30*.75
policy.odom.pose[2] += action[2]/30
assert policy.steps < 200
assert np.linalg.norm(policy.odom.pose[:2]-goal[:2]) < .10
def test_discovery_rejects_wood_but_keeps_rich_orange_can():
from gpt6_b1k.perception import floor_objects
x, y = np.meshgrid(np.linspace(.70, .77, 20), np.linspace(-.30, -.23, 20))
cloud = np.stack((x, y, np.linspace(.025, .135, 400).reshape(20, 20)), -1)
rgb = np.tile([150, 110, 75], (20, 20, 1))
obs = SimpleNamespace(cloud=lambda camera: cloud, get=lambda camera, modality: rgb,
eef=lambda arm: np.array([-2., 0., 0.]), strict_can_colors=True)
assert not floor_objects(obs)
rgb[:] = [220, 95, 12]
assert len(floor_objects(obs)) == 1