<jupyter_start><jupyter_text>CONFIDENCE MEASURE FOR VIRTUAL INTERVIEWS<jupyter_code>import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import cv2
from glob import glob
import os
import time
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import EarlyStopping
# from google.colab import drive
# drive.mount('/content/drive/')
base_dir = 'train/'
_dir  = ['confident','unconfident' ]
fig = plt.figure(figsize =(20, 32))
file = glob(os.path.join(base_dir, _dir[0], 'Training_11204.jpg'))
img = cv2.imread(file[0])
print("Original Size of Image is ", img.shape)
ax = fig.add_subplot(10, 2,1)
imgplot = plt.imshow(img)
resized_img = cv2.resize(img, (64, 64))
print("Resized Size of Image is ", resized_img.shape)
ax = fig.add_subplot(10, 2,2)
imgplot = plt.imshow(resized_img)
plt.show()<jupyter_output>Original Size of Image is  (48, 48, 3)
Resized Size of Image is  (64, 64, 3)<jupyter_text>**Loading Image**<jupyter_code>train_images = [] 
train_labels = []
print('Loading directory' + base_dir + '{}'.format(_dir[0]))
files = glob(os.path.join(base_dir, _dir[0], '*.jpg'))
for file in files:
  img = cv2.imread(file)
  train_images.append(cv2.resize(img, (64,64)))
  train_labels.append(0)
print('Loading directory' + base_dir + '{}'.format(_dir[1]))
files = glob(os.path.join(base_dir, _dir[1], '*.jpg'))
for file in files:
  img = cv2.imread(file)
  train_images.append(cv2.resize(img, (64,64)))
  train_labels.append(1)

len(train_images), len(train_labels)<jupyter_output>Loading directorytrain/unconfident<jupyter_text>Training And Splitting<jupyter_code>x_train, x_test, y_train, y_test =  train_test_split(train_images,train_labels, test_size = 0.2, stratify=train_labels)
y_train = tf.keras.utils.to_categorical(y_train, num_classes=2)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=2)

x_train, y_train = np.asarray(x_train), np.asarray(y_train)
x_test, y_test = np.asarray(x_test), np.asarray(y_test)
print(x_train.shape, y_train.shape)<jupyter_output>(22965, 64, 64, 3) (22965, 2)<jupyter_text>Model -1<jupyter_code>model = Sequential()

size = 32
model.add(Conv2D(size, (3, 3), padding='same', input_shape=(64, 64,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(size*2, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(size*4, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(size*8, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))


model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('sigmoid'))#softmax is used to get maximun value in a row

# initiate RMSprop optimizer
opt = tf.keras.optimizers.SGD(lr=0.0005)

# Let's train the model using RMSprop
model.compile(loss='binary_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

model.summary()
#these will print summary of the model
start = time.time()
history = model.fit(x_train, y_train, 
          validation_data=(x_test, y_test),
          epochs=40, batch_size=512, )
stop = time.time()
print(f"Training time: {stop - start}s")
model.save('model1.h5')

np.argmax(model.predict(x_train[0].reshape(1,64, 64, 3))), y_train[0]<jupyter_output><empty_output><jupyter_text>training<jupyter_code>import seaborn as sns
from sklearn.metrics import classification_report
pred =  model.predict(x_train)
pred = np.argmax(pred,axis=1 )
y = np.argmax(y_train,axis=1 )
clf_report = classification_report(y,pred,output_dict=True)
sns.heatmap(pd.DataFrame(clf_report).iloc[:-1, :].T, annot=True)
plt.show()<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>y = np.argmax(y_test,axis=1 )
y_index_0 = np.where(y == 0)
y_index_1 = np.where(y == 1)
len(y_index_0[0]),len(y_index_1[0])
pred =  model.predict(x_test)
pred = np.argmax(pred,axis=1 )
y = np.argmax(y_test,axis=1 )
print(classification_report(y, pred))
clf_report = classification_report(y,pred,output_dict=True)
sns.heatmap(pd.DataFrame(clf_report).iloc[:-1, :].T, annot=True)
plt.show()
plt.figure(figsize=(8,8))
plt.subplot(211)
plt.title('Cross Entropy Loss')
plt.plot(history.history['loss'], color='blue', label='train')
plt.plot(history.history['val_loss'], color='orange', label='test')
# plot accuracy
plt.subplot(212)
plt.title('Classification Accuracy')
plt.plot(history.history['accuracy'], color='blue', label='train')
plt.plot(history.history['val_accuracy'], color='orange', label='test')<jupyter_output><empty_output><jupyter_text>Model - 2<jupyter_code>model = Sequential()

size = 32
model.add(Conv2D(size, (3, 3), padding='same', input_shape=(64, 64,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(size*2, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(size*4, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(size*8, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))


model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('sigmoid'))#softmax is used to get maximun value in a row

# initiate RMSprop optimizer
opt = tf.keras.optimizers.Adam(lr=0.0005, decay=1e-6)

# Let's train the model using RMSprop
model.compile(loss='binary_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

start = time.time()
history = model.fit(x_train, y_train, 
          validation_data=(x_test, y_test),
          epochs=40, batch_size=512, )
stop = time.time()
print(f"Training time: {stop - start}s")
model.save('model2.h5')

pred =  model.predict(x_train)
pred = np.argmax(pred,axis=1 )
y = np.argmax(y_train,axis=1 )
clf_report = classification_report(y,pred,output_dict=True)
sns.heatmap(pd.DataFrame(clf_report).iloc[:-1, :].T, annot=True)
plt.show()
pred =  model.predict(x_test)
pred = np.argmax(pred,axis=1 )
y = np.argmax(y_test,axis=1 )
print(classification_report(y, pred))
clf_report = classification_report(y,pred,output_dict=True)
sns.heatmap(pd.DataFrame(clf_report).iloc[:-1, :].T, annot=True)
plt.show()
plt.figure(figsize=(8,8))
plt.subplot(211)
plt.title('Cross Entropy Loss')
plt.plot(history.history['loss'], color='blue', label='train')
plt.plot(history.history['val_loss'], color='orange', label='test')
# plot accuracy
plt.subplot(212)
plt.title('Classification Accuracy')
plt.plot(history.history['accuracy'], color='blue', label='train')
plt.plot(history.history['val_accuracy'], color='orange', label='test')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1691771269177127, 'fraction_numerical': 0.03305439330543933, 'mean_word_length': 6.880219780219781, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '623442', 'license': 'MIT', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2762, 'n_words': 474, 'path': 'confidence_measure.ipynb', 'repo_name': 'parth-jaiswal/Confidence-Measure-for-Virtual-Inteviews', 'size': '7170'}