-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestimator.go
315 lines (292 loc) · 8.31 KB
/
estimator.go
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
package openpose
import (
"errors"
"image"
"io/ioutil"
"math"
"os"
"path"
"sort"
"sync"
"github.com/bububa/openpose/gaussian"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
// PoseEstimator represents pose estimator instance
type PoseEstimator struct {
model *tf.SavedModel
modelPath string
modelName string
modelTags []string
modelType ModelType
sharpenSigma float64
minSize float64
scaleFactor float64
scaleTimes int
upsampleSize int
mutex sync.Mutex
}
// NewPoseEstimator returns a new TensorFlow PoseEstimator instance.
func NewPoseEstimator(modelPath string, modelType ModelType) *PoseEstimator {
return &PoseEstimator{
modelPath: modelPath,
modelType: modelType,
modelTags: []string{"serve"},
minSize: 5,
scaleFactor: 0.709,
scaleTimes: 5,
upsampleSize: 4,
sharpenSigma: DefaultSharpenSigma,
}
}
// SetSharpenSigma set sharpen sigma for image preprocessing
func (t *PoseEstimator) SetSharpenSigma(sigma float64) {
t.sharpenSigma = sigma
}
// Estimate returns estimated Humans in an image
func (t *PoseEstimator) Estimate(img image.Image, modelSize ModelSize) ([]Human, error) {
if err := t.LoadModel(); err != nil {
return nil, err
}
/*
for _, operation := range t.model.Graph.Operations() {
log.Println(operation.Name())
}
*/
preprocessedImage, normPadding := ImagePreprocess(img, modelSize, t.sharpenSigma)
return t.estimatePose(preprocessedImage, normPadding)
}
func (t *PoseEstimator) estimatePose(img image.Image, normPadding Size) ([]Human, error) {
pafMat, heatMat, err := t.getMats(img)
if err != nil {
return nil, err
}
heatMat = gaussian.ApplyFilter(heatMat, 5, 2.5)
//dump(pafMat, "./pafMat.json")
//dump(heatMat, "./heatMat.json")
nmsThreshold := math.Max(float64(matAverage(heatMat)*4), NMS_Threshold)
nmsThreshold = math.Min(nmsThreshold, 0.3)
//log.Printf("nms, th=%f, mat:%f\n", nmsThreshold, matAverage(heatMat))
scales := scales(float64(len(heatMat[0])), float64(len(heatMat[0][0])), t.scaleFactor, t.minSize)
coords := make([][2][]int, 0, TotalBodyParts)
nmsThresholdf32 := float32(nmsThreshold)
for _, plain := range heatMat[0:TotalBodyParts] {
nms, err := nonMaxSuppression(plain, scales[0], nmsThresholdf32)
if err != nil {
return nil, err
}
coords = append(coords, maxiumFilter(nms, nmsThresholdf32))
}
//dump(coords, "./coords.json")
// connect parts
var connections []Connection
connectionPool := &sync.Pool{
New: func() interface{} {
return new(Connection)
},
}
for idx, cocoPair := range CocoPairs {
pairNetwork := CocoPairsNetwork[idx]
conns := t.estimatePosePair(connectionPool, coords, cocoPair[0], cocoPair[1], pafMat[pairNetwork[0]], pafMat[pairNetwork[1]], heatMat, normPadding)
connections = append(connections, conns...)
}
heatMatRows := float64(len(heatMat[0]))
heatMatCols := float64(len(heatMat[0][0]))
return connectionsToHumans(connections, heatMatRows, heatMatCols), nil
}
func (t *PoseEstimator) estimatePosePair(connectionPool *sync.Pool, coords [][2][]int, part1 CocoPart, part2 CocoPart, pafMatX [][]float32, pafMatY [][]float32, heatMat [][][]float32, normPadding Size) []Connection {
peakCoord1, peakCoord2 := coords[part1], coords[part2]
var abovePairs = [][2]CocoPart{
{CocoPartRShoulder}, {CocoPartRElbow},
{CocoPartRElbow}, {CocoPartRWrist},
{CocoPartLShoulder}, {CocoPartLElbow},
{CocoPartLElbow}, {CocoPartLWrist},
}
var inAboveParts bool
for _, pair := range abovePairs {
if part1 == pair[0] && part1 == pair[1] {
inAboveParts = true
break
}
}
candidates := make([]Connection, 0, len(peakCoord1[0])*len(peakCoord2[0]))
for idx1, y1 := range peakCoord1[0] {
x1 := peakCoord1[1][idx1]
for idx2, y2 := range peakCoord2[0] {
x2 := peakCoord2[1][idx2]
x1f64, y1f64, x2f64, y2f64 := float64(x1), float64(y1), float64(x2), float64(y2)
score, count := t.getScore(x1f64, y1f64, x2f64, y2f64, pafMatX, pafMatY)
//log.Printf("part:%d-%d, score:%f, count:%d, p1:%d-%d, p2:%d-%d\n", part1, part2, score, count, x1, y1, x2, y2)
if inAboveParts && count < InterMinAboveThreshold {
continue
} else if !inAboveParts && (count < InterMinAboveThreshold || score <= 0) {
continue
}
candidate := connectionPool.Get().(*Connection)
candidate.NormPadding = normPadding
candidate.Score = score
candidate.Coords = [2]image.Point{
image.Pt(x1, y1),
image.Pt(x2, y2),
}
candidate.Idx = [2]int{idx1, idx2}
candidate.Parts = [2]CocoPart{part1, part2}
candidate.Scores = [2]float32{
heatMat[part1][y1][x1],
heatMat[part2][y2][x2],
}
candidate.UPartIdx = candidate.GetUPartIdx()
connectionPool.Put(candidate)
candidates = append(candidates, *candidate)
}
}
sort.SliceStable(candidates, func(i, j int) bool { return candidates[i].Score > candidates[j].Score })
connections := make([]Connection, 0, len(candidates))
var (
usedIdx1 = make(map[int]struct{}, len(peakCoord1[0]))
usedIdx2 = make(map[int]struct{}, len(peakCoord2[0]))
)
for _, candidate := range candidates {
// check not connected
_, found1 := usedIdx1[candidate.Idx[0]]
_, found2 := usedIdx2[candidate.Idx[1]]
if found1 || found2 {
continue
}
connections = append(connections, candidate)
usedIdx1[candidate.Idx[0]] = struct{}{}
usedIdx2[candidate.Idx[1]] = struct{}{}
}
return connections
}
func (t *PoseEstimator) getScore(x1, y1, x2, y2 float64, pafMatX, pafMatY [][]float32) (float32, int) {
dx, dy := x2-x1, y2-y1
normVec := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
if normVec < 1e-4 {
return 0, 0
}
vx, vy := float32(dx/normVec), float32(dy/normVec)
var (
numIter int = 10
numIterf float64 = 10
)
stepX, stepY := dx/numIterf, dy/numIterf
var (
xs = make([]int, 0, numIter)
ys = make([]int, 0, numIter)
)
var i int
xv, yv := x1, y1
for i < numIter {
xs = append(xs, int(xv+0.5))
ys = append(ys, int(yv+0.5))
if x1 != x2 {
xv += stepX
}
if y1 != y2 {
yv += stepY
}
i++
}
var (
score float32
count int
)
for idx, x := range xs {
y := ys[idx]
pafX := pafMatX[y][x]
pafY := pafMatY[y][x]
localScore := pafX*vx + pafY*vy
if localScore > InterThreashold {
score += localScore
count++
}
}
return score, count
}
func (t *PoseEstimator) getMats(img image.Image) ([][][]float32, [][][]float32, error) {
tensor, err := makeTensorFromImage(img)
if err != nil {
return nil, nil, err
}
var (
inOp map[tf.Output]*tf.Tensor
outOp []tf.Output
)
switch t.modelType {
case CMU:
inOp = map[tf.Output]*tf.Tensor{
t.model.Graph.Operation("image").Output(0): tensor,
}
outOp = []tf.Output{
t.model.Graph.Operation("Mconv7_stage6_L1/BiasAdd").Output(0),
t.model.Graph.Operation("Mconv7_stage6_L2/BiasAdd").Output(0),
}
case MobileNet:
inOp = map[tf.Output]*tf.Tensor{
t.model.Graph.Operation("image").Output(0): tensor,
}
outOp = []tf.Output{
t.model.Graph.Operation("Openpose/MConv_Stage6_L1_5_pointwise/BatchNorm/FusedBatchNorm").Output(0),
t.model.Graph.Operation("Openpose/MConv_Stage6_L2_5_pointwise/BatchNorm/FusedBatchNorm").Output(0),
}
default:
return nil, nil, errors.New("invalid model type")
}
output, err := t.model.Session.Run(
inOp,
outOp,
nil)
if err != nil {
return nil, nil, err
}
if len(output) != 2 {
return nil, nil, errors.New("inference failed, no output")
}
pafMat := output[0].Value().([][][][]float32)[0]
heatMat := output[1].Value().([][][][]float32)[0]
if output[1].Shape()[3] == 19 {
heatMat = rollAxis(heatMat, 2, 0)
}
if output[0].Shape()[3] == 38 {
pafMat = rollAxis(pafMat, 2, 0)
}
return pafMat, heatMat, nil
}
// ModelLoaded tests if the TensorFlow model is loaded.
func (t *PoseEstimator) ModelLoaded() bool {
return t.model != nil
}
// LoadModel load tensorfow model
func (t *PoseEstimator) LoadModel() error {
t.mutex.Lock()
defer t.mutex.Unlock()
if t.ModelLoaded() {
return nil
}
modelPath := path.Join(t.modelPath)
// Load model
//model, err := tf.LoadSavedModel(modelPath, t.modelTags, nil)
fn, err := os.Open(modelPath)
if err != nil {
return err
}
defer fn.Close()
graph := tf.NewGraph()
data, err := ioutil.ReadAll(fn)
if err != nil {
return err
}
if err := graph.Import(data, ""); err != nil {
return err
}
session, err := tf.NewSession(graph, nil)
if err != nil {
return err
}
model := &tf.SavedModel{
Graph: graph,
Session: session,
}
t.model = model
return nil
}