-
Notifications
You must be signed in to change notification settings - Fork 16
Add fine-tuned SuperAnimal-Quadruped support and improve demo setup #30
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
Open
xiu-cs
wants to merge
43
commits into
main
Choose a base branch
from
ti_dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 39 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
d5d6136
Add arguments for 2D pose model overrides in opts class
xiu-cs 3aea87e
Add configuration file for HRNet-w32 backbone fine-tuned on Animal3D
xiu-cs 0d7d16f
Add initial configuration file for animal 2D detector and HRNet-w32 f…
xiu-cs cfe20da
Enhance SuperAnimalConfig with fine-tuning options and detailed mode …
xiu-cs f66e63f
Update vis_animals.sh to include saved_2d_model_path and reset saved_…
xiu-cs 59c960d
Refactor 2D pose estimation in vis_animals.py to use SuperAnimalEstim…
xiu-cs 06cbfc1
Refactor resolve_weights_path function in weights.py for improved cla…
xiu-cs 832c2d4
Update README.md to clarify pre-trained model usage and auto-download…
xiu-cs c439d22
Remove unused joint variables from vis_animals.sh
xiu-cs 674afbe
Update model weights path resolution to include file extension
xiu-cs e4ec633
Enhance SuperAnimalEstimator to support fine-tuned model loading and …
xiu-cs 090dcfc
Update README.md to enhance SuperAnimalEstimator description with fin…
xiu-cs 95293b4
Update model weights path resolution to include file extension for lo…
xiu-cs 87dc5f4
Remove unused modules and classes related to 3D pose estimation
xiu-cs ed3f084
Update DatasetConfig references from "rat7m" to "animal3d" and adjust…
xiu-cs bc4a886
Remove references to Rat7M dataset from Graph class and related docum…
xiu-cs f32ca40
Update action placeholder in main_animal3d.py for Animal3D dataset
xiu-cs 54d1cb6
Update dataset default value and root path in arguments.py for Animal…
xiu-cs ff801e5
Add SuperAnimalConfig support and unit tests for fine-tuned mode in S…
xiu-cs 3279e88
Refactor get_pose2D function to streamline 2D pose estimation using S…
xiu-cs 4c20e0f
Refactor 2D and 3D pose estimation functions to build estimators once…
xiu-cs 54ffab2
Add 'mot' to ignore words list in Codespell workflow
xiu-cs e77e7f0
Refactor model loading to use device-agnostic code for CUDA compatibi…
xiu-cs 6335c0b
Refactor HRNetPose2d to use device-agnostic code for model and input …
xiu-cs 4ebd20c
Update Python version requirement and refine dependency constraints i…
xiu-cs e3fef16
Remove unused import of coco_h36m from utilitys.py
xiu-cs 621b6a6
Remove unnecessary configuration files and associated data
xiu-cs 34a824f
Update README.md to clarify Python version requirement and add PyTorc…
xiu-cs c6b1c4a
Fix checkpoint directory handling and improve weight loading logic in…
xiu-cs 6131b5e
Refactor model path handling in test_animal3d.sh to clarify usage of …
xiu-cs f77f5e8
Update fmpose3d/animals/models/graph_frames.py
xiu-cs ba130a8
Refactor build_2d_estimator function to simplify model path handling …
xiu-cs d6d26ac
Refactor weight resolution logic in SuperAnimalEstimator to improve s…
xiu-cs 882ea0b
Fix test_auto_download_finetuned to reuse resolved path and update ca…
xiu-cs aa3df33
Refactor SuperAnimalEstimator to streamline fine-tuned model configur…
xiu-cs a5ecbc1
Update fmpose3d/animals/configs/__init__.py
xiu-cs 9048629
Remove unused estimator and postprocessor from FMPose3DInference setup
xiu-cs e45fe2c
Add assertions for auto_download_finetuned and pose_snapshot_path in …
xiu-cs 30ff652
Refactor mock function in SuperAnimalPrediction tests to handle multi…
xiu-cs 6e7337d
Update docstring in build_3d_lifter to clarify return value as eval m…
xiu-cs d4c2061
Enhance 2D and 3D pose estimation by including scores and validity ch…
xiu-cs 760cdf7
Remove redundant import of resolve_weights_path in build_3d_lifter fu…
xiu-cs 0db80b0
Fix docstring formatting in weights.py for clarity
xiu-cs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| import imageio | ||
| from fmpose3d.animals.common.arguments import opts as parse_args | ||
| from fmpose3d.common.camera import normalize_screen_coordinates, camera_to_world | ||
| from fmpose3d.common.config import SuperAnimalConfig | ||
| from fmpose3d.inference_api.fmpose3d import SuperAnimalEstimator | ||
|
|
||
| args = parse_args().parse() | ||
| os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu | ||
|
|
@@ -46,21 +48,6 @@ | |
| from fmpose3d.models import get_model | ||
| CFM = get_model(args.model_type) | ||
|
|
||
| try: | ||
| from deeplabcut.pose_estimation_pytorch.apis import ( # pyright: ignore[reportMissingImports] | ||
| superanimal_analyze_images, | ||
| ) | ||
| except ImportError: | ||
| raise ImportError( | ||
| "DeepLabCut is required for the animal demo. " | ||
| "Install it with: pip install \"fmpose3d[animals]\"" | ||
| ) from None | ||
|
|
||
| superanimal_name = "superanimal_quadruped" | ||
| model_name = "hrnet_w32" | ||
| detector_name = "fasterrcnn_resnet50_fpn_v2" | ||
| max_individuals = 1 | ||
|
|
||
| def compute_limb_regularization_matrix(gt_3d): | ||
| """ | ||
| Compute regularization matrix to align limb directions to vertical (0,0,1). | ||
|
|
@@ -145,108 +132,35 @@ def apply_regularization(pose_3d, R): | |
| """ | ||
| return (R @ pose_3d.T).T | ||
|
|
||
| def get_pose2D(path, output_dir, type): | ||
| def build_2d_estimator(): | ||
| """Build the 2D pose estimator once. | ||
|
|
||
| Empty --saved_2d_model_path -> auto-download fine-tuned snapshot from HF | ||
| on first predict. | ||
| Non-empty path -> use as a local override. | ||
| """ | ||
| cfg = SuperAnimalConfig( | ||
| pose_snapshot_path=args.saved_2d_model_path, | ||
| pytorch_config_path=args.pytorch_config_2d_path, | ||
| auto_download_finetuned=True, | ||
| ) | ||
| snapshot = cfg.pose_snapshot_path or "auto-download from HF on first predict" | ||
| print(f"[2D] pose snapshot = {snapshot}") | ||
| return SuperAnimalEstimator(cfg) | ||
|
|
||
|
|
||
| def get_pose2D(estimator, path, output_dir, type): | ||
|
|
||
| print('\nGenerating 2D pose...') | ||
|
|
||
| # Check if this is the special debug case for 000000119761_horse | ||
| filename = Path(path).stem | ||
| is_debug_case = "000000119761_horse" in filename | ||
|
|
||
| if is_debug_case: | ||
| print(f"DEBUG MODE: Using provided 2D pose for {filename}") | ||
| # User provided 2D pose (26 keypoints, x, y coordinates, ignoring the last dimension) | ||
| provided_pose = np.array([ | ||
| [361, 230], [361, 237], [363, 279], [257, 359], [251, 374], | ||
| [164, 365], [68, 372], [99, 206], [247, 266], [253, 285], | ||
| [127, 275], [101, 285], [267, 217], [268, 229], [273, 318], | ||
| [250, 340], [128, 311], [76, 305], [313, 220], [48, 310], | ||
| [351, 203], [352, 210], [340, 257], [340, 261], [373, 276], | ||
| [55, 247] | ||
| ], dtype=np.float32) | ||
|
|
||
| # Reshape to match expected format: (1, 26, 2) for single individual | ||
| provided_pose = provided_pose.reshape(1, 26, 2) | ||
|
|
||
| # Create xy_preds dict with the provided pose | ||
| xy_preds = {path: provided_pose} | ||
| print(f"Using provided 2D pose with shape: {provided_pose.shape}") | ||
| else: | ||
| # Normal prediction flow | ||
| predictions = superanimal_analyze_images( | ||
| superanimal_name, | ||
| model_name, | ||
| detector_name, | ||
| path, | ||
| max_individuals, | ||
| out_folder=output_dir | ||
| ) | ||
| print("predictions:", predictions) | ||
|
|
||
| # get the 2D keypoints from the predictions | ||
| xy_preds = {} | ||
| # predictions is a dict: {image_path: {"bodyparts": (N, K, 3), "bboxes": ..., "bbox_scores": ...}} | ||
| for img_path, payload in predictions.items(): | ||
| bodyparts = payload.get("bodyparts") | ||
| if bodyparts is None: | ||
| continue | ||
| # bodyparts shape: (num_individuals, num_keypoints, 3) -> [:, :, :2] keeps x,y | ||
| xy_preds[img_path] = bodyparts[..., :2] | ||
|
|
||
| print("2D keypoints (x,y) by image:") | ||
| for img_path, xy in xy_preds.items(): | ||
| print(f"{img_path}: shape {xy.shape}") | ||
|
|
||
| # For debug case, the provided pose is already in Animal3D format (26 keypoints) | ||
| # So we skip the mapping step | ||
| if is_debug_case: | ||
| print("DEBUG MODE: Skipping keypoint mapping (already in Animal3D format)") | ||
| mapped_keypoints = xy_preds | ||
| else: | ||
| # now map the keypoints to a different set of keypoints (used in Animal3D) | ||
| # keypoint mapping from quadruped80K super keypotints to animal3d keypoints | ||
| keypoint_mapping = {"quadruped80k":[10, 5, -1, 26, 29, 30, 35, 22, 24, 27, 31, 32, -1, -1, 25, 28, 33, 34, 15, 23, 11, 6, 4, 3, 0, -1]} | ||
|
|
||
| # for the keypoint_mapping, -1 indicates that there is no corresponding keypoint in the source set, but we can interpolate | ||
| # for index 2, we can interpolate between keypoints 3 and 4 in the source set to get a better estimate of the missing keypoint | ||
| # for index 25, we can interpolate between keypoints 22 and 23 in the source set | ||
| # for index 12, we can interpolate between keypoints 24 and 19 in the source set | ||
| # for index 13, we can interpolate between keypoints 27 and 19 in the source set | ||
|
|
||
| # Define interpolation rules for -1 indices: {target_idx: (source_idx1, source_idx2)} | ||
| interpolation_rules = { | ||
| 2: (3, 4), # interpolate between source keypoints 3 and 4 | ||
| 12: (24, 19), # interpolate between source keypoints 24 and 19 | ||
| 13: (27, 19), # interpolate between source keypoints 27 and 19 | ||
| 25: (22, 23), # interpolate between source keypoints 22 and 23 | ||
| } | ||
|
|
||
| # map the keypoints | ||
| mapped_keypoints = {} | ||
| mapping_indices = keypoint_mapping["quadruped80k"] | ||
|
|
||
| for img_path, xy in xy_preds.items(): | ||
| # xy shape: (num_individuals, num_keypoints, 2) | ||
| num_individuals, num_keypoints, _ = xy.shape | ||
| num_target_keypoints = len(mapping_indices) | ||
|
|
||
| # Initialize mapped array with NaN or zeros | ||
| mapped_xy = np.full((num_individuals, num_target_keypoints, 2), np.nan) | ||
|
|
||
| for target_idx, source_idx in enumerate(mapping_indices): | ||
| if source_idx != -1 and source_idx < num_keypoints: | ||
| # Copy the keypoint from source to target position | ||
| mapped_xy[:, target_idx, :] = xy[:, source_idx, :] | ||
| elif source_idx == -1 and target_idx in interpolation_rules: | ||
| # Perform interpolation for -1 indices | ||
| src1, src2 = interpolation_rules[target_idx] | ||
| if src1 < num_keypoints and src2 < num_keypoints: | ||
| # Interpolate as the average of the two source keypoints | ||
| mapped_xy[:, target_idx, :] = (xy[:, src1, :] + xy[:, src2, :]) / 2.0 | ||
| print(f"Interpolated keypoint {target_idx} from source keypoints {src1} and {src2}") | ||
|
|
||
| mapped_keypoints[img_path] = mapped_xy | ||
| print(f"Mapped {img_path}: {xy.shape} -> {mapped_xy.shape}") | ||
|
|
||
| img_bgr = cv2.imread(path) | ||
| if img_bgr is None: | ||
| raise FileNotFoundError(f"Failed to read image: {path}") | ||
|
|
||
| # predict() returns (kpts (1, N, 26, 2), scores (1, N, 26), valid_mask (N,)). | ||
| kpts, _scores, _mask = estimator.predict(img_bgr[None]) | ||
| # Pack into the {img_path: (1, 26, 2)} format expected by the save/vis code below. | ||
| mapped_keypoints = {path: kpts[:, 0, :, :]} | ||
|
|
||
| print('Generating 2D pose successful!') | ||
|
|
||
|
|
@@ -259,7 +173,6 @@ def get_pose2D(path, output_dir, type): | |
| # Save in the same format as vis_in_the_wild.py for compatibility | ||
| output_npz = output_dir_2D + 'keypoints.npz' | ||
| np.savez_compressed(output_npz, reconstruction=mapped_xy) | ||
| print(f"Saved keypoints to {output_npz}") | ||
|
|
||
| # Also save as npy for backup | ||
| img_name = Path(img_path).stem | ||
|
|
@@ -275,7 +188,6 @@ def get_pose2D(path, output_dir, type): | |
| index=[f'keypoint_{i}' for i in range(mapped_xy.shape[1])] | ||
| ) | ||
| df.to_csv(csv_file) | ||
| print(f"Saved individual {ind_idx} keypoints to {csv_file}") | ||
|
|
||
| # Visualize mapped keypoints on image | ||
| img = Image.open(img_path) | ||
|
|
@@ -328,39 +240,38 @@ def get_pose2D(path, output_dir, type): | |
| plt.tight_layout() | ||
| plt.savefig(vis_file, dpi=150, bbox_inches='tight') | ||
| plt.close(fig) | ||
| print(f"Saved visualization to {vis_file}") | ||
|
|
||
|
|
||
| def get_pose3D(path, output_dir, type='image'): | ||
| """ | ||
| Generate 3D pose from 2D keypoints using the model. | ||
| This function reads the 2D keypoints saved by get_pose2D and generates 3D poses. | ||
| def build_3d_lifter(): | ||
| """Build the 3D lifter once and return (model, device). | ||
|
|
||
| Empty --saved_model_path -> auto-download fmpose3d_animals.pth from HF. | ||
| Non-empty path is used as a local override. | ||
| """ | ||
|
Comment on lines
+253
to
258
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! I have updated the docstring |
||
| print('\nGenerating 3D pose...') | ||
| print(f"args.n_joints: {args.n_joints}, args.out_joints: {args.out_joints}") | ||
|
|
||
| ## Reload model | ||
| from fmpose3d.utils.weights import resolve_weights_path | ||
|
|
||
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
| model = CFM(args).to(device) | ||
|
|
||
| model = {} | ||
| model['CFM'] = CFM(args).to(device) | ||
|
|
||
| model_dict = model['CFM'].state_dict() | ||
| model_path = args.saved_model_path | ||
| print(f"Loading model from: {model_path}") | ||
| model_path = resolve_weights_path(args.saved_model_path, f"{args.model_type}.pth") | ||
| print(f"[3D] lifter weights = {model_path}") | ||
| pre_dict = torch.load(model_path, map_location=device, weights_only=True) | ||
| for name, key in model_dict.items(): | ||
| model_dict = model.state_dict() | ||
| for name in model_dict: | ||
| model_dict[name] = pre_dict[name] | ||
| model['CFM'].load_state_dict(model_dict) | ||
| print("Model loaded successfully!") | ||
|
|
||
| model = model['CFM'].eval() | ||
| model.load_state_dict(model_dict) | ||
| return model.eval() | ||
|
|
||
|
|
||
| def get_pose3D(model, path, output_dir, type='image'): | ||
| """ | ||
| Generate 3D pose from 2D keypoints using the model. | ||
| Reads the 2D keypoints saved by get_pose2D and generates 3D poses. | ||
| """ | ||
| print('\nGenerating 3D pose...') | ||
|
|
||
| ## Load input 2D keypoints | ||
| keypoints = np.load(output_dir + 'input_2D/keypoints.npz', allow_pickle=True)['reconstruction'] | ||
| print(f"Loaded keypoints shape: {keypoints.shape}") | ||
|
|
||
| ## Generate 3D poses | ||
| if type == "image": | ||
| i = 0 | ||
| img = cv2.imread(path) | ||
|
|
@@ -422,9 +333,6 @@ def euler_sample(c_2d, y_local, steps, model_3d): | |
| return y_local | ||
|
|
||
| ## Estimation (without TTA for better results) | ||
| print("input_2D.shape:", input_2D.shape) | ||
| print("input_2D:", input_2D[0, 0]) | ||
|
|
||
| # Single inference without flip augmentation | ||
| # Create 3D random noise with shape (1, 1, J, 3) | ||
| y = torch.randn(input_2D.size(0), input_2D.size(1), input_2D.size(2), 3, device=device) | ||
|
|
@@ -492,7 +400,6 @@ def euler_sample(c_2d, y_local, steps, model_3d): | |
| output_dir_2D_img = output_dir + 'pose2D_on_image/' | ||
| os.makedirs(output_dir_2D_img, exist_ok=True) | ||
| cv2.imwrite(f'{output_dir_2D_img}{i:04d}_2d.png', img_copy) | ||
| print(f"Saved 2D pose on image to {output_dir_2D_img}{i:04d}_2d.png") | ||
|
|
||
| ## Save 3D pose as npz | ||
| output_dir_3D = output_dir + 'pose3D/' | ||
|
|
@@ -603,46 +510,46 @@ def img2gif(video_path, name, output_dir, duration=0.25): | |
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu | ||
|
|
||
| path = args.path # file path or folder path | ||
|
|
||
| # Check if path is a directory | ||
|
|
||
| # Build the 2D estimator and 3D lifter ONCE; reuse across all images/frames. | ||
| # This avoids redundant HF resolution and DLC/torch model reloads. | ||
| estimator_2d = build_2d_estimator() | ||
| model_3d = build_3d_lifter() | ||
|
|
||
| if os.path.isdir(path): | ||
| # Get all image files in the directory | ||
| image_extensions = ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.JPG', '*.JPEG', '*.PNG', '*.BMP'] | ||
| image_files = [] | ||
| for ext in image_extensions: | ||
| image_files.extend(glob.glob(os.path.join(path, ext))) | ||
| image_files.sort() | ||
|
|
||
| if len(image_files) == 0: | ||
| print(f"No image files found in {path}") | ||
| exit(0) | ||
|
|
||
| print(f"Found {len(image_files)} images in {path}") | ||
|
|
||
| # Process each image | ||
|
|
||
| for img_path in tqdm(image_files, desc="Processing images"): | ||
| filename = img_path.split('/')[-1].split('.')[0] | ||
| output_dir = './predictions/' + filename + '/' | ||
|
|
||
| print(f"\nProcessing: {img_path}") | ||
| get_pose2D(img_path, output_dir, args.type) | ||
| get_pose3D(img_path, output_dir, args.type) | ||
| get_pose2D(estimator_2d, img_path, output_dir, args.type) | ||
| get_pose3D(model_3d, img_path, output_dir, args.type) | ||
|
|
||
| print(f'\nAll {len(image_files)} images processed successfully!') | ||
| else: | ||
| # Single file processing | ||
| filename = path.split('/')[-1].split('.')[0] | ||
| output_dir = './predictions/' + filename + '/' | ||
|
|
||
| get_pose2D(path, output_dir, args.type) | ||
| get_pose3D(path, output_dir, args.type) | ||
| get_pose2D(estimator_2d, path, output_dir, args.type) | ||
| get_pose3D(model_3d, path, output_dir, args.type) | ||
|
|
||
| if args.type=="video": | ||
| if args.type == "video": | ||
| img2video(path, filename, output_dir) | ||
| img2gif(path, filename, output_dir) | ||
|
|
||
| print('Generating demo successful!') | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed now