<jupyter_start><jupyter_text>**ETHNICITY PREDICTIONS **USING TRANSFER LEARNING<jupyter_code>from tensorflow.keras.applications import VGG19
from tensorflow.keras.layers import Input,Dense,Flatten
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Model,Sequential<jupyter_output><empty_output><jupyter_text>**PRETRAINED MODEL (VGG19-IMAGENET)**<jupyter_code>image_size = [150,150]
model = VGG19(input_shape = image_size+[3],include_top=False,weights='imagenet')

for layer in model.layers:
    layer.trainable = False
model.summary()
final = Model(inputs = model.input,outputs = Dense(1,activation = 'sigmoid')(Flatten()(model.output)))
final.summary()
final.compile(loss = 'binary_crossentropy',optimizer='adam',metrics = ['accuracy'])
from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Mounted at /content/drive<jupyter_text>**DATA AUGMENTATION AND INTERPOLATION**<jupyter_code>train = '/content/drive/My Drive/ethinicity_predictor/train'
train_aug = ImageDataGenerator(rescale=1./255,
                                  rotation_range=40,
                                  width_shift_range=0.2,
                                  height_shift_range=0.2,
                                  shear_range=0.2,
                                  zoom_range=0.2, 
                                  horizontal_flip=True, 
                                  fill_mode='nearest')
final_train = train_aug.flow_from_directory(train,target_size=(150,150),batch_size=15,class_mode ='binary')
import matplotlib.pyplot as plt
import random
import os
import cv2
categories = ['asian','african']
data = []
for cat in categories:
  folder = os.path.join(train,cat)
  label = categories.index(cat)
  for img in os.listdir(folder):
    img_path = os.path.join(folder,img)
    img_arr = cv2.imread(img_path)
    img_arr = cv2.resize(img_arr,(150,150))
    data.append([img_arr,label])
    plt.imshow(img_arr)
x = []
y = []
for features,labels in data:
  x.append(features)
  y.append(labels)
import numpy as np

x = np.array(x)
y = np.array(y)
x.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3)<jupyter_output><empty_output><jupyter_text>**TRAINING AND TESTING**<jupyter_code>vgg19 = final.fit(X_train,y_train,epochs=10,batch_size=35,validation_data=(X_test, y_test))
final.save('imagevgg19')
import numpy as np
predictions = final.predict(
    X_test, steps=None, callbacks=None, max_queue_size=10, workers=1,
    use_multiprocessing=False, verbose=0)# Vector of probabilities
#pred_labels = np.argmax(predictions, axis = 1) # We take the highest probability
z = []
def get_class(predictions):
  if predictions > 0.5:
    z.append(1)
  else:
    z.append(0)
for predictions in predictions:
  get_class(predictions)
z = np.array(z)
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, z)
print('Accuracy: %.2f' % acc)<jupyter_output>Accuracy: 0.80<jupyter_text>**LOSS AND ACCURACY PLOT**<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix
acc = vgg19.history['accuracy']
val_acc = vgg19.history['val_accuracy']
loss = vgg19.history['loss']
val_loss = vgg19.history['val_loss']

epochs = range(10)
plt.figure(figsize=(5, 5))
plt.plot(epochs,acc, label='Training Accuracy')
plt.plot(epochs,val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.show()
plt.subplot(1, 1, 1)
plt.plot(epochs, loss, label='Training Loss')
plt.plot(epochs, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
%matplotlib inline
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt
cm = confusion_matrix(y_true=y_test, y_pred=z)
def plot_confusion_matrix(cm, classes,
                        normalize=False,
                        title='Confusion matrix',
                        cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    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)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

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

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
cm_plot_labels = ['asian','african']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')<jupyter_output>Confusion matrix, without normalization
[[61 16]
 [18 76]]<jupyter_text>**CONFUSION** **MATRIX**<jupyter_code>cm_plot_labels = ['asian','african']
plot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')<jupyter_output>Confusion matrix, without normalization
[[61 16]
 [18 76]]<jupyter_text>**ACCURACY, PRECISION, RECALL_SCORE, F1_SCORE, COHEN_KAPPA_SCORE, ROC/auc SCORE**<jupyter_code>from sklearn.datasets import make_circles
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
from keras.models import Sequential
from keras.layers import Dense
# predict probabilities for test set
yhat_probs = final.predict(X_test, verbose=0)
# predict crisp classes for test set
yhat_classes = z
# reduce to 1d array

# accuracy: (tp + tn) / (p + n)
accuracy = accuracy_score(y_test, yhat_classes)
print('Accuracy: %f' % accuracy)
# precision tp / (tp + fp)
precision = precision_score(y_test, yhat_classes)
print('Precision: %f' % precision)
# recall: tp / (tp + fn)
recall = recall_score(y_test, yhat_classes)
print('Recall: %f' % recall)
# f1: 2 tp / (2 tp + fp + fn)
f1 = f1_score(y_test, yhat_classes)
print('F1 score: %f' % f1)

# kappa
kappa = cohen_kappa_score(y_test, yhat_classes)
print('Cohens kappa: %f' % kappa)
# ROC AUC
auc = roc_auc_score(y_test, yhat_probs)
print('ROC AUC: %f' % auc)
# confusion matrix
matrix = confusion_matrix(y_test, yhat_classes)
print(matrix)<jupyter_output>Accuracy: 0.801170
Precision: 0.826087
Recall: 0.808511
F1 score: 0.817204
Cohens kappa: 0.599311
ROC AUC: 0.830547
[[61 16]
 [18 76]]<jupyter_text>**AUC** **PLOT**<jupyter_code>import matplotlib.pyplot as plt

y_true = y_test
y_probas = yhat_probs
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

fpr, tpr, thresholds = metrics.roc_curve(y_true, y_probas, pos_label=0)

# Print ROC curve
plt.plot(fpr,tpr)
plt.show() 

# Print AUC
auc = np.trapz(tpr,fpr)
print('AUC:', auc)<jupyter_output><empty_output><jupyter_text>**ROC** **CURVE**<jupyter_code>import plot_metric
from plot_metric.functions import BinaryClassification
# Visualisation with plot_metric
bc = BinaryClassification(y_true, y_probas, labels=["african", "asian"])

# Figures
plt.figure(figsize=(5,5))
bc.plot_roc_curve()
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup