-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathmodel.py
More file actions
66 lines (52 loc) · 2 KB
/
Copy pathmodel.py
File metadata and controls
66 lines (52 loc) · 2 KB
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
from __future__ import annotations
from dataclasses import dataclass
import torch
from torch import nn
@dataclass(frozen=True)
class ModelConfig:
in_channels: int = 1
hidden_channels: int = 24
num_blocks: int = 3
num_keypoints: int = 10
dropout: float = 0.0
class HandPoseRegressor(nn.Module):
def __init__(self, cfg: ModelConfig) -> None:
super().__init__()
in_ch = int(cfg.in_channels)
hidden = int(cfg.hidden_channels)
blocks: list[nn.Module] = []
for idx in range(int(cfg.num_blocks)):
out_ch = hidden * (2**idx)
blocks.extend(
[
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2),
]
)
in_ch = out_ch
self.backbone = nn.Sequential(*blocks)
self.head = nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(),
nn.Dropout(float(cfg.dropout)),
nn.Linear(in_ch, int(cfg.num_keypoints) * 2),
)
def forward(self, images: torch.Tensor) -> torch.Tensor:
features = self.backbone(images.to(torch.float32))
return torch.sigmoid(self.head(features))
def hand_pose_loss(predictions: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
return torch.nn.functional.mse_loss(predictions, targets)
def mean_pose_l2_pixels(predictions: torch.Tensor, targets: torch.Tensor, *, image_size: int) -> float:
with torch.no_grad():
pred_xy = predictions.reshape(predictions.shape[0], -1, 2) * float(image_size - 1)
target_xy = targets.reshape(targets.shape[0], -1, 2) * float(image_size - 1)
error = torch.linalg.vector_norm(pred_xy - target_xy, ord=2, dim=-1)
return float(error.mean().item())
__all__ = [
"HandPoseRegressor",
"ModelConfig",
"hand_pose_loss",
"mean_pose_l2_pixels",
]