Search code examples
tensorflowkerasneural-networkconv-neural-networkconv1d

TensorFlow/Keras CONV1D cannot train


I am trying to train a network to recognize gestures with an accelerometer in a wristband. I am no expert in deep learning, either python programming.

The data below is the original data that I was trying to train but had no luck with anything on this being the preferred way of doing it? ` #First Gesture for Right Swipe

RightSwipeTrain = {"x": [639, 989, 934, 783, 683, 829, 570,479, 454, 566],"y": [911, 580, 331, 244, -640, -483, 265, 125, 101, 197],"Z": [132, 324, 307, 385, -309, -762, 748, 1035, 742, 622]}
df = pd.DataFrame(RightSwipeTrain, index = ["0.00", "0.25", "0.45", "0.65", "0.85", "1.05", "1.25", "1.45", "1.65", "1.85"])print(df)`

The data below is the data I am now trying to train with, [[x-axis,y-axis,z-axis], [x-axis,y-axis,z-axis]] <- This is the configuration of the data. Anything wrong with doing that let me know.

`TimeSeries_RightTrain = [639, 911, 132, 989, 850, 324, 934, 331, 307, 783, 244, 385, 683, -640, -309, 829, -483, -762, 570, 265, 748, 479, 125, 1035, 454, 101, 742, 566, 197, 622]
df = pd.DataFrame(TimeSeries_RightTrain)print(df)`

Model (This is just a test to actually be able to train a model)

`   num_vectors = 3num_features = 3
input = ([[566, 359, 668, 1386, 513, 1086, 1276, 443, 387, 107, 83, 26, 63, 17, 838, 246, 765, 1072, 729, 1407, 1096, 955, 775, 704, 855, 539, 768, -82, -345, 328 ], [1028, 823, 420, 595, 568, 596, 192, 647, 1312, 647, 991, 735, 1573, 449, -131, 1281, -271, -114, 947, -123, 242, 762, -40, 198, 906, 414, 723, 796, 881, 270], [639, 911, 132, 989, 850, 324, 934, 331, 307, 783, 244, 385, 683, -640, -309, 829, -483, -762, 570, 265, 748, 479, 125, 1035, 454, 101, 742, 566, 197, 622]])
output = ( [1,0,0], [0,1,0], [0,0,1] )


#print training vectors
for i,c in enumerate(input):
print("input: {}, output: {}".format(c, output[i]))

from keras.activations import linear
from keras.layers.pooling.max_pooling1d import MaxPool1D
l0 = tf.keras.layers.Dense(units=3, input_shape=[30,1], activation='relu')l1 = tf.keras.layers.Conv1D(filters=10, kernel_size=3, strides=1, padding='valid', activation='relu', kernel_initializer="glorot_uniform")l2 = tf.keras.layers.Dense(units=4,activation='softmax')
model = tf.keras.Sequential([l0, l1, l2])model.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(input, output, epochs=100, verbose=True)`

If anybody could help me out I would very much appreciate it.

Feedback from GoogleColab:

ValueError: in user code:

File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1249, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1233, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1222, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1023, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 295, in assert_input_compatibility
    raise ValueError(

ValueError: Input 0 of layer "sequential_5" is incompatible with the layer: expected shape=(None, 30, 1), found shape=(None, 10, 3)

Training a neural network for gesture recognition with accelerometer data. Cannot get model to train

Training a network for gesture recognition, cannot train


Solution

  • Following things need to be changed. First of all, danse layer requires you to use 2 dimentinal array as input. so you need to change your input shape as follows:

    input = input.reshape(-1,30,1)
    

    Now your model should be able to get build but if you try to train your will get error regarding to connections between conv layer and last layer. after using convolution you need to flatten the outputs before feeding them into another layer. you can add the following line to your model:

    flat = tf.keras.layers.Flatten()
    

    so in the end your code will be as follows:

    
    from keras.activations import linear
    import tensorflow as tf
    from tensorflow.keras.layers import MaxPool1D
    
    input = ([[566, 359, 668, 1386, 513, 1086, 1276, 443, 387, 107, 83, 26, 63, 17, 838, 246, 765, 1072, 729, 1407, 1096, 955, 775, 704, 855, 539, 768, -82, -345, 328 ], [1028, 823, 420, 595, 568, 596, 192, 647, 1312, 647, 991, 735, 1573, 449, -131, 1281, -271, -114, 947, -123, 242, 762, -40, 198, 906, 414, 723, 796, 881, 270], [639, 911, 132, 989, 850, 324, 934, 331, 307, 783, 244, 385, 683, -640, -309, 829, -483, -762, 570, 265, 748, 479, 125, 1035, 454, 101, 742, 566, 197, 622]])
    output = ( [1,0,0], [0,1,0], [0,0,1] )
    
    input = np.array(input)
    input = input.reshape(-1,30,1)
    output = np.array(output)
    
    l0 = tf.keras.layers.Dense(units=3, input_shape=[30,1], activation='relu')
    l1 = tf.keras.layers.Conv1D(filters=10, kernel_size=3, strides=1, padding='valid', activation='relu', kernel_initializer="glorot_uniform")
    flat = tf.keras.layers.Flatten()
    l2 = tf.keras.layers.Dense(units=3,activation='softmax')
    model = tf.keras.Sequential([l0, l1, flat, l2])
    model.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(0.1))
    model.fit(input, output, epochs=5)