<jupyter_start><jupyter_text>Traning Model<jupyter_code>import tensorflow as tf
from tensorflow.keras.applications.inception_v3 import InceptionV3
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Transfer learning<jupyter_code>#Use (InceptionV3) model from keras
pre_trained_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(150, 150, 3))
pre_trained_model.summary()
#freeze the base model
pre_trained_model.trainable = False
    
last_layer = pre_trained_model.get_layer('mixed7')
print('Last layer output shape :', last_layer.output_shape)
last_output = last_layer.output
len(pre_trained_model.layers)
x = tf.keras.layers.Flatten()(last_output)
x = tf.keras.layers.Dense(1024, activation='relu')(x)
x = tf.keras.layers.Dropout(0.3)(x)
#The Final layer with 3 outputs for 3 categories
x = tf.keras.layers.Dense(3, activation='softmax')(x)

model = tf.keras.models.Model(inputs=pre_trained_model.input, outputs=x)

model.summary()
#load features and target numpy
features = np.load('../data/features.npy')
target = np.load('../data/target.npy')

print(features.shape, target.shape)
# Normalize
features = features / 255.
X_train, X_test, Y_train, Y_test = train_test_split(features, target, test_size=0.2)
epochs = 25
model.compile(optimizer='adam',
              loss=tf.losses.categorical_crossentropy,
              metrics=['acc'])

H = model.fit(X_train, 
                    Y_train, 
                    epochs=epochs, 
                    validation_split=0.12)
# PLOT LOSS AND ACCURACY
acc = H.history['acc']
val_acc = H.history['val_acc']
loss = H.history['loss']
val_loss = H.history['val_loss']

plt.figure(figsize=(15, 6))
plt.subplot(121)
plt.plot(acc, label='Training')
plt.plot(val_acc, label='Validation')
plt.title('Training and validation accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()

plt.subplot(122)
plt.plot(loss, label='Training')
plt.plot(val_loss, label='Validation')
plt.title('Training and validation loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.show()
model.save('../data/facemask-inceptionV3-model.h5')<jupyter_output><empty_output><jupyter_text>Test the model<jupyter_code>model.evaluate(X_test, Y_test)
Y_pred = np.argmax(model.predict(X_test), axis=1)

sns.heatmap(confusion_matrix(np.argmax(Y_test, axis=1), Y_pred), annot=True, fmt='g', cmap=plt.cm.Blues)
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.show()
from sklearn.metrics import classification_report

print(classification_report(np.argmax(Y_test, axis=1), Y_pred))
# plot the training loss and accuracy

import matplotlib.pyplot as plt

plt.style.use("ggplot")
plt.figure()

plt.plot(np.arange(0, epochs), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, epochs), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, epochs), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, epochs), H.history["val_acc"], label="val_acc")

plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>Test the best model we have.<jupyter_code>model = tf.keras.models.load_model('../data/best-facemask-inceptionV3-model.h5')
model.evaluate(X_test, Y_test)
Y_pred = np.argmax(model.predict(X_test), axis=1)

sns.heatmap(confusion_matrix(np.argmax(Y_test, axis=1), Y_pred), annot=True, fmt='g', cmap=plt.cm.Blues)
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.show()
from sklearn.metrics import classification_report

print(classification_report(np.argmax(Y_test, axis=1), Y_pred))
# plot the training loss and accuracy

import matplotlib.pyplot as plt

plt.style.use("ggplot")
plt.figure()

plt.plot(np.arange(0, epochs), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, epochs), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, epochs), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, epochs), H.history["val_acc"], label="val_acc")

plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15341701534170155, 'fraction_numerical': 0.013482101348210134, 'mean_word_length': 6.406196213425129, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '254528', 'license': 'MIT', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1558, 'n_words': 279, 'path': 'notebooks/Training-model.ipynb', 'repo_name': 'bnati5/FaceMe', 'size': '4302'}