Download code/resnet_point_feature.py from qinxunhui/deepellipse-circleid: direct link, hf CLI and curl.
- Browser
- Download file 18.2 kB
-
https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/resnet_point_feature.py
- Command line
-
hf download hf://qinxunhui/deepellipse-circleid/code/resnet_point_feature.py
-
curl -L -o resnet_point_feature.py https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/resnet_point_feature.py
18.2 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| @Time : 2026/4/3 11:44 | |
| @File : resnet_point_feature.py | |
| @Description : 基于 ResNet 特征图位置的深度特征提取;支持单张/批量多图多点采样。 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import os | |
| import random | |
| from pathlib import Path | |
| from typing import List, Optional, Sequence, Tuple, Union | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from torchvision import models, transforms | |
| from elipse_feature import ( | |
| DEFAULT_IMG_SIZE, | |
| DEFAULT_N_THETA, | |
| EllipseFeatureBundle, | |
| extract_ellipse_features, | |
| preprocess_rgb_to_skeleton, | |
| ) | |
| PathLike = Union[str, Path, np.ndarray] | |
| RESNET_DEEP_RAW_DIM = 448 # layer1(64) + layer2(128) + layer3(256) | |
| RESNET_CHECKPOINT_WINDOWS = "model/model_epoch_60_image.pth" | |
| RESNET_CHECKPOINT_LINUX = "checkpoints/model_resnet/resnet_rgb_0.49_0.48.pth" | |
| def default_resnet_checkpoint() -> str: | |
| """§4.1.3 ResNet 权重:Windows 用 model/,Linux 等用 checkpoints/model_resnet/。""" | |
| if os.name == "nt": | |
| return RESNET_CHECKPOINT_WINDOWS | |
| return RESNET_CHECKPOINT_LINUX | |
| DEFAULT_CHECKPOINT = Path(default_resnet_checkpoint()) | |
| IMAGENET_MEAN = [0.485, 0.456, 0.406] | |
| IMAGENET_STD = [0.229, 0.224, 0.225] | |
| def _default_device() -> torch.device: | |
| return torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| def _rgb_bgr_to_tensor( | |
| rgb_bgr: np.ndarray, | |
| device: Optional[torch.device] = None, | |
| ) -> Tuple[torch.Tensor, int, int]: | |
| """BGR uint8 图像 → 归一化 Tensor [1, 3, H, W]。""" | |
| device = device or _default_device() | |
| rgb = cv2.cvtColor(rgb_bgr, cv2.COLOR_BGR2RGB) | |
| orig_h, orig_w = rgb.shape[:2] | |
| transform = transforms.Compose([ | |
| transforms.ToPILImage(), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD), | |
| ]) | |
| tensor = transform(rgb).unsqueeze(0).to(device) | |
| return tensor, orig_h, orig_w | |
| def _batch_rgb_bgr_to_tensor( | |
| rgb_bgr_list: Sequence[np.ndarray], | |
| device: Optional[torch.device] = None, | |
| ) -> torch.Tensor: | |
| """多张 BGR 图像(需同尺寸)→ [B, 3, H, W]。""" | |
| device = device or _default_device() | |
| tensors = [_rgb_bgr_to_tensor(img, device)[0] for img in rgb_bgr_list] | |
| return torch.cat(tensors, dim=0) | |
| def keypoints_from_ellipse_bundle(bundle: EllipseFeatureBundle) -> np.ndarray: | |
| """从椭圆特征 bundle 取 ResNet 采样坐标 s_base_px,形状 (N_theta, 2),顺序 (x, y)。""" | |
| return bundle.sequence.s_base_px.astype(np.float32) | |
| def extract_ellipse_keypoints_from_bgr( | |
| rgb_bgr: np.ndarray, | |
| img_size: int = DEFAULT_IMG_SIZE, | |
| n_theta: int = DEFAULT_N_THETA, | |
| min_valid: int = 180, | |
| ) -> Optional[Tuple[np.ndarray, np.ndarray, EllipseFeatureBundle]]: | |
| """ | |
| BGR 原图 → 骨架化 → 椭圆序列 → 关键点坐标。 | |
| Returns | |
| ------- | |
| rgb_resized, keypoints_xy, bundle | |
| rgb_resized: resize 后的 BGR,与 keypoints 坐标系一致 | |
| keypoints_xy: (N_theta, 2),像素坐标 (x, y) | |
| """ | |
| prep = preprocess_rgb_to_skeleton(rgb_bgr, size=img_size) | |
| bundle = extract_ellipse_features( | |
| prep["skeleton"], | |
| n_theta=n_theta, | |
| half_norm=img_size / 2.0, | |
| compute_fft=False, | |
| ) | |
| valid_count = int(np.sum(bundle.sequence.interp_flag == 0)) | |
| if valid_count < min_valid: | |
| return None | |
| return prep["rgb"], keypoints_from_ellipse_bundle(bundle), bundle | |
| def extract_ellipse_keypoints_from_path( | |
| image_path: PathLike, | |
| **kwargs, | |
| ) -> Optional[Tuple[np.ndarray, np.ndarray, EllipseFeatureBundle]]: | |
| bgr = cv2.imread(str(image_path), cv2.IMREAD_COLOR) | |
| if bgr is None: | |
| raise FileNotFoundError(str(image_path)) | |
| return extract_ellipse_keypoints_from_bgr(bgr, **kwargs) | |
| class ResNetFeatureExtractor(nn.Module): | |
| """基于 ResNet18 多层特征图的双线性插值采样器。""" | |
| def __init__( | |
| self, | |
| checkpoint_path: Optional[PathLike] = None, | |
| device: Optional[torch.device] = None, | |
| ): | |
| super().__init__() | |
| self.device = device or _default_device() | |
| resnet = models.resnet18(weights=None) | |
| ckpt = Path(checkpoint_path) if checkpoint_path else DEFAULT_CHECKPOINT | |
| if ckpt.is_file(): | |
| state = torch.load(ckpt, map_location=self.device, weights_only=False) | |
| state_dict = state.get("model_state_dict", state) | |
| cleaned = {} | |
| for k, v in state_dict.items(): | |
| name = k[7:] if k.startswith("module.") else k | |
| cleaned[name] = v | |
| resnet.load_state_dict(cleaned, strict=False) | |
| print(f"[ok] loaded checkpoint: {ckpt}") | |
| else: | |
| print(f"[warn] checkpoint not found: {ckpt}, using random init") | |
| self.conv1 = resnet.conv1 | |
| self.bn1 = resnet.bn1 | |
| self.relu = resnet.relu | |
| self.maxpool = resnet.maxpool | |
| self.layer1 = resnet.layer1 | |
| self.layer2 = resnet.layer2 | |
| self.layer3 = resnet.layer3 | |
| self.layer4 = resnet.layer4 | |
| self.feature_layers = nn.ModuleDict({ | |
| "layer1": self.layer1, | |
| "layer2": self.layer2, | |
| "layer3": self.layer3, | |
| }) | |
| self.strides = {"layer1": 4, "layer2": 8, "layer3": 16} | |
| self.out_channels = {"layer1": 64, "layer2": 128, "layer3": 256} | |
| self.total_dim = sum(self.out_channels.values()) | |
| def forward(self, images: torch.Tensor, keypoints: torch.Tensor) -> torch.Tensor: | |
| """ | |
| Parameters | |
| ---------- | |
| images : [B, 3, H, W] | |
| keypoints : [B, N, 2],像素坐标 (x, y) | |
| Returns | |
| ------- | |
| [B, N, total_dim] | |
| """ | |
| x = self.conv1(images) | |
| x = self.bn1(x) | |
| x = self.relu(x) | |
| x = self.maxpool(x) | |
| feature_maps = {} | |
| for name, layer in self.feature_layers.items(): | |
| x = layer(x) | |
| feature_maps[name] = x | |
| sampled_features_list = [] | |
| for name, fmap in feature_maps.items(): | |
| stride = self.strides[name] | |
| _, _, fh, fw = fmap.shape | |
| scaled_kps = keypoints / stride | |
| if fw > 1: | |
| norm_x = (scaled_kps[..., 0] / (fw - 1)) * 2 - 1 | |
| else: | |
| norm_x = torch.zeros_like(scaled_kps[..., 0]) | |
| if fh > 1: | |
| norm_y = (scaled_kps[..., 1] / (fh - 1)) * 2 - 1 | |
| else: | |
| norm_y = torch.zeros_like(scaled_kps[..., 1]) | |
| norm_x = torch.clamp(norm_x, -1, 1) | |
| norm_y = torch.clamp(norm_y, -1, 1) | |
| grid = torch.stack([norm_x, norm_y], dim=-1).unsqueeze(2) | |
| sampled = F.grid_sample( | |
| fmap, grid, mode="bilinear", align_corners=True, padding_mode="border" | |
| ) | |
| sampled = sampled.squeeze(-1).permute(0, 2, 1) | |
| sampled_features_list.append(sampled) | |
| return torch.cat(sampled_features_list, dim=-1) | |
| def extract_resnet_point_features( | |
| rgb_bgr: np.ndarray, | |
| points_xy: np.ndarray, | |
| extractor: Optional[ResNetFeatureExtractor] = None, | |
| checkpoint_path: Optional[PathLike] = None, | |
| device: Optional[torch.device] = None, | |
| ) -> torch.Tensor: | |
| """单张图多点采样,返回 (N, RESNET_DEEP_RAW_DIM)。""" | |
| device = device or _default_device() | |
| if extractor is None: | |
| extractor = ResNetFeatureExtractor(checkpoint_path=checkpoint_path, device=device) | |
| extractor.to(device) | |
| extractor.eval() | |
| image_tensor, _, _ = _rgb_bgr_to_tensor(rgb_bgr, device) | |
| kps = torch.from_numpy(points_xy.astype(np.float32)).unsqueeze(0).to(device) | |
| _, _, h, w = image_tensor.shape | |
| kps[..., 0] = kps[..., 0].clamp(0, w - 1) | |
| kps[..., 1] = kps[..., 1].clamp(0, h - 1) | |
| return extractor(image_tensor, kps).squeeze(0) | |
| def extract_resnet_point_features_batch( | |
| rgb_bgr_list: Sequence[np.ndarray], | |
| points_xy_list: Sequence[np.ndarray], | |
| extractor: Optional[ResNetFeatureExtractor] = None, | |
| checkpoint_path: Optional[PathLike] = None, | |
| device: Optional[torch.device] = None, | |
| ) -> torch.Tensor: | |
| """ | |
| 批量多图多点采样。 | |
| Parameters | |
| ---------- | |
| rgb_bgr_list : 长度 B,每张图 resize 后尺寸需一致 | |
| points_xy_list : 长度 B,每项 (N, 2);通常 N=360 | |
| Returns | |
| ------- | |
| (B, N, RESNET_DEEP_RAW_DIM) | |
| """ | |
| if len(rgb_bgr_list) != len(points_xy_list): | |
| raise ValueError("rgb_bgr_list 与 points_xy_list 长度须一致") | |
| if not rgb_bgr_list: | |
| raise ValueError("输入列表为空") | |
| device = device or _default_device() | |
| if extractor is None: | |
| extractor = ResNetFeatureExtractor(checkpoint_path=checkpoint_path, device=device) | |
| extractor.to(device) | |
| extractor.eval() | |
| images = _batch_rgb_bgr_to_tensor(rgb_bgr_list, device) | |
| _, _, h, w = images.shape | |
| kps = torch.stack([ | |
| torch.from_numpy(pts.astype(np.float32)) for pts in points_xy_list | |
| ], dim=0).to(device) | |
| kps[..., 0] = kps[..., 0].clamp(0, w - 1) | |
| kps[..., 1] = kps[..., 1].clamp(0, h - 1) | |
| return extractor(images, kps) | |
| def project_deep_features( | |
| raw_features: torch.Tensor, | |
| proj: nn.Linear, | |
| ) -> torch.Tensor: | |
| """§4.1.3 线性投影:(*, RESNET_DEEP_RAW_DIM) → (*, d_deep)。""" | |
| return proj(raw_features) | |
| def draw_keypoints_on_bgr( | |
| bgr: np.ndarray, | |
| keypoints_xy: np.ndarray, | |
| interp_flag: Optional[np.ndarray] = None, | |
| step: int = 1, | |
| point_radius: int = 2, | |
| draw_index: bool = False, | |
| draw_trajectory: bool = True, | |
| bundle: Optional[EllipseFeatureBundle] = None, | |
| ) -> np.ndarray: | |
| """ | |
| 在 BGR 图像上绘制 kps_list 中的采样点。 | |
| Parameters | |
| ---------- | |
| bgr : 与 keypoints_xy 同一坐标系的图像(通常为 resize 后的 rgb) | |
| keypoints_xy : (N, 2),(x, y) 像素坐标 | |
| interp_flag : (N,) 可选,1=插值点,0=实测点;None 则全部同色 | |
| """ | |
| vis = bgr.copy() | |
| h, w = vis.shape[:2] | |
| n = keypoints_xy.shape[0] | |
| if bundle is not None: | |
| box = cv2.boxPoints(bundle.ellipse_cv) | |
| cv2.polylines(vis, [np.int32(box)], True, (0, 200, 0), 1, cv2.LINE_AA) | |
| if draw_trajectory and n > 1: | |
| pts = [] | |
| for t in range(0, n, max(1, step)): | |
| x, y = int(round(keypoints_xy[t, 0])), int(round(keypoints_xy[t, 1])) | |
| if 0 <= x < w and 0 <= y < h: | |
| pts.append([x, y]) | |
| if len(pts) > 1: | |
| cv2.polylines(vis, [np.int32(pts)], False, (255, 180, 0), 1, cv2.LINE_AA) | |
| for t in range(0, n, max(1, step)): | |
| x, y = int(round(keypoints_xy[t, 0])), int(round(keypoints_xy[t, 1])) | |
| if not (0 <= x < w and 0 <= y < h): | |
| continue | |
| if interp_flag is not None and interp_flag[t] == 1: | |
| color = (0, 165, 255) # 插值:橙 | |
| else: | |
| color = (0, 0, 255) # 实测:红 | |
| cv2.circle(vis, (x, y), point_radius, color, -1, cv2.LINE_AA) | |
| if t == 0: | |
| cv2.circle(vis, (x, y), point_radius + 2, (0, 255, 0), 1, cv2.LINE_AA) | |
| if draw_index and t % max(1, step * 4) == 0: | |
| cv2.putText( | |
| vis, str(t), (x + 2, y - 2), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255, 255, 255), 1, cv2.LINE_AA, | |
| ) | |
| return vis | |
| def scale_keypoints_to_image( | |
| keypoints_xy: np.ndarray, | |
| src_shape: Tuple[int, int], | |
| dst_shape: Tuple[int, int], | |
| ) -> np.ndarray: | |
| """将关键点从 src 图像坐标系缩放到 dst 图像坐标系。shape 为 (H, W)。""" | |
| src_h, src_w = src_shape | |
| dst_h, dst_w = dst_shape | |
| scaled = keypoints_xy.astype(np.float32).copy() | |
| scaled[:, 0] *= dst_w / src_w | |
| scaled[:, 1] *= dst_h / src_h | |
| return scaled | |
| def visualize_kps_list( | |
| rgb_list: Sequence[np.ndarray], | |
| kps_list: Sequence[np.ndarray], | |
| out_dir: Union[str, Path], | |
| orig_bgr_list: Optional[Sequence[np.ndarray]] = None, | |
| bundles: Optional[Sequence[EllipseFeatureBundle]] = None, | |
| names: Optional[Sequence[str]] = None, | |
| step: int = 2, | |
| ) -> List[Path]: | |
| """ | |
| 将 kps_list 画到对应图像上并保存。 | |
| 每张图输出两版: | |
| - *_kps_proc.png : resize 后输入图(与 ResNet 采样坐标一致) | |
| - *_kps_orig.png : 原始分辨率图(关键点已按比例缩放) | |
| """ | |
| out_root = Path(out_dir) | |
| out_root.mkdir(parents=True, exist_ok=True) | |
| saved: List[Path] = [] | |
| for i, (rgb, kps) in enumerate(zip(rgb_list, kps_list)): | |
| name = names[i] if names else f"sample_{i:03d}" | |
| stem = Path(name).stem if "/" in name or "\\" in name else name | |
| interp = None | |
| bundle = bundles[i] if bundles is not None else None | |
| if bundle is not None: | |
| interp = bundle.sequence.interp_flag | |
| proc_vis = draw_keypoints_on_bgr( | |
| rgb, kps, interp_flag=interp, step=step, bundle=bundle, | |
| ) | |
| proc_path = out_root / f"{stem}_kps_proc.png" | |
| cv2.imwrite(str(proc_path), proc_vis) | |
| saved.append(proc_path) | |
| if orig_bgr_list is not None: | |
| orig = orig_bgr_list[i] | |
| kps_orig = scale_keypoints_to_image( | |
| kps, rgb.shape[:2], orig.shape[:2], | |
| ) | |
| orig_vis = draw_keypoints_on_bgr( | |
| orig, kps_orig, interp_flag=interp, step=step, | |
| point_radius=max(2, int(round(min(orig.shape[:2]) / 80))), | |
| bundle=None, | |
| ) | |
| orig_path = out_root / f"{stem}_kps_orig.png" | |
| cv2.imwrite(str(orig_path), orig_vis) | |
| saved.append(orig_path) | |
| return saved | |
| def run_batch_test( | |
| image_dir: Union[str, Path] = "data/images", | |
| n_samples: int = 3, | |
| seed: int = 42, | |
| img_size: int = DEFAULT_IMG_SIZE, | |
| checkpoint_path: Optional[PathLike] = None, | |
| out_dir: Union[str, Path] = "outputs/resnet_point_feat_test", | |
| ) -> None: | |
| """从目录随机抽多张图,用椭圆特征关键点批量提取 ResNet 深度特征。""" | |
| root = Path(image_dir) | |
| paths = sorted(root.glob("*.png")) + sorted(root.glob("*.jpg")) | |
| if not paths: | |
| raise FileNotFoundError(f"目录下无图像: {image_dir}") | |
| random.seed(seed) | |
| picks = random.sample(paths, min(n_samples, len(paths))) | |
| device = _default_device() | |
| print(f"[device] {device}") | |
| print(f"[images] ({len(picks)}): {[p.name for p in picks]}") | |
| rgb_list: List[np.ndarray] = [] | |
| kps_list: List[np.ndarray] = [] | |
| orig_bgr_list: List[np.ndarray] = [] | |
| bundle_list: List[EllipseFeatureBundle] = [] | |
| meta: List[dict] = [] | |
| for p in picks: | |
| orig_bgr = cv2.imread(str(p), cv2.IMREAD_COLOR) | |
| if orig_bgr is None: | |
| print(f" [skip] {p.name}: cannot read image") | |
| continue | |
| result = extract_ellipse_keypoints_from_bgr(orig_bgr, img_size=img_size) | |
| if result is None: | |
| print(f" [skip] {p.name}: valid ellipse points insufficient") | |
| continue | |
| rgb, kps, bundle = result | |
| valid = int(np.sum(bundle.sequence.interp_flag == 0)) | |
| rgb_list.append(rgb) | |
| kps_list.append(kps) | |
| orig_bgr_list.append(orig_bgr) | |
| bundle_list.append(bundle) | |
| meta.append({"path": str(p), "valid_points": valid, "n_theta": kps.shape[0]}) | |
| print(f" [ok] {p.name}: {kps.shape[0]} pts, measured {valid}") | |
| if not rgb_list: | |
| raise RuntimeError("无可用样本,批量测试失败") | |
| out_root = Path(out_dir) | |
| out_root.mkdir(parents=True, exist_ok=True) | |
| viz_paths = visualize_kps_list( | |
| rgb_list, | |
| kps_list, | |
| out_dir=out_root / "kps_vis", | |
| orig_bgr_list=orig_bgr_list, | |
| bundles=bundle_list, | |
| names=[Path(m["path"]).name for m in meta], | |
| step=2, | |
| ) | |
| print(f"[viz] saved {len(viz_paths)} keypoint overlay images -> {out_root / 'kps_vis'}") | |
| for vp in viz_paths: | |
| print(f" {vp.name}") | |
| extractor = ResNetFeatureExtractor(checkpoint_path=checkpoint_path, device=device) | |
| extractor.to(device) | |
| extractor.eval() | |
| features = extract_resnet_point_features_batch( | |
| rgb_list, kps_list, extractor=extractor, device=device | |
| ) | |
| print("\n[batch result]") | |
| print(f" batch size: {len(rgb_list)}") | |
| print(f" keypoints (logical): [{features.shape[0]}, {features.shape[1]}, 2]") | |
| print(f" features: {tuple(features.shape)} -> [B, N, {RESNET_DEEP_RAW_DIM}]") | |
| print(f" expected dim: {extractor.total_dim}") | |
| if features.shape[-1] == extractor.total_dim: | |
| print("[ok] dimension match") | |
| else: | |
| print("[fail] dimension mismatch") | |
| for i, m in enumerate(meta): | |
| fi = features[i] | |
| print( | |
| f" [{i}] {Path(m['path']).name}: " | |
| f"feat mean={fi.mean().item():.4f}, std={fi.std().item():.4f}" | |
| ) | |
| out_root = Path(out_dir) | |
| out_root.mkdir(parents=True, exist_ok=True) | |
| np.save(out_root / "batch_deep_features.npy", features.cpu().numpy()) | |
| np.save(out_root / "batch_keypoints.npy", np.stack(kps_list, axis=0)) | |
| with open(out_root / "batch_meta.txt", "w", encoding="utf-8") as f: | |
| for i, m in enumerate(meta): | |
| f.write(f"{i}\t{m['path']}\tvalid={m['valid_points']}\n") | |
| print(f"[saved] {out_root}") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="ResNet 多点深度特征批量测试") | |
| parser.add_argument("--image-dir", default="data/images") | |
| parser.add_argument("--n", type=int, default=30, help="随机测试图像数量") | |
| parser.add_argument("--seed", type=int, default=42) | |
| parser.add_argument("--img-size", type=int, default=DEFAULT_IMG_SIZE) | |
| parser.add_argument("--checkpoint", default=None) | |
| parser.add_argument("--out-dir", default="outputs/resnet_point_feat_test") | |
| args = parser.parse_args() | |
| run_batch_test( | |
| image_dir=args.image_dir, | |
| n_samples=args.n, | |
| seed=args.seed, | |
| img_size=args.img_size, | |
| checkpoint_path=args.checkpoint, | |
| out_dir=args.out_dir, | |
| ) | |