Kaggle competition: Northeastern SMILE Lab - Recognizing Faces in the Wild
aggregates = ['sum', 'mean']
sent_agg = ['sum']
train_metadata_desc = train_dfs_metadata.groupby(['PetID'])['metadata_annots_top_desc'].unique()
train_metadata_desc = train_metadata_desc.reset_index()
train_metadata_desc[
'metadata_annots_top_desc'] = train_metadata_desc[
'metadata_annots_top_desc'].apply(lambda x: ' '.join(x))
prefix = 'metadata'
train_metadata_gr = train_dfs_metadata.drop(['metadata_annots_top_desc'], axis=1)
 for i in train_metadata_gr.columns:
if 'PetID' not in i:
train_metadata_gr[i] = train_metadata_gr[i].astype(float)
 train_metadata_gr = train_metadata_gr.groupby(['PetID'] ).agg(aggregates)
 train_metadata_gr.columns = pd.Index([f'{c[0]}_{c[1].upper() }' for c in train_metadata_gr.columns.tolist() ])
 train_metadata_gr = train_metadata_gr.reset_index()
train_sentiment_desc = train_dfs_sentiment.groupby(['PetID'])['sentiment_entities'].unique()
train_sentiment_desc = train_sentiment_desc.reset_index()
train_sentiment_desc[
'sentiment_entities'] = train_sentiment_desc[
'sentiment_entities'].apply(lambda x: ' '.join(x))
prefix = 'sentiment'
train_sentiment_gr = train_dfs_sentiment.drop(['sentiment_entities'], axis=1)
 for i in train_sentiment_gr.columns:
if 'PetID' not in i:
train_sentiment_gr[i] = train_sentiment_gr[i].astype(float)
 train_sentiment_gr = train_sentiment_gr.groupby(['PetID'] ).agg(sent_agg)
 train_sentiment_gr.columns = pd.Index([f'{c[0]}' for c in train_sentiment_gr.columns.tolist() ])
 train_sentiment_gr = train_sentiment_gr.reset_index()
test_metadata_desc = test_dfs_metadata.groupby(['PetID'])['metadata_annots_top_desc'].unique()
test_metadata_desc = test_metadata_desc.reset_index()
test_metadata_desc[
'metadata_annots_top_desc'] = test_metadata_desc[
'metadata_annots_top_desc'].apply(lambda x: ' '.join(x))
prefix = 'metadata'
test_metadata_gr = test_dfs_metadata.drop(['metadata_annots_top_desc'], axis=1)
 for i in test_metadata_gr.columns:
if 'PetID' not in i:
test_metadata_gr[i] = test_metadata_gr[i].astype(float)
 test_metadata_gr = test_metadata_gr.groupby(['PetID'] ).agg(aggregates)
 test_metadata_gr.columns = pd.Index([f'{c[0]}_{c[1].upper() }' for c in test_metadata_gr.columns.tolist() ])
 test_metadata_gr = test_metadata_gr.reset_index()
test_sentiment_desc = test_dfs_sentiment.groupby(['PetID'])['sentiment_entities'].unique()
test_sentiment_desc = test_sentiment_desc.reset_index()
test_sentiment_desc[
'sentiment_entities'] = test_sentiment_desc[
'sentiment_entities'].apply(lambda x: ' '.join(x))
prefix = 'sentiment'
test_sentiment_gr = test_dfs_sentiment.drop(['sentiment_entities'], axis=1)
 for i in test_sentiment_gr.columns:
if 'PetID' not in i:
test_sentiment_gr[i] = test_sentiment_gr[i].astype(float)
 test_sentiment_gr = test_sentiment_gr.groupby(['PetID'] ).agg(sent_agg)
 test_sentiment_gr.columns = pd.Index([f'{c[0]}' for c in test_sentiment_gr.columns.tolist() ])
 test_sentiment_gr = test_sentiment_gr.reset_index()<merge>
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
from keras.layers.core import Lambda, Flatten, Dense
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.models import Model
from keras import backend as K
import pandas as pd
import numpy as np
import keras
from keras.models import *
from keras.layers import *
from keras.preprocessing import *
from keras.callbacks import *
from keras.optimizers import *
from tqdm import tqdm
import glob
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.preprocessing import image
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import sys
import cv2
from random import choice, sample
import datetime
from sklearn.metrics import roc_auc_score
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping
import threading
from keras.initializers import glorot_normal
import tensorflow as tf
import numpy as np
import os
from numpy import genfromtxt
from keras.layers import Conv2D, ZeroPadding2D, Activation
from keras.layers.normalization import BatchNormalization
train_proc = train.copy()
train_proc = train_proc.merge(
 train_sentiment_gr, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_metadata_gr, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_metadata_desc, how='left', on='PetID')
 train_proc = train_proc.merge(
 train_sentiment_desc, how='left', on='PetID')
 test_proc = test.copy()
test_proc = test_proc.merge(
 test_sentiment_gr, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_metadata_gr, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_metadata_desc, how='left', on='PetID')
 test_proc = test_proc.merge(
 test_sentiment_desc, how='left', on='PetID')
 print(train_proc.shape, test_proc.shape)
 assert train_proc.shape[0] == train.shape[0]
assert test_proc.shape[0] == test.shape[0]<merge>
def outer_product(x):

 return keras.backend.batch_dot(
 x[0]
, x[1]
, axes=[1,1] 
)/ x[0].get_shape().as_list() [1]
def signed_sqrt(x):

 return keras.backend.sign(x)* keras.backend.sqrt(keras.backend.abs(x)+ 1e-9)
 def L2_norm(x, axis=-1):

 return keras.backend.l2_normalize(x, axis=axis)
 def auc(y_true, y_pred):
return tf.py_func(roc_auc_score,(y_true, y_pred), tf.double)
 def euclidean_distance(vects):
x, y = vects
sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
 return K.sqrt(K.maximum(sum_square, K.epsilon()))
def eucl_dist_output_shape(shapes):
shape1, shape2 = shapes
return(shape1[0], 1)
 def contrastive_loss(y_true, y_pred):

 margin = 1
sqaure_pred = K.square(y_pred)
 margin_square = K.square(K.maximum(margin - y_pred, 0))
return K.mean(y_true * sqaure_pred +(1 - y_true)* margin_square)
 def create_base_network(input_shape):
new_mod = Model(mod.input,mod.layers[-5].output)
 return new_mod
def compute_accuracy(y_true, y_pred):

 pred = y_pred.ravel() < 0.5
return np.mean(pred == y_true)
 def accuracy(y_true, y_pred):

 return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
