# ---
# 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
# ---

# +
import os
import tensorflow as tf
import numpy as np
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras import models
from tensorflow.keras import layers
from sklearn.model_selection import KFold
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from tensorflow.keras import utils as np_utils
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import CSVLogger


from tensorflow.keras.callbacks import ModelCheckpoint,CSVLogger
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.utils import plot_model

# +
# # !pip install tensorflow-gpu
# -

import tensorflow as tf
if tf.test.gpu_device_name():
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
    print("Please install GPU version of TF")

orig_dir=os.getcwd()
orig_dir

# +
# Retrieve Full HOF Data
features_data_hof_full=[]
features_label_hof_full=[]

path="C:/Users/Ying/Downloads/LBP_TOP/HOF_cropped_classified"
folders=os.listdir(path)
t_count=0
for f in folders:
    count=1
    folder_path=os.path.join(path, f)
    files=os.listdir(folder_path)
    for file in files:
        file_path=os.path.join(folder_path,file)
        features=os.listdir(file_path)
        for feature in features:
            feature_path=os.path.join(file_path,feature)
            histogram=np.load(feature_path)
            histogram = np.mean(histogram, axis=0)
            features_data_hof_full.append(histogram)
            features_label_hof_full.append(f)
            count=count+1
            t_count=t_count+1
# -

x = features_data_hof_full
length = max(map(len, x))
features_data_hof_full = np.array([np.concatenate((xi,[0]*(length-len(xi)))) for xi in x])


# features_data_hof_full=np.reshape(features_data_hof_full,(10,15)).T
# print(features_data_hof_full.shape)
print(features_data_hof_full.shape)


# +
# X = features_data_hof_full
# y = features_label_hof_full

# +
# Retrieve Full LBP Data
features_data_l=[]
features_label_l=[]

path="C:/Users/Ying/Downloads/LBP_TOP/LBP_TOP_cropped_classified"
folders=os.listdir(path)
t_count=0
for f in folders:
    count=1
    folder_path=os.path.join(path, f)
    files=os.listdir(folder_path)
    for file in files:
        file_path=os.path.join(folder_path,file)
        features=os.listdir(file_path)
        for feature in features:
            feature_path=os.path.join(file_path,feature)
            histogram=np.load(feature_path)
            histogram = np.mean(histogram, axis=0)
            features_data_l.append(histogram)
            features_label_l.append(f)
            count=count+1
            t_count=t_count+1

# +
# Process to correct format for DNN
features_data_l=np.concatenate(features_data_l, axis=1).T

print(features_data_l.shape)


# +
# X = features_data_l
# y = features_label_l
# -

X = np.concatenate((features_data_hof_full,features_data_l),axis=1)
y = features_label_l

X.shape

# One hot encoder of labels
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(y)
dummy_y = np_utils.to_categorical(integer_encoded)

save_location='C:/Users/Ying/Downloads/LBP_TOP/Run_time_data'
modelname= 'RB_FF_NN13'
savelocation=os.path.join(save_location, modelname)
filepath=savelocation + ".hdf5"
csv_logger=CSVLogger(savelocation +'.csv')
#callbacks_list  = [checkpoint,csv_logger,LRScheduler]

model = Sequential()
model.add(Dense(512, activation='sigmoid', input_shape=(X.shape[1],)))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(512, activation='sigmoid'))
model.add(Dense(4, activation='softmax'))
model.summary()

callbacks=[csv_logger, ModelCheckpoint(filepath,monitor='val_loss', save_best_only=True)]

# + jupyter={"outputs_hidden": true} tags=[]
opt='RMSProp'
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
X_train, X_test, y_train, y_test = train_test_split(X, dummy_y, test_size = 0.20)
history = model.fit(X_train, y_train, batch_size=2, epochs=200, verbose=1,validation_split=0.005, callbacks=callbacks)
# -

yhat_classes = model.predict_classes(X_test, verbose=0)
y_test_original = y_test.argmax(1)
y_train_original = y_train.argmax(1)
labels = np.unique(y_test_original)
a= confusion_matrix(y_test_original, yhat_classes)
print(a)
print(classification_report(y_test_original, yhat_classes))

# +
from sklearn import svm
from sklearn.metrics import confusion_matrix
svm_model = svm.SVC(kernel = 'linear', C = 2,decision_function_shape='ovo').fit(X_train,y_train_original)

y_pred = svm_model.predict(X_test)

print(confusion_matrix(y_test_original, y_pred))

print("train score:",svm_model.score(X_train,y_train_original))
print("test score:",svm_model.score(X_test,y_test_original))
# -



StarcoderdataJupyterScriptsDedupFiltered