UnityGiles commited on
Commit
a6cf8fd
1 Parent(s): 67b0fb4
Files changed (1) hide show
  1. README.md +85 -0
README.md CHANGED
@@ -1,3 +1,88 @@
1
  ---
2
  license: apache-2.0
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ library_name: unity-sentis
4
+ pipeline_tag: object-detection
5
  ---
6
+
7
+ # BlazePose in Sentis
8
+
9
+ BlazePose is a fast, light-weight hand detector from Google Research. Pretrained models are available as part of Google's [MediaPipe](https://ai.google.dev/edge/mediapipe/solutions/vision/hand_landmarker) framework.
10
+
11
+ ![](./images/pose.jpg)
12
+
13
+ The BlazePose models have been converted from TFLite to ONNX for use in Sentis using [tf2onnx](https://github.com/onnx/tensorflow-onnx) with the default export parameters. Three variants of the landmarker model (lite, full, heavy) are provided which can be interchanged. The larger models may provide more accurate results but take longer to run.
14
+
15
+ ## Functional API
16
+
17
+ The BlazePose detector model takes a (1, 224, 224, 3) input image tensor and outputs a (1, 2254, 12) boxes tensor and a (1, 2254, 1) scores tensor.
18
+
19
+ Each of the 2254 boxes consists of:
20
+ - [x position, y position, width, height] for the head bounding box. The position is relative to the anchor position for the given index, these are precalculated and loaded from a csv file.
21
+ - [x position, y position] for each of 4 body keypoints relative to the anchor position.
22
+
23
+ We adapt the model using the Sentis functional API to apply arg max to filter the box with the highest score.
24
+ ```
25
+ var detectionScores = ScoreFiltering(rawScores, 100f); // (1, 2254, 1)
26
+ var bestScoreIndex = Functional.ArgMax(rawScores, 1).Squeeze();
27
+
28
+ var selectedBoxes = Functional.IndexSelect(rawBoxes, 1, bestScoreIndex).Unsqueeze(0); // (1, 1, 12)
29
+ var selectedScores = Functional.IndexSelect(detectionScores, 1, bestScoreIndex).Unsqueeze(0); // (1, 1, 1)
30
+ ```
31
+
32
+ The BlazePose landmarker model takes a (1, 256, 256, 3) input image tensor cropped to the detected body and outputs a (1, 165) tensor consisting of the x, y, and z coordinates and visibility and presence for each of 33 pose keypoints. We use this model without adaptation.
33
+
34
+ ## Model inference
35
+
36
+ We use the dimensions of the texture to set up an affine transformation matrix to go from the 224x224 tensor coordinates to the image coordinates. We then fill the input tensor using a compute shader with this affine transformation, points outside the image will correspond to zeros in the input tensor.
37
+ ```
38
+ var size = Mathf.Max(texture.width, texture.height);
39
+
40
+ // The affine transformation matrix to go from tensor coordinates to image coordinates
41
+ var scale = size / (float)detectorInputSize;
42
+ var M = BlazeUtils.mul(BlazeUtils.TranslationMatrix(0.5f * (new Vector2(texture.width, texture.height) + new Vector2(-size, size))), BlazeUtils.ScaleMatrix(new Vector2(scale, -scale)));
43
+ BlazeUtils.SampleImageAffine(texture, m_DetectorInput, M);
44
+
45
+ m_PoseDetectorWorker.Schedule(m_DetectorInput);
46
+ ```
47
+
48
+ Execution is scheduled using an [Awaitable](https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Awaitable.html) and the output tensors are downloaded and awaited. This frees up the main thread while the GPU computation and download takes place.
49
+ ```
50
+ var outputIdxAwaitable = (m_PoseDetectorWorker.PeekOutput(0) as Tensor<int>).ReadbackAndCloneAsync();
51
+ var outputScoreAwaitable = (m_PoseDetectorWorker.PeekOutput(1) as Tensor<float>).ReadbackAndCloneAsync();
52
+ var outputBoxAwaitable = (m_PoseDetectorWorker.PeekOutput(2) as Tensor<float>).ReadbackAndCloneAsync();
53
+
54
+ using var outputIdx = await outputIdxAwaitable;
55
+ using var outputScore = await outputScoreAwaitable;
56
+ using var outputBox = await outputBoxAwaitable;
57
+ ```
58
+ The output tensors of the detector model are now on the CPU and can be read. If the score passes our score threshold, we use the keypoint positions to set up a second affine transformation. This is calculated so that the body will be centred, rotated and scaled to fill the landmarker input tensor. We use the box and keypoint positions to set the transforms on bounding box and circle for visualization.
59
+
60
+ ![](./images/pose_landmarker_input.png)
61
+ ```
62
+ var face_ImageSpace = BlazeUtils.mul(M, anchorPosition + new float2(outputBox[0, 0, 0], outputBox[0, 0, 1]));
63
+ var faceTopRight_ImageSpace = BlazeUtils.mul(M, anchorPosition + new float2(outputBox[0, 0, 0] + 0.5f * outputBox[0, 0, 2], outputBox[0, 0, 1] + 0.5f * outputBox[0, 0, 3]));
64
+
65
+ var kp1_ImageSpace = BlazeUtils.mul(M, anchorPosition + new float2(outputBox[0, 0, 4 + 2 * 0 + 0], outputBox[0, 0, 4 + 2 * 0 + 1]));
66
+ var kp2_ImageSpace = BlazeUtils.mul(M, anchorPosition + new float2(outputBox[0, 0, 4 + 2 * 1 + 0], outputBox[0, 0, 4 + 2 * 1 + 1]));
67
+ var delta_ImageSpace = kp2_ImageSpace - kp1_ImageSpace;
68
+ var dscale = 1.25f;
69
+ var radius = dscale * math.length(delta_ImageSpace);
70
+ var theta = math.atan2(delta_ImageSpace.y, delta_ImageSpace.x);
71
+ var origin2 = new float2(0.5f * landmarkerInputSize, 0.5f * landmarkerInputSize);
72
+ var scale2 = radius / (0.5f * landmarkerInputSize);
73
+ var M2 = BlazeUtils.mul(BlazeUtils.mul(BlazeUtils.mul(BlazeUtils.TranslationMatrix(kp1_ImageSpace), BlazeUtils.ScaleMatrix(new float2(scale2, -scale2))), BlazeUtils.RotationMatrix(0.5f * Mathf.PI - theta)), BlazeUtils.TranslationMatrix(-origin2));
74
+ BlazeUtils.SampleImageAffine(texture, m_LandmarkerInput, M2);
75
+
76
+ var boxSize = 2f * (faceTopRight_ImageSpace - face_ImageSpace);
77
+
78
+ posePreview.SetBoundingBox(true, ImageToWorld(face_ImageSpace), boxSize / m_TextureHeight);
79
+ posePreview.SetBoundingCircle(true, ImageToWorld(kp1_ImageSpace), radius / m_TextureHeight);
80
+
81
+ m_PoseLandmarkerWorker.Schedule(m_LandmarkerInput);
82
+ ```
83
+ The output tensor of the landmarker model is asynchronously downloaded and once the values are on the CPU we use them together with the affine transformation matrix to set the transforms on the keypoints for visualization.
84
+
85
+ ## WebGPU
86
+ Unity 6 supports access to the WebGPU backend in early access. Sentis has full support for running models on the web using the WebGPU backend. Discover how to gain early access and test WebGPU in our [graphics forum](https://discussions.unity.com/t/early-access-to-the-new-webgpu-backend-in-unity-2023-3/933493).
87
+
88
+ ![](./images/pose_webgpu.png)