train_breed_main = train_proc[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
train_breed_main = train_breed_main.iloc[:, 2:]
train_breed_main = train_breed_main.add_prefix('main_breed_')
 train_breed_second = train_proc[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
train_breed_second = train_breed_second.iloc[:, 2:]
train_breed_second = train_breed_second.add_prefix('second_breed_')
 train_proc = pd.concat(
 [train_proc, train_breed_main, train_breed_second], axis=1)
 test_breed_main = test_proc[['Breed1']].merge(
 labels_breed, how='left',
left_on='Breed1', right_on='BreedID',
suffixes=('', '_main_breed'))
test_breed_main = test_breed_main.iloc[:, 2:]
test_breed_main = test_breed_main.add_prefix('main_breed_')
 test_breed_second = test_proc[['Breed2']].merge(
 labels_breed, how='left',
left_on='Breed2', right_on='BreedID',
suffixes=('', '_second_breed'))
test_breed_second = test_breed_second.iloc[:, 2:]
test_breed_second = test_breed_second.add_prefix('second_breed_')
 test_proc = pd.concat(
 [test_proc, test_breed_main, test_breed_second], axis=1)
 print(train_proc.shape, test_proc.shape)
<merge>
def LRN2D(x):
return tf.nn.lrn(x, alpha=1e-4, beta=0.75)
 def conv2d_bn(
 x,
layer=None,
cv1_out=None,
cv1_filter=(1, 1),
cv1_strides=(1, 1),
cv2_out=None,
cv2_filter=(3, 3),
cv2_strides=(1, 1),
padding=None, 
):
num = '' if cv2_out == None else '1'
tensor = Conv2D(cv1_out, cv1_filter, strides=cv1_strides, name=layer+'_conv'+num )(x)
 tensor = BatchNormalization(axis=3, epsilon=0.00001, name=layer+'_bn'+num )(tensor)
 tensor = Activation('relu' )(tensor)
 if padding == None:
return tensor
tensor = ZeroPadding2D(padding=padding )(tensor)
 if cv2_out == None:
return tensor
tensor = Conv2D(cv2_out, cv2_filter, strides=cv2_strides, name=layer+'_conv'+'2' )(tensor)
 tensor = BatchNormalization(axis=3, epsilon=0.00001, name=layer+'_bn'+'2' )(tensor)
 tensor = Activation('relu' )(tensor)
 return tensor
X = pd.concat([train_proc, test_proc], ignore_index=True, sort=False)
 X_temp = X.copy()
text_columns = ['Description', 'metadata_annots_top_desc', 'sentiment_entities']
categorical_columns = ['main_breed_BreedName', 'second_breed_BreedName']
to_drop_columns = ['PetID', 'Name', 'RescuerID']
rescuer_count = X.groupby(['RescuerID'])['PetID'].count().reset_index()
rescuer_count.columns = ['RescuerID', 'RescuerID_COUNT']
X_temp = X_temp.merge(rescuer_count, how='left', on='RescuerID')
 for i in categorical_columns:
X_temp.loc[:, i] = pd.factorize(X_temp.loc[:, i])[0]
X_text = X_temp[text_columns]
for i in X_text.columns:
X_text.loc[:, i] = X_text.loc[:, i].fillna('none' )<feature_engineering>
ROOT = ".. /input/recognizing-faces-in-the-wild/"
def read_img(im_path):
im1 = image.load_img(im_path, target_size=(96, 96))
im1 = image.img_to_array(im1)
 im1 = np.expand_dims(im1, axis=0)
 im1 = preprocess_input(im1,mode='tf')
 return im1[0]
allPhotos = defaultdict(list)
 for family in glob.glob(ROOT+"train/*"):
for mem in glob.glob(family+'/*'):
for photo in glob.glob(mem+'/*'):
allPhotos[mem].append(photo)
 ppl = list(allPhotos.keys())
 len(ppl)
 data = pd.read_csv(ROOT+'train_relationships.csv')
 data.p1 = data.p1.apply(lambda x: ROOT+'train/'+x)
 data.p2 = data.p2.apply(lambda x: ROOT+'train/'+x)
 print(data.shape)
 data.head()
data = data[(( data.p1.isin(ppl)) &(data.p2.isin(ppl)))]
data = [(x[0], x[1])for x in data.values ]
len(data)
 train = [ x for x in data if 'F09' not in x[0] ]
val = [ x for x in data if 'F09' in x[0] ]
len(train), len(val)
 def getImages(p1,p2):
p1 = read_img(choice(allPhotos[p1]))
p2 = read_img(choice(allPhotos[p2]))
return p1,p2
def getMiniBatch(batch_size=16, data=train):
p1 = []; p2 = []; Y = []
batch = sample(data, batch_size//2)
 for x in batch:
_p1, _p2 = getImages(*x)
 p1.append(_p1);p2.append(_p2);Y.append(1)
 while len(Y)< batch_size:
_p1,_p2 = tuple(np.random.choice(ppl,size=2, replace=False))
if(_p1,_p2)not in train+val and(_p2,_p1)not in train+val:
_p1,_p2 = getImages(_p1,_p2)
 p1.append(_p1);p2.append(_p2);Y.append(0)
 return [np.array(p1),np.array(p2)], np.array(Y)
 def create_model() :
myInput = Input(shape=(96, 96, 3))
x = ZeroPadding2D(padding=(3, 3), input_shape=(96, 96, 3))(myInput)
 x = Conv2D(64,(7, 7), strides=(2, 2), name='conv1' )(x)
 x = BatchNormalization(axis=3, epsilon=0.00001, name='bn1' )(x)
 x = Activation('relu' )(x)
 x = ZeroPadding2D(padding=(1, 1))(x)
 x = MaxPooling2D(pool_size=3, strides=2 )(x)
 x = Lambda(LRN2D, name='lrn_1' )(x)
 x = Conv2D(64,(1, 1), name='conv2' )(x)
 x = BatchNormalization(axis=3, epsilon=0.00001, name='bn2' )(x)
 x = Activation('relu' )(x)
 x = ZeroPadding2D(padding=(1, 1))(x)
 x = Conv2D(192,(3, 3), name='conv3' )(x)
 x = BatchNormalization(axis=3, epsilon=0.00001, name='bn3' )(x)
 x = Activation('relu' )(x)
 x = Lambda(LRN2D, name='lrn_2' )(x)
 x = ZeroPadding2D(padding=(1, 1))(x)
 x = MaxPooling2D(pool_size=3, strides=2 )(x)
 inception_3a_3x3 = Conv2D(96,(1, 1), name='inception_3a_3x3_conv1' )(x)
 inception_3a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_3x3_bn1' )(inception_3a_3x3)
 inception_3a_3x3 = Activation('relu' )(inception_3a_3x3)
 inception_3a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3a_3x3)
 inception_3a_3x3 = Conv2D(128,(3, 3), name='inception_3a_3x3_conv2' )(inception_3a_3x3)
 inception_3a_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_3x3_bn2' )(inception_3a_3x3)
 inception_3a_3x3 = Activation('relu' )(inception_3a_3x3)
 inception_3a_5x5 = Conv2D(16,(1, 1), name='inception_3a_5x5_conv1' )(x)
 inception_3a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_5x5_bn1' )(inception_3a_5x5)
 inception_3a_5x5 = Activation('relu' )(inception_3a_5x5)
 inception_3a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3a_5x5)
 inception_3a_5x5 = Conv2D(32,(5, 5), name='inception_3a_5x5_conv2' )(inception_3a_5x5)
 inception_3a_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_5x5_bn2' )(inception_3a_5x5)
 inception_3a_5x5 = Activation('relu' )(inception_3a_5x5)
 inception_3a_pool = MaxPooling2D(pool_size=3, strides=2 )(x)
 inception_3a_pool = Conv2D(32,(1, 1), name='inception_3a_pool_conv' )(inception_3a_pool)
 inception_3a_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_pool_bn' )(inception_3a_pool)
 inception_3a_pool = Activation('relu' )(inception_3a_pool)
 inception_3a_pool = ZeroPadding2D(padding=(( 3, 4),(3, 4)) )(inception_3a_pool)
 inception_3a_1x1 = Conv2D(64,(1, 1), name='inception_3a_1x1_conv' )(x)
 inception_3a_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3a_1x1_bn' )(inception_3a_1x1)
 inception_3a_1x1 = Activation('relu' )(inception_3a_1x1)
 inception_3a = concatenate([inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1], axis=3)
 inception_3b_3x3 = Conv2D(96,(1, 1), name='inception_3b_3x3_conv1' )(inception_3a)
 inception_3b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_3x3_bn1' )(inception_3b_3x3)
 inception_3b_3x3 = Activation('relu' )(inception_3b_3x3)
 inception_3b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3b_3x3)
 inception_3b_3x3 = Conv2D(128,(3, 3), name='inception_3b_3x3_conv2' )(inception_3b_3x3)
 inception_3b_3x3 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_3x3_bn2' )(inception_3b_3x3)
 inception_3b_3x3 = Activation('relu' )(inception_3b_3x3)
 inception_3b_5x5 = Conv2D(32,(1, 1), name='inception_3b_5x5_conv1' )(inception_3a)
 inception_3b_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_5x5_bn1' )(inception_3b_5x5)
 inception_3b_5x5 = Activation('relu' )(inception_3b_5x5)
 inception_3b_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3b_5x5)
 inception_3b_5x5 = Conv2D(64,(5, 5), name='inception_3b_5x5_conv2' )(inception_3b_5x5)
 inception_3b_5x5 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_5x5_bn2' )(inception_3b_5x5)
 inception_3b_5x5 = Activation('relu' )(inception_3b_5x5)
 inception_3b_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_3a)
 inception_3b_pool = Conv2D(64,(1, 1), name='inception_3b_pool_conv' )(inception_3b_pool)
 inception_3b_pool = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_pool_bn' )(inception_3b_pool)
 inception_3b_pool = Activation('relu' )(inception_3b_pool)
 inception_3b_pool = ZeroPadding2D(padding=(4, 4))(inception_3b_pool)
 inception_3b_1x1 = Conv2D(64,(1, 1), name='inception_3b_1x1_conv' )(inception_3a)
 inception_3b_1x1 = BatchNormalization(axis=3, epsilon=0.00001, name='inception_3b_1x1_bn' )(inception_3b_1x1)
 inception_3b_1x1 = Activation('relu' )(inception_3b_1x1)
 inception_3b = concatenate([inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1], axis=3)
 inception_3c_3x3 = conv2d_bn(inception_3b,
layer='inception_3c_3x3',
cv1_out=128,
cv1_filter=(1, 1),
cv2_out=256,
cv2_filter=(3, 3),
cv2_strides=(2, 2),
padding=(1, 1))
inception_3c_5x5 = conv2d_bn(inception_3b,
layer='inception_3c_5x5',
cv1_out=32,
cv1_filter=(1, 1),
cv2_out=64,
cv2_filter=(5, 5),
cv2_strides=(2, 2),
padding=(2, 2))
inception_3c_pool = MaxPooling2D(pool_size=3, strides=2 )(inception_3b)
 inception_3c_pool = ZeroPadding2D(padding=(( 0, 1),(0, 1)) )(inception_3c_pool)
 inception_3c = concatenate([inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3)
 inception_4a_3x3 = conv2d_bn(inception_3c,
layer='inception_4a_3x3',
cv1_out=96,
cv1_filter=(1, 1),
cv2_out=192,
cv2_filter=(3, 3),
cv2_strides=(1, 1),
padding=(1, 1))
inception_4a_5x5 = conv2d_bn(inception_3c,
layer='inception_4a_5x5',
cv1_out=32,
cv1_filter=(1, 1),
cv2_out=64,
cv2_filter=(5, 5),
cv2_strides=(1, 1),
padding=(2, 2))
inception_4a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_3c)
 inception_4a_pool = conv2d_bn(inception_4a_pool,
layer='inception_4a_pool',
cv1_out=128,
cv1_filter=(1, 1),
padding=(2, 2))
inception_4a_1x1 = conv2d_bn(inception_3c,
layer='inception_4a_1x1',
cv1_out=256,
cv1_filter=(1, 1))
inception_4a = concatenate([inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1], axis=3)
 inception_4e_3x3 = conv2d_bn(inception_4a,
layer='inception_4e_3x3',
cv1_out=160,
cv1_filter=(1, 1),
cv2_out=256,
cv2_filter=(3, 3),
cv2_strides=(2, 2),
padding=(1, 1))
inception_4e_5x5 = conv2d_bn(inception_4a,
layer='inception_4e_5x5',
cv1_out=64,
cv1_filter=(1, 1),
cv2_out=128,
cv2_filter=(5, 5),
cv2_strides=(2, 2),
padding=(2, 2))
inception_4e_pool = MaxPooling2D(pool_size=3, strides=2 )(inception_4a)
 inception_4e_pool = ZeroPadding2D(padding=(( 0, 1),(0, 1)) )(inception_4e_pool)
 inception_4e = concatenate([inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3)
 inception_5a_3x3 = conv2d_bn(inception_4e,
layer='inception_5a_3x3',
cv1_out=96,
cv1_filter=(1, 1),
cv2_out=384,
cv2_filter=(3, 3),
cv2_strides=(1, 1),
padding=(1, 1))
inception_5a_pool = AveragePooling2D(pool_size=(3, 3), strides=(3, 3))(inception_4e)
 inception_5a_pool = conv2d_bn(inception_5a_pool,
layer='inception_5a_pool',
cv1_out=96,
cv1_filter=(1, 1),
padding=(1, 1))
inception_5a_1x1 = conv2d_bn(inception_4e,
layer='inception_5a_1x1',
cv1_out=256,
cv1_filter=(1, 1))
inception_5a = concatenate([inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3)
 inception_5b_3x3 = conv2d_bn(inception_5a,
layer='inception_5b_3x3',
cv1_out=96,
cv1_filter=(1, 1),
cv2_out=384,
cv2_filter=(3, 3),
cv2_strides=(1, 1),
padding=(1, 1))
inception_5b_pool = MaxPooling2D(pool_size=3, strides=2 )(inception_5a)
 inception_5b_pool = conv2d_bn(inception_5b_pool,
layer='inception_5b_pool',
cv1_out=96,
cv1_filter=(1, 1))
inception_5b_pool = ZeroPadding2D(padding=(1, 1))(inception_5b_pool)
 inception_5b_1x1 = conv2d_bn(inception_5a,
layer='inception_5b_1x1',
cv1_out=256,
cv1_filter=(1, 1))
inception_5b = concatenate([inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3)
 av_pool = AveragePooling2D(pool_size=(3, 3), strides=(1, 1))(inception_5b)
 reshape_layer = Flatten()(av_pool)
 dense_layer = Dense(128, name='dense_layer' )(reshape_layer)
 norm_layer = Lambda(lambda x: K.l2_normalize(x, axis=1), name='norm_layer' )(dense_layer)
 return Model(inputs=[myInput], outputs=norm_layer)
 mod = create_model()
mod.load_weights('.. /input/kin-detection/nn4.small2.v1.h5')
n_components = 5
text_features = []
for i in X_text.columns:
print(f'generating features from: {i}')
 tfv = TfidfVectorizer(min_df=2, max_features=None,
strip_accents='unicode', analyzer='word', token_pattern=r'(?u)\b\w+\b',
ngram_range=(1, 3), use_idf=1, smooth_idf=1, sublinear_tf=1)
 svd_ = TruncatedSVD(
 n_components=n_components, random_state=1337)
 tfidf_col = tfv.fit_transform(X_text.loc[:, i].values)
 svd_col = svd_.fit_transform(tfidf_col)
 svd_col = pd.DataFrame(svd_col)
 svd_col = svd_col.add_prefix('TFIDF_{}_'.format(i))
text_features.append(svd_col)
 text_features = pd.concat(text_features, axis=1)
 X_temp = pd.concat([X_temp, text_features], axis=1)
 for i in X_text.columns:
X_temp = X_temp.drop(i, axis=1 )<prepare_x_and_y>
callbacks = [keras.callbacks.EarlyStopping(monitor='val_loss',
patience=8,
verbose=1,
min_delta=1e-4),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=4,
verbose=1,
epsilon=1e-4),
keras.callbacks.ModelCheckpoint('model_best_checkpoint.h5',save_best_only=True,
verbose=0, save_weights_only=True,monitor='val_auc',mode='max'),
]
X_temp = X_temp.drop(to_drop_columns, axis=1)
 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)
 assert X_train.shape[0] == train.shape[0]
assert X_test.shape[0] == test.shape[0]
train_cols = X_train.columns.tolist()
train_cols.remove('AdoptionSpeed')
 test_cols = X_test.columns.tolist()
assert np.all(train_cols == test_cols)
 X_train_non_null = X_train.fillna(-1)
 X_test_non_null = X_test.fillna(-1 )<import_modules>
input_shape =(96,96,3)
 input_a = Input(shape=input_shape)
 input_b = Input(shape=input_shape)
 base_network = create_base_network(input_shape)
 processed_a = base_network(input_a)
 processed_b = base_network(input_b)
 x_detector = processed_a
shape_detector = processed_a.shape
shape_extractor = processed_b.shape
x_extractor = processed_b
x_detector = keras.layers.Reshape([shape_detector[1] * shape_detector[2] , shape_detector[-1]] )(x_detector)
 x_extractor = keras.layers.Reshape([shape_extractor[1] * shape_extractor[2] , shape_extractor[-1]] )(x_extractor)
 x = keras.layers.Lambda(outer_product )([x_detector, x_extractor])
 x = keras.layers.Reshape([shape_detector[-1]*shape_extractor[-1]] )(x)
 x = keras.layers.Lambda(signed_sqrt )(x)
 x = keras.layers.Lambda(L2_norm )(x)
 x = Dense(100, activation="relu" )(x)
 x = Dropout(0.01 )(x)
 out = Dense(units=1,kernel_regularizer=keras.regularizers.l2(1e-8),kernel_initializer='glorot_normal',activation="sigmoid" )(x)
 model = Model([input_a, input_b], out)
 model.compile(loss="binary_crossentropy", metrics=['acc',auc], optimizer=Adam(0.001))
print(model.summary())
 def Generator(batch_size, data):
while True:
yield getMiniBatch(batch_size=batch_size, data=data)
 train_gen = Generator(batch_size=16,data=train)
 val_gen = Generator(batch_size=16,data=val)
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 )<train_on_grid>
model.fit_generator(train_gen,steps_per_epoch=200,use_multiprocessing=True,
epochs=100,validation_data=val_gen,validation_steps=100,callbacks=callbacks)
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']<train_model>
test_path = ".. /input/recognizing-faces-in-the-wild/test/"
xgb_params = {
'eval_metric': 'rmse',
'seed': 1337,
'silent': 1,
}
def run_xgb(params, X_train, X_test):
n_splits = 5
verbose_eval = 1000
num_rounds = 30000
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
model, oof_train, oof_test = run_xgb(xgb_params, X_train_non_null, X_test_non_null )<compute_train_metric>
model.load_weights('model_best_checkpoint.h5')
optR = OptimizedRounder()
optR.fit(oof_train, X_train['AdoptionSpeed'].values)
 coefficients = optR.coefficients()
valid_pred = optR.predict(oof_train, coefficients)
 qwk = quadratic_weighted_kappa(X_train['AdoptionSpeed'].values, valid_pred)
 print("QWK = ", qwk )<predict_on_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')
 predictions = []
coefficients_ = coefficients.copy()
coefficients_[0] = 1.65
train_predictions = optR.predict(oof_train, coefficients_ ).astype(np.int8)
 test_predictions = optR.predict(oof_test.mean(axis=1), coefficients_ ).astype(np.int8)
 submission2 = pd.DataFrame({'PetID': test['PetID'].values, 'AdoptionSpeed': test_predictions} )<load_from_csv>
predictions = []
for batch in tqdm(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
<define_variables><EOS> 
submission['is_related'] = predictions
submission.to_csv("sub_detect_kinship.csv", index=False )