Skip to content

Commit

Permalink
Added DTW KNN notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
richardyang committed Mar 23, 2018
1 parent 1998027 commit 42d7582
Show file tree
Hide file tree
Showing 15 changed files with 2,822 additions and 1,930 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
/poses
__pycache__/
*.pyc
.ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb
.ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb
.ipynb_checkpoints/DTW-FrontRaise-checkpoint.ipynb
.ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb
.ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb
.ipynb_checkpoints/DTW-ShoulderPress-checkpoint.ipynb
.ipynb_checkpoints/DTW-ShoulderShrug-checkpoint.ipynb
.ipynb_checkpoints/ShoulderPress-checkpoint.ipynb
.ipynb_checkpoints/ShoulderPressAnalysis-checkpoint.ipynb
__init__.py
.ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb
ml_notebooks/.ipynb_checkpoints/DTW-FrontRaise-checkpoint.ipynb
ml_notebooks/.ipynb_checkpoints/DTW-ShoulderPress-Copy1-checkpoint.ipynb
113 changes: 0 additions & 113 deletions .ipynb_checkpoints/ShoulderPress-checkpoint.ipynb

This file was deleted.

776 changes: 0 additions & 776 deletions .ipynb_checkpoints/analysis_shoulderpress-checkpoint.ipynb

This file was deleted.

File renamed without changes.
146 changes: 146 additions & 0 deletions DTW-FrontRaise.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"import glob\n",
"import utils\n",
"\n",
"from parse import load_ps\n",
"from pprint import pprint\n",
"from scipy.signal import medfilt\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.neighbors import DistanceMetric\n",
"from sklearn.metrics import classification_report"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"files = utils.files_in_order('poses_compressed/frontraise')\n",
"X_train_names, X_test_names = train_test_split(files, test_size=0.4, random_state=42)\n",
"y_train = utils.get_labels(X_train_names)\n",
"y_test = utils.get_labels(X_test_names)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def load_features(names):\n",
" output1 = [] # List of torso-arm vectors\n",
" for filename in names:\n",
" ps = load_ps('poses_compressed/frontraise/'+filename)\n",
" poses = ps.poses\n",
"\n",
" right_present = [1 for pose in poses \n",
" if pose.rshoulder.exists and pose.relbow.exists and pose.rwrist.exists]\n",
" left_present = [1 for pose in poses\n",
" if pose.lshoulder.exists and pose.lelbow.exists and pose.lwrist.exists]\n",
" right_count = sum(right_present)\n",
" left_count = sum(left_present)\n",
" side = 'right' if right_count > left_count else 'left'\n",
"\n",
" if side == 'right':\n",
" joints = [(pose.rshoulder, pose.relbow, pose.rwrist, pose.rhip, pose.neck) for pose in poses]\n",
" else:\n",
" joints = [(pose.lshoulder, pose.lelbow, pose.lwrist, pose.lhip, pose.neck) for pose in poses]\n",
"\n",
" # filter out data points where a part does not exist\n",
" joints = [joint for joint in joints if all(part.exists for part in joint)]\n",
" joints = np.array(joints)\n",
" \n",
" # Shoulder to hip \n",
" torso_vecs = np.array([(joint[0].x - joint[3].x, joint[0].y - joint[3].y) for joint in joints])\n",
" # Arm\n",
" arm_vecs = np.array([(joint[0].x - joint[2].x, joint[0].y - joint[2].y) for joint in joints])\n",
"\n",
" # normalize vectors\n",
" torso_vecs = torso_vecs / np.expand_dims(np.linalg.norm(torso_vecs, axis=1), axis=1)\n",
" arm_vecs = arm_vecs / np.expand_dims(np.linalg.norm(arm_vecs, axis=1), axis=1)\n",
" \n",
" angles = np.degrees(np.arccos(np.clip(np.sum(np.multiply(torso_vecs, arm_vecs), axis=1), -1.0, 1.0)))\n",
" \n",
" output1.append(angles.tolist())\n",
" return output1\n",
"\n",
"X_train_1 = load_features(X_train_names)\n",
"X_test_1 = load_features(X_test_names)\n",
"#features = load_features(files)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" correct 1.00 1.00 1.00 6\n",
" incorrect 1.00 1.00 1.00 6\n",
"\n",
"avg / total 1.00 1.00 1.00 12\n",
"\n"
]
}
],
"source": [
"predictions = []\n",
"for example in range(len(X_test_names)):\n",
" # Store the average distance to good and bad training examples\n",
" f1_good, f1_bad = [[] for i in range(2)]\n",
" \n",
" # Compare distance of current test example with all training examples\n",
" for i in range(len(X_train_1)):\n",
" dist1 = utils.DTWDistance(X_train_1[i], X_test_1[example])\n",
" if y_train[i]:\n",
" f1_good.append(dist1)\n",
" else:\n",
" f1_bad.append(dist1)\n",
" good_score = np.mean(f1_good)\n",
" bad_score = np.mean(f1_bad)\n",
" if good_score < bad_score:\n",
" predictions.append(1)\n",
" else:\n",
" predictions.append(0)\n",
"print(classification_report(y_test, predictions, target_names=['correct', 'incorrect']))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"outputs": [],
"source": [
"files = utils.files_in_order('poses_compressed/shoulderpress')\n",
"X_train_names, X_test_names = train_test_split(files, test_size=0.2, random_state=42)\n",
"X_train_names, X_test_names = train_test_split(files, test_size=0.4, random_state=42)\n",
"y_train = utils.get_labels(X_train_names)\n",
"y_test = utils.get_labels(X_test_names)"
]
Expand Down Expand Up @@ -103,10 +103,10 @@
"text": [
" precision recall f1-score support\n",
"\n",
" correct 0.20 0.33 0.25 3\n",
" incorrect 0.33 0.20 0.25 5\n",
" correct 0.67 0.86 0.75 7\n",
" incorrect 0.83 0.62 0.71 8\n",
"\n",
"avg / total 0.28 0.25 0.25 8\n",
"avg / total 0.76 0.73 0.73 15\n",
"\n"
]
}
Expand Down
Loading

0 comments on commit 42d7582

Please sign in to comment.