forked from hb2kang-zz/sEMG_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_train.py
86 lines (68 loc) · 2.31 KB
/
model_train.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
# Dependencies
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
import time
import math
# Data loader
import preprocess
from numpy import array
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# Neural Network
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTM
## Normal sEMG dataset
file_path = "./SEMG_DB1/N_TXT/"
input_array, output_array = preprocess.preprocess(file_path)
print(input_array.shape)
print(input_array)
## One Hot Encode Prediction
print(output_array)
# convert output_array to one hot encoded classes
values = array(output_array)
print(values)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(onehot_encoded)
# Retrieve from input array shape
time_steps = input_array.shape[1]
n_features = input_array.shape[2]
# LSTM with 100 neurons and dropout.
model = Sequential()
model.add(LSTM(100, return_sequences=False, input_shape=(time_steps, n_features)))
model.add(Dropout(0.5))
#model.add(LSTM(100)) dramatically worse results
model.add(Dense(3, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(input_array, onehot_encoded, batch_size=16, epochs=20)
## Abnormal sEMG dataset
abnormal_path = "./SEMG_DB1/A_TXT/"
abnormal_input_array, abnormal_output_array = preprocess.preprocess(file_path)
# convert output_array to one hot encoded classes
values = array(output_array)
print(values)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
abnormal_onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print(abnormal_onehot_encoded)
# test on abnormal dataset
score = model.evaluate(abnormal_input_array, abnormal_onehot_encoded, batch_size=16)
print(score)