Kaggle competition: Northeastern SMILE Lab - Recognizing Faces in the Wild
<SOS> metric: AUC Kaggle data source: northeastern-smile-lab-recognizing-faces-in-the-wild<categorify>
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
from imageio import imread
from skimage.transform import resize
from keras.models import load_model
import pandas as pd
from tqdm import tqdm
class TargetEncodingSmoothing(BaseEstimator, TransformerMixin):
def __init__(self, columns_names,k, f):
self.columns_names = columns_names
self.learned_values = {}
self.dataset_mean = np.nan
self.k = k
self.f = f
def smoothing_func(self, N):
return 1 /(1 + np.exp(-(N-self.k)/self.f))
def fit(self, X, y, **fit_params):
X_ = X.copy()
self.learned_values = {}
self.dataset_mean = np.mean(y)
 X_["__target__"] = y
for c in [x for x in X_.columns if x in self.columns_names]:
stats =(X_[[c,"__target__"]] 
.groupby(c)["__target__"].
 agg(['mean', 'size']))
stats["alpha"] = self.smoothing_func(stats["size"])
 stats["__target__"] =(stats["alpha"]*stats["mean"]
+(1-stats["alpha"])*self.dataset_mean)
 stats =(stats 
.drop([x for x in stats.columns if x not in ["__target__",c]], axis = 1)
.reset_index())
 self.learned_values[c] = stats
self.dataset_mean = np.mean(y)
 return self
def transform(self, X, **fit_params):
transformed_X = X[self.columns_names].copy()
for c in transformed_X.columns:
transformed_X[c] =(transformed_X[[c]] 
.merge(self.learned_values[c], on = c, how = 'left')
)["__target__"]
transformed_X = transformed_X.fillna(self.dataset_mean)
 return transformed_X
def fit_transform(self, X, y, **fit_params):
self.fit(X,y)
 return self.transform(X )<categorify>
!pip install git+https://github.com/rcmalli/keras-vggface.git
%matplotlib inline
x = np.linspace(0,100,100)
 plot = pd.DataFrame()
te = TargetEncodingSmoothing([], 1,1)
 plot["k=1|f=1"] = te.smoothing_func(x)
 te = TargetEncodingSmoothing([], 33,5)
 plot["k=33|f=5"] = te.smoothing_func(x)
 te = TargetEncodingSmoothing([], 66,15)
 plot["k=66|f=15"] = te.smoothing_func(x)
 plot.plot(figsize =(15,8))<categorify>
import h5py
from collections import defaultdict
from glob import glob
from random import choice, sample
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import backend as K
from keras.layers import Input, Dense, Flatten, GlobalMaxPool2D, GlobalAvgPool2D, Concatenate, Multiply, Dropout, Subtract, Add, Conv2D, Lambda, Reshape
from keras.models import Model
from keras.optimizers import Adam
from keras_vggface.utils import preprocess_input
from keras_vggface.vggface import VGGFace
scores = []
tr_scores = []
for train_index, test_index in skf.split(train, y):
train_df = train.loc[train_index,col4train].reset_index(drop = True)
 valid_df = train.loc[test_index,col4train].reset_index(drop = True)
 train_y, valid_y = y[train_index], y[test_index]
te = TargetEncodingSmoothing(
 columns_names= col4train,
k = 3, f = 1.5 
)
 X_tr = te.fit_transform(train_df, train_y ).values
X_val = te.transform(valid_df ).values
model = get_model()
model.fit(X_tr,train_y)
 predictions = model.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(valid_y, predictions))
train_preds = model.predict_proba(X_tr)[:,1]
tr_scores.append(roc_auc_score(train_y, train_preds))
print("Train AUC score: {:.4f} Valid AUC score: {:.4f}, STD: {:.4f}".format(
 np.mean(tr_scores), np.mean(scores), np.std(scores)
))<categorify>
train_file_path = ".. /input/recognizing-faces-in-the-wild/train_relationships.csv"
train_folders_path = ".. /input/recognizing-faces-in-the-wild/train/"
val_families = "F09"
def get_CV_target_encoding(data, y, encoder, cv = 5):
skfTE = StratifiedKFold(n_splits=cv, random_state = 545167, shuffle = True)
 result = []
