# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="payawHPMIJsw" colab_type="text"
# #### Next two cells are only needed for a Google Colab environment.

# + colab_type="code" id="uH_MN7TG48nz" colab={}
from google.colab import drive
drive.mount('/content/drive')

# + colab_type="code" id="kZkj5TG-HlJk" colab={"base_uri": "https://localhost:8080/", "height": 0} outputId="002ef393-3770-46b6-c17e-7e98016c6936" executionInfo={"status": "ok", "timestamp": 1565059344830, "user_tz": 420, "elapsed": 897, "user": {"displayName": "<NAME>", "photoUrl": "https://lh5.googleusercontent.com/-bIpqbmaPLJc/AAAAAAAAAAI/AAAAAAAAABM/KNhnIL0QRiQ/s64/photo.jpg", "userId": "13752494364938472639"}}
# %cd '/content/drive/My Drive/CodingProjects/skateboard_trick_classification'

# + colab_type="code" id="KWWb3-1_j4eM" colab={}
import numpy as np

from keras.callbacks import EarlyStopping
from keras.layers import concatenate, Dense, Dropout, GlobalAveragePooling3D, Softmax
from keras.models import load_model, Model
from keras.optimizers import Adam
from sklearn.metrics import classification_report, confusion_matrix

from utils import config
from utils.data_generator import DataGenerator
from utils.i3d_inception import Inception_Inflated3d

# + [markdown] id="QBeuNIe-iY-_" colab_type="text"
# ## Data Generators

# + colab_type="code" id="ALnDTlKj6J0c" colab={}
training_generator = DataGenerator(config.VIDEO_TRAINING_DIR, 
                                   config.RGB_TRAINING_BATCH_SIZE, 
                                   is_training=True, rgb_data_only=True)

validation_generator = DataGenerator(config.VIDEO_VALIDATION_DIR, 
                                     config.RGB_VALIDATION_BATCH_SIZE, 
                                     is_training=False, rgb_data_only=True)

test_generator = DataGenerator(config.VIDEO_TEST_DIR, 
                               config.RGB_TEST_BATCH_SIZE, 
                               is_training=False, rgb_data_only=True)

# + [markdown] colab_type="text" id="vVnPjYfZbVgP"
# # Train RGB Model

# + colab_type="code" id="U1RjInS8PxEe" colab={}
input_shape = (None, config.RGB_FRAME_HEIGHT, config.RGB_FRAME_WIDTH, config.CHANNELS)
i3d_model = Inception_Inflated3d(include_top=False, weights='rgb_imagenet_and_kinetics', 
                                 input_shape=input_shape, 
                                 classes=config.RGB_N_CLASSES)
x = Dropout(0.5)(i3d_model.output)
x = Dense(config.RGB_N_CLASSES, name='rgb_predictions')(x)
output = Softmax()(x)

model = Model(inputs=i3d_model.input, outputs=output)
model.compile(optimizer=Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# + colab_type="code" id="CMBlJUpVPc2r" colab={}
early_stopping = EarlyStopping(patience=4, restore_best_weights=True, verbose=1)
model.fit_generator(training_generator, 
                    epochs=100,
                    validation_data=validation_generator, 
                    callbacks=[early_stopping])

# + colab_type="code" id="KqYzbhv0S_mR" colab={}
# recompiling the model will reduce the size of the saved model by resetting the optimizer state
model.compile(optimizer=Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.save(config.RGB_MODEL_FILEPATH)

# + [markdown] colab_type="text" id="5J9LM1VUe2B8"
# # Evaluate Model

# + colab_type="code" id="BW7MvDHkzTGW" colab={}
model = load_model(config.RGB_MODEL_FILEPATH)

# + [markdown] id="CMULp_xS2b9d" colab_type="text"
# ## Validation Set

# + id="wd7tUcC62f5x" colab_type="code" outputId="efd52c8e-10d6-413d-d6ed-7023b68a1d7c" executionInfo={"status": "ok", "timestamp": 1564961730983, "user_tz": 420, "elapsed": 178812, "user": {"displayName": "<NAME>", "photoUrl": "https://lh5.googleusercontent.com/-bIpqbmaPLJc/AAAAAAAAAAI/AAAAAAAAABM/KNhnIL0QRiQ/s64/photo.jpg", "userId": "13752494364938472639"}} colab={"base_uri": "https://localhost:8080/", "height": 374}
y_true = validation_generator.labels
predictions = model.predict_generator(validation_generator)
y_pred = np.argmax(predictions, axis=1)

report = classification_report(y_true, y_pred, 
                               target_names=config.RGB_CLASS_NAMES, 
                               digits=4)
print(report)
print(confusion_matrix(y_true, y_pred))

# + [markdown] id="T9zJsjIF2hYs" colab_type="text"
# ## Test Set

# + id="_eSR7m0Ltg5B" colab_type="code" outputId="248c636f-9ce0-46fc-d58d-dce562017461" executionInfo={"status": "ok", "timestamp": 1565059505555, "user_tz": 420, "elapsed": 146792, "user": {"displayName": "<NAME>", "photoUrl": "https://lh5.googleusercontent.com/-bIpqbmaPLJc/AAAAAAAAAAI/AAAAAAAAABM/KNhnIL0QRiQ/s64/photo.jpg", "userId": "13752494364938472639"}} colab={"base_uri": "https://localhost:8080/", "height": 374}
y_true = test_generator.labels
predictions = model.predict_generator(test_generator)
y_pred = np.argmax(predictions, axis=1)

report = classification_report(y_true, y_pred, 
                               target_names=config.RGB_CLASS_NAMES, 
                               digits=4)
print(report)
print(confusion_matrix(y_true, y_pred))

StarcoderdataJupyterScriptsDedupFiltered