# Calculate PCK
def calculate_pck(predicted_keypoints, ground_truth_keypoints, threshold=0.2):
# Calculate Euclidean distance between predicted and ground truth keypoints
distances = torch.linalg.norm(predicted_keypoints - ground_truth_keypoints, dim=2)
# Determine correct keypoints (distance < threshold)
correct_keypoints = (distances < threshold).float()
# Calculate PCK as mean of correct keypoints
pck = correct_keypoints.mean().item()
return pck
# Example usage
predicted = torch.rand(16, 17, 2) # batch_size, num_keypoints, coordinates
ground_truth = torch.rand(16, 17, 2)
accuracy = calculate_pck(predicted, ground_truth)
print(f"PCK Accuracy: {accuracy:.4f}")