Commit 8ae5d281 authored by Brummans, Nick's avatar Brummans, Nick
Browse files

Added code and example to save, load and use the scenario models

parent 0d9a4bc1
%% Cell type:code id:bb258807 tags:
``` python
## Note: Currently we are using basic values like left/right/front for location classes. And these are hard coded
#In future version, we will map to positions where the object moved, not just high-level movements
```
%% Cell type:code id:dc4557a6 tags:
``` python
#Import the Classifier, feature extractor
from common_utils.classifier import Classifier
from common_utils.feature_extraction import ExtractFeatures
import numpy as np
```
%% Cell type:code id:5946b736 tags:
``` python
#Mention the directory where the scenario models are saved
#Note that each model directory should have 3 pkl files which constitute a single model
# label_encoder.pkl model.pkl X_scaler_model.pkl
model_dir = "/home/ur5/sandbox/har_scenario_modeling/models/scenario1"
#Instantiate the model
my_model = Classifier(model_dir)
```
%% Cell type:code id:7c0728ea tags:
``` python
#Load data from the csv file and extract the features
data_path = "/home/ur5/remote_dir/KB Autonomous Robotics/2021/LabGradingSortingDataset/Side-view-KB00-05-03-2021/scenario-1/mandarins"
#data_path is the folder which is recursively parsed to search for the csv files (assumes that the format is correct)
fe = ExtractFeatures(data_path)
X, Labels = fe.generate_features()
```
%% Cell type:markdown id:2c8f3d3e tags:
# Example prediction usage
%% Cell type:code id:a5322149 tags:
``` python
# Examples of predictions
# x = X[0,:].reshape(1,-1) #Just one recording
# prediction = my_model.predict(x)
# print(prediction, Labels[0])
# #Some four recordings
x = X[:,:]
prediction = my_model.predict(x)
print(prediction, '\n' , Labels[17:21])
```
%% Output
['large_left' 'large_left' 'large_left' 'large_left' 'large_left'
'large_left' 'large_left' 'large_left' 'large_left' 'large_left'
'large_left' 'large_left' 'large_left' 'large_left' 'small_right'
'small_right' 'small_right' 'small_right' 'small_right' 'small_right'
'small_right' 'small_right' 'small_right' 'small_right']
[['small']
['small']
['small']
['small']]
%% Cell type:code id:2c18c877 tags:
``` python
np.unique(prediction)
```
%% Output
array(['large_left', 'small_right'], dtype=object)
%% Cell type:markdown id:b3789a29 tags:
# Example usage for saving summary of multiple activities as a json
### Given multiple recordings, we make predictions, but instead of saving all the prediction, we save the unique predictions alone
%% Cell type:code id:67ebe3c1 tags:
``` python
scenario_f = '/tmp/scenario2.json'
prediction = my_model.predict_and_save(x, scenario_f)
```
%% Output
{'large': 'left', 'small': 'right'}
%% Cell type:code id:b2a71d73 tags:
``` python
import json
with open(scenario_f) as json_file:
scenario_json = json.load(json_file)
```
%% Cell type:code id:fc71f00c tags:
``` python
print(scenario_json)
```
%% Output
{'large': 'left', 'small': 'right'}
%% Cell type:code id:db33c4ce tags:
``` python
```
%% Cell type:code id:bb258807 tags:
``` python
## Note: Currently we are using basic values like left/right/front for location classes. And these are hard coded
#In future version, we will map to positions where the object moved, not just high-level movements
```
%% Cell type:code id:dc4557a6 tags:
``` python
#Import the Classifier, feature extractor
from common_utils.classifier import Classifier
from common_utils.feature_extraction import ExtractFeatures
import numpy as np
```
%% Cell type:code id:5946b736 tags:
``` python
#Mention the directory where the scenario models are saved
#Note that each model directory should have 3 pkl files which constitute a single model
# label_encoder.pkl model.pkl X_scaler_model.pkl
model_dir = "/home/ur5/sandbox/har_scenario_modeling/models/scenario1"
#Instantiate the model
my_model = Classifier(model_dir)
```
%% Cell type:code id:7c0728ea tags:
``` python
#Load data from the csv file and extract the features
data_path = "/home/ur5/remote_dir/KB Autonomous Robotics/2021/LabGradingSortingDataset/Side-view-KB00-05-03-2021/scenario-1/mandarins"
#data_path is the folder which is recursively parsed to search for the csv files (assumes that the format is correct)
fe = ExtractFeatures(data_path)
X, Labels = fe.generate_features()
```
%% Cell type:markdown id:2c8f3d3e tags:
# Example prediction usage
%% Cell type:code id:a5322149 tags:
``` python
# Examples of predictions
# x = X[0,:].reshape(1,-1) #Just one recording
# prediction = my_model.predict(x)
# print(prediction, Labels[0])
# #Some four recordings
x = X[:,:]
prediction = my_model.predict(x)
print(prediction, '\n' , Labels[17:21])
```
%% Output
['large_left' 'large_left' 'large_left' 'large_left' 'large_left'
'large_left' 'large_left' 'large_left' 'large_left' 'large_left'
'large_left' 'large_left' 'large_left' 'large_left' 'small_right'
'small_right' 'small_right' 'small_right' 'small_right' 'small_right'
'small_right' 'small_right' 'small_right' 'small_right']
[['small']
['small']
['small']
['small']]
%% Cell type:code id:2c18c877 tags:
``` python
np.unique(prediction)
```
%% Output
array(['large_left', 'small_right'], dtype=object)
%% Cell type:markdown id:b3789a29 tags:
# Example usage for saving summary of multiple activities as a json
### Given multiple recordings, we make predictions, but instead of saving all the prediction, we save the unique predictions alone
%% Cell type:code id:67ebe3c1 tags:
``` python
scenario_f = '/tmp/scenario2.json'
prediction = my_model.predict_and_save(x, scenario_f)