<jupyter_start><jupyter_text>(1) Import all relevant libraries, layer and classes<jupyter_code># General
import os
import time

# Python libraries
import numpy as np, collections
import pydot as pyd
import matplotlib.pyplot as plt
from IPython.display import SVG

# Tensorflow
import tensorflow as tf

# Keras
from keras import optimizers
from keras import metrics, losses, regularizers
from keras import initializers
from keras.models import load_model, Sequential
from keras.layers import LSTM, Dense, Dropout, Flatten, TimeDistributed
from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, CSVLogger, EarlyStopping
from keras.utils import plot_model
from keras.utils.vis_utils import model_to_dot

# Scikit-learn
import sklearn.preprocessing as skprep
from sklearn import metrics
from sklearn.model_selection import train_test_split, StratifiedKFold

# CoreML 
import coremltools
import tfcoreml as tf_converter

# Keypoint loader
import auxilary_functions #, keypoint_reader, poses<jupyter_output>Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W0715 11:15:38.114793 139746819770176 __init__.py:71] TensorFlow version 1.14.0 detected. Last version known to be fully compatible is 1.13.1 .<jupyter_text>(2) Load and prepare goalkeeper pose data<jupyter_code>DATASET_PATH = "/Data/TrainingsVideo/Exercises/"

X_train_path = DATASET_PATH + "X_train.txt"
X_test_path = DATASET_PATH + "X_test.txt"

y_train_path = DATASET_PATH + "Y_train.txt"
y_test_path = DATASET_PATH + "Y_test.txt"

# load input/output trainings data
# X_train = [samples, timesteps, features]
# y_train = [samples, labels]
X_train, y_train, y = load_all_data()

#printPoseSequence(X_train[150][62][:])

# load input/output test data
#X_test = load_X(X_test_path)
#y_test = load_y(y_test_path)
# TODO<jupyter_output><empty_output><jupyter_text>(3) Define constants and additional parameters<jupyter_code># Input features
INPUT = [
    "HEAD_X",
    "HEAD_Y_",
    "BODY_X",
    "BODY_Y",
    "LEFT_ARM_X",
    "LEFT_ARM_Y",
    "RIGHT_ARM_X",
    "RIGHT_ARM_Y",
    "LEFT_LEG_X",
    "LEFT_LEG_Y",
    "RIGHT_LEG_X",
    "RIGHT_LEG_Y",
]

# Output classes
LABELS = [    
    "SHORT_LEFT_DIVE",
    "LEFT_DIVE",
    "LONG_LEFT_DIVE",
    "SHORT_RIGHT_DIVE",
    "RIGHT_DIVE",
    "LONG_RIGHT_DIVE",
    #"LOW_CATCH",
    #"MIDDLE_CATCH",
    #"HIGH_CATCH",
]

# Input Data
n_timesteps = 139 #len(X_train[1])  # n-timesteps per series per series
n_features = 24 #len(X_train[0][0])  # n input parameters per timestep

# LSTM Neural Network's internal structure
input_shape = (n_timesteps, n_features)
n_mem_units = 69 # Hidden layer num of features
n_classes = len(LABELS) # n classes (should go up, or should go down)

# Training - Hyperparameter  
learning_rate = 0.004 
optimizer = optimizers.RMSprop(lr=learning_rate, decay=256e-4) 
n_epochs = 10  
batch_size = 48

# Some debugging info
print("Bsic information regardin the data:")
print("---------------------------------------")
print("Trainings data...")
print("... input shape=" + str(X_train.shape))
print("... input mean=" + str(np.mean(X_train)))
print("... input std=" + str(np.std(X_train)))
print("... target shape=" + str(y_train.shape))
print("---------------------------------------")
#print("Test data...")
#print("... input shape=" + str(X_test.shape))
#print("... input mean=" + str(np.mean(X_test)))
#print("... input std=" + str(np.std(X_test)))
#print("... target shape=" + str(y_train.shape))
#print("---------------------------------------")<jupyter_output><empty_output><jupyter_text>(4) Define and build sequence model<jupyter_code>model = create_model(input_shape, n_mem_units, n_classes, optimizer=optimizer, loss=losses.categorical_crossentropy, init='glorot_uniform')

# Print model details/summary
model.summary()

# Build model 
model.compile(loss=losses.categorical_crossentropy, 
              optimizer=optimizer,
              metrics=['mse', 'accuracy'])  # for binary classfication



# Visualize model
plot_model(model, to_file='./training_history/model_visualize/model_' + time_stamp() + '.png', show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>(5) Record and store process of models/training<jupyter_code># Tensorboard
logdir = './full_approach_training_history/logs/' + time_stamp()

## Check whether the target directoy exist and 
if not os.path.exists(logdir):
    os.makedirs(logdir)
    
## Create callback for tensorboard/trainings-history with the path to the logs directory
tensorboard = tf.keras.callbacks.TensorBoard(log_dir=logdir)

# Model saving
## Create directory which will contain trained models 
model_directory = "./full_approach_training_history/saved_models/" + time_stamp()

## Check whether the target directoy exist and 
if not os.path.exists(model_directory):
    os.makedirs(model_directory)

## Create callback which stores model-weights checkpoints
checkpointer = ModelCheckpoint(filepath=model_directory + '/weights.{epoch:03d}-{val_loss:.2f}.hdf5', save_best_only=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=10, min_lr=0.001)
#earlyStopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=10, baseline=0.8, restore_best_weights=True)
csv_logger = CSVLogger(model_directory + '/training.log')

# Generate callback list 
callbacks = [tensorboard, checkpointer, reduce_lr]<jupyter_output><empty_output><jupyter_text>(6) Train model<jupyter_code># Train model on trainings data 
   
history = model.fit(X_train, 
                    y_train, 
                    verbose=1, 
                    shuffle=True, 
                    epochs=n_epochs, 
                    validation_split=0.1, 
                    batch_size=batch_size, 
                    callbacks=callbacks)

print(model.metrics_names)
print(model.evaluate(X_train, y_train))<jupyter_output><empty_output><jupyter_text>(7) Predict and evaluate<jupyter_code># Plot history 
def plot_loss_acc(model):
  color_red = 'tab:red'
  color_blue = 'tab:blue'

  epochs=np.array(model.history.epoch)+1 # Add one to the list of epochs which is zero-indexed

  loss=np.array(model.history.history['loss'])
  acc=np.array(model.history.history['acc'])
    
  val_loss=np.array(model.history.history['val_loss'])
  val_acc=np.array(model.history.history['val_acc'])
  
  # Create subplots
  fig, (ax1, ax3) = plt.subplots(1,2, figsize=(20,6))

  # TRAING DIAGRAM
  ax1.title.set_text('TRAININGS DATA')
  
  ax1.set_xlabel('Epochs',fontsize=15)
  ax1.set_ylabel('Loss', color=color_red,fontsize=15)
  ax1.plot(epochs, loss, color=color_red,lw=2)
  ax1.tick_params(axis='y', labelcolor=color_red)
  ax1.grid(True)

  ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
  ax2.set_ylabel('Accuracy', color=color_blue,fontsize=15)
  ax2.plot(epochs, acc, color=color_blue,lw=2)
  ax2.tick_params(axis='y', labelcolor=color_blue)

  # VALIDATION DIAGRAM
  ax3.title.set_text('VALIDATION DATA')
  
  ax3.set_xlabel('Epochs',fontsize=15)
  ax3.set_ylabel('Validation Loss', color=color_red,fontsize=15)
  ax3.plot(epochs, val_loss, dashes=[2, 2, 10, 2], color=color_red,lw=2)
  ax3.tick_params(axis='y', labelcolor=color_red)
  ax3.grid(True)

  ax4 = ax3.twinx()  # instantiate a second axes that shares the same x-axis
  ax4.set_ylabel('Validation Accuracy', color=color_blue,fontsize=15)
  ax4.plot(epochs, val_acc, dashes=[2, 2, 10, 2], color=color_blue,lw=2)
  ax4.tick_params(axis='y', labelcolor=color_blue)
  fig.tight_layout()

  plt.legend(['Train', 'Test'], loc='upper right')
  plt.show()


plot_loss_acc(model)

# Plot evaluation
# evaluate the model
train_acc = model.evaluate(X_train, y_train, verbose=0)
test_acc = model.evaluate(X_train, y_train, verbose=1)
print('Train accuracy: ' + str(train_acc))
print('Test accuracy: ' + str(test_acc))<jupyter_output><empty_output><jupyter_text>(8) Save model<jupyter_code># Save trained model
saved_model = model_directory + "/model_" + time_stamp() + ".h5"
model.save(saved_model)

# Convert trained model into tflite model 
#converter = tf.lite.TFLiteConverter.from_saved_model('./trained_model.h5')
#tflite_model = converter.convert()
#open(model_directory + "/lite_model.tflite", "wb").write(tflite_model)


# Convert trained model into CoreML
coreml_model = coremltools.converters.keras.convert(saved_model, 
                                                    class_labels=LABELS, 
                                                    input_names=['pose'])

#tf_converter.convert(tf_model_path=saved_model,
#                     mlmodel_path='coreml_model.mlmodel',
#                     output_feature_names=['softmax'],
#                     input_name_shape_dict={'input': [1, 227, 227, 3]},
#                     use_coreml_3=True)

print(coreml_model)

# Store CoreML model 
coreml_model.save(model_directory + "/mlmodel_" + time_stamp() + ".mlmodel")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13326619671030548, 'fraction_numerical': 0.018686360076088173, 'mean_word_length': 4.339307048984469, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '625850', 'license': 'MIT', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3083, 'n_words': 634, 'path': '.ipynb_checkpoints/Full ARE with Search-checkpoint.ipynb', 'repo_name': 'SamiIbishi/iOS-Praktikum-GOALPLAY', 'size': '8937'}