Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_model>
from IPython.display import Image
def run_training(model, train_df, test_df,sample_submission, fold_col,
orig_features, mixture_val_cols,mixture_label_cols, target_col,
benchmark,
outlier_col=None, nn=False, epochs=10,batch_size=32, verbose=False,
dense=70, dout=0.15, dense_reg = 0.000001,act='elu'):
FOLD_VALUES = sorted([x for x in train_df[fold_col].unique() ])
 oof = np.zeros(( len(train_df),))
test_predictions = np.zeros(( len(sample_submission),))
fig,axes=plt.subplots(figsize=(15,6))
axes.set_ylim(0.69,0.75)
 RANDOM_SEEDS = [0,42,100,1000]
for rs in RANDOM_SEEDS:
seed_everything(seed=rs)
 for fold in FOLD_VALUES:
print(' --- ')
 print('running random seed', rs, 'fold', fold)
 if outlier_col:
trn_idx =(train_df[fold_col]!=fold)&(~train_df[outlier_col])
 else:
trn_idx = train_df[fold_col]!=fold
val_idx = train_df[fold_col]==fold
X_train_orig = train_df.loc[trn_idx,orig_features].values
X_train = train_df.loc[trn_idx,mixture_val_cols].values
X_train_mask = train_df.loc[trn_idx,mixture_label_cols].values
y_train = train_df.loc[trn_idx, target_col].values
X_val_orig = train_df.loc[val_idx,orig_features].values
X_val = train_df.loc[val_idx,mixture_val_cols].values
X_val_mask = train_df.loc[val_idx,mixture_label_cols].values
y_val = train_df.loc[val_idx, target_col].values
model = keras_model(orig_features, mixture_val_cols,mixture_label_cols,dout=dout,
dense=dense,act=act, dense_reg = dense_reg, descend_fraction = 0.9)
 K.clear_session()
back_count = 5
epoch_list=list(range(epochs-back_count, epochs))
class CustomModelCheckpoint(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if epoch in epoch_list:
self.model.save_weights('model_fold_'+str(fold)+'_epoch_'+str(epoch)+'.h5', overwrite=True)
 else:
pass
cbk = CustomModelCheckpoint()
history = model.fit([X_train_orig, X_train,X_train_mask ], y_train, epochs=epochs, batch_size=batch_size,shuffle=True,
validation_data=([X_val_orig, X_val,X_val_mask], y_val), verbose=verbose,
callbacks=[cbk])
 print('Fold Last Epoch Train Error', history.history['root_mean_squared_error'][-1])
 print('Fold Last Epoch Valid Error', history.history['val_root_mean_squared_error'][-1])
 model.save_weights('model_final_'+str(fold)+'.h5')
 sns.lineplot(x=range(epochs), y=history.history['loss'], color='Blue')
 sns.lineplot(x=range(epochs), y=history.history['val_loss'], color='Red')
 val_preds = np.zeros(( len(X_val),))
for e in epoch_list:
model = keras_model(orig_features, mixture_val_cols,mixture_label_cols,dout=dout,
dense=dense,act=act, dense_reg = dense_reg, descend_fraction = 0.9)
 model.load_weights('model_fold_'+str(fold)+'_epoch_'+str(e)+'.h5')
 val_preds += model.predict([X_val_orig, X_val,X_val_mask] ).mean(axis=1)
 test_predictions += model.predict([test_df[orig_features].values,test_df[mixture_val_cols].values,
test_df[mixture_label_cols].values] ).mean(axis=1)
 val_preds = val_preds / len(epoch_list)
 oof[val_idx]+=val_preds
val_error = np.sqrt(mse(y_val, val_preds))
print('Fold multi epoch weight prediction error', val_error)
 oof = oof / len(RANDOM_SEEDS)
 total_val_error = np.sqrt(mse(train_df[target_col], oof))
print('final OOF MSE', total_val_error)
 fold_errors = []
for fold in train_df[fold_col].unique() :
val_idx = train_df[fold_col]==fold
fold_errors+=[np.sqrt(mse(train_df.loc[val_idx, target_col].values, oof[val_idx])) ]
test_predictions = test_predictions/(NFOLDS * len(RANDOM_SEEDS)* len(epoch_list))
return oof, test_predictions, fold_errors<choose_model_class>
Image('.. /input/deepfake-kernel-data/google_cloud_compute_engine_launch_vm.png' )
def keras_model(ft_orig, mixture_values, mixture_labels, n_layer=3,bnorm=True,dout=0.2,
dense=20,act='elu', dense_reg = 0.000001, descend_fraction = 0.9):
input1 = L.Input(shape=(len(ft_orig)) , name='input_orig')
 input1_do = L.Dropout(0.1 )(input1)
 XA = L.Dense(dense, activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg), name='dense_orig' )(input1_do)
 if bnorm:
XA = L.BatchNormalization(name='bn_1' )(XA)
 XA1 = L.Dropout(dout, name='do_orig' )(XA)
 input2 = L.Input(shape=(len(mixture_values)) , name='input_new')
 input3 = L.Input(shape=(len(mixture_labels)) , name='input_new2')
 all_input_combo = tf.keras.layers.Concatenate(axis=1, name='concat_i2' )([input2, input3])
 input2_do = L.Dropout(0.2 )(all_input_combo)
 XB = L.Dense(dense, activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg), name='dense_new' )(input2_do)
 if bnorm:
XB = L.BatchNormalization(name='bn_2' )(XB)
 XB1 = L.Dropout(dout, name='do_new' )(XB)
 all_input_combo = tf.keras.layers.Concatenate(axis=1, name='concat_new' )([XA1, XB1])
 X = L.Dense(int(dense), activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg))(all_input_combo)
 if bnorm:
X = L.BatchNormalization(name='bn_3' )(X)
 X2 = L.Dropout(dout, name='do_3' )(X)
 X2 = tf.keras.layers.concatenate([all_input_combo, X2], axis=1)
 X = L.Dense(int(( descend_fraction**2)*dense), activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg))(X2)
 if bnorm:
X = L.BatchNormalization()(X)
 X3 = L.Dropout(dout )(X)
 X3 = tf.keras.layers.concatenate([X2, X3], axis=1)
 X = L.Dense(int(( descend_fraction**3)*dense), activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg *2))(X3)
 if bnorm:
X = L.BatchNormalization()(X)
 X4 = L.Dropout(dout )(X)
 X4 = tf.keras.layers.concatenate([X3, X4],axis=1)
 X = L.Dense(int(( descend_fraction**4)*dense), activation=act, activity_regularizer=tf.keras.regularizers.L2(dense_reg*4))(X4)
 if bnorm:
X = L.BatchNormalization()(X)
 X5 = L.Dropout(dout )(X)
 X5 = tf.keras.layers.concatenate([X4, X5], axis=1)
 X = L.Dense(5, activation=act, activity_regularizer=tf.keras.regularizers.L2(0.0001))(X5)
 output1 = L.Dense(1, activation='linear' )(X)
 model = tf.keras.Model(inputs=[input1, input2, input3], outputs=output1)
 model.compile(loss=root_mean_squared_error,
optimizer=tfa.optimizers.AdamW(learning_rate=0.000809028893821181, weight_decay=9.83479875802558E-06),
metrics=tf.keras.metrics.RootMeanSquaredError())
 return model
K.clear_session()
model = keras_model(FT_COLS, mixture_title_cols, mixture_value_cols,
dense=70,
dout=0.15,
dense_reg = 0.000001,
act='elu',
descend_fraction = 0.9)
 model.summary()<train_model>