for train_indexTE, test_indexTE in skfTE.split(data, y):
encoder.fit(data.iloc[train_indexTE,:].reset_index(drop = True), y[train_indexTE])
 tmp = encoder.transform(data.iloc[test_indexTE,:].reset_index(drop = True))
tmp["index"] = test_indexTE
result.append(tmp)
 result = pd.concat(result, ignore_index = True)
 result = result.sort_values('index' ).reset_index(drop = True ).drop('index', axis = 1)
 return result<categorify>
all_images = glob(train_folders_path + "*/*/*.jpg")
 print(all_images[0] )
scores = []
tr_scores = []
for train_index, test_index in skf.split(train, y):
train_df = train.loc[train_index,col4train].reset_index(drop = True)
 valid_df = train.loc[test_index,col4train].reset_index(drop = True)
 train_y, valid_y = y[train_index], y[test_index]
te = TargetEncodingSmoothing(
 columns_names= col4train,
k = 3, f = 1.5 
)
 X_tr = get_CV_target_encoding(train_df, train_y, te, cv = 5)
 te.fit(train_df, train_y)
 X_val = te.transform(valid_df ).values
model = get_model()
model.fit(X_tr,train_y)
 predictions = model.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(valid_y, predictions))
train_preds = model.predict_proba(X_tr)[:,1]
tr_scores.append(roc_auc_score(train_y, train_preds))
print("Train AUC score: {:.4f} Valid AUC score: {:.4f}, STD: {:.4f}".format(
 np.mean(tr_scores), np.mean(scores), np.std(scores)
))<categorify>
train_images = [x for x in all_images if val_families not in x]
val_images = [x for x in all_images if val_families in x]
class TargetEncodingExpandingMean(BaseEstimator, TransformerMixin):
def __init__(self, columns_names):
self.columns_names = columns_names
self.learned_values = {}
self.dataset_mean = np.nan
def fit(self, X, y, **fit_params):
X_ = X.copy()
self.learned_values = {}
self.dataset_mean = np.mean(y)
 X_["__target__"] = y
for c in [x for x in X_.columns if x in self.columns_names]:
stats =(X_[[c,"__target__"]] 
.groupby(c)["__target__"] 
.agg(['mean', 'size']))
stats["__target__"] = stats["mean"]
stats =(stats 
.drop([x for x in stats.columns if x not in ["__target__",c]], axis = 1)
.reset_index())
 self.learned_values[c] = stats
return self
def transform(self, X, **fit_params):
transformed_X = X[self.columns_names].copy()
for c in transformed_X.columns:
transformed_X[c] =(transformed_X[[c]] 
.merge(self.learned_values[c], on = c, how = 'left')
)["__target__"]
transformed_X = transformed_X.fillna(self.dataset_mean)
 return transformed_X
def fit_transform(self, X, y, **fit_params):
self.fit(X,y)
 X_ = X[self.columns_names].copy().reset_index(drop = True)
 X_["__target__"] = y
X_["index"] = X_.index
X_transformed = pd.DataFrame()
for c in self.columns_names:
X_shuffled = X_[[c,"__target__", "index"]].copy()
X_shuffled = X_shuffled.sample(n = len(X_shuffled),replace=False)
 X_shuffled["cnt"] = 1
X_shuffled["cumsum"] =(X_shuffled 
.groupby(c,sort=False)['__target__'] 
.apply(lambda x : x.shift().cumsum()))
X_shuffled["cumcnt"] =(X_shuffled 
.groupby(c,sort=False)['cnt'] 
.apply(lambda x : x.shift().cumsum()))
X_shuffled["encoded"] = X_shuffled["cumsum"] / X_shuffled["cumcnt"]
X_shuffled["encoded"] = X_shuffled["encoded"].fillna(self.dataset_mean)
 X_transformed[c] = X_shuffled.sort_values("index")["encoded"].values
