I ran into problems in tinyml workshop in Google Colab.
I have followed the tutorial for the gesture recognition, yet there were errors when I tried to use Google Colab
Here is the aforementioned tutorial:
https://docs.arduino.cc/tutorials/nano-33-ble-sense/get-started-with-machine-learning
In Google Colab, the error came up in the "Parse and prepare the data" section
Here the code for that section:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
print(f"TensorFlow version = {tf.__version__}\n")
# Set a fixed random seed value, for reproducibility, this will allow us to get
# the same random numbers each time the notebook is run
SEED = 1337
np.random.seed(SEED)
tf.random.set_seed(SEED)
# the list of gestures that data is available for
GESTURES = [
"punch",
"flex",
]
SAMPLES_PER_GESTURE = 119
NUM_GESTURES = len(GESTURES)
# create a one-hot encoded matrix that is used in the output
ONE_HOT_ENCODED_GESTURES = np.eye(NUM_GESTURES)
inputs = []
outputs = []
# read each csv file and push an input and output
for gesture_index in range(NUM_GESTURES):
gesture = GESTURES[gesture_index]
print(f"Processing index {gesture_index} for gesture '{gesture}'.")
output = ONE_HOT_ENCODED_GESTURES[gesture_index]
df = pd.read_csv("/content/" + gesture + ".csv")
# calculate the number of gesture recordings in the file
num_recordings = int(df.shape[0] / SAMPLES_PER_GESTURE)
print(f"\tThere are {num_recordings} recordings of the {gesture} gesture.")
for i in range(num_recordings):
tensor = []
for j in range(SAMPLES_PER_GESTURE):
index = i * SAMPLES_PER_GESTURE + j
# normalize the input data, between 0 to 1:
# - acceleration is between: -4 to +4
# - gyroscope is between: -2000 to +2000
tensor += [
(df['aX'][index] + 4) / 8,
(df['aY'][index] + 4) / 8,
(df['aZ'][index] + 4) / 8,
(df['gX'][index] + 2000) / 4000,
(df['gY'][index] + 2000) / 4000,
(df['gZ'][index] + 2000) / 4000
]
inputs.append(tensor)
outputs.append(output)
# convert the list to numpy array
inputs = np.array(inputs)
outputs = np.array(outputs)
print("Data set parsing and preparation complete.")
There was a red squiggly line on the line:
(df['aX'][index] + 4) / 8,
Not sure what it meant
Here is the error message:
TensorFlow version = 2.12.0
Processing index 0 for gesture 'punch'.
There are 11 recordings of the punch gesture.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3801 try:
-> 3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
4 frames
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'aX'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-3-8fc096c79e96> in <cell line: 31>()
50 # - gyroscope is between: -2000 to +2000
51 tensor += [
---> 52 (df['aX'][index] + 4) / 8,
53 (df['aY'][index] + 4) / 8,
54 (df['aZ'][index] + 4) / 8,
/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in __getitem__(self, key)
3805 if self.columns.nlevels > 1:
3806 return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809 indexer = [indexer]
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'aX'
There was also an error on the graphing part
Graphing part code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
filename = "punch.csv"
df = pd.read_csv("/content/" + filename)
index = range(1, len(df['aX']) + 1)
plt.rcParams["figure.figsize"] = (20,10)
plt.plot(index, df['aX'], 'g.', label='x', linestyle='solid', marker=',')
plt.plot(index, df['aY'], 'b.', label='y', linestyle='solid', marker=',')
plt.plot(index, df['aZ'], 'r.', label='z', linestyle='solid', marker=',')
plt.title("Acceleration")
plt.xlabel("Sample #")
plt.ylabel("Acceleration (G)")
plt.legend()
plt.show()
plt.plot(index, df['gX'], 'g.', label='x', linestyle='solid', marker=',')
plt.plot(index, df['gY'], 'b.', label='y', linestyle='solid', marker=',')
plt.plot(index, df['gZ'], 'r.', label='z', linestyle='solid', marker=',')
plt.title("Gyroscope")
plt.xlabel("Sample #")
plt.ylabel("Gyroscope (deg/sec)")
plt.legend()
plt.show()
And the corresponding error message:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3801 try:
-> 3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
4 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'aX'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'aX'
The only input I had were the flex.csv and punch.csv files which both had 11 samples (the tutorial said more than ten is enough) and were correctly placed the content folder.
Would someone kindly lend a hand? Please forgive me if I had made a rookie mistake, it is my first time in a forum and also Google Colab.