File size: 18,164 Bytes
519ab98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | #!/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)
@torch.no_grad()
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)
@torch.no_grad()
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,
)
|