Skip to content

Commit

Permalink
test megapose inference added
Browse files Browse the repository at this point in the history
  • Loading branch information
petrikvladimir committed Sep 18, 2023
1 parent 03207c7 commit de16a78
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jobs:
python -m happypose.toolbox.utils.download --cosypose_model=coarse-bop-ycbv-pbr--724183
python -m happypose.toolbox.utils.download --cosypose_model=refiner-bop-ycbv-pbr--604090
python -m happypose.toolbox.utils.download --megapose_models
cd tests/data
git clone https://github.com/petrikvladimir/happypose_test_data.git crackers_example
Expand Down
3 changes: 2 additions & 1 deletion happypose/toolbox/utils/transform_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def load_SO3_grid(resolution):
Returns:
rotmats: [N,3,3]
"""
data_fname = PROJECT_DIR / f"src/megapose/data/data_{resolution}.qua"
meagpose_dir = PROJECT_DIR / "happypose" / "pose_estimators" / "megapose"
data_fname = meagpose_dir / f"src/megapose/data/data_{resolution}.qua"

assert data_fname.is_file(), f"File {data_fname} not found"

Expand Down
5 changes: 3 additions & 2 deletions tests/test_cosypose_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ def _load_pose_models(
refiner_model = TestCosyPoseInference._load_pose_model(refiner_run_id, **kwargs)
return coarse_model, refiner_model

def _load_crackers_example_observation(self):
@staticmethod
def _load_crackers_example_observation():
"""Load cracker example observation tensor"""
data_dir = Path(__file__).parent.joinpath("data").joinpath("crackers_example")
camera_data = CameraData.from_json((data_dir / "camera_data.json").read_text())
rgb = np.array(Image.open(data_dir / "image_rgb.png"), dtype=np.uint8)
self.assertEqual(rgb.shape[:2], camera_data.resolution)
assert rgb.shape[:2] == camera_data.resolution
return ObservationTensor.from_numpy(rgb=rgb, K=camera_data.K)

def test_detector(self):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_megapose_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Set of unit tests for testing inference example for MegaPose."""
import unittest

import numpy as np
import pinocchio as pin
from pathlib import Path

from .test_cosypose_inference import TestCosyPoseInference
from happypose.toolbox.datasets.bop_object_datasets import BOPObjectDataset
from happypose.toolbox.utils.load_model import NAMED_MODELS, load_named_model


class TestMegaPoseInference(unittest.TestCase):
"""Unit tests for MegaPose inference example."""

def test_meagpose_pipeline(self):
"""Run detector from CosyPose with coarse and refiner from MegaPose"""
observation = TestCosyPoseInference._load_crackers_example_observation()

detector = TestCosyPoseInference._load_detector()
detections = detector.get_detections(observation=observation)

self.assertGreater(len(detections), 0)
detections = detections[:1] # let's keep the most promising one only.

object_dataset = BOPObjectDataset(
Path(__file__).parent / "data" / "crackers_example" / "models",
label_format="ycbv-{label}",
)

model_info = NAMED_MODELS["megapose-1.0-RGB"]
pose_estimator = load_named_model("megapose-1.0-RGB", object_dataset).to("cpu")
preds, _ = pose_estimator.run_inference_pipeline(
observation, detections=detections, **model_info["inference_parameters"]
)

self.assertEqual(len(preds), 1)
self.assertEqual(preds.infos.label[0], "ycbv-obj_000002")

pose = pin.SE3(preds.poses[0].numpy())
exp_pose = pin.SE3(
pin.exp3(np.array([1.44, 1.19, -0.91])), np.array([0, 0, 0.52])
)
diff = pose.inverse() * exp_pose
self.assertLess(np.linalg.norm(pin.log6(diff).vector), 0.2)


if __name__ == "__main__":
unittest.main()

0 comments on commit de16a78

Please sign in to comment.