<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import Conv2D,Flatten,Dense,Dropout,Input,MaxPooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import plot_model
import pandas as pd
from zipfile import ZipFile

import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
data = np.load('/content/drive/MyDrive/Vision Datasets/ORL_faces.npz')
data
X_train = data['trainX']
X_test = data['testX']

x_train = np.array(X_train,dtype = 'float32')/255.0
x_test = np.array(X_test,dtype = 'float32')/255.0



y_train = data['trainY']
y_test = data['testY']

X_train.shape , y_train.shape
x_test.shape,y_test.shape
y_train
def plot_image(arr):
    plt.imshow(arr.reshape((112,92)),cmap = 'gray')
    plt.show()
x_train[0,:].shape
plot_image(x_train[1,:])
print("Label is ",y_train[1])
x_train,x_valid,y_train,y_valid = train_test_split(x_train,y_train, test_size = .1,random_state = 42)
im_rows = 112
im_cols = 92
im_shape = (im_rows,im_cols,1)
x_train  = x_train.reshape(x_train.shape[0],*im_shape)
x_valid  = x_valid.reshape(x_valid.shape[0],*im_shape)
x_test = x_test.reshape(x_test.shape[0],*im_shape)
print(x_train.shape , x_test.shape , x_valid.shape)
y_train.shape,y_valid.shape,y_test.shape
batch_size = 512
train_dataset = tf.data.Dataset.from_tensor_slices((x_train,y_train)).batch(512).shuffle(1024).prefetch(tf.data.AUTOTUNE)
test_dataset = tf.data.Dataset.from_tensor_slices((x_test,y_test)).batch(512).shuffle(1024).prefetch(tf.data.AUTOTUNE)
valid_dataset = tf.data.Dataset.from_tensor_slices((x_valid,y_valid)).batch(512).shuffle(1024).prefetch(tf.data.AUTOTUNE)
def face_model():

    inputs = Input(shape = im_shape)

    x = Conv2D(filters = 36 , kernel_size = 7 , activation = 'relu' )(inputs)
    x = MaxPooling2D(pool_size = 2)(x)
    x = Conv2D(filters = 54 , kernel_size = 5 , activation = 'relu' )(x)
    x = MaxPooling2D(pool_size = 2)(x)

    x = Flatten()(x)

    x = Dense(2028,activation = 'relu')(x)
    x = Dropout(0.5)(x)

    x = Dense(1024,activation = 'relu')(x)
    x = Dropout(0.5)(x)

    x = Dense(512,activation = 'relu')(x)
    x = Dropout(0.5)(x)

    outputs = Dense(20,activation = 'softmax')(x)

    model = Model(inputs = inputs , outputs = outputs)
    return model




model = face_model()
model.summary()
plot_model(model,show_shapes = True)
model.compile(loss = "sparse_categorical_crossentropy" , optimizer = 'adam',metrics = ['accuracy'])
history  = model.fit(train_dataset,validation_data = valid_dataset,epochs = 250)
model.evaluate(test_dataset)
print(history.history.keys())

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
import itertools
from sklearn.metrics import accuracy_score,classification_report
from keras.utils import np_utils
predicted =np.array(model.predict(x_test))



ynew = model.predict(x_test)
ynew = (np.argmax(ynew,axis = 1))

Acc=accuracy_score(y_test, ynew)
print("accuracy : ")
print(Acc)

cnf_matrix=confusion_matrix(np.array(y_test), ynew)

y_test1 = np_utils.to_categorical(y_test, 20)



def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        #print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    #print(cm)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()


print('Confusion matrix, without normalization')
print(cnf_matrix)

plt.figure()
plot_confusion_matrix(cnf_matrix[1:10,1:10], classes=[0,1,2,3,4,5,6,7,8,9],
                      title='Confusion matrix, without normalization')

plt.figure()
plot_confusion_matrix(cnf_matrix[11:20,11:20], classes=[10,11,12,13,14,15,16,17,18,19],
                      title='Confusion matrix, without normalization')

print("Confusion matrix:\n%s" % confusion_matrix(np.array(y_test), ynew))
print(classification_report(np.array(y_test), ynew))<jupyter_output>accuracy : 
0.95
Confusion matrix, without normalization
[[8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 4 0 0 0 2 0 0 0 0 0 0 0 0 2 0 0]
 [0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 2 0 6 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0]
 [0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 6 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8]]
Confusion matrix, without normalization
{'fraction_non_alphanumeric': 0.1328176431863068, 'fraction_numerical': 0.09052007899934167, 'mean_word_length': 3.569172932330827, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '410790', 'license': 'MIT', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2361, 'n_words': 727, 'path': 'Face_Recognition.ipynb', 'repo_name': 'prikshit-2000/Machine-Learning-Assignments', 'size': '6076'}