Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lincior committed Sep 4, 2018
1 parent 0406cf4 commit 4a9ed82
Show file tree
Hide file tree
Showing 8 changed files with 1,165 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/code/data/models/*
/code/data/full/*
/code/data/reduced/*
/code/data/notebook/*
/papers/*
1,056 changes: 1,056 additions & 0 deletions code/.ipynb_checkpoints/HAR_system-checkpoint.ipynb

Large diffs are not rendered by default.

135 changes: 106 additions & 29 deletions code/TaskB.ipynb → code/HAR_system.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task B - single subject - model ?\n",
"# HAR system - Lincetto Riccardo, Drago Matteo\n",
"This notebook runs:\n",
"- Classification with null class (One Shot classification);\n",
"- Binary classification for activity detection (Two Steps - detection);\n",
"- Classification without null class (Two Steps - classification);\n",
"- Cascade of the last to methods.\n",
"\n",
"The operations performed here are very similar to those execute in 'main.py', with the exception that here the program is executed for specified user and model.\n",
"\n",
"## Notebook setup\n",
"This first cell contains the parameters that can be tuned for code execution:\n",
"- subject: select the subject on which to test the model, between [1,4];\n",
"- label: index of feature column to be selected to perform activity detection, between [0,6]. The default value for task B is 6;\n",
"- folder: directory name where '.mat' files are stored;\n",
"- task: choose \"A\" for locomotion classification or \"B\" for gesture recognition;\n",
"- model_name: choose between \"Convolutional\", \"Convolutional1DRecurrent\", \"Convolutional2DRecurrent\" and \"ConvolutionalDeepRecurrent\";\n",
"- data_folder: directory name where '.mat' files are stored;\n",
"- window_size: parameter that sets the length of temporal windows on which to perform the convolution;\n",
"- stride: step length to chose the next window."
"- stride: step length to chose the next window;\n",
"- GPU: boolean flag indicatin wheter GPU is present on the machine that executes the code;\n",
"- epochs: number of complete sweeps of the data signals during training;\n",
"- batch_size: number of forward propagations in the networks between consecutives backpropagations."
]
},
{
Expand All @@ -22,10 +33,21 @@
"outputs": [],
"source": [
"subject = 1\n",
"label = 0\n",
"folder = \"../data/full/\"\n",
"task = \"A\"\n",
"model_name = \"Convolutional\"\n",
"data_folder = \"./data/full/\"\n",
"window_size = 15\n",
"stride = 5"
"stride = 5\n",
"GPU = True\n",
"epochs = 10\n",
"batch_size = 32"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the useful functions are imported."
]
},
{
Expand All @@ -47,21 +69,22 @@
"import preprocessing\n",
"import models\n",
"import utils\n",
"import os\n",
"import numpy as np\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.metrics import classification_report, f1_score, confusion_matrix\n",
"from keras.models import load_model\n",
"from keras.optimizers import Adam\n",
"from keras.callbacks import ModelCheckpoint\n",
"from keras.utils import to_categorical"
"from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
"from keras.utils import to_categorical\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following cell, we make use of some functions of Keras which have been removed, but of which the code is still available at https://github.com/keras-team/keras/commit/a56b1a55182acf061b1eb2e2c86b48193a0e88f7. These are used to evaulate the f1 score during training on batches of data: this is only an approximation though, which is the reason why they have been removed."
"Differently from 'main.py', all results saved from this notebook are going to be stored in a dedicated folder: './data/notebook/'. For proper execution, this folder needs first to be created."
]
},
{
Expand All @@ -70,50 +93,104 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"if not(os.path.exists(\"./data\")):\n",
" os.mkdir(\"./data\")"
" os.mkdir(\"./data\")\n",
"if not(os.path.exists(\"./data/notebook\")):\n",
" os.mkdir(\"./data/notebook\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# One-shot classification\n",
"Here classification is performed with null class.\n",
"### Preprocessing"
"If task A is selected, calssifications in the following notebook are based on the labels of column 0; if instead it's task B, column 6 labels are used."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Task A uses labels column 0\n"
]
}
],
"source": [
"X_train, Y_train, X_test, Y_test, n_features, n_classes, class_weights = preprocessing.loadData(subject=subject,\n",
" label=label,\n",
" folder=folder,\n",
" window_size=window_size,\n",
" stride=stride,\n",
" make_binary=False,\n",
" null_class=True,\n",
" print_info=False)"
"if task == \"A\":\n",
" label = 0\n",
"elif task == \"B\":\n",
" label = 6\n",
"else:\n",
" print(\"Error: invalid task.\")\n",
"print(\"Task\", task, \"uses labels column\", label)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Y_train and Y_test contain the correct labels for each signals window. Y_test in particular will be used to evaluate predictions for both this (one-shot) and the two-steps models. For this reason it is here saved with a different name, to avoid having it being overwritten later."
"## Classification with null class: One Shot classification\n",
"Here classification is performed considering inactivity as a class, alongside with the others. In the case of locomotion classification (task A), this becomes a 5-class problem, while in the case of gesture recognition (task B) the classes become 18. In the following cell are perfomed in order:\n",
"- preprocessing;\n",
"- model selection;\n",
"- model compilation;\n",
"- training.\n",
"\n",
"Note that in case \"Convolutional2DRecurrent\" is the model selected, then the preprocessed data need to be reshaped, adding one dimension; this is automatically done by the code."
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"Y_test_true = Y_test"
"# preprocessing\n",
"X_train, Y_train, X_test, Y_test, n_features, n_classes, class_weights = preprocessing.loadData(subject=subject,\n",
" label=label,\n",
" folder=data_folder,\n",
" window_size=window_size,\n",
" stride=stride,\n",
" make_binary=False,\n",
" null_class=True,\n",
" print_info=False)\n",
"\n",
"# model selection\n",
"if model_name == \"Convolutional\":\n",
" model = models.Convolutional((window_size, n_features), n_classes, print_info=False)\n",
"elif model_name == \"Convolutional1DRecurrent\":\n",
" model = models.Convolutional1DRecurrent((window_size, n_features), n_classes, GPU=GPU, print_info=False)\n",
"elif model_name == \"Convolutional2DRecurrent\":\n",
" model = models.Convolutional2DRecurrent((window_size, n_features, 1), n_classes, GPU=GPU, print_info=False)\n",
" # reshaping for 2D convolutional model\n",
" X_train = X_train.reshape(X_train.shape[0], window_size, n_features, 1)\n",
" X_test = X_test.reshape(X_test.shape[0], window_size, n_features, 1)\n",
"elif model_name == \"ConvolutionalDeepRecurrent\":\n",
" model = models.ConvolutionalDeepRecurrent((window_size, n_features), n_classes, GPU=GPU, print_info=False)\n",
"else:\n",
" print(\"Model not found.\")\n",
" break\n",
"\n",
"# model compilation\n",
"model.compile(optimizer = Adam(lr=0.001), loss = \"categorical_crossentropy\", metrics = [\"accuracy\"])\n",
"save_model_name = task + \"_\" + model_name + \"_OS_\" + str(s)\n",
"filepath = './data/notebook/'+save_model_name+'.hdf5'\n",
"print(\"Model:\", save_model_name, \"\\nLocation:\", filepath, \"\\n\")\n",
"\n",
"# training\n",
"checkpointer = ModelCheckpoint(filepath=filepath, verbose=1, save_best_only=True)\n",
"lr_reducer = ReduceLROnPlateau(factor=0.1, patience=5, min_lr=0.00001, verbose=1)\n",
"model.fit(x = X_train, \n",
" y = to_categorical(Y_train), \n",
" epochs = epochs, \n",
" batch_size = batch_size,\n",
" verbose = 1,\n",
" validation_data=(X_test, to_categorical(Y_test)),\n",
" callbacks=[checkpointer, lr_reducer])"
]
},
{
Expand Down
Binary file modified code/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified code/__pycache__/preprocessing.cpython-36.pyc
Binary file not shown.
Binary file modified code/__pycache__/utils.cpython-36.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
subject = [1,2,3,4]
task = "A" # choose between "A" or "B"
model_names = ["Convolutional", "Convolutional1DRecurrent", "Convolutional2DRecurrent", "ConvolutionalDeepRecurrent"]
data_folder = "../data/full/"
data_folder = "./data/full/"
window_size = 15
stride = 5
GPU = True
Expand Down
2 changes: 1 addition & 1 deletion code/main_multiuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
subject = [23]
task = "A" # choose between "A" or "B"
model_names = ["Convolutional", "Convolutional1DRecurrent", "Convolutional2DRecurrent", "ConvolutionalDeepRecurrent"]
data_folder = "../data/full/"
data_folder = "./data/full/"
window_size = 15
stride = 5
GPU = True
Expand Down

0 comments on commit 4a9ed82

Please sign in to comment.