Image('.. /input/deepfake-kernel-data/google_cloud_vm.png' )
oof, test_predictions, fold_errors = run_training(model, train, test,submission, 'fold', FT_COLS,
mixture_value_cols, mixture_title_cols, TARGET, cv_bm,
outlier_col='outlier_filter',
epochs=25,
batch_size=256,
verbose=False,
dense=70,
dout=0.15,
dense_reg = 0.000001,
act='elu', )<save_to_csv>
Image('.. /input/deepfake-kernel-data/lr_15e-2_epochs_42_patience_5.png' )
print('Save Out of Fold Predictions')
 oof = pd.DataFrame(columns=['oof_prediction'], index=train['id'], data=oof + TARGET_MEAN)
 oof.to_csv('oof_predictions.csv', index=True)
 oof.head(10 )<compute_test_metric>
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_10_patience_5.png' )
print('fold errors', fold_errors)
 print('fold error std', np.array(fold_errors ).std() )<save_to_csv>
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_20_patience_5.png' )
submission['target'] = test_predictions + TARGET_MEAN
submission.to_csv('submission.csv', index=False)
 submission.head(5 )<import_modules>
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_12_patience_2.png' )
import numpy as np
import pandas as pd
from math import sqrt
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.impute import SimpleImputer
from sklearn.ensemble import VotingRegressor
from sklearn.feature_selection import VarianceThreshold
from sklearn.metrics import make_scorer, mean_squared_error
from sklearn.model_selection import GridSearchCV, ParameterGrid, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer, StandardScaler<define_variables>
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_30_patience_2.png' )
RANDOM_STATE = 2021
CROSS_VALIDATION = 3<define_variables>
Image('.. /input/deepfake-kernel-data/google_cloud_vm_deepfake_training_screenshot.png' )
data_dir = '/kaggle/input/tabular-playground-series-jan-2021'<load_from_csv>
%matplotlib inline
warnings.filterwarnings("ignore" )
df = pd.read_csv(f"{data_dir}/train.csv" ).set_index('id' ).convert_dtypes()
display(df.shape)
 df.head(2 )<split>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )
X = df.copy()
y = X.pop('target')
 X_train, X_valid, y_train, y_valid = train_test_split(
 X, y, train_size=0.8, test_size=0.2, random_state=RANDOM_STATE,
)<define_variables>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
preprocessor = Pipeline(steps=[ 
('imputer', SimpleImputer()), 
('log', FunctionTransformer(np.log1p)) , 
('scaler', StandardScaler()),
] )<choose_model_class>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
pipeline = Pipeline([ 
('preprocessor', preprocessor), 
('variance_drop', VarianceThreshold(threshold=(0.95 *(1 - 0.95)))) , 
('voting', 'passthrough'),
] )<choose_model_class>
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )
parameters = [
{
'voting': [VotingRegressor([ 
('lgbm', LGBMRegressor(random_state=RANDOM_STATE)) , 
('xgb', XGBRegressor(random_state=RANDOM_STATE))
])],
'voting__lgbm__n_estimators': [2000],
'voting__lgbm__max_depth': [12],
'voting__lgbm__learning_rate': [0.01],
'voting__lgbm__num_leaves': [256],
'voting__lgbm__min_child_weight': [12],
'voting__lgbm__feature_fraction': [0.4],
'voting__lgbm__bagging_fraction': [0.7],
'voting__lgbm__bagging_freq': [5],
'voting__lgbm__min_child_samples': [32],
'voting__lgbm__lambda_l1':[9],
'voting__lgbm__lambda_l2': [0.13],
'voting__xgb__n_estimators': [2000],
'voting__xgb__max_depth': [12],
'voting__xgb__learning_rate': [0.01],
'voting__xgb__alpha': [5],
'voting__xgb__gamma': [3],
'voting__xgb__lambda': [3],
'voting__xgb__subsample': [0.8],
'voting__xgb__colsample_bytree': [0.4],
}
]<find_best_params>
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )
total = CROSS_VALIDATION * len(ParameterGrid(parameters))
display(f"Number of combination that will be run by the GridSearch: {total}" )<compute_test_metric>
input_size = 224
custom_scoring = make_scorer(
 score_func=lambda y, y_pred: mean_squared_error(y, y_pred, squared=False),
greater_is_better=False,
)<choose_model_class>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
grid_search = GridSearchCV(
 pipeline,
param_grid=parameters,
cv=CROSS_VALIDATION,
scoring=custom_scoring,
n_jobs=-1,
verbose=True,
)<train_on_grid>
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )
grid_search.fit(X_train, y_train )<find_best_params>
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
display(abs(grid_search.best_score_))
display(grid_search.best_params_ )<compute_train_metric>
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.481
preds = grid_search.best_estimator_.predict(X_valid)
 mean_squared_error(y_valid, preds, squared=False )<load_from_csv>
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )
X_test = pd.read_csv(f"{data_dir}/test.csv" ).set_index('id' ).convert_dtypes()
display(X_test.shape)
 X_test.head(2 )<save_to_csv>
