-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathblazehand_landmark.py
115 lines (92 loc) · 3.4 KB
/
blazehand_landmark.py
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from blazebase import BlazeLandmark, BlazeBlock
class BlazeHandLandmark(BlazeLandmark):
"""The hand landmark model from MediaPipe.
"""
def __init__(self):
super(BlazeHandLandmark, self).__init__()
# size of ROIs used for input
self.resolution = 256
self._define_layers()
def _define_layers(self):
self.backbone1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=24, kernel_size=3, stride=2, padding=0, bias=True),
nn.ReLU(inplace=True),
BlazeBlock(24, 24, 5),
BlazeBlock(24, 24, 5),
BlazeBlock(24, 48, 5, 2),
)
self.backbone2 = nn.Sequential(
BlazeBlock(48, 48, 5),
BlazeBlock(48, 48, 5),
BlazeBlock(48, 96, 5, 2),
)
self.backbone3 = nn.Sequential(
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5, 2),
)
self.backbone4 = nn.Sequential(
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5, 2),
)
self.blaze5 = BlazeBlock(96, 96, 5)
self.blaze6 = BlazeBlock(96, 96, 5)
self.conv7 = nn.Conv2d(96, 48, 1, bias=True)
self.backbone8 = nn.Sequential(
BlazeBlock(48, 48, 5),
BlazeBlock(48, 48, 5),
BlazeBlock(48, 48, 5),
BlazeBlock(48, 48, 5),
BlazeBlock(48, 96, 5, 2),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 96, 5),
BlazeBlock(96, 288, 5, 2),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5, 2),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5, 2),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5, 2),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
BlazeBlock(288, 288, 5),
)
self.hand_flag = nn.Conv2d(288, 1, 2, bias=True)
self.handed = nn.Conv2d(288, 1, 2, bias=True)
self.landmarks = nn.Conv2d(288, 63, 2, bias=True)
def forward(self, x):
if x.shape[0] == 0:
return torch.zeros((0,)), torch.zeros((0,)), torch.zeros((0, 21, 3))
x = F.pad(x, (0, 1, 0, 1), "constant", 0)
x = self.backbone1(x)
y = self.backbone2(x)
z = self.backbone3(y)
w = self.backbone4(z)
z = z + F.interpolate(w, scale_factor=2, mode='bilinear')
z = self.blaze5(z)
y = y + F.interpolate(z, scale_factor=2, mode='bilinear')
y = self.blaze6(y)
y = self.conv7(y)
x = x + F.interpolate(y, scale_factor=2, mode='bilinear')
x = self.backbone8(x)
hand_flag = self.hand_flag(x).view(-1).sigmoid()
handed = self.handed(x).view(-1).sigmoid()
landmarks = self.landmarks(x).view(-1, 21, 3) / 256
return hand_flag, handed, landmarks