Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposed unity random seed to python api surface #823

Merged
merged 7 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 75 additions & 60 deletions ai2thor/tests/test_unity.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,9 @@ def fifo_controller():
fifo_wsgi = [_fifo_controller, _wsgi_controller]
fifo_wsgi_stoch = [_fifo_controller, _wsgi_controller, _stochastic_controller]

BASE_FP28_POSITION = dict(
x=-1.5,
z=-1.5,
y=0.901,
)
BASE_FP28_POSITION = dict(x=-1.5, z=-1.5, y=0.901,)
BASE_FP28_LOCATION = dict(
**BASE_FP28_POSITION,
rotation={"x": 0, "y": 0, "z": 0},
horizon=0,
standing=True,
**BASE_FP28_POSITION, rotation={"x": 0, "y": 0, "z": 0}, horizon=0, standing=True,
)


Expand Down Expand Up @@ -126,6 +119,14 @@ def assert_near(point1, point2, error_message=""):
)


def assert_images_near(image1, image2, max_mean_pixel_diff=1):
return np.mean(np.abs(image1 - image2).flatten()) <= max_mean_pixel_diff


def assert_images_far(image1, image2, min_mean_pixel_diff=10):
return np.mean(np.abs(image1 - image2).flatten()) >= min_mean_pixel_diff


def test_stochastic_controller(stochastic_controller):
stochastic_controller.reset(TEST_SCENE)
assert stochastic_controller.last_event.metadata["lastActionSuccess"]
Expand Down Expand Up @@ -199,9 +200,7 @@ def test_deprecated_segmentation_params(fifo_controller):
# renderClassImage has been renamed to renderSemanticSegmentation

fifo_controller.reset(
TEST_SCENE,
renderObjectImage=True,
renderClassImage=True,
TEST_SCENE, renderObjectImage=True, renderClassImage=True,
)
event = fifo_controller.last_event
with warnings.catch_warnings():
Expand All @@ -218,9 +217,7 @@ def test_deprecated_segmentation_params2(fifo_controller):
# renderClassImage has been renamed to renderSemanticSegmentation

fifo_controller.reset(
TEST_SCENE,
renderSemanticSegmentation=True,
renderInstanceSegmentation=True,
TEST_SCENE, renderSemanticSegmentation=True, renderInstanceSegmentation=True,
)
event = fifo_controller.last_event

Expand Down Expand Up @@ -434,32 +431,40 @@ def test_add_third_party_camera(controller):
"action: AddThirdPartyCamera has an invalid argument: orthographicSize"
)


def test_third_party_camera_depth(fifo_controller):
fifo_controller.reset(
TEST_SCENE,
width=300,
height=300,
renderDepthImage=True
)
fifo_controller.reset(TEST_SCENE, width=300, height=300, renderDepthImage=True)

agent_position = {'x': -2.75, 'y': 0.9009982347488403, 'z': -1.75}
agent_rotation = {'x': 0.0, 'y': 90.0, 'z': 0.0}
agent_position = {"x": -2.75, "y": 0.9009982347488403, "z": -1.75}
agent_rotation = {"x": 0.0, "y": 90.0, "z": 0.0}

agent_init_position = {'x': -2.75, 'y': 0.9009982347488403, 'z': -1.25}
camera_position = {'x': -2.75, 'y': 1.5759992599487305, 'z': -1.75}
camera_rotation = {'x': 0.0, 'y': 90.0, 'z': 0.0}
agent_init_position = {"x": -2.75, "y": 0.9009982347488403, "z": -1.25}
camera_position = {"x": -2.75, "y": 1.5759992599487305, "z": -1.75}
camera_rotation = {"x": 0.0, "y": 90.0, "z": 0.0}
# teleport agent into a position the third-party camera won't see
fifo_controller.step(action="Teleport", position=agent_init_position, rotation=agent_rotation, horizon=0.0, standing=True)
fifo_controller.step(
action="Teleport",
position=agent_init_position,
rotation=agent_rotation,
horizon=0.0,
standing=True,
)

camera_event = fifo_controller.step(
dict(
action=Actions.AddThirdPartyCamera,
position=camera_position,
rotation=camera_rotation
rotation=camera_rotation,
)
)
camera_depth = camera_event.third_party_depth_frames[0]
agent_event = fifo_controller.step(action="Teleport", position=agent_position, rotation=agent_rotation, horizon=0.0, standing=True)
agent_event = fifo_controller.step(
action="Teleport",
position=agent_position,
rotation=agent_rotation,
horizon=0.0,
standing=True,
)
agent_depth = agent_event.depth_frame
mse = np.square((np.subtract(camera_depth, agent_depth))).mean()
# if the clipping planes aren't the same between the agent and third-party camera
Expand Down Expand Up @@ -670,9 +675,7 @@ def test_open_interactable_with_filter(controller):
controller.step(dict(action="SetObjectFilter", objectIds=[]))
assert controller.last_event.metadata["objects"] == []
controller.step(
action="OpenObject",
objectId=fridge["objectId"],
raise_for_failure=True,
action="OpenObject", objectId=fridge["objectId"], raise_for_failure=True,
)

controller.step(dict(action="ResetObjectFilter"))
Expand Down Expand Up @@ -704,9 +707,7 @@ def test_open_interactable(controller):
assert fridge["visible"], "Object is not interactable!"
assert_near(controller.last_event.metadata["agent"]["position"], position)
event = controller.step(
action="OpenObject",
objectId=fridge["objectId"],
raise_for_failure=True,
action="OpenObject", objectId=fridge["objectId"], raise_for_failure=True,
)
fridge = next(
obj
Expand Down Expand Up @@ -1148,8 +1149,7 @@ def test_teleport(controller):
# Teleporting too high
before_position = controller.last_event.metadata["agent"]["position"]
controller.step(
"Teleport",
**{**BASE_FP28_LOCATION, "y": 1.0},
"Teleport", **{**BASE_FP28_LOCATION, "y": 1.0},
)
assert not controller.last_event.metadata[
"lastActionSuccess"
Expand All @@ -1160,17 +1160,15 @@ def test_teleport(controller):

# Teleporting into an object
controller.step(
"Teleport",
**{**BASE_FP28_LOCATION, "z": -3.5},
"Teleport", **{**BASE_FP28_LOCATION, "z": -3.5},
)
assert not controller.last_event.metadata[
"lastActionSuccess"
], "Should not be able to teleport into an object."

# Teleporting into a wall
controller.step(
"Teleport",
**{**BASE_FP28_LOCATION, "z": 0},
"Teleport", **{**BASE_FP28_LOCATION, "z": 0},
)
assert not controller.last_event.metadata[
"lastActionSuccess"
Expand Down Expand Up @@ -1624,8 +1622,7 @@ def test_manipulathor_move(controller):
event = controller.reset(scene=TEST_SCENE, agentMode="arm", gridSize=0.25)

assert_near(
point1=start_position,
point2=event.metadata["agent"]["position"],
point1=start_position, point2=event.metadata["agent"]["position"],
)

event = controller.step(action="MoveAgent", ahead=0.25, right=0.15)
Expand All @@ -1636,8 +1633,7 @@ def test_manipulathor_move(controller):

event = controller.step(action="MoveAgent", ahead=-0.25, right=-0.15)
assert_near(
point1=start_position,
point2=event.metadata["agent"]["position"],
point1=start_position, point2=event.metadata["agent"]["position"],
)

event = controller.step(action="MoveRight")
Expand All @@ -1648,8 +1644,7 @@ def test_manipulathor_move(controller):

event = controller.step(action="MoveLeft")
assert_near(
point1=start_position,
point2=event.metadata["agent"]["position"],
point1=start_position, point2=event.metadata["agent"]["position"],
)

event = controller.step(action="MoveAhead")
Expand All @@ -1660,18 +1655,13 @@ def test_manipulathor_move(controller):

event = controller.step(action="MoveBack")
assert_near(
point1=start_position,
point2=event.metadata["agent"]["position"],
point1=start_position, point2=event.metadata["agent"]["position"],
)


@pytest.mark.parametrize("controller", fifo_wsgi)
def test_manipulathor_rotate(controller):
event = controller.reset(
scene=TEST_SCENE,
agentMode="arm",
rotateStepDegrees=90
)
event = controller.reset(scene=TEST_SCENE, agentMode="arm", rotateStepDegrees=90)
assert_near(
point1={"x": -0.0, "y": 180.0, "z": 0.0},
point2=event.metadata["agent"]["rotation"],
Expand Down Expand Up @@ -1722,6 +1712,34 @@ def test_unsupported_manipulathor(controller):
assert not event, "PickupObject(objectId) should have failed with agentMode=arm"


@pytest.mark.parametrize("controller", fifo_wsgi)
def test_set_random_seed(controller):
orig_frame = controller.reset().frame
controller.step(action="SetRandomSeed", seed=41)
s41_frame = controller.step(action="RandomizeMaterials").frame
controller.step(action="SetRandomSeed", seed=42)
s42_frame = controller.step(action="RandomizeMaterials").frame

assert_images_far(s42_frame, s41_frame)
assert_images_far(s42_frame, orig_frame)
assert_images_far(s41_frame, orig_frame)

f1_1 = controller.reset().frame
f1_2 = controller.step(action="SetRandomSeed", seed=42).frame
f1_3 = controller.step(action="RandomizeMaterials").frame

f2_1 = controller.reset().frame
f2_2 = controller.step(action="SetRandomSeed", seed=42).frame
f2_3 = controller.step(action="RandomizeMaterials").frame

assert_images_near(f1_1, f2_1)
assert_images_near(f1_1, f1_2)
assert_images_near(f2_1, f2_2)
assert_images_near(f1_3, f2_3)
assert_images_far(f2_1, f2_3)
assert_images_far(f1_1, f1_3)


@pytest.mark.parametrize("controller", fifo_wsgi)
def test_randomize_materials_scenes(controller):
for p in [0, 200, 300, 400]:
Expand Down Expand Up @@ -1865,8 +1883,7 @@ def test_randomize_materials_params(controller):
== 325
)
assert controller.step(
action="RandomizeMaterials",
inRoomTypes=["Kitchen", "LivingRoom"],
action="RandomizeMaterials", inRoomTypes=["Kitchen", "LivingRoom"],
)
assert (
controller.last_event.metadata["actionReturn"]["totalMaterialsConsidered"]
Expand All @@ -1877,8 +1894,7 @@ def test_randomize_materials_params(controller):

controller.reset(scene="FloorPlan_Train5_2")
assert not controller.step(
action="RandomizeMaterials",
inRoomTypes=["Kitchen", "LivingRoom"],
action="RandomizeMaterials", inRoomTypes=["Kitchen", "LivingRoom"],
)
assert not controller.step(action="RandomizeMaterials", inRoomTypes=["LivingRoom"])
assert controller.step(action="RandomizeMaterials", inRoomTypes=["RoboTHOR"])
Expand All @@ -1889,8 +1905,7 @@ def test_randomize_materials_params(controller):

controller.reset(scene="FloorPlan_Val3_2")
assert not controller.step(
action="RandomizeMaterials",
inRoomTypes=["Kitchen", "LivingRoom"],
action="RandomizeMaterials", inRoomTypes=["Kitchen", "LivingRoom"],
)
assert not controller.step(action="RandomizeMaterials", inRoomTypes=["LivingRoom"])
assert controller.step(action="RandomizeMaterials", inRoomTypes=["RoboTHOR"])
Expand Down
Loading