speed_test = False
preds_test = grid_search.best_estimator_.predict(X_test)
 output = pd.DataFrame(
 {'Id': X_test.index, 'target': preds_test})
 output.to_csv(f"submission.csv", index=False )<install_modules>
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )
!pip install pytorch_tabular
!pip install torch_optimizer<import_modules>
predictions = predict_on_video_set(test_videos, num_workers=4 )
import numpy as np
import pandas as pd
import time
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from pytorch_tabular import TabularModel
from pytorch_tabular.models import CategoryEmbeddingModelConfig, NodeConfig, TabNetModelConfig
from pytorch_tabular.config import DataConfig, OptimizerConfig, TrainerConfig, ExperimentConfig
from pytorch_tabular.categorical_encoders import CategoricalEmbeddingTransformer
from torch_optimizer import QHAdam
import category_encoders as ce
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor<load_from_csv>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
df_train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 display(df_train.head())
 df_test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 display(df_test.head())
 features = ['cont1', 'cont2', 'cont3', 'cont4', 'cont5',
'cont6', 'cont7', 'cont8', 'cont9', 'cont10',
'cont11', 'cont12', 'cont13', 'cont14']<categorify>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
enc = KBinsDiscretizer(n_bins=10, encode='ordinal', strategy="quantile")
 enc.fit(df_train[features])
 binned_df_train = enc.transform(df_train[features])
 binned_df_test = enc.transform(df_test[features])
 for i, feature in enumerate(features):