return X_transformed<categorify>
ppl = [x.split("/")[-3] + "/" + x.split("/")[-2] for x in all_images]
print(ppl[0] )
scores = []
tr_scores = []
for train_index, test_index in skf.split(train, y):
train_df = train.loc[train_index,col4train].reset_index(drop = True)
 valid_df = train.loc[test_index,col4train].reset_index(drop = True)
 train_y, valid_y = y[train_index], y[test_index]
te = TargetEncodingExpandingMean(columns_names=col4train)
 X_tr = te.fit_transform(train_df, train_y)
 X_val = te.transform(valid_df ).values
model = get_model()
model.fit(X_tr,train_y)
 predictions = model.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(valid_y, predictions))
train_preds = model.predict_proba(X_tr)[:,1]
tr_scores.append(roc_auc_score(train_y, train_preds))
print("Train AUC score: {:.4f} Valid AUC score: {:.4f}, STD: {:.4f}".format(
 np.mean(tr_scores), np.mean(scores), np.std(scores)
))<data_type_conversions>
train_person_to_images_map = defaultdict(list)
 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_families not in x[0]]
val = [x for x in relationships if val_families in x[0]]
train[col4train] = train[col4train].values.astype(str)
 test[col4train] = test[col4train].values.astype(str)
 new_col4train = col4train
for c1,c2 in combinations(col4train, 2):
name = "{}_{}".format(c1,c2)
 new_col4train.append(name)
 train[name] = train[c1] + "_" + train[c2]
test[name] = test[c1] + "_" + test[c2]<count_unique_values>
def prewhiten(x):
if x.ndim == 4:
axis =(1, 2, 3)
 size = x[0].size
elif x.ndim == 3:
axis =(0, 1, 2)
 size = x.size
else:
raise ValueError('Dimension should be 3 or 4')
 mean = np.mean(x, axis=axis, keepdims=True)
 std = np.std(x, axis=axis, keepdims=True)
 std_adj = np.maximum(std, 1.0/np.sqrt(size))
y =(x - mean)/ std_adj
return y
train[new_col4train].apply(lambda x: len(x.unique()))<categorify>
model_path = '.. /input/facenet-keras/facenet_keras.h5'
model_fn = load_model(model_path )
scores = []
tr_scores = []
for train_index, test_index in skf.split(train, y):
train_df = train.loc[train_index,new_col4train].reset_index(drop = True)
 valid_df = train.loc[test_index,new_col4train].reset_index(drop = True)
 train_y, valid_y = y[train_index], y[test_index]
te = TargetEncodingExpandingMean(columns_names=new_col4train)
 X_tr = te.fit_transform(train_df, train_y)
 X_val = te.transform(valid_df)
 te2 = TargetEncodingSmoothing(
 columns_names= new_col4train,
k = 3, f = 1.5, 
)
 X_tr2 = get_CV_target_encoding(train_df, train_y, te2, cv = 5)
 te2.fit(train_df, train_y)
 X_val2 = te2.transform(valid_df)
 X_tr = pd.concat([X_tr, X_tr2], axis = 1)
 X_val = pd.concat([X_val, X_val2], axis = 1)
 model = get_model()
model.fit(X_tr,train_y)
 predictions = model.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(valid_y, predictions))
train_preds = model.predict_proba(X_tr)[:,1]
tr_scores.append(roc_auc_score(train_y, train_preds))
print("Train AUC score: {:.4f} Valid AUC score: {:.4f}, STD: {:.4f}".format(
 np.mean(tr_scores), np.mean(scores), np.std(scores)
))<categorify>
for layer in model_fn.layers[:-3]:
layer.trainable = True
te = TargetEncodingExpandingMean(columns_names=new_col4train)
 X_tr = te.fit_transform(train[new_col4train], y)
 X_val = te.transform(test[new_col4train])
 te2 = TargetEncodingSmoothing(
 columns_names= new_col4train,
k = 3, f = 1.5, 
)
 X_tr2 = get_CV_target_encoding(train[new_col4train], y, te2, cv = 5)
 te2.fit(train[new_col4train], y)
 X_val2 = te2.transform(test[new_col4train])
 X = pd.concat([X_tr, X_tr2], axis = 1)
 X_te = pd.concat([X_val, X_val2], axis = 1)
 model = get_model()
model.fit(X,y)
 predictions = model.predict_proba(X_te)[:,1]
submit = pd.DataFrame()
submit["Id"] = test["id"]
submit["ACTION"] = predictions
submit.to_csv("submission.csv", index = False )<import_modules>
model_vgg = VGGFace(model='resnet50', include_top=False)
 for layer in model_vgg.layers[:-3]:
layer.trainable = True
def confusion_matrix(rater_a, rater_b, min_rating=None, max_rating=None):

 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(rater_a + rater_b)
 if max_rating is None:
max_rating = max(rater_a + rater_b)
 num_ratings = int(max_rating - min_rating + 1)
 conf_mat = [[0 for i in range(num_ratings)]
for j in range(num_ratings)]
for a, b in zip(rater_a, rater_b):
conf_mat[a - min_rating][b - min_rating] += 1
return conf_mat
def histogram(ratings, min_rating=None, max_rating=None):

 if min_rating is None:
min_rating = min(ratings)
 if max_rating is None:
max_rating = max(ratings)
 num_ratings = int(max_rating - min_rating + 1)
 hist_ratings = [0 for x in range(num_ratings)]
for r in ratings:
hist_ratings[r - min_rating] += 1
return hist_ratings
def quadratic_weighted_kappa(y, y_pred):

 rater_a = y
rater_b = y_pred
min_rating=None
max_rating=None
rater_a = np.array(rater_a, dtype=int)
 rater_b = np.array(rater_b, dtype=int)
 assert(len(rater_a)== len(rater_b))
if min_rating is None:
min_rating = min(min(rater_a), min(rater_b))
if max_rating is None:
max_rating = max(max(rater_a), max(rater_b))
conf_mat = confusion_matrix(rater_a, rater_b,
min_rating, max_rating)
 num_ratings = len(conf_mat)
 num_scored_items = float(len(rater_a))
hist_rater_a = histogram(rater_a, min_rating, max_rating)
 hist_rater_b = histogram(rater_b, min_rating, max_rating)
 numerator = 0.0
denominator = 0.0
for i in range(num_ratings):
for j in range(num_ratings):
expected_count =(hist_rater_a[i] * hist_rater_b[j]
/ num_scored_items)
 d = pow(i - j, 2.0)/ pow(num_ratings - 1, 2.0)
 numerator += d * conf_mat[i][j] / num_scored_items
denominator += d * expected_count / num_scored_items
return(1.0 - numerator / denominator )<load_from_csv>
IMG_SIZE_FN = 160
IMG_SIZE_VGG = 224
train = pd.read_csv('.. /input/petfinder-adoption-prediction/train/train.csv')
 test = pd.read_csv('.. /input/petfinder-adoption-prediction/test/test.csv')
 X = pd.concat([train, test], ignore_index=True, sort=False)
 X.head()<compute_train_metric>
def read_img_fn(path):
img = cv2.imread(path)
 img = cv2.resize(img,(IMG_SIZE_FN,IMG_SIZE_FN))
img = np.array(img ).astype(np.float)
 return prewhiten(img)
 def read_img_vgg(path):
img = cv2.imread(path)
 img = cv2.resize(img,(IMG_SIZE_VGG,IMG_SIZE_VGG))
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_FN = np.array([read_img_fn(x)for x in X1])
 X1_VGG = np.array([read_img_vgg(x)for x in X1])
 X2 = [choice(person_to_images_map[x[1]])for x in batch_tuples]
