Kaggle competition: Northeastern SMILE Lab - Recognizing Faces in the Wild
<SOS> metric: AUC Kaggle data source: northeastern-smile-lab-recognizing-faces-in-the-wild<data_type_conversions>
import numpy as np
import pandas as pd
import os
from collections import defaultdict
from glob import glob
from random import choice, sample
from keras.preprocessing import image
import cv2
from tqdm import tqdm_notebook
import numpy as np
import pandas as pd
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras.layers import Input, Dense, GlobalMaxPool2D, GlobalAvgPool2D, Concatenate, Multiply, Dropout, Subtract
from keras.models import Model
from keras.optimizers import Adam
for col in train.columns:
if train[col].dtype == 'object':
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values)) 
<prepare_x_and_y>
!pip install git+https://github.com/rcmalli/keras-vggface.git
X = train.sort_values('TransactionDT' ).drop(['isFraud', 'TransactionDT'], axis=1)
 y = train.sort_values('TransactionDT')['isFraud']
X_test = test.drop(['TransactionDT'], axis=1)
 del train, test
gc.collect()<drop_column>
from keras_vggface.utils import preprocess_input
from keras_vggface.vggface import VGGFace
X = reduce_mem_usage(X)
 X_test = reduce_mem_usage(X_test )<choose_model_class>
train_file_path = ".. /input/train_relationships.csv"
train_folders_path = ".. /input/train/"
clf = xgb.XGBClassifier(
 n_estimators=500,
max_depth=9,
learning_rate=0.05,
subsample=0.9,
colsample_bytree=0.9,
random_state=51,
tree_method='gpu_hist' 
)
<choose_model_class>
val_famillies_list = ["F07", "F08", "F09"]
clf = xgb.XGBClassifier(
 n_estimators=500,
max_depth=9,
learning_rate=0.05,
subsample=0.9,
colsample_bytree=0.9,
random_state=51,
tree_method='gpu_hist' 
)
<train_model>
%%time
all_images = glob(train_folders_path + "*/*/*.jpg" )
clf.fit(X, y )<predict_on_test>
def get_train_val(family_name):
val_famillies = family_name
train_images = [x for x in all_images if val_famillies not in x]
val_images = [x for x in all_images if val_famillies in x]
train_person_to_images_map = defaultdict(list)
 ppl = [x.split("/")[-3] + "/" + x.split("/")[-2] for x in all_images]
for x in train_images:
train_person_to_images_map[x.split("/")[-3] + "/" + x.split("/")[-2]].append(x)
 val_person_to_images_map = defaultdict(list)
 for x in val_images:
val_person_to_images_map[x.split("/")[-3] + "/" + x.split("/")[-2]].append(x)
 relationships = pd.read_csv(train_file_path)
 relationships = list(zip(relationships.p1.values, relationships.p2.values))
relationships = [x for x in relationships if x[0] in ppl and x[1] in ppl]
train = [x for x in relationships if val_famillies not in x[0]]
val = [x for x in relationships if val_famillies in x[0]]
return train, val, train_person_to_images_map, val_person_to_images_map
y_train_pred = clf.predict_proba(X)[:,1]
print('AUC score: ',roc_auc_score(y, y_train_pred))<save_to_csv>
def read_img(path):
img = image.load_img(path, target_size=(197, 197))
img = np.array(img ).astype(np.float)
 return preprocess_input(img, version=2)
 def gen(list_tuples, person_to_images_map, batch_size=16):
ppl = list(person_to_images_map.keys())
 while True:
