Reputation: 1038
I am starting with the pose estimation tflite model for getting keypoints on humans.
https://www.tensorflow.org/lite/models/pose_estimation/overview
I have started with fitting a single image or a person and invoking the model:
img = cv.imread('photos\standing\\3.jpg')
img = tf.reshape(tf.image.resize(img, [257,257]), [1,257,257,3])
model = tf.lite.Interpreter('models\posenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite')
model.allocate_tensors()
input_details = model.get_input_details()
output_details = model.get_output_details()
floating_model = input_details[0]['dtype'] == np.float32
if floating_model:
img = (np.float32(img) - 127.5) / 127.5
model.set_tensor(input_details[0]['index'], img)
model.invoke()
output_data = model.get_tensor(output_details[0]['index'])# o()
offset_data = model.get_tensor(output_details[1]['index'])
results = np.squeeze(output_data)
offsets_results = np.squeeze(offset_data)
print("output shape: {}".format(output_data.shape))
np.savez('sample3.npz', results, offsets_results)
but I am struggling with parsing the output correctly to get the coordinates/confidences of each body part. Does anyone have a python example for interpreting this models results? (for example: using them to map keypoints back to the original image)
My code (a snippet from a class which essentially takes the np array directly from the model output):
def get_keypoints(self, data):
height, width, num_keypoints = data.shape
keypoints = []
for keypoint in range(0, num_keypoints):
maxval = data[0][0][keypoint]
maxrow = 0
maxcol = 0
for row in range(0, width):
for col in range(0,height):
if data[row][col][keypoint] > maxval:
maxrow = row
maxcol = col
maxval = data[row][col][keypoint]
keypoints.append(KeyPoint(keypoint, maxrow, maxcol, maxval))
# keypoints = [Keypoint(x,y,z) for x,y,z in ]
return keypoints
def get_image_coordinates_from_keypoints(self, offsets):
height, width, depth = (257,257,3)
# [(x,y,confidence)]
coords = [{ 'point': k.body_part,
'location': (k.x / (width - 1)*width + offsets[k.y][k.x][k.index],
k.y / (height - 1)*height + offsets[k.y][k.x][k.index]),
'confidence': k.confidence}
for k in self.keypoints]
return coords
after matching the indexes to the parts my output is:
Some of the coordinates here are negative, which can't be correct. Where is my mistake?
Upvotes: 6
Views: 3578
Reputation: 1038
import numpy as np
For a pose estimation model which outputs a heatmap and offsets. The desired points can be obtained by:
Performing a sigmoid operation on the heatmap:
scores = sigmoid(heatmaps)
Each keypoint of those pose is usually represented by a 2-D matrix, the maximum value in that matrix is related to where the model thinks that point is located in the input image. Use argmax2D to obtain the x and y index of that value in each matrix, the value itself represents the confidence value:
x,y = np.unravel_index(np.argmax(scores[:,:,keypointindex]),
scores[:,:,keypointindex].shape)
confidences = scores[x,y,keypointindex]
That x,y is used to find the corresponding offset vector for calculating the final location of the keypoint:
offset_vector = (offsets[y,x,keypointindex], offsets[y,x,num_keypoints+keypointindex])
After you have obtained your keypoint coords and offsets you can calculate the final position of the keypoint by using ():
image_positions = np.add(np.array(heatmap_positions) * output_stride, offset_vectors)
See this for determining how to get the output stride, if you don't already have it. The tflite pose estimation has an output stride of 32.
A function which takes output from that Pose Estimation model and outputs keypoints. Not including KeyPoint
class
def get_keypoints(self, heatmaps, offsets, output_stride=32):
scores = sigmoid(heatmaps)
num_keypoints = scores.shape[2]
heatmap_positions = []
offset_vectors = []
confidences = []
for ki in range(0, num_keypoints ):
x,y = np.unravel_index(np.argmax(scores[:,:,ki]), scores[:,:,ki].shape)
confidences.append(scores[x,y,ki])
offset_vector = (offsets[y,x,ki], offsets[y,x,num_keypoints+ki])
heatmap_positions.append((x,y))
offset_vectors.append(offset_vector)
image_positions = np.add(np.array(heatmap_positions) * output_stride, offset_vectors)
keypoints = [KeyPoint(i, pos, confidences[i]) for i, pos in enumerate(image_positions)]
return keypoints
Keypoint class:
PARTS = {
0: 'NOSE',
1: 'LEFT_EYE',
2: 'RIGHT_EYE',
3: 'LEFT_EAR',
4: 'RIGHT_EAR',
5: 'LEFT_SHOULDER',
6: 'RIGHT_SHOULDER',
7: 'LEFT_ELBOW',
8: 'RIGHT_ELBOW',
9: 'LEFT_WRIST',
10: 'RIGHT_WRIST',
11: 'LEFT_HIP',
12: 'RIGHT_HIP',
13: 'LEFT_KNEE',
14: 'RIGHT_KNEE',
15: 'LEFT_ANKLE',
16: 'RIGHT_ANKLE'
}
class KeyPoint():
def __init__(self, index, pos, v):
x, y = pos
self.x = x
self.y = y
self.index = index
self.body_part = PARTS.get(index)
self.confidence = v
def point(self):
return int(self.y), int(self.x)
def to_string(self):
return 'part: {} location: {} confidence: {}'.format(
self.body_part, (self.x, self.y), self.confidence)
Upvotes: 9