File size: 9,523 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ConvNeXt-Tiny 多层特征图双线性插值深度点特征(§4.1.3)。
采样 stage1–3(stride 4/8/16),拼接 672 维 raw 特征。
"""
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

from elipse_feature import (
    DEFAULT_IMG_SIZE,
    DEFAULT_N_THETA,
    EllipseFeatureBundle,
    extract_ellipse_features,
    preprocess_rgb_to_skeleton,
)
from resnet_point_feature import (
    _batch_rgb_bgr_to_tensor,
    _rgb_bgr_to_tensor,
    extract_ellipse_keypoints_from_bgr,
)

PathLike = Union[str, Path, np.ndarray]

CONVNEXT_DEEP_RAW_DIM = 672  # stage1(96) + stage2(192) + stage3(384)

CONVNEXT_CHECKPOINT_WINDOWS = "model/model_epoch_60_convnext_image.pth"
CONVNEXT_CHECKPOINT_LINUX = "./checkpoints/0619_convnext_rgb_aug/model_epoch60_best_0.517.pth"


def default_convnext_checkpoint() -> str:
    if os.name == "nt":
        return CONVNEXT_CHECKPOINT_WINDOWS
    return CONVNEXT_CHECKPOINT_LINUX


DEFAULT_CHECKPOINT = Path(default_convnext_checkpoint())


def _default_device() -> torch.device:
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


def _load_features_only(model: nn.Module, state_dict: dict) -> int:
    cleaned = {}
    for k, v in state_dict.items():
        name = k[7:] if k.startswith("module.") else k
        if name.startswith("features.") or name.startswith("backbone.features."):
            key = name.split("backbone.", 1)[-1] if "backbone." in name else name
            if key.startswith("features."):
                cleaned[key] = v
    if not cleaned:
        for k, v in state_dict.items():
            name = k[7:] if k.startswith("module.") else k
            if ".features." in name:
                suffix = name.split(".features.", 1)[1]
                cleaned[f"features.{suffix}"] = v
    missing, unexpected = model.load_state_dict(cleaned, strict=False)
    loaded = len(cleaned)
    if loaded:
        print(f"[ok] loaded {loaded} feature tensors (strict=False)")
    if unexpected:
        print(f"[info] unexpected keys ignored: {len(unexpected)}")
    return loaded


class ConvNeXtFeatureExtractor(nn.Module):
    """ConvNeXt-Tiny stage1–3 双线性插值采样(stride 4/8/16)。"""

    def __init__(
        self,
        checkpoint_path: Optional[PathLike] = None,
        device: Optional[torch.device] = None,
    ):
        super().__init__()
        self.device = device or _default_device()
        backbone = models.convnext_tiny(weights=None)
        self.features = backbone.features

        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)
            n = _load_features_only(self, state_dict)
            if n == 0:
                print(f"[warn] no features.* in {ckpt}, using random init")
            else:
                print(f"[ok] loaded checkpoint: {ckpt}")
        else:
            print(f"[warn] checkpoint not found: {ckpt}, using random init")

        self.strides = {"s1": 4, "s2": 8, "s3": 16}
        self.out_channels = {"s1": 96, "s2": 192, "s3": 384}
        self.total_dim = sum(self.out_channels.values())

    def _forward_feature_maps(self, images: torch.Tensor) -> dict:
        x = self.features[0](images)
        x = self.features[1](x)
        fmaps = {"s1": x}
        x = self.features[2](x)
        x = self.features[3](x)
        fmaps["s2"] = x
        x = self.features[4](x)
        x = self.features[5](x)
        fmaps["s3"] = x
        return fmaps

    def forward(self, images: torch.Tensor, keypoints: torch.Tensor) -> torch.Tensor:
        """
        images : [B, 3, H, W]
        keypoints : [B, N, 2] 像素坐标 (x, y)
        returns : [B, N, total_dim]
        """
        feature_maps = self._forward_feature_maps(images)
        sampled_features_list = []
        for name in ("s1", "s2", "s3"):
            fmap = feature_maps[name]
            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_convnext_point_features(
    rgb_bgr: np.ndarray,
    points_xy: np.ndarray,
    extractor: Optional[ConvNeXtFeatureExtractor] = None,
    checkpoint_path: Optional[PathLike] = None,
    device: Optional[torch.device] = None,
) -> torch.Tensor:
    device = device or _default_device()
    if extractor is None:
        extractor = ConvNeXtFeatureExtractor(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_convnext_point_features_batch(
    rgb_bgr_list: Sequence[np.ndarray],
    points_xy_list: Sequence[np.ndarray],
    extractor: Optional[ConvNeXtFeatureExtractor] = None,
    checkpoint_path: Optional[PathLike] = None,
    device: Optional[torch.device] = None,
) -> torch.Tensor:
    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 = ConvNeXtFeatureExtractor(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 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/convnext_point_feat_test",
) -> None:
    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}")

    rgb_list: List[np.ndarray] = []
    kps_list: List[np.ndarray] = []

    for p in picks:
        orig_bgr = cv2.imread(str(p), cv2.IMREAD_COLOR)
        if orig_bgr is None:
            continue
        result = extract_ellipse_keypoints_from_bgr(orig_bgr, img_size=img_size)
        if result is None:
            print(f"  [skip] {p.name}")
            continue
        rgb, kps, _ = result
        rgb_list.append(rgb)
        kps_list.append(kps)
        print(f"  [ok] {p.name}: {kps.shape[0]} pts")

    if not rgb_list:
        raise RuntimeError("无可用样本")

    extractor = ConvNeXtFeatureExtractor(checkpoint_path=checkpoint_path, device=device)
    extractor.to(device)
    extractor.eval()
    features = extract_convnext_point_features_batch(
        rgb_list, kps_list, extractor=extractor, device=device,
    )

    print(f"[batch] features {tuple(features.shape)} expected dim {CONVNEXT_DEEP_RAW_DIM}")
    out_root = Path(out_dir)
    out_root.mkdir(parents=True, exist_ok=True)
    np.save(out_root / "batch_deep_features.npy", features.cpu().numpy())


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="ConvNeXt 多点深度特征测试")
    parser.add_argument("--image-dir", default="data/images")
    parser.add_argument("--n", type=int, default=3)
    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/convnext_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,
    )