df_train[f"{feature}_binned"] = binned_df_train[:,i]
df_test[f"{feature}_binned"] = binned_df_test[:,i]<define_variables>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )
def get_configs(train):
epochs = 25
batch_size = 512
steps_per_epoch = int(( len(train)//batch_size)*0.9)
 data_config = DataConfig(
 target=['target'],
continuous_cols=['cont1', 'cont2', 'cont3', 'cont4', 'cont5', 'cont6', 'cont7',
'cont8', 'cont9', 'cont10', 'cont11', 'cont12', 'cont13', 'cont14'],
categorical_cols=['cont1_binned', 'cont2_binned', 'cont3_binned',
'cont4_binned', 'cont5_binned', 'cont6_binned', 'cont7_binned',
'cont8_binned', 'cont9_binned', 'cont10_binned', 'cont11_binned',
'cont12_binned', 'cont13_binned', 'cont14_binned'],
continuous_feature_transform="quantile_normal" 
)
 trainer_config = TrainerConfig(
 auto_lr_find=False,
batch_size=batch_size,
max_epochs=epochs,
gpus=1, 
)
 optimizer_config = OptimizerConfig(lr_scheduler="OneCycleLR", lr_scheduler_params={"max_lr":0.005, "epochs": epochs, "steps_per_epoch":steps_per_epoch})
 model_config = NodeConfig(
 task="regression",
num_layers=2,
num_trees=1024,
depth=6,
embed_categorical=True,
learning_rate = 1e-3,
target_range=[(df_train[col].min() ,df_train[col].max())for col in ['target']] 
)
 return data_config, trainer_config, optimizer_config, model_config<drop_column>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
rnd_seed_cv = 1234
rnd_seed_reg = 1234
kf = KFold(n_splits=5, random_state=rnd_seed_cv, shuffle=True)
 df_train.drop(columns='id', inplace=True)
 df_test.drop(columns='id', inplace=True)
 df_test['target'] = 0<train_model>
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )
def node(train, valid, df_test):
data_config, trainer_config, optimizer_config, model_config = get_configs(train)
 tabular_model = TabularModel(
 data_config=data_config,
model_config=model_config,
optimizer_config=optimizer_config,
trainer_config=trainer_config 
)
 tabular_model.fit(train=train, validation=valid, optimizer=QHAdam,
optimizer_params={"nus":(0.7, 1.0), "betas":(0.95, 0.998)})
 result = tabular_model.evaluate(valid)
 return np.sqrt(result[0]["test_mean_squared_error"]), tabular_model.predict(valid)["target_prediction"].values, tabular_model.predict(df_test)["target_prediction"].values, tabular_model
def lgbm(train, valid, df_test):
lgb_model = LGBMRegressor(n_estimators=10000,
learning_rate=0.005,
early_stopping_rounds=50,
feature_pre_filter = False,
num_leaves=102,
min_child_samples=20,
colsample_bytree = 0.4,
subsample = 1,
subsample_freq = 0,
lambda_l1 = 4.6,
lambda_l2 = 1.9,
random_state=42)
 lgb_model.fit(train.drop(columns='target'), train['target'], eval_set=(valid.drop(columns='target'),valid.loc[:,'target']))
lgb_preds = lgb_model.predict(valid.drop(columns='target'))
score = mean_squared_error(valid['target'].values, lgb_preds, squared=False)
 return score, lgb_model.predict(valid.drop(columns='target')) , lgb_model.predict(df_test.drop(columns='target'))
def cb(train, valid, df_test):
best_params = {
'grow_policy': 'Lossguide',
'boosting_type': 'Plain',
'depth': 20,
'l2_leaf_reg': 3.699746597668451,
'min_data_in_leaf': 4,
'random_strength': 4.9263987954247455,
'rsm': 1.0,
"n_estimators": 10000,
"learning_rate": 0.5,
"od_type": "Iter",
"od_wait": 50,
}
catboost_model = CatBoostRegressor(**best_params)
 catboost_model.fit(train.drop(columns='target'), train['target'], eval_set=(valid.drop(columns='target'),valid.loc[:,'target']))
cb_preds = catboost_model.predict(valid.drop(columns='target'))
score = mean_squared_error(valid['target'].values, cb_preds, squared=False)
 return score, catboost_model.predict(valid.drop(columns='target')) , catboost_model.predict(df_test.drop(columns='target'))<feature_engineering>
frames_per_video = 64
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )
CV_node = []
CV_lgb = []
CV_cb = []
preds_train_node = []
preds_train_lgb = []
preds_train_cb = []
preds_test_node = []
preds_test_lgb = []
preds_test_cb = []
cross_validated_preds = []
t1 = time.time()
for train_index, test_index in kf.split(df_train):
train = df_train.iloc[train_index]
valid = df_train.iloc[test_index]
cv_val = valid.copy()
node_score, node_train_pred, node_test_pred, tabular_model = node(train, valid, df_test)
 CV_node.append(node_score)
 cv_val['pred_node'] = node_train_pred
preds_train_node.append(node_train_pred)
 preds_test_node.append(node_test_pred)
 transformer = CategoricalEmbeddingTransformer(tabular_model)
 train_transform = transformer.fit_transform(train)
 val_transform = transformer.transform(valid)
 df_test_transform = transformer.transform(df_test)
 lgbm_score, lgbm_train_pred, lgbm_test_pred = lgbm(train_transform, val_transform, df_test_transform)
 CV_lgb.append(lgbm_score)
 cv_val['pred_lgb'] = lgbm_train_pred