X2_FN = np.array([read_img_fn(x)for x in X2])
 X2_VGG = np.array([read_img_vgg(x)for x in X2])
 yield [X1_FN, X2_FN, X1_VGG, X2_VGG], labels
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return -cohen_kappa_score(y, preds, weights='quadratic')
 def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X = X, y = y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
preds = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4])
 return preds
def coefficients(self):
return self.coef_['x']<drop_column>
def signed_sqrt(x):
return K.sign(x)*K.sqrt(K.abs(x)+1e-9 )
X_temp = X.copy()
to_drop_columns = ['PetID', 'Name', 'RescuerID', 'Description']
X_temp = X_temp.drop(to_drop_columns, axis=1 )<drop_column>
def baseline_model() :
input_1 = Input(shape=(IMG_SIZE_FN, IMG_SIZE_FN, 3))
input_2 = Input(shape=(IMG_SIZE_FN, IMG_SIZE_FN, 3))
input_3 = Input(shape=(IMG_SIZE_VGG, IMG_SIZE_VGG, 3))
input_4 = Input(shape=(IMG_SIZE_VGG, IMG_SIZE_VGG, 3))
x1 = model_fn(input_1)
 x2 = model_fn(input_2)
 x3 = model_vgg(input_3)
 x4 = model_vgg(input_4)
 x1 = Reshape(( 1, 1 ,128))(x1)
 x2 = Reshape(( 1, 1 ,128))(x2)
 x1 = Concatenate(axis=-1 )([GlobalMaxPool2D()(x1), GlobalAvgPool2D()(x1)])
 x2 = Concatenate(axis=-1 )([GlobalMaxPool2D()(x2), GlobalAvgPool2D()(x2)])
 x1t = Lambda(lambda tensor : K.square(tensor))(x1)
 x2t = Lambda(lambda tensor : K.square(tensor))(x2)
 x3t = Lambda(lambda tensor : K.square(tensor))(x3)
 x4t = Lambda(lambda tensor : K.square(tensor))(x4)
 merged_add_fn = Add()([x1, x2])
 merged_add_vgg = Add()([x3, x4])
 merged_sub1_fn = Subtract()([x1,x2])
 merged_sub1_vgg = Subtract()([x3,x4])
 merged_sub2_fn = Subtract()([x2,x1])
 merged_sub2_vgg = Subtract()([x4,x3])
 merged_mul1_fn = Multiply()([x1,x2])
 merged_mul1_vgg = Multiply()([x3,x4])
 merged_sq1_fn = Add()([x1t,x2t])
 merged_sq1_vgg = Add()([x3t,x4t])
 merged_sqrt_fn = Lambda(lambda tensor : signed_sqrt(tensor))(merged_mul1_fn)
 merged_sqrt_vgg = Lambda(lambda tensor : signed_sqrt(tensor))(merged_mul1_vgg)
 merged_add_vgg = Conv2D(128 , [1,1] )(merged_add_vgg)
 merged_sub1_vgg = Conv2D(128 , [1,1] )(merged_sub1_vgg)
 merged_sub2_vgg = Conv2D(128 , [1,1] )(merged_sub2_vgg)
 merged_mul1_vgg = Conv2D(128 , [1,1] )(merged_mul1_vgg)
 merged_sq1_vgg = Conv2D(128 , [1,1] )(merged_sq1_vgg)
 merged_sqrt_vgg = Conv2D(128 , [1,1] )(merged_sqrt_vgg)
 merged = Concatenate(axis=-1 )([Flatten()(merged_add_vgg),(merged_add_fn), Flatten()(merged_sub1_vgg),(merged_sub1_fn),
Flatten()(merged_sub2_vgg),(merged_sub2_fn), Flatten()(merged_mul1_vgg),(merged_mul1_fn),
Flatten()(merged_sq1_vgg),(merged_sq1_fn), Flatten()(merged_sqrt_vgg),(merged_sqrt_fn)])
 merged = Dense(100, activation="relu" )(merged)
 merged = Dropout(0.1 )(merged)
 merged = Dense(25, activation="relu" )(merged)
 merged = Dropout(0.1 )(merged)
 out = Dense(1, activation="sigmoid" )(merged)
 model = Model([input_1, input_2, input_3, input_4], out)
 model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model
