I'm trying to do human activity recognition using CNN and accelerometer. This is the balanced dataset i'm using for training:
balanced_data['activity']. value_counts()
Walking 3555
Jogging 3555
Upstairs 3555
Downstairs 3555
Sitting 3555
Standing 3555
Name: activity, dtype: int64
This is my code:
#imports...`
balanced_data = pd.DataFrame()
balanced_data = balanced_data.append([Walking, Jogging, Upstairs, Downstairs, Sitting, Standing])
label = LabelEncoder()
balanced_data['label'] = label.fit_transform(balanced_data['activity'])
balanced_data.head()
X = balanced_data[['x', 'y', 'z']]
y = balanced_data['label']
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
scaled_X = pd.DataFrame(data = X, columns = ['x', 'y', 'z'])
scaled_X['label'] = y.values
scaled_X.head()
Fs = 20
frame_size = Fs*4 # 80
hop_size = Fs*2 # 40
def get_frames(df, frame_size, hop_size):
N_FEATURES = 3
frames = []
labels = []
for i in range(0, len(df) - frame_size, hop_size):
x = df['x'].values[i: i + frame_size]
y = df['y'].values[i: i + frame_size]
z = df['z'].values[i: i + frame_size]
# Retrieve the most often used label in this segment
label = stats.mode(df['label'][i: i + frame_size])[0][0]
frames.append([x, y, z])
labels.append(label)
# Bring the segments into a better shape
frames = np.asarray(frames).reshape(-1, frame_size, N_FEATURES)
labels = np.asarray(labels)
return frames, labels
X, y = get_frames(scaled_X, frame_size, hop_size)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0, stratify = y)
x_train_bf = X_train
X_train = X_train.reshape(425, 80, 3, 1)
X_test = X_test.reshape(107, 80, 3, 1)
model = Sequential()
model.add(Conv2D(16, (2, 2), activation = 'relu', input_shape = X_train[0].shape))
model.add(Dropout(0.1))
model.add(Conv2D(32, (2, 2), activation='relu'))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax'))
model.compile(optimizer=Adam(learning_rate = 0.001), loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
history = model.fit(X_train, y_train, epochs = 10, validation_data= (X_test, y_test), verbose=1)
def accuracy (predicted_y, excepted_y):
k=0;
for i in predicted_y:
if(i==excepted_y):
k=k+1;
return k*100/len(predicted_y);`
Training the model i get an accuracy of 0.9
I used other data from the same dataset to make a prediction (es. for jogging), this is the code:
jo_data = dataset[dataset['activity']=='Jogging'].copy()
jo_data = jo_data [['x','y','z']]
jo_data_1 = scaler.transform(jo_data)
jo_scaled = pd.DataFrame(data = jo_data_1, columns = ['x', 'y','z'])
jo_frames = get_frames_2(jo_scaled,frame_size,hop_size)
jo_re = jo_frames.reshape((len(jo_frames)),80,3,1)
y_jo = model.predict_classes(jo_re)
acc_y_jo = accuracy(y_jo, 1);
print('Jogging Accuracy : ', acc_y_jo)
With this prediction I can get an accuracy of 0.7, and it is the same if I choose "Walking" as the label to predict.
I have even tried to collect my own data (from my smartphone accelerometer, same frequency 20 Hz), but i get 0%!! accuracy trying to predict the activity i'm doing!
Can someone help me? Thank you a lot!