batch_tuples = sample(list_tuples, batch_size // 2)
 labels = [1] * len(batch_tuples)
 while len(batch_tuples)< batch_size:
p1 = choice(ppl)
 p2 = choice(ppl)
 if p1 != p2 and(p1, p2)not in list_tuples and(p2, p1)not in list_tuples:
batch_tuples.append(( p1, p2))
labels.append(0)
 for x in batch_tuples:
if not len(person_to_images_map[x[0]]):
print(x[0])
 X1 = [choice(person_to_images_map[x[0]])for x in batch_tuples]
X1 = np.array([read_img(x)for x in X1])
 X2 = [choice(person_to_images_map[x[1]])for x in batch_tuples]
X2 = np.array([read_img(x)for x in X2])
 yield [X1, X2], labels
def focal_loss(gamma=2., alpha=.25):
def focal_loss_fixed(y_true, y_pred):
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.sum(alpha * K.pow(1.- pt_1, gamma)* K.log(K.epsilon() +pt_1)) -K.sum(( 1-alpha)* K.pow(pt_0, gamma)* K.log(1.- pt_0 + K.epsilon()))
return focal_loss_fixed
def baseline_model() :
input_1 = Input(shape=(197, 197, 3))
input_2 = Input(shape=(197, 197, 3))
base_model = VGGFace(model='resnet50', include_top=False)
 for x in base_model.layers[:-3]:
x.trainable = True
for x in base_model.layers[-3:]:
x.trainable=False
x1 = base_model(input_1)
 x2 = base_model(input_2)
 x1 = Concatenate(axis=-1 )([GlobalMaxPool2D()(x1), GlobalAvgPool2D()(x1)])
 x2 = Concatenate(axis=-1 )([GlobalMaxPool2D()(x2), GlobalAvgPool2D()(x2)])
 x3 = Subtract()([x1, x2])
 x3 = Multiply()([x3, x3])
 x1_ = Multiply()([x1, x1])
 x2_ = Multiply()([x2, x2])
 x4 = Subtract()([x1_, x2_])
 x = Concatenate(axis=-1 )([x4, x3])
 x = Dense(100, activation="relu" )(x)
 x = Dropout(0.01 )(x)
 out = Dense(1, activation="sigmoid" )(x)
 model = Model([input_1, input_2], out)
 model.compile(loss=[focal_loss(alpha=.25, gamma=2)],
metrics=['acc'],
optimizer=Adam(0.00003))
model.summary()
return model
y_pred = clf.predict_proba(X_test)[:,1]
sample_submission = pd.read_csv('.. /input/ieee-fraud-detection/sample_submission.csv', index_col='TransactionID')
 sample_submission['isFraud'] = y_pred
sample_submission.to_csv('submission1.csv' )<compute_train_metric>
n_val_famillies_list = len(val_famillies_list )
def create_eval_metric_df(y,y_pred,model_name='model'):
total = np.shape(y)[0]
tn, fp, fn, tp = metrics.confusion_matrix(y, y_pred ).ravel()
tp /= float(total)
 tn /= float(total)
 fp /= float(total)
 fn /= float(total)
 roc_auc = metrics.roc_auc_score(y, y_pred)
 columns_list = ['model_name',
"total_number",
"cond_pos", "cond_neg",
"true_pos", "true_neg", "false_pos", "false_neg",
"recall", "specificity", "precision", "neg_pred_val", "miss_rate", "false_pos_rate", "false_disc_rate", "false_omission_rate",
"accuracy", "f1_score", "matthews_corr_coef", "informedness", "markedness",
"roc_auc_score"
]
eval_metrics = [model_name,
total,
tp+fn, tn+fp,
tp, tn, fp, fn, 
(tp)/(tp + fn),(tn)/(tn + fp),(tp)/(tp + fp),(tn)/(tn + fn),(fn)/(fn + tp),(fp)/(fp + tn),(fp)/(fp + tp),(fn)/(fn + tn), 
(tp + tn),(2*tp)/(2*tp + fp + fn),(tp*tn - fp*fn)/(np.sqrt(( tp + fp)*(tp + fn)*(tn + fp)*(tn + fn))),(tp)/(tp + fn)+(tn)/(tn + fp)- 1,(tp)/(tp + fp)+(tn)/(tn + fn)- 1,
roc_auc
]
df_eval = pd.DataFrame(eval_metrics)
 df = df_eval.T
df.columns = columns_list
return df 
<predict_on_test>
for i in tqdm_notebook(range(n_val_famillies_list)) :
train, val, train_person_to_images_map, val_person_to_images_map = get_train_val(val_famillies_list[i])
 file_path = f"vgg_face_{i}.h5"
checkpoint = ModelCheckpoint(file_path, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 reduce_on_plateau = ReduceLROnPlateau(monitor="val_acc", mode="max", factor=0.5, patience=10, verbose=1)
 es = EarlyStopping(monitor="val_acc", min_delta = 0.001, patience=30, verbose=1)
 callbacks_list = [checkpoint, reduce_on_plateau, es]
history = model.fit_generator(gen(train, train_person_to_images_map, batch_size=32),
use_multiprocessing=True,
validation_data=gen(val, val_person_to_images_map, batch_size=32),
epochs=300, verbose=1,
workers=4, callbacks=callbacks_list,
steps_per_epoch=400, validation_steps=200 )
y_train_pred = clf.predict(X )<compute_train_metric>
test_path = ".. /input/test/"
submission = pd.read_csv('.. /input/sample_submission.csv')
 def chunker(seq, size=32):
return(seq[pos:pos + size] for pos in range(0, len(seq), size))
df_eval = create_eval_metric_df(y,y_train_pred,model_name='xgboost1')
 print(df_eval )<split>
preds_for_sub = np.zeros(submission.shape[0])
 for i in tqdm_notebook(range(n_val_famillies_list)) :
file_path = f"vgg_face_{i}.h5"
model.load_weights(file_path)
 predictions = []
for batch in tqdm_notebook(chunker(submission.img_pair.values)) :
X1 = [x.split("-")[0] for x in batch]
X1 = np.array([read_img(test_path + x)for x in X1])
 X2 = [x.split("-")[1] for x in batch]
X2 = np.array([read_img(test_path + x)for x in X2])
 pred = model.predict([X1, X2] ).ravel().tolist()
predictions += pred
preds_for_sub += np.array(predictions)/ n_val_famillies_list
X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=0 )<choose_model_class>
submission['is_related'] = preds_for_sub
submission.to_csv("vgg_face.csv", index=False )