Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<predict_on_test>
from IPython.display import Image
if training:
predictor_0 = treelite_runtime.Predictor(f'./mymodel_{0}.so', verbose=True)
 predictor_1 = treelite_runtime.Predictor(f'./mymodel_{1}.so', verbose=True)
 predictor_2 = treelite_runtime.Predictor(f'./mymodel_{2}.so', verbose=True)
 predictor_3 = treelite_runtime.Predictor(f'./mymodel_{3}.so', verbose=True)
 predictor_4 = treelite_runtime.Predictor(f'./mymodel_{4}.so', verbose=True )<define_variables>
Image('.. /input/deepfake-kernel-data/google_cloud_compute_engine_launch_vm.png' )
env = janestreet.make_env()
iter_test = env.iter_test()<categorify>
Image('.. /input/deepfake-kernel-data/google_cloud_vm.png' )
f = np.median
index_features = [n for n in range(1,(len(features)+ 1)) ]
for(test_df, pred_df)in tqdm(iter_test):
if test_df['weight'].item() > 0:
x_tt = test_df.values[0][index_features].reshape(1,-1)
 if np.isnan(x_tt[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
batch = treelite_runtime.Batch.from_npy2d(x_tt)
 pred_0 = predictor_0.predict(batch)
 pred_1 = predictor_1.predict(batch)
 pred_2 = predictor_2.predict(batch)
 pred_3 = predictor_3.predict(batch)
 pred_4 = predictor_4.predict(batch)
 pred = np.stack([pred_0,pred_1,pred_2,pred_3,pred_4],axis=0 ).T
pred = f(pred)
 pred_df.action = int(pred >= TRADING_THRESHOLD)
 else:
pred_df['action'].values[0] = 0
env.predict(pred_df )<import_modules>
Image('.. /input/deepfake-kernel-data/lr_15e-2_epochs_42_patience_5.png' )
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import KFold
from xgboost import XGBRegressor
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import StandardScaler
from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer
import time<load_from_csv>
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_10_patience_5.png' )
X = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')
 X_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')
 X<prepare_x_and_y>
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_20_patience_5.png' )
y = X.SalePrice
X.drop(columns=['SalePrice'], axis=1, inplace=True )<count_unique_values>
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_12_patience_2.png' )
X.isnull().sum() , X.nunique() , X.dtypes<define_variables>
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_30_patience_2.png' )
cat_cols = [cname for cname in X.columns if
X[cname].dtype == "object"]
num_cols = [cname for cname in X.columns if
X[cname].dtype in ['int64', 'float64']]<categorify>
Image('.. /input/deepfake-kernel-data/google_cloud_vm_deepfake_training_screenshot.png' )
num_transf = Pipeline(steps=[ 
('imputer', SimpleImputer()), 
('sca', StandardScaler())
 ])
 cat_transf = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', num_transf, num_cols), 
('cat', cat_transf, cat_cols)
 ])
 model = XGBRegressor(tree_method='gpu_hist', use_label_encoder=False, eval_metric='rmse', random_state=7)
 pipe = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ] )<init_hyperparams>
%matplotlib inline
warnings.filterwarnings("ignore" )
params = {'model__n_estimators':np.arange(100, 800, 10),'model__learning_rate':np.arange(0.001, 0.2, 0.001),
'model__max_depth':np.arange(3, 12, 1)}
kfold = KFold(n_splits=7, shuffle=True, random_state=7)
 scan = RandomizedSearchCV(pipe, params, n_iter=100, scoring='neg_root_mean_squared_error', cv=kfold, random_state=7)
 result = scan.fit(X, y)
 print("Best: %f using %s" %(-1*result.best_score_, result.best_params_)) 
<find_best_params>
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 )
best_parameters = dict(result.best_params_.copy())
 for k in best_parameters.keys() :
best_parameters[k.replace("model__","")] = best_parameters.pop(k)
 best_parameters['reg_alpha'] = best_parameters.pop('model__reg_alpha')
 best_parameters['n_estimators'] = best_parameters.pop('model__n_estimators')
 best_parameters<save_to_csv>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
model_scan = XGBRegressor(**best_parameters, eval_metric='rmse', use_label_encoder=False, tree_method='gpu_hist')
 pipe_scan = Pipeline(steps=[('preprocessing', preprocessor),('model', model_scan)])
 pipe_scan.fit(X, y)
 pred_scan = pipe_scan.predict(X_test)
 output = pd.DataFrame({'Id': X_test.index,
'SalePrice': pred_scan})
 output.to_csv('submission.csv', index=False)
 output<import_modules>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
import pandas as pd
from tqdm import tqdm<load_from_csv>
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 )
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
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 )
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )<feature_engineering>
input_size = 224
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
submission.to_csv('submission.csv', index=False )<save_to_csv>
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 )
submission.to_csv('submission.csv', index=False )<set_options>
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
sns.set()
sns.set_style('darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn<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.5
training_data = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv" )<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 )
testing_data = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<train_model>
speed_test = False
print("Training data shape: ",training_data.shape)
 print("Testing data shape: ",testing_data.shape )<define_variables>
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)) )
test_id = testing_data["Id"]<drop_column>
predictions = predict_on_video_set(test_videos, num_workers=4 )
training_data.drop("Id", axis = 1, inplace = True)
 testing_data.drop("Id", axis = 1, inplace = True )<train_model>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
print("Training data shape: ",training_data.shape)
 print("Testing data shape: ",testing_data.shape )<feature_engineering>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
training_data["SalePrice"] = np.log1p(training_data["SalePrice"] )<prepare_x_and_y>
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 )
n_train = training_data.shape[0]
n_test = testing_data.shape[0]
y_train = training_data.SalePrice.iloc[1:]
all_data = pd.concat(( training_data,testing_data)).reset_index(drop = True)
 all_data.drop(['SalePrice'],axis = 1, inplace = True)
 print('All Data size: {}'.format(all_data.shape))<create_dataframe>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({"Missing Ratio" : all_data_na})
 missing_data.head(30 )<data_type_conversions>
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 )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None" )<data_type_conversions>
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 )
all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None" )<data_type_conversions>
input_size = 150
all_data["Alley"] = all_data["Alley"].fillna("None" )<data_type_conversions>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
all_data["Fence"] = all_data["Fence"].fillna("None" )<data_type_conversions>
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 )
all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )<categorify>
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.5
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
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 )
col =("GarageType" , "GarageFinish" , "GarageQual" ,"GarageCond")
 for c in col:
all_data[c] = all_data[c].fillna("None" )<data_type_conversions>
speed_test = False
col2 =("GarageYrBlt" , "GarageArea" , "GarageCars")
 for c2 in col2:
all_data[c2] = all_data[c2].fillna(0 )<data_type_conversions>
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)) )
col3 =("BsmtFinSF1" , "BsmtFinSF2" , "BsmtUnfSF", "TotalBsmtSF" ,
"BsmtFullBath" , "BsmtHalfBath")
 for c3 in col3:
all_data[c3] = all_data[c3].fillna(0 )<data_type_conversions>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
col4 =("BsmtQual" , "BsmtCond", "BsmtExposure" , "BsmtFinType1" , "BsmtFinType2")
 for c4 in col4:
all_data[c4] = all_data[c4].fillna("None" )<data_type_conversions>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )<data_type_conversions>
submission_df = pd.DataFrame({"filename": test_videos})
 submission_df["label"] = 0.51*submission_df_resnext["label"] + 0.5*submission_df_xception["label"]
<drop_column><EOS> 
submission_df.to_csv("submission.csv", index=False )