file_path = "facenet_vgg.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.1, patience=20, verbose=1)
 callbacks_list = [checkpoint, reduce_on_plateau]
model = baseline_model()
X_train = X_temp.loc[np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_temp.loc[~np.isfinite(X_temp.AdoptionSpeed), :]
X_test = X_test.drop(['AdoptionSpeed'], axis=1)
 X_train_non_null = X_train.fillna(-1)
 X_test_non_null = X_test.fillna(-1 )<init_hyperparams>
model.fit_generator(gen(train, train_person_to_images_map, batch_size=16), use_multiprocessing=True,
validation_data=gen(val, val_person_to_images_map, batch_size=16), epochs=150, verbose=1,
workers = 4, callbacks=callbacks_list, steps_per_epoch=200, validation_steps=100 )
xgb_params = {
'eval_metric': 'rmse',
'seed': 1337,
'eta': 0.0123,
'subsample': 0.8,
'colsample_bytree': 0.85,
'tree_method': 'gpu_hist',
'device': 'gpu',
'silent': 1,
}<prepare_x_and_y>
plot_model(model, to_file='model.png',show_shapes = True )
def run_xgb(params, X_train, X_test):
n_splits = 10
verbose_eval = 1000
num_rounds = 60000
early_stop = 500
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=1337)
 oof_train = np.zeros(( X_train.shape[0]))
oof_test = np.zeros(( X_test.shape[0], n_splits))
i = 0
for train_idx, valid_idx in kf.split(X_train, X_train['AdoptionSpeed'].values):
X_tr = X_train.iloc[train_idx, :]
X_val = X_train.iloc[valid_idx, :]
y_tr = X_tr['AdoptionSpeed'].values
X_tr = X_tr.drop(['AdoptionSpeed'], axis=1)
 y_val = X_val['AdoptionSpeed'].values
X_val = X_val.drop(['AdoptionSpeed'], axis=1)
 d_train = xgb.DMatrix(data=X_tr, label=y_tr, feature_names=X_tr.columns)
 d_valid = xgb.DMatrix(data=X_val, label=y_val, feature_names=X_val.columns)
 watchlist = [(d_train, 'train'),(d_valid, 'valid')]
model = xgb.train(dtrain=d_train, num_boost_round=num_rounds, evals=watchlist,
early_stopping_rounds=early_stop, verbose_eval=verbose_eval, params=params)
 valid_pred = model.predict(xgb.DMatrix(X_val, feature_names=X_val.columns), ntree_limit=model.best_ntree_limit)
 test_pred = model.predict(xgb.DMatrix(X_test, feature_names=X_test.columns), ntree_limit=model.best_ntree_limit)
 oof_train[valid_idx] = valid_pred
oof_test[:, i] = test_pred
i += 1
return model, oof_train, oof_test<train_model>
test_path = ".. /input/recognizing-faces-in-the-wild/test/"
def chunker(seq, size=32):
return(seq[pos:pos + size] for pos in range(0, len(seq), size))
submission = pd.read_csv('.. /input/recognizing-faces-in-the-wild/sample_submission.csv' )
<compute_train_metric><EOS> 
predictions = []
for batch in tqdm(chunker(submission.img_pair.values)) :
X1 = [x.split("-")[0] for x in batch]
X1_FN = np.array([read_img_fn(test_path + x)for x in X1])
 X1_VGG = np.array([read_img_vgg(test_path + x)for x in X1])
 X2 = [x.split("-")[1] for x in batch]
X2_FN = np.array([read_img_fn(test_path + x)for x in X2])
 X2_VGG = np.array([read_img_vgg(test_path + x)for x in X2])
 pred = model.predict([X1_FN, X2_FN, X1_VGG, X2_VGG] ).ravel().tolist()
predictions += pred
submission['is_related'] = predictions
submission.to_csv("facenetvgg.csv", index=False )