-
Notifications
You must be signed in to change notification settings - Fork 16
/
exermote.py
executable file
·216 lines (178 loc) · 8.33 KB
/
exermote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from tensorflow.python.lib.io import file_io
import argparse
from pandas import read_csv
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, LSTM, Conv1D
from keras.callbacks import TensorBoard, ModelCheckpoint
from numpy import array, split
import keras.backend as k
import tensorflow as tf
from keras.models import load_model
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def
import coremltools
# training parameters
epochs = 50
batch_size = 100
validation_split = 0.2
# model parameters
dropout = 0.2
timesteps = 40
timesteps_in_future = 20
nodes_per_layer = 32
filter_length = 3
def train_model(train_file='data_classes_4_squats_adjusted.csv', job_dir='leeeeeroooooyyyyyjeeeeeenkins', **args):
parameter_string = 'final_25_classes_4_squats_adjusted' + '_dropout_' + str(dropout) + '_timesteps_' + str(
timesteps) + '_timesteps_in_future_' + str(timesteps_in_future) + '_nodes_per_layer_' + str(
nodes_per_layer) + '_filter_length_' + str(filter_length)
if 'gs://' in job_dir:
logs_path = 'gs://exermotemachinelearningengine' + '/logs/' + parameter_string
else:
logs_path = '.' + '/logs/' + parameter_string
print('-----------------------')
print('Using train_file located at {}'.format(train_file))
print('Using logs_path located at {}'.format(logs_path))
print('-----------------------')
# load data
file_stream = file_io.FileIO(train_file, mode='r')
dataframe = read_csv(file_stream, header=0)
dataframe.fillna(0, inplace=True)
dataset = dataframe.values
X = dataset[:, [
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, # Device: xGravity, yGravity, zGravity, xAcceleration, yAcceleration, zAcceleration, pitch, roll, yaw, xRotationRate, yRotationRate, zRotationRate
# 14,15,16,17, # Right Hand: rssi, xAcceleration, yAcceleration, zAcceleration
# 18,19,20,21, # Left Hand: rssi, xAcceleration, yAcceleration, zAcceleration
# 22,23,24,25, # Right Foot: rssi, xAcceleration, yAcceleration, zAcceleration
# 26,27,28,29, # Left Foot: rssi, xAcceleration, yAcceleration, zAcceleration
# 30,31,32,33, # Chest: rssi, xAcceleration, yAcceleration, zAcceleration
# 34,35,36,37 # Belly: rssi, xAcceleration, yAcceleration, zAcceleration
]].astype(float)
y = dataset[:, 0] # ExerciseType (Index 1 is ExerciseSubType)
# data parameters
data_dim = X.shape[1]
num_classes = len(set(y))
# scale X
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X) # X*scaler.scale_+scaler.min_ (columnwise)
print('Multiplying each row in X elementwise: {}'.format(scaler.scale_))
print('Increasing each row in X elemtwise: {}'.format(scaler.min_))
# encode Y
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y) # encoder.classes_
print('Hotencoding Y: {}'.format(encoder.classes_))
hot_encoded_y = np_utils.to_categorical(encoded_y)
# prepare data for LSTM
def create_LSTM_dataset(x, y, timesteps):
dataX, dataY = [], []
for i in range(len(x) - timesteps + 1):
dataX.append(x[i:i + timesteps, :])
dataY.append(y[i + timesteps - timesteps_in_future - 1, :])
return array(dataX), array(dataY)
X, hot_encoded_y = create_LSTM_dataset(X, hot_encoded_y, timesteps)
# define model
model = Sequential([
Conv1D(nodes_per_layer, filter_length, strides=2, activation='relu', input_shape=(timesteps, data_dim),
name='accelerations'),
Conv1D(nodes_per_layer, filter_length, strides=1, activation='relu'),
LSTM(nodes_per_layer, return_sequences=True),
LSTM(nodes_per_layer, return_sequences=False),
Dropout(dropout),
Dense(num_classes),
Activation('softmax', name='scores'),
])
model.summary()
# compile model
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# define callbacks
callbacks = []
tensor_board = TensorBoard(log_dir=logs_path, histogram_freq=1, write_graph=False, write_images=False)
callbacks.append(tensor_board)
checkpoint_path = 'best_weights.h5'
checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks.append(checkpoint)
# train model
model.fit(X, hot_encoded_y,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=validation_split,
callbacks=callbacks
)
# load best checkpoint
model.load_weights('best_weights.h5')
# evaluate best model
def non_shuffling_train_test_split(X, y, test_size=validation_split):
i = int((1 - test_size) * X.shape[0]) + 1
X_train, X_test = split(X, [i])
y_train, y_test = split(y, [i])
return X_train, X_test, y_train, y_test
_, X_test, _, y_test = non_shuffling_train_test_split(X, hot_encoded_y, test_size=validation_split)
scores = model.evaluate(X_test, y_test, verbose=0)
acc = scores[1]
# save model
model_h5_name = 'model_acc_' + str(acc) + '.h5'
model.save(model_h5_name)
# save model.h5 on to google storage
with file_io.FileIO(model_h5_name, mode='r') as input_f:
with file_io.FileIO(logs_path + '/' + model_h5_name, mode='w+') as output_f:
output_f.write(input_f.read())
# reset session
# Note: If this piece of code did help you to achieve your goal, please upvote my solution under:
# https://stackoverflow.com/questions/41959318/deploying-keras-models-via-google-cloud-ml/44232441#44232441
# Thank you so much :)
k.clear_session()
sess = tf.Session()
k.set_session(sess)
# disable loading of learning nodes
k.set_learning_phase(0)
# load model
model = load_model(model_h5_name)
config = model.get_config()
weights = model.get_weights()
new_Model = Sequential.from_config(config)
new_Model.set_weights(weights)
# export coreml model
coreml_model = coremltools.converters.keras.convert(new_Model, input_names=['accelerations'],
output_names=['scores'])
model_mlmodel_name = 'model_acc_' + str(acc) + '.mlmodel'
coreml_model.save(model_mlmodel_name)
# save model.mlmodel on to google storage
with file_io.FileIO(model_mlmodel_name, mode='r') as input_f:
with file_io.FileIO(logs_path + '/' + model_mlmodel_name, mode='w+') as output_f:
output_f.write(input_f.read())
# export saved model
# Note: If this piece of code did help you to achieve your goal, please upvote my solution under:
# https://stackoverflow.com/questions/41959318/deploying-keras-models-via-google-cloud-ml/44232441#44232441
# Thank you so much :)
export_path = logs_path + "/export"
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'accelerations': new_Model.input},
outputs={'scores': new_Model.output})
with k.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
builder.save()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Input Arguments
parser.add_argument(
'--train-file',
help='GCS or local paths to training data',
required=True
)
parser.add_argument(
'--job-dir',
help='GCS location to write checkpoints and export models',
required=True
)
args = parser.parse_args()
arguments = args.__dict__
train_model(**arguments)