-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathblazepalm.py
157 lines (124 loc) · 5.35 KB
/
blazepalm.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
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from blazebase import BlazeDetector, BlazeBlock
class BlazePalm(BlazeDetector):
"""The palm detection model from MediaPipe. """
def __init__(self):
super(BlazePalm, self).__init__()
# These are the settings from the MediaPipe example graph
# mediapipe/graphs/hand_tracking/subgraphs/hand_detection_gpu.pbtxt
self.num_classes = 1
self.num_anchors = 2944
self.num_coords = 18
self.score_clipping_thresh = 100.0
self.x_scale = 256.0
self.y_scale = 256.0
self.h_scale = 256.0
self.w_scale = 256.0
self.min_score_thresh = 0.5
self.min_suppression_threshold = 0.3
self.num_keypoints = 7
# These settings are for converting detections to ROIs which can then
# be extracted and feed into the landmark network
# use mediapipe/calculators/util/detections_to_rects_calculator.cc
self.detection2roi_method = 'box'
# mediapipe/graphs/hand_tracking/subgraphs/hand_detection_cpu.pbtxt
self.kp1 = 0
self.kp2 = 2
self.theta0 = np.pi/2
self.dscale = 2.6
self.dy = -0.5
self._define_layers()
def _define_layers(self):
self.backbone1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=2, padding=0, bias=True),
nn.ReLU(inplace=True),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 32),
BlazeBlock(32, 64, stride=2),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 64),
BlazeBlock(64, 128, stride=2),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
BlazeBlock(128, 128),
)
self.backbone2 = nn.Sequential(
BlazeBlock(128, 256, stride=2),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
)
self.backbone3 = nn.Sequential(
BlazeBlock(256, 256, stride=2),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
BlazeBlock(256, 256),
)
self.conv_transpose1 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=2, stride=2, padding=0, bias=True)
self.blaze1 = BlazeBlock(256, 256)
self.conv_transpose2 = nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=2, stride=2, padding=0, bias=True)
self.blaze2 = BlazeBlock(128, 128)
self.classifier_32 = nn.Conv2d(128, 2, 1, bias=True)
self.classifier_16 = nn.Conv2d(256, 2, 1, bias=True)
self.classifier_8 = nn.Conv2d(256, 6, 1, bias=True)
self.regressor_32 = nn.Conv2d(128, 36, 1, bias=True)
self.regressor_16 = nn.Conv2d(256, 36, 1, bias=True)
self.regressor_8 = nn.Conv2d(256, 108, 1, bias=True)
def forward(self, x):
b = x.shape[0] # batch size, needed for reshaping later
x = F.pad(x, (0, 1, 0, 1), "constant", 0)
x = self.backbone1(x) # (b, 128, 32, 32)
y = self.backbone2(x) # (b, 256, 16, 16)
z = self.backbone3(y) # (b, 256, 8, 8)
y = y + F.relu(self.conv_transpose1(z), True)
y = self.blaze1(y)
x = x + F.relu(self.conv_transpose2(y), True)
x = self.blaze2(x)
# Note: Because PyTorch is NCHW but TFLite is NHWC, we need to
# permute the output from the conv layers before reshaping it.
c1 = self.classifier_8(z) # (b, 2, 16, 16)
c1 = c1.permute(0, 2, 3, 1) # (b, 16, 16, 2)
c1 = c1.reshape(b, -1, 1) # (b, 512, 1)
c2 = self.classifier_16(y) # (b, 6, 8, 8)
c2 = c2.permute(0, 2, 3, 1) # (b, 8, 8, 6)
c2 = c2.reshape(b, -1, 1) # (b, 384, 1)
c3 = self.classifier_32(x) # (b, 6, 8, 8)
c3 = c3.permute(0, 2, 3, 1) # (b, 8, 8, 6)
c3 = c3.reshape(b, -1, 1) # (b, 384, 1)
c = torch.cat((c3, c2, c1), dim=1) # (b, 896, 1)
r1 = self.regressor_8(z) # (b, 32, 16, 16)
r1 = r1.permute(0, 2, 3, 1) # (b, 16, 16, 32)
r1 = r1.reshape(b, -1, 18) # (b, 512, 16)
r2 = self.regressor_16(y) # (b, 96, 8, 8)
r2 = r2.permute(0, 2, 3, 1) # (b, 8, 8, 96)
r2 = r2.reshape(b, -1, 18) # (b, 384, 16)
r3 = self.regressor_32(x) # (b, 96, 8, 8)
r3 = r3.permute(0, 2, 3, 1) # (b, 8, 8, 96)
r3 = r3.reshape(b, -1, 18) # (b, 384, 16)
r = torch.cat((r3, r2, r1), dim=1) # (b, 896, 16)
return [r, c]