Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<import_modules>
%matplotlib inline
import gc
import random as r
import joblib<set_options>
frames_per_vid = [17, 25, 30, 32, 35, 36, 40]
public_LB = [0.46788, 0.46776, 0.46611, 0.46542, 0.46643, 0.46484, 0.46635]
df_viz = pd.DataFrame({'frames_per_vid': frames_per_vid, 'public_LB':public_LB} )
pd.options.display.max_rows = None
pd.options.display.max_columns = None<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"])
 frame_h = 5
frame_l = 5
len(test_videos )
main_path = ".. /input/fraud-detection-categorified-and-split/"<init_hyperparams>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
buildLv = False
buildSkf = True
AV=True<prepare_x_and_y>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
%%time
dset_str="all"
to = load_pickle(main_path+"to_"+dset_str+"c.pkl")
 to_tst = load_pickle(main_path+"to_tst_"+dset_str+"c.pkl")
 xs,y = to.train.xs,to.train.y
valid_xs,valid_y = to.valid.xs,to.valid.y
test_xs = to_tst.xs.copy()
xs.shape, test_xs.shape<drop_column>
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 )
del to, to_tst; x=gc.collect()<compute_train_metric>
frames_per_video = 38
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 )
def m_rep(m,xs,y): return classification_report(y, m.predict(xs), labels=[1,0], digits=4, output_dict=True)
 def metrics(m, xs, y , valid_xs, valid_y):
tr_rep = m_rep(m,xs,y)
 vd_rep = m_rep(m,valid_xs,valid_y)
 tr_auc = roc_auc_score(y,m.predict_proba(xs)[:,1])
 oob_auc=0.0000
vd_auc = roc_auc_score(valid_y,m.predict_proba(valid_xs)[:,1])
 print('{:.4f} ; {:.4f} ; {:.4f}' 
.format(tr_auc, oob_auc, vd_auc))<save_to_csv>
input_size = 224
def format_test_preds(preds,test_xs,comment):
print(pd.DataFrame(preds ).describe())
 df_pred = pd.DataFrame({"TransactionID":test_xs.TransactionID.to_list() ,
"isFraud": preds})
 df_pred.to_csv(comment+"my_subs.csv",index=False)
 print(df_pred.shape)
 del df_pred; x=gc.collect()<choose_model_class>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
def xgb_lvd_fun(get_feat_imp):
clf = xgb.XGBClassifier(n_estimators=2000,
max_depth=8,
learning_rate= 0.05,
subsample= 0.6,
colsample_bytree= 0.4,
random_state = r.randint(0,9999),
use_label_encoder=False,
tree_method='gpu_hist')
 m = clf.fit(xs[cols].iloc[idxT], y.iloc[idxT], eval_set=[(xs[cols].iloc[idxV],y.iloc[idxV])],
eval_metric= "auc", verbose=100, early_stopping_rounds=100)
 tr_auc = roc_auc_score(y.iloc[idxT], m.predict_proba(xs[cols].iloc[idxT])[:,1])
 vd_auc = roc_auc_score(y.iloc[idxV],m.predict_proba(xs[cols].iloc[idxV])[:,1])
 te_pred = m.predict_proba(test_xs[cols])[:,1]
if get_feat_imp:
fi = xgb_fi(m, xs[cols])
 fi.to_csv("fi_lv.csv",index=False)
 print(fi)
 del m, clf; x=gc.collect()
return [tr_auc, vd_auc], te_pred 
<compute_train_metric>
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 )
def xgb_pred_fun() :
skf = KFold(n_splits = 5, shuffle = False)
 tr_pred = np.zeros(len(xs))
oof_pred = np.zeros(len(xs))
te_pred = np.zeros(len(test_xs))
for i,(idxT, idxV)in enumerate(skf.split(xs,y)) :
print('Fold',i)
 print(' n_rows of train =',len(idxT),'rows of holdout =',min(idxV),"to",max(idxV))
clf = xgb.XGBClassifier(n_estimators=1000,
max_depth=12,
learning_rate= 0.02,
subsample= 0.8,
colsample_bytree= 0.4,
random_state = r.randint(0,9999),
use_label_encoder=False,
tree_method='gpu_hist')
 m = clf.fit(xs[cols].iloc[idxT], y.iloc[idxT], eval_set=[(xs[cols].iloc[idxV],y.iloc[idxV])],
eval_metric= "auc", verbose=100, early_stopping_rounds=100)
 tr_pred[idxT] += m.predict_proba(xs[cols].iloc[idxT])[:,1]/(skf.n_splits-1)
 oof_pred[idxV] += m.predict_proba(xs[cols].iloc[idxV])[:,1]
te_pred += m.predict_proba(test_xs[cols])[:,1]/skf.n_splits
if i==4:
fi = xgb_fi(m, xs[cols])
 fi.to_csv("fi_lv.csv",index=False)
 print(fi)
 del m; x=gc.collect()
print('{:.4f} ; {:.4f} ; {:.4f}' 
.format(roc_auc_score(y,tr_pred),roc_auc_score(y,oof_pred),0.000))
return te_pred<define_variables>
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
v = []
v += ['V1', 'V3', 'V11', 'V9', 'V5', 'V7']
v += ['V13', 'V17', 'V24', 'V14', 'V20', 'V27', 'V34', 'V26', 'V30']
v += ['V36', 'V37', 'V47', 'V40', 'V48', 'V52', 'V41', 'V45']
v += ['V54', 'V65', 'V60', 'V67', 'V56', 'V68', 'V62', 'V55', 'V70']
v += ['V76', 'V89', 'V91', 'V81', 'V82', 'V87', 'V78', 'V88']
v += ['V127', 'V121', 'V99', 'V110', 'V104', 'V130', 'V129', 'V131', 'V109', 'V136', 'V116', 'V120', 'V125', 'V113', 'V118', 'V98', 'V107', 'V117', 'V115']
v += ['V138', 'V140', 'V142', 'V147', 'V155', 'V162']
v += ['V165', 'V160', 'V166']
v += ['V203', 'V207', 'V216', 'V187', 'V176', 'V173', 'V183', 'V215']
v += ['V169', 'V195', 'V201', 'V171', 'V174', 'V175', 'V209', 'V185', 'V188', 'V210', 'V198', 'V180']
v += ['V274', 'V264', 'V265', 'V261', 'V235', 'V223', 'V258', 'V260', 'V246', 'V252', 'V241', 'V266', 'V240', 'V277', 'V228', 'V226']
v += ['V220', 'V239', 'V271', 'V221', 'V234', 'V251']
v += ['V307', 'V291', 'V285', 'V290', 'V312', 'V297', 'V305', 'V320', 'V303', 'V286', 'V309', 'V284', 'V310']
v += ['V281', 'V301', 'V282', 'V315', 'V289', 'V296', 'V314', 'V283']
v += ['V332', 'V338', 'V337', 'V336', 'V325', 'V326', 'V328', 'V335']
len(v)
 def set_approach(a,b):
return list(set(a)-set(b))
tf_V = [bool(re.search("^V"+"[0-9]+",col)) for col in xs.columns.to_list() ]
cols = xs.columns[list(~np.array(tf_V)) ].to_list()<define_variables>
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
cols +=v
print(len(cols))<define_variables>
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 )
cols_rem = ['TransactionID', 'TransactionDT']
cols_rem +=["DayNum","HrOfDay","WkDayNum",'R_emaildomain1', 'R_emaildomain2', 'P_emaildomain1', 'P_emaildomain2','id_31_browser',"DeviceInfo_make"]
cols = set_approach(cols,cols_rem )<data_type_conversions>
speed_test = False
def ohe(df,cols):
for col in cols:
df[col+"_OHE"] = df[col].isna().astype(int )<compute_test_metric>
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)) )
cols_ohe = ["D2","D9"]
ohe(xs,cols_ohe)
 ohe(test_xs,cols_ohe)
 cols += ["D2_OHE","D9_OHE"]<concatenate>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<define_variables><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )