Kaggle competition: Categorical Feature Encoding Challenge II
<SOS> metric: AUC Kaggle data source: categorical-feature-encoding-challenge-ii<choose_model_class>
import category_encoders as ce
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import MaxPool1D, Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras import callbacks
from sklearn.metrics import accuracy_score, roc_auc_score, auc
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn import metrics, preprocessing
from tensorflow.keras import backend as K
def baseline_model() :
input_1 = Input(shape=(224, 224, 3))
input_2 = Input(shape=(224, 224, 3))
base_model = VGGFace(model='resnet50', include_top=False)
 base_model = Model(base_model.input, base_model.layers[-2].output)
 x1 = base_model(input_1)
 x2 = base_model(input_2)
 x1 = Concatenate(axis=-1 )([GlobalAvgPool2D()(x1), GlobalAvgPool2D()(x1)])
 x2 = Concatenate(axis=-1 )([GlobalAvgPool2D()(x2), GlobalAvgPool2D()(x2)])
 x3 = Subtract()([x1, x2])
 x4 = Multiply()([x3, x3])
 x5 = Multiply()([x1, x1])
 x6 = Multiply()([x2, x2])
 x7 = Subtract()([x5, x6])
 x = Concatenate(axis=-1 )([x5, x6, Multiply()([x1, x2])])
 x = Dense(2048, activation="relu" )(x)
 x = Dropout(0.01 )(x)
 x = Dense(512, activation="relu" )(x)
 x = Dropout(0.01 )(x)
 x = Dense(128, activation="relu" )(x)
 cos_dis = Lambda(cosine_distance, output_shape=cos_dist_output_shape )([x1, x2])
 x = Concatenate(axis=-1 )([x, cos_dis])
 x = Dense(32, activation="relu" )(x)
 x = Dropout(0.1 )(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<choose_model_class>
sample_submission = pd.read_csv(".. /input/cat-in-the-dat-ii/sample_submission.csv")
 test = pd.read_csv(".. /input/cat-in-the-dat-ii/test.csv")
 train = pd.read_csv(".. /input/cat-in-the-dat-ii/train.csv" )
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]<set_options>
def encoding(train, test, smooth):
print('Target encoding...')
 train.sort_index(inplace=True)
 target = train['target']
test_id = test['id']
train.drop(['target', 'id'], axis=1, inplace=True)
 test.drop('id', axis=1, inplace=True)
 cat_feat_to_encode = train.columns.tolist()
smoothing=smooth
oof = pd.DataFrame([])
 for tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state=2020, shuffle=True ).split(train, target):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train.iloc[tr_idx, :], target.iloc[tr_idx])
 oof = oof.append(ce_target_encoder.transform(train.iloc[oof_idx, :]), ignore_index=False)
 ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(train, target)
 train = oof.sort_index()
test = ce_target_encoder.transform(test)
 features = list(train)
 print('Target encoding done!')
 return train, test, test_id, features, target
gc.collect()<train_model>
train, test, test_id, features, target = encoding(train, test, 0.3 )
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 )<categorify>
train['target']=target
scaler=StandardScaler()
test_data=scaler.fit_transform(test[features])
 test_data=test_data.reshape(test_data.shape[0],test_data.shape[1],1 )
test_path = ".. /input/test/"
def chunker(seq, size=32):
return(seq[pos:pos + size] for pos in range(0, len(seq), size))
def read_img(path):
img = cv2.imread(path)
 img = np.array(img ).astype(np.float)
 return preprocess_input(img, version=2 )<load_from_csv>
def NN_model() :
model=Sequential()
model.add(Flatten(input_shape=(23,1)))
 model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer=Adam(lr=1e-3), loss='binary_crossentropy', metrics=['accuracy'])
 return model
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<save_to_csv>
oof_preds = np.zeros(( len(train)))
 test_preds = np.zeros(( len(test)))
 skf = StratifiedKFold(n_splits=10)
 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, X_test.target
X_train=scaler.fit_transform(X_train[features])
 X_test=scaler.fit_transform(X_test[features])
 y_train=y_train.to_numpy()
y_test=y_test.to_numpy()
X_train=X_train.reshape(X_train.shape[0],X_train.shape[1],1)
 X_test=X_test.reshape(X_test.shape[0],X_test.shape[1],1)
 es = callbacks.EarlyStopping(monitor='val_accuracy', min_delta=0.001, patience=5,verbose=1, mode='max', baseline=None, restore_best_weights=True)
 rlr = callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.5,patience=3, min_lr=1e-6, mode='max', verbose=1)
 model = NN_model()
class_weight = {0: 0.25, 1: 0.75}
model.fit(X_train, y_train, epochs=25, batch_size=1024, callbacks=[es, rlr], verbose=1,validation_data=(X_test, y_test), class_weight=class_weight)
 valid_fold_preds = model.predict(X_test)
 test_fold_preds = model.predict(test_data)
 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()
submission['is_related'] = predictions
submission.to_csv("vgg_face.csv", index=False )<feature_engineering>
test_preds /= 10
print("Saving submission file")
 submission = pd.DataFrame.from_dict({
'id': test_id,
'target': test_preds
})
 submission.to_csv("submission.csv", index=False )