preds_train_lgb.append(lgbm_train_pred)
 preds_test_lgb.append(lgbm_test_pred)
 cb_score, cb_train_pred, cb_test_pred = cb(train_transform, val_transform, df_test_transform)
 CV_cb.append(cb_score)
 cv_val['pred_cb'] = cb_train_pred
preds_train_cb.append(cb_train_pred)
 preds_test_cb.append(cb_test_pred)
 cross_validated_preds.append(cv_val)
 t2 = time.time()
print('Elapsed time [s]: ', t2-t1 )<compute_test_metric>
input_size = 150
print('CV performance [RMSE]: ', np.mean(CV_node, axis=0))
print('CV performance [RMSE]: ', np.mean(CV_lgb, axis=0))
print('CV performance [RMSE]: ', np.mean(CV_cb, axis=0))<save_to_csv>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
cross_val_pred_df = pd.concat(cross_validated_preds, sort=False)
 cross_val_pred_df.to_csv("cross_val_preds.csv")
 joblib.dump(preds_test_node, "preds_test_node.sav")
 joblib.dump(preds_test_lgb, "preds_test_lgb.sav")
 joblib.dump(preds_test_cb, "preds_test_cb.sav" )<prepare_output>
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 class Pooling(nn.Module):
def __init__(self):
super(Pooling, self ).__init__()
self.p1 = nn.AdaptiveAvgPool2d(( 1,1))
self.p2 = nn.AdaptiveMaxPool2d(( 1,1))
def forward(self, x):
x1 = self.p1(x)
 x2 = self.p2(x)
 return(x1+x2)* 0.5
model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,1)))
 class Head(torch.nn.Module):
def __init__(self, in_f, out_f):
super(Head, self ).__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, 512)
 self.d = nn.Dropout(0.5)
 self.o = nn.Linear(512, out_f)
 self.b1 = nn.BatchNorm1d(in_f)
 self.b2 = nn.BatchNorm1d(512)
 self.r = nn.ReLU()
def forward(self, x):
x = self.f(x)
 x = self.b1(x)
 x = self.d(x)
 x = self.l(x)
 x = self.r(x)
 x = self.b2(x)
 x = self.d(x)
 out = self.o(x)
 return out
class FCN(torch.nn.Module):
def __init__(self, base, in_f):
super(FCN, self ).__init__()
self.base = base
self.h1 = Head(in_f, 1)
 def forward(self, x):
x = self.base(x)
 return self.h1(x)
 net = []
model = FCN(model, 2048)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-kernel-data/model_50epochs_lr0001_patience5_factor01_batchsize32.pth'))
net.append(model )
avg_cb_pred = np.mean(preds_test_cb, axis=0)
 avg_lgb_pred = np.mean(preds_test_lgb, axis=0)
 avg_node_pred = np.mean(preds_test_node, axis=0)
 pred_test = np.average([avg_node_pred, avg_lgb_pred, avg_cb_pred], axis=0, weights=[-0.15447081, 1.1021915 , 0.06145868] )<load_from_csv>
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.481
df_sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv')
 df_sub.target = pred_test
df_sub.head()<save_to_csv>
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )
df_sub.to_csv('submission.csv', index=False )<load_from_csv>
speed_test = False
train = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/train.csv', index_col='id')
 test = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv', index_col='id' )<import_modules>
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )
import seaborn as sns<filter>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
train[train['target'] <= 4.5]<drop_column>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
train.drop([241352, 284103, 300936, 307139, 355831], axis = 0, inplace=True)
 train[train['target'] <= 4.5]<drop_column>
submission_df = pd.DataFrame({"filename": test_videos})
 submission_df["label"] = 0.51*submission_df_resnext["label"] + 0.5*submission_df_xception["label"]
target = train['target']
train.drop('target', axis=1, inplace=True )<split>
submission_df.to_csv("submission.csv", index=False )