Kaggle competition: Categorical Feature Encoding Challenge II
np.save('test_preds_libffm.npy', test_preds_avg)
 np.save('oof_preds_libffm.npy', oof_preds )<load_from_csv>
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from tensorflow.keras import layers
from tensorflow.keras import Sequential
from tensorflow.keras import Model
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
import os
import gc
import joblib
from sklearn.model_selection import StratifiedKFold
from sklearn import metrics
from tensorflow.keras import optimizers
from tensorflow.keras.models import Model, load_model
from tensorflow.keras import callbacks
from tensorflow.keras import backend as K
from tensorflow.keras import utils
import tensorflow as tf
subs = [
'/kaggle/input/bestpublicscores/sub_623.csv',
'/kaggle/input/bestpublicscores/sub_634.csv',
'/kaggle/input/bestpublicscores/sub_626.csv',
'/kaggle/input/bestpublicscores/sub_600.csv',
'/kaggle/input/bestpublicscores/sub_590.csv',
'/kaggle/input/otherbestpublicscores/sub_659.csv',
'/kaggle/input/otherbestpublicscores/sub_606.csv',
'/kaggle/input/otherbestpublicscores/sub_563.csv',
'/kaggle/input/otherbestpublicscores/sub_620.csv',
'/kaggle/input/bestpublicscores3/sub_589.csv',
'libffm_sub_531.csv'
] 
<load_from_csv>
train=pd.read_csv('.. /input/cat-in-the-dat-ii/train.csv')
 test=pd.read_csv('.. /input/cat-in-the-dat-ii/test.csv' )
predictions = pd.concat([pd.read_csv(sub, index_col='id')for sub in subs], axis=1 ).reset_index(drop=True)
 predictions.columns = ['sub_'+str(i)for i in range(11)]
predictions<feature_engineering>
print("training data size: ", train.shape)
 print("training data size: ", test.shape )
for col in predictions.columns:
predictions[col]=predictions[col].rank() /predictions.shape[0]<save_to_csv>
def create_model(data, features):
inputs=[]
output=[]
for c in features:
num_unique_values = int(data[c].nunique())
 embed_dim = int(min(np.ceil(( num_unique_values)/2), 50))
inp = layers.Input(shape=(1,))
out = layers.Embedding(num_unique_values + 1, embed_dim, name=c )(inp)
 out = layers.SpatialDropout1D(0.3 )(out)
 out = layers.Reshape(target_shape=(embed_dim,))(out)
 inputs.append(inp)
 output.append(out)
 x=layers.Concatenate()(output)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(300, activation="relu" )(x)
 x = layers.Dropout(0.3 )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dense(300, activation="relu" )(x)
 x = layers.Dropout(0.3 )(x)
 x = layers.BatchNormalization()(x)
 y = layers.Dense(2, activation="softmax" )(x)
 model = Model(inputs=inputs, outputs=y)
 return model
submission['target'] = blend
submission.to_csv('TopPublicBlend.csv',index=False)
 submission.head()<import_modules>
def auc(y_true, y_pred):
def fallback_auc(y_true, y_pred):
try:
return metrics.roc_auc_score(y_true, y_pred)
 except:
return 0.5
return tf.py_function(fallback_auc,(y_true, y_pred), tf.double )
import numpy as np
import pandas as pd
import category_encoders as ce
from sklearn.metrics import roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold 
<load_from_csv>
test["target"] = -1
data = pd.concat([train, test] ).reset_index(drop=True)
 features = [x for x in train.columns if x not in ["id", "target"]]
for feat in features:
lbl_enc = LabelEncoder()
data[feat] = lbl_enc.fit_transform(data[feat].fillna("-1" ).astype(str ).values)
 train = data[data.target != -1].reset_index(drop=True)
 test = data[data.target == -1].reset_index(drop=True)
 test_data = [test.loc[:, features].values[:, k] for k in range(test.loc[:, features].values.shape[1])]
oof_preds = np.zeros(( len(train)))
 test_preds = np.zeros(( len(test)))
 skf = StratifiedKFold(n_splits=50)
 for train_index, test_index in skf.split(train, train.target.values):
X_train, X_test = train.iloc[train_index, :], train.iloc[test_index, :]
X_train = X_train.reset_index(drop=True)
 X_test = X_test.reset_index(drop=True)
 y_train, y_test = X_train.target.values, X_test.target.values
model = create_model(data, features)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[auc])
 X_train = [X_train.loc[:, features].values[:, k] for k in range(X_train.loc[:, features].values.shape[1])]
X_test = [X_test.loc[:, features].values[:, k] for k in range(X_test.loc[:, features].values.shape[1])]
es = callbacks.EarlyStopping(monitor='val_auc', min_delta=0.001, patience=5,
verbose=1, mode='max', baseline=None, restore_best_weights=True)
 rlr = callbacks.ReduceLROnPlateau(monitor='val_auc', factor=0.5,
patience=3, min_lr=1e-6, mode='max', verbose=1)
 model.fit(X_train,
utils.to_categorical(y_train),
validation_data=(X_test, utils.to_categorical(y_test)) ,
verbose=1,
batch_size=1024,
callbacks=[es, rlr],
epochs=100 
)
 valid_fold_preds = model.predict(X_test)[:, 1]
test_fold_preds = model.predict(test_data)[:, 1]
oof_preds[test_index] = valid_fold_preds.ravel()
test_preds += test_fold_preds.ravel()
print(metrics.roc_auc_score(y_test, valid_fold_preds))
K.clear_session()
train = pd.read_csv('.. /input/cat-in-the-dat-ii/train.csv')
 test = pd.read_csv('.. /input/cat-in-the-dat-ii/test.csv' )<prepare_x_and_y>
tf.keras.models.save_model(model, '.. /output/kaggle/working/', overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None )
train.sort_index(inplace=True)
 train_y = train['target']; test_id = test['id']
train.drop(['target', 'id'], axis=1, inplace=True); test.drop('id', axis=1, inplace=True )<categorify>
print("Overall AUC={}".format(metrics.roc_auc_score(train.target.values, oof_preds)) )
folds = 20
smoothing=100
cat_feat_to_encode = train.columns[:].tolist()
oof = np.zeros(train.shape)
 test_oof = np.zeros(test.shape)
 for tr_idx, oof_idx in StratifiedKFold(n_splits=folds, random_state= 1032, shuffle=True ).split(train, train_y):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train.iloc[tr_idx, :], train_y.iloc[tr_idx])
 oof[oof_idx,:] = ce_target_encoder.transform(train.iloc[oof_idx, :] ).values
test_oof[:,:] += ce_target_encoder.transform(test ).values
test_oof /= folds 
<create_dataframe>
test_preds /= 50
test_ids = test.id.values
print("Saving submission file")
 submission = pd.DataFrame.from_dict({
'id': test_ids,
'target': test_preds
})
 submission.to_csv("submission.csv", index=False )
train = pd.DataFrame(data=oof,columns=cat_feat_to_encode,index=train.index.values)
 test = pd.DataFrame(data=test_oof,columns=cat_feat_to_encode,index=test.index.values)
<train_model>
train_df['target'].value_counts()
glm =LogisticRegression(random_state=2, solver='lbfgs', max_iter=20600, fit_intercept=True, penalty='l2', verbose=0)
 glm.fit(train, train_y )<compute_test_metric>
target=train_df['target']
del train_df['target']
roc_auc_score(train_y,glm.predict_proba(train)[:,1] )<save_to_csv>
train_df['nom_0'].value_counts()
pd.DataFrame({'id': test_id, 'target': glm.predict_proba(test)[:,1]} ).to_csv('submission.csv', index=False )<import_modules>
train_df['nom_1'].value_counts()
from collections import defaultdict
from glob import glob
from random import choice, sample
from keras.preprocessing import image
import cv2
import numpy as np
import pandas as pd
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.layers import Input, Dense, GlobalMaxPool2D, GlobalAvgPool2D, Concatenate, Multiply, Dropout, Subtract
from keras.models import Model
from keras.optimizers import Adam<install_modules>
train_df['nom_2'].value_counts()
!pip install git+https://github.com/rcmalli/keras-vggface.git<import_modules>
train_df['nom_3'].value_counts()
from keras_vggface.utils import preprocess_input
from keras_vggface.vggface import VGGFace<define_variables>
for i in range(10):
print(train_df['nom_{}'.format(i)].value_counts())
 print('
' )
train_file_path = ".. /input/train_relationships.csv"
train_folders_path = ".. /input/train/"
val_famillies = "F09"
all_images = glob(train_folders_path + "*/*/*.jpg")
 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 )<load_from_csv>
data=pd.concat([train_df, test_df] )
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]] 
<categorify>
cols=['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4']
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 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
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="binary_crossentropy", metrics=['acc'], optimizer=Adam(0.00001))
model.summary()
return model<train_model>
mode = data.filter(cols ).mode()
data[cols]=data[cols].fillna(mode.iloc[0] )
file_path = "vgg_face.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()
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=100, verbose=2,
workers=4, callbacks=callbacks_list, steps_per_epoch=200, validation_steps=100)
<predict_on_test>
data['bin_3']=data['bin_3'].map({'F': 0, 'T':1})
 data['bin_4']=data['bin_4'].map({'Y':1, 'N':0} )
test_path = ".. /input/test/"
def chunker(seq, size=32):
return(seq[pos:pos + size] for pos in range(0, len(seq), size))
submission = pd.read_csv('.. /input/sample_submission.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
submission['is_related'] = predictions
submission.to_csv("vgg_face.csv", index=False )<import_modules>
data[['bin_0', 'bin_1', 'bin_2']]=data[['bin_0', 'bin_1', 'bin_2']].astype('int8' )
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<concatenate>
cols=[]
for i in range(10):
cols.append("nom_{}".format(i))
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)))
<choose_model_class>
data=data.fillna(-1 )
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<normalization>
data=data.drop('id', axis=1 )
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')
<choose_model_class>
data=data.drop('index', axis=1 )
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'),
]<define_variables>
features=[i for i in data.columns]
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)
<train_model>
le=LabelEncoder()
for i in features:
data[i]=le.fit_transform(data[i].astype(str))
model.fit_generator(train_gen,steps_per_epoch=200,use_multiprocessing=True,
epochs=100,validation_data=val_gen,validation_steps=100,callbacks=callbacks)
<define_variables>
train_data=data[:600000]
test_data=data[600000:]
test_path = ".. /input/recognizing-faces-in-the-wild/test/"<load_pretrained>
X_train, X_test, y_train, y_test=train_test_split(train_data, target, test_size=.20 )
model.load_weights('model_best_checkpoint.h5')
<load_from_csv>
classifier=RandomForestClassifier(n_estimators=1200, max_depth=100, n_jobs=-1, verbose=2, max_features='sqrt' )
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 = []<predict_on_test>
classifier.fit(X_train, y_train, )
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<save_to_csv>
pred=classifier.predict(X_test )
submission['is_related'] = predictions
submission.to_csv("sub_detect_kinship.csv", index=False )<install_modules>
from sklearn.metrics import f1_score
!pip install git+https://github.com/rcmalli/keras-vggface.git<import_modules>
fs=f1_score(y_test, pred )
from collections import defaultdict
from glob import glob
from random import choice, sample
import cv2
import numpy as np
import pandas as pd
import keras.backend as K
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras.layers import Lambda
from keras.layers import Input, Dense, GlobalMaxPool2D, GlobalAvgPool2D
from keras.layers import Concatenate, Multiply, Dropout, Subtract, Add
from keras.models import Model
from keras import layers, models, optimizers
from keras.optimizers import Adam
from keras_vggface.utils import preprocess_input
from keras_vggface.vggface import VGGFace
from imgaug import augmenters as iaa<define_variables>
import lightgbm as lgb
<compute_test_metric><EOS> 
param = {
'num_leaves': 31,
'objective': 'binary'
'metric': ''}
param['metric'] = ['auc', 'binary_logloss']