<jupyter_start><jupyter_text>**Installing latest pickle to unserialize our data from database**<jupyter_code>!pip install pickle5<jupyter_output>Collecting pickle5
[?25l  Downloading https://files.pythonhosted.org/packages/f7/4c/5c4dd0462c8d3a6bc4af500a6af240763c2ebd1efdc736fc2c946d44b70a/pickle5-0.0.11.tar.gz (132kB)

[K     |██▌                             | 10kB 21.7MB/s eta 0:00:01
[K     |█████                           | 20kB 29.3MB/s eta 0:00:01
[K     |███████▍                        | 30kB 35.1MB/s eta 0:00:01
[K     |██████████                      | 40kB 32.9MB/s eta 0:00:01
[K     |████████████▍                   | 51kB 34.6MB/s eta 0:00:01
[K     |██████████████▉                 | 61kB 36.7MB/s eta 0:00:01
[K     |█████████████████▍              | 71kB 25.8MB/s eta 0:00:01
[K     |███████████████████▉            | 81kB 23.1MB/s eta 0:00:01
[K     |██████████████████████▎         | 92kB 24.6MB/s eta 0:00:01
[K     |████████████████████████▉       | 102kB 22.3MB/s eta 0:00:01
[K     |███████████████████████████▎    | 112kB 22.3MB/s eta 0:00:01
[K     |█████████████████████████████▊  | 122kB 22.3MB/s eta [...]<jupyter_text>Connecting to drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Mounted at /content/drive<jupyter_text>**Adding needed imports**<jupyter_code>import os
import  pickle5 as pickle
from HandReading import HandReading
from Imu import Imu
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, SimpleRNN, Dropout, LSTM, GRU
from sklearn.metrics import accuracy_score as acc
from tensorflow.keras.utils import plot_model
from sklearn.metrics import classification_report
from tensorflow.keras.callbacks import EarlyStopping<jupyter_output><empty_output><jupyter_text>**Creating dictionary mappers**<jupyter_code>dict_word_id = {}
dict_id_word = {}

list_word = os.listdir('/content/drive/MyDrive/database') 
for j in range(len(list_word)) : 
  dict_word_id[list_word[j]] = j
  dict_id_word[j] = list_word[j]
print(dict_word_id)
print(dict_id_word)<jupyter_output>{'oi': 0, 'tchau': 1, 'abdullah': 2, 'prazer-em-te-conhecer': 3, 'silencio': 4, 'lucas': 5, '23': 6, 'qual-seu-nome': 7, 'qual-a-sua-idade': 8, 'meu-nome-e': 9, 'tudo-bem': 10}
{0: 'oi', 1: 'tchau', 2: 'abdullah', 3: 'prazer-em-te-conhecer', 4: 'silencio', 5: 'lucas', 6: '23', 7: 'qual-seu-nome', 8: 'qual-a-sua-idade', 9: 'meu-nome-e', 10: 'tudo-bem'}<jupyter_text>**Preparing input and labels data**<jupyter_code>input_data, output = [], []

list_word = os.listdir('/content/drive/MyDrive/database') 

for word in list_word:
  list_pickle_file = os.listdir(f'/content/drive/MyDrive/database/{word}')
  k = 0 
  for pickle_file in list_pickle_file:
    k+=1
    with open(f'/content/drive/MyDrive/database/{word}/{pickle_file}', 'rb') as input:
      readings = pickle.load(input)
      for reading in readings:
        finger = []
        for i in range(5):
          finger.append(np.float16(reading.imus[i].accel[0]/255.0))
          finger.append(np.float16(reading.imus[i].accel[1]/255.0))
          finger.append(np.float16(reading.imus[i].accel[2]/255.0))
          finger.append(np.float16(reading.imus[i].gyro[0]/255.0))
          finger.append(np.float16(reading.imus[i].gyro[1]/255.0))
          finger.append(np.float16(reading.imus[i].gyro[2]/255.0))
          pass
      
        input_data.append(finger)
        
        out = np.zeros(11)
        out[dict_word_id[word]] = 1.0
        output.append(out)

input_data = np.array(input_data)
input_data = input_data.reshape((input_data.shape[0], input_data.shape[1], 1))
output = np.array(output)<jupyter_output><empty_output><jupyter_text>**Creating neural network model**<jupyter_code>model = Sequential()
model.add(GRU(units=50, input_shape=input_data[0].shape))
model.add(Dense(11, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
gru (GRU)                    (None, 50)                7950      
_________________________________________________________________
dense (Dense)                (None, 11)                561       
=================================================================
Total params: 8,511
Trainable params: 8,511
Non-trainable params: 0
_________________________________________________________________<jupyter_text>**Separate data and train**<jupyter_code>from sklearn.model_selection import StratifiedKFold
kfoldK = 10
kfold = StratifiedKFold(n_splits=10, shuffle=True)
callback = EarlyStopping(monitor='val_accuracy', min_delta = 0.001, patience=3)
iterations = 0 
sumOfAccuracy = 0
maxepoch = 100
for train, test in kfold.split(input_data, output.argmax(1)):
  iterations += 1
  trainX, testX = input_data[train], input_data[test]
  trainY, testY = output[train], output[test]

  trainX, validX, trainY, validY = train_test_split(
    trainX, trainY, test_size=0.3, stratify=trainY, random_state=42)
  
  model.fit(trainX, trainY, epochs=maxepoch, batch_size=32, verbose=2, callbacks=[callback], validation_data=(validX, validY))
  predicted = model.predict(testX)
  pred = []
  for j in predicted:
    arg = np.argmax(j)
    pred.append(arg)
  pred = np.array(pred)
  y_one = []
  for y in testY :
    arg = np.argmax(y)
    y_one.append(arg)
  y_one = np.array(y_one)
  sumOfAccuracy += acc(y_one,pred)
  avgAccuray = sumOfAccuracy/iterations
  print(f"average accuracy = {avgAccuray}")<jupyter_output>Epoch 1/100
4119/4119 - 19s - loss: 2.0997 - accuracy: 0.2239 - val_loss: 1.9373 - val_accuracy: 0.2786
Epoch 2/100
4119/4119 - 17s - loss: 1.8494 - accuracy: 0.3102 - val_loss: 1.7741 - val_accuracy: 0.3361
Epoch 3/100
4119/4119 - 17s - loss: 1.7115 - accuracy: 0.3592 - val_loss: 1.6644 - val_accuracy: 0.3760
Epoch 4/100
4119/4119 - 17s - loss: 1.6277 - accuracy: 0.3894 - val_loss: 1.6121 - val_accuracy: 0.3907
Epoch 5/100
4119/4119 - 17s - loss: 1.5756 - accuracy: 0.4073 - val_loss: 1.5773 - val_accuracy: 0.4042
Epoch 6/100
4119/4119 - 17s - loss: 1.5402 - accuracy: 0.4197 - val_loss: 1.5420 - val_accuracy: 0.4207
Epoch 7/100
4119/4119 - 17s - loss: 1.5148 - accuracy: 0.4281 - val_loss: 1.5149 - val_accuracy: 0.4254
Epoch 8/100
4119/4119 - 16s - loss: 1.4938 - accuracy: 0.4349 - val_loss: 1.4936 - val_accuracy: 0.4354
Epoch 9/100
4119/4119 - 16s - loss: 1.4761 - accuracy: 0.4414 - val_loss: 1.4886 - val_accuracy: 0.4376
Epoch 10/100
4119/4119 - 16s - loss: 1.4636 - accuracy: 0.4461 -[...]<jupyter_text>**Analyse accuracies**<jupyter_code>prd = model.predict(testX)
pred = []
for j in range(len(prd)):
  lis = prd[j]
  arg = np.argmax(lis)
  pred.append(arg)
pred = np.array(pred)

y_one = []
for y in testY :
  arg = np.argmax(y)
  y_one.append(arg)
y_one = np.array(y_one)

print(classification_report(y_one, pred))
print(dict_id_word)
print()
print(acc(y_one,pred))<jupyter_output>precision    recall  f1-score   support

           0       0.54      0.39      0.45      1902
           1       0.76      0.52      0.62      1902
           2       0.64      0.49      0.56      1902
           3       0.22      0.32      0.26      1902
           4       1.00      1.00      1.00      1902
           5       0.59      0.50      0.54      1902
           6       0.90      0.48      0.63      1902
           7       0.74      0.37      0.49      1902
           8       0.20      0.60      0.30      1902
           9       0.89      0.42      0.57      1902
          10       0.40      0.41      0.41      1902

    accuracy                           0.50     20922
   macro avg       0.63      0.50      0.53     20922
weighted avg       0.63      0.50      0.53     20922

{0: 'oi', 1: 'tchau', 2: 'abdullah', 3: 'prazer-em-te-conhecer', 4: 'silencio', 5: 'lucas', 6: '23', 7: 'qual-seu-nome', 8: 'qual-a-sua-idade', 9: 'meu-nome-e', 10: 'tudo-bem'}

0.4995698[...]<jupyter_text>**Real life usage example**<jupyter_code>with open(f'/content/drive/MyDrive/database/tchau/11.pkl', 'rb') as input:
  readings = pickle.load(input)
  input_test_data = []
  j = 0
  for reading in readings:
    finger = []
    for i in range(5):
      finger.append(np.float16(reading.imus[i].accel[0]/255.0))
      finger.append(np.float16(reading.imus[i].accel[1]/255.0))
      finger.append(np.float16(reading.imus[i].accel[2]/255.0))
      finger.append(np.float16(reading.imus[i].gyro[0]/255.0))
      finger.append(np.float16(reading.imus[i].gyro[1]/255.0))
      finger.append(np.float16(reading.imus[i].gyro[2]/255.0))
    
  
    input_test_data.append(finger)
    
input_test_data = np.array(input_test_data)
input_test_data = input_test_data.reshape((input_test_data.shape[0], input_test_data.shape[1], 1))

for inputData in input_test_data:
  result1 = model.predict(inputData.reshape(1,30,1))
  print(dict_id_word[np.argmax(result1)])<jupyter_output>prazer-em-te-conhecer
prazer-em-te-conhecer
abdullah
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
lucas
oi
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
tchau
qual-a-sua-idade
qual-a-sua-idade
lucas
qual-a-sua-idade
lucas
tudo-bem
qual-a-sua-idade
prazer-em-te-conhecer
tudo-bem
qual-a-sua-idade
qual-a-sua-idade
prazer-em-te-conhecer
oi
oi
prazer-em-te-conhecer
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
lucas
qual-a-sua-idade
qual-seu-nome
qual-a-sua-idade
qual-a-sua-idade
prazer-em-te-conhecer
prazer-em-te-conhecer
qual-a-sua-idade
oi
tudo-bem
tudo-bem
tudo-bem
qual-a-sua-idade
prazer-em-te-conhecer
oi
tudo-bem
qual-a-sua-idade
tudo-bem
oi
tudo-bem
tudo-bem
tudo-bem
prazer-em-te-conhecer
prazer-em-te-conhecer
prazer-em-te-conhecer
qual-a-sua-idade
qual-seu-nome
prazer-em-te-conhecer
oi
oi
prazer-em-te-conhecer
prazer-em-te-conhecer
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
qual-a-sua-idade
tudo-bem
qual-a-su[...]<jupyter_text>**Plot model structure**<jupyter_code>plot_model(model, to_file='topology.png',show_shapes=True)
model.save(f"simple-gru-{maxepoch}maxepoch-k{kfoldK}fold-{avgAccuray}avgacc.h5", save_format='.h5')
model.save(f"simple-gru-{maxepoch}maxepoch-k{kfoldK}fold-{avgAccuray}avgacc")<jupyter_output>INFO:tensorflow:Assets written to: simple-gru-100maxepoch-k10fold-0.48624414491922385avgacc/assets
{'fraction_non_alphanumeric': 0.16568213783403657, 'fraction_numerical': 0.08616971401781529, 'mean_word_length': 3.1992125984251967, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '369816', 'license': 'MIT', 'n_tokens_mistral': 4978, 'n_tokens_neox': 4261, 'n_words': 748, 'path': 'kfold-gru-simple.ipynb', 'repo_name': 'abdullah-zaiter/Recurrent-NN-Sign-Language', 'size': '10665'}