<jupyter_start><jupyter_text>Load the .pkl file for train and CV containing the images array and their actual labels.<jupyter_code>#Path to the .pkl files for train and cv
x_train=joblib.load('/content/drive/MyDrive/Hashim/Final/x_train.pkl')
y_train=joblib.load('/content/drive/MyDrive/Hashim/Final/y_train.pkl')
y_cv=joblib.load('/content/drive/MyDrive/Hashim/Final/y_cv.pkl')
x_cv=joblib.load('/content/drive/MyDrive/Hashim/Final/x_cv.pkl')
print ("DONE")
print("Shape of Train data {}".format(x_train.shape))
print("Shape of Cv data {}".format(x_cv.shape))<jupyter_output>Shape of Train data (16126, 299, 299, 3)
Shape of Cv data (3952, 299, 299, 3)<jupyter_text>Conversion of lables from test and cv to categorical to fit the shape of the output layer.<jupyter_code>#Conversion of lables from single array to shape(-1,2) for categorical output
Y_train=to_categorical(y_train)
Y_cv = to_categorical(y_cv)
print (Y_train.shape)
print (Y_cv.shape)<jupyter_output>(16126, 2)
(3952, 2)<jupyter_text>**The training of the model starts here. **
 The steps followed to train the model are as follow: **bold text**
1.  Import the Xception base model with imagenet weights without the final and polling layer.
2.  To fit the weights of the model, according to our training data, its hidden layer training is made True.
3.  Add global average and a new output layer with 2 outputs on top of the base model.
4.  Train the model for three epochs on our training data to adjust its weights.
5.  Freeze the weights of the model.
6.  Remove the output layer and replace it with the fresh output layer with 2 outputs.
7.  Train the model for another 15 epochs.
8.  Save the model and evaluate its performance.<jupyter_code>#Import Xception pretrained model with imagenet weights
base_model=Xception(include_top=False,
           weights='imagenet',
           input_shape=(299,299,3)
           )
# Unfreeze the base_model
base_model.trainable = True
#Creat a new model sequence
model = keras.Sequential()
#add base model to it
model.add(base_model)
#add average polling layer
model.add(keras.layers.GlobalAveragePooling2D())
#add new output layer with softmax activation
model.add(Dense(2,activation ='softmax'))

#summary of model
model.summary()
#compile the model with parameters from FaceForensic++ paper hyperparameters
model.compile(loss=keras.losses.categorical_crossentropy,
          optimizer=keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.9, beta_2=0.999, epsilon=1e-08),
          metrics=['accuracy'])
#Train the model on our traing data for 3 epochs to adjust irs weights
pretraining_Xception =model.fit(x_train, Y_train,
                                verbose=1,
                                batch_size=32,  
                                epochs=3)
#Freez the model
model.trainable = False
model.summary()
#Creat a new sequence model the previous model to it with out last output layer
model_post = keras.Sequential()
for layer in model.layers[:-1]: # go through until last layer
    model_post.add(layer)

#model_post.trainable = False
model_post.summary()
#Add new output layer to new sequence
model_post.add(Dense(2, activation='softmax'))
model_post.summary()
#Compile the new sequence using the same hyperparameters
model_post.compile(loss=keras.losses.categorical_crossentropy,
          optimizer=keras.optimizers.Adam(learning_rate=0.0002, beta_1=0.9, beta_2=0.999, epsilon=1e-08),
          metrics=['accuracy'])
#Train and evaluate the model for 15 epochs
finetuning_XceptionNet=model_post.fit(x_train, Y_train,verbose=1,batch_size=32,
                     epochs=15,
                     validation_data=(x_cv, Y_cv)
                      )
#Plot the training and evaluation accuracy and loss curves
i =0
EPOCHS = 15
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 4))
t = f.suptitle('Pre-trained Xception with Transfer Learning ', fontsize=12)
f.subplots_adjust(top=0.85, wspace=0.3)

epoch_list = list(range(1,EPOCHS+1))

print (epoch_list)
ax1.plot(epoch_list, finetuning_XceptionNet.history['accuracy'], label='Train Accuracy')
ax1.plot(epoch_list, finetuning_XceptionNet.history['val_accuracy'], label='Validation Accuracy',linestyle='dashed')
ax1.set_xticks(np.arange(0, EPOCHS+1, 1))
ax1.set_ylabel('Accuracy Value')
ax1.set_xlabel('Epoch #')
ax1.set_title('Accuracy')
l1 = ax1.legend(loc="best")

ax2.plot(epoch_list, finetuning_XceptionNet.history['loss'], label='Train Loss')
ax2.plot(epoch_list, finetuning_XceptionNet.history['val_loss'], label='Validation Loss',linestyle='dashed')
ax2.set_xticks(np.arange(0, EPOCHS+1, 1))
ax2.set_ylabel('Loss Value')
ax2.set_xlabel('Epoch #')
ax2.set_title('Loss')
l2 = ax2.legend(loc="best")
#Save the model
model_post.save("/content/drive/MyDrive/Hashim/MXXCEPTIONFINALMODEL/Xception Fnal Last ry/model_finetuned_xception.hdf5")<jupyter_output><empty_output><jupyter_text>Save the training parameters to a csv file<jupyter_code>import pandas as pd

pd.DataFrame.from_dict(finetuning_XceptionNet.history).to_csv('/content/drive/MyDrive/Hashim/MXXCEPTIONFINALMODEL/Xception Fnal Last ry/history variables.csv',index=False)
#Evaluate model performance on train data
model_post.evaluate(x_train,Y_train)<jupyter_output>504/504 [==============================] - 39s 76ms/step - loss: 0.0012 - accuracy: 0.9998<jupyter_text>Evaluate model performance on cv data<jupyter_code>model_post.evaluate(x_cv,Y_cv)<jupyter_output><empty_output><jupyter_text>**Cration of confusion matrix for the training and CV data**<jupyter_code>from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import seaborn as sn
import pandas as pd
model_Xc1 = models.load_model('/content/drive/MyDrive/Hashim/MXXCEPTIONFINALMODEL/Xception Fnal Last ry/model_finetuned_xception.hdf5')
#Output confusion matrix
def print_confusion_matrix(y_true, y_pred):
    cm = confusion_matrix(y_true, y_pred)
    print('True positive = ', cm[0][0])
    print('False positive = ', cm[0][1])
    print('False negative = ', cm[1][0])
    print('True negative = ', cm[1][1])
    print('\n')
    df_cm = pd.DataFrame(cm, range(2), range(2))
    sn.set(font_scale=1.4) # for label size
    sn.heatmap(df_cm, annot=True, annot_kws={"size": 16}, fmt='g') # font size
    plt.ylabel('Actual label', size = 20)
    plt.xlabel('Predicted label', size = 20)
    plt.xticks(np.arange(2), ['Real', 'Fake'], size = 16)
    plt.yticks(np.arange(2), ['Real', 'Fake'], size = 16)
    plt.ylim([2, 0])
    plt.show()
    
print_confusion_matrix(y_cv, model_Xc1.predict_classes(x_cv))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11718284693269804, 'fraction_numerical': 0.028886241810601548, 'mean_word_length': 4.555831265508685, 'pattern_counts': {'":': 1, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '410874', 'license': 'MIT', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2293, 'n_words': 569, 'path': 'Model Training.ipynb', 'repo_name': 'Zahak-Anjum/DeepFake-Detection-Using-Xception-Net', 'size': '6716'}