← Back to the animated process

docs/PHASE4_YOLO_PNP_GICP.md

Implementation snapshot supporting the published lesson. Workflow guide. Configuration and recorded examples describe this project snapshot.

1# Phase 4: YOLO segmentation + 8 keypoints → PnP → GICP23The canonical convention throughout this phase is:45```text6P_camera = T_camera_object * P_object7```89`reference.ply`, `object_keypoints.yaml`, PnP, GICP, and the final TF all use10the calibrated `box_center` frame. Do not invert or re-center the generated11reference at runtime.1213For new collections, `scripts/collect_process/04_build_reference.sh` integrates14the reviewed masked depth views using their saved `T_camera_object` poses. The15production TSDF point cloud is therefore generated directly in `box_center`,16the same frame as the eight PnP corners. The moving-camera Scan PLY is retained17only for diagnostics.1819## Train2021The paired export contains one file per captured source view plus its foreground22mask. Backgrounds are sampled on the fly for both tasks, so no `__bg_*` training23PNGs are required. Validation remains unchanged. Horizontal geometric flipping24is disabled because it would change the semantic corner ordering.2526```bash27../objectSegmentation/.venv/bin/python -E scripts/train_yolo_seg_pose.py \28  --dataset datasets/my_object_fresh/yolo_seg_pose_onthefly \29  --project training/yolo \30  --name my_object_fresh --task both --epoch-repeats 12 \31  --backgrounds ../objectSegmentation/backgrounds/picsum32```3334Outputs:3536```text37training/yolo/my_object_fresh_seg/weights/best.pt38training/yolo/my_object_fresh_pose/weights/best.pt39```4041`--epoch-repeats 12` replaces the training volume formerly supplied by twelve42materialized copies. Each repeated read chooses a fresh background, while the4321 source images and the validation set remain unmodified.4445For the current small capture set, the deployed pose checkpoint is the fully46unfrozen refinement pass:4748```bash49../objectSegmentation/.venv/bin/python -E scripts/train_yolo_seg_pose.py \50  --task pose \51  --dataset datasets/my_object_fresh/yolo_seg_pose_onthefly \52  --pose-model training/yolo/my_object_rgbd_unfrozen_pose/weights/best.pt \53  --project training/yolo --name my_object_fresh_pose_refined \54  --epochs 100 --patience 25 --freeze 0 --lr0 0.00005 --epoch-repeats 1255```5657Its output is58`training/yolo/my_object_fresh_pose_refined_pose/weights/best.pt` and Phase 4 uses59that checkpoint by default.6061The older `my_object_fresh` reference was cropped from a moving-camera scan.62For that historical asset, residual scan translation was aligned against the63calibrated capture depths without redefining `box_center`:6465```bash66.venv-ros-cuda/bin/python scripts/align_reference_to_capture.py \67  --dataset datasets/my_object_fresh \68  --reference datasets/my_object_fresh/references/my_object/reference.ply69```7071The default Phase 4 launcher still uses that historical72`reference_aligned.ply`. The collection script's live step overrides the path73with the current session's calibrated `reference.ply`.7475The eight PnP corners come from the capture calibration's metric box. Before76launching the collection script's live step, check the current asset contract77with:7879```bash80.venv-ros-cuda/bin/python scripts/verify_phase4_frame_contract.py \81  datasets/my_object_20260925 my_object82```8384## Build and run8586```bash87scripts/build_phase1.sh88scripts/run_phase4.sh89```9091Use the installed CUDA environment for GPU YOLO inference:9293```bash94scripts/build_cuda.sh95scripts/run_phase4_cuda.sh96```9798For external already-aligned RGB-D topics:99100```bash101scripts/run_phase4.sh camera:=false102```103104The default `.venv-ros` installation runs both YOLO models on CPU. A CUDA ROS105environment can select `config/devices_cuda.yaml` after CUDA PyTorch and the106ROS dependencies are installed together.107108Main outputs:109110- `/object_pose/pnp`, `/object_pose/gicp`, `/object_pose/filtered`111- `/object_pose/base` when `base_link → camera_color_optical_frame` is available112- compatibility output `/object/pose`113- TF frames `box_center`, plus optional `object_pnp`, `object_gicp`, and114  `object_filtered`115- `/object_pose/tracking_state` (`INITIALIZATION`, `TRACKING`, or `LOST`)116- `/object_pose/detection_image` (`sensor_msgs/Image`, `rgb8`): color frames117  with the selected segmentation bounding box and confidence. The original118  RGB frame is published when there is no detection.119- `/object_pose/box_image` (`sensor_msgs/Image`, `rgb8`): camera frames with the120  calibrated eight-corner 3D box projected from the accepted filtered GICP pose.121  Rejected frames show `NO ACCEPTED POSE` instead of an old box.122- `/object_pose/pnp_reprojection`, `/object/reference_cloud_pnp`, and123  `/object/reference_cloud_gicp`124125PnP is used for startup/reinitialization. After one accepted GICP result, the126previous accepted unfiltered result initializes the next GICP fit. Rejected127poses never enter the temporal filter.128129## Independent geometry tests130131PnP observation JSON accepts ordered arrays or objects with `id`, `u`, `v`,132`confidence`, and `valid`, plus `camera_matrix`/`K`:133134```bash135.venv/bin/python scripts/offline_pnp_test.py \136  --object-keypoints datasets/my_object_fresh/yolo_seg_pose_onthefly/object_keypoints.yaml \137  --observation observation.json --image frame.png --overlay /tmp/pnp.png138```139140GICP accepts a reference, observed PLY/PCD, and JSON initial transform:141142```bash143.venv-ros/bin/python scripts/offline_gicp_test.py \144  --reference datasets/my_object_fresh/references/my_object/reference_aligned.ply \145  --observed observed.ply --initial T_camera_object.json --visualize146```147148All quality gates and coarse/medium/fine settings are in `config/phase4.yaml`.149Diagnostics report PnP inliers/reprojection error, GICP fitness/RMSE/overlap,150per-level runtime, temporal jumps, initializer selection, and total runtime.151