Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<load_from_url>
%%capture
!pip install --no-deps.. /input/maruti/maruti-1.3.1-py3-none-any.whl
!pip install /kaggle/input/facenetmtcnn/facenet_pytorch-2.1.1-py3-none-any.whl
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
!pip install sentencepiece<import_modules>
import os
import torch
from facenet_pytorch import MTCNN
import pandas as pd
import cv2
from tqdm import tqdm
from PIL import Image
import numpy as np
import math
from torchvision import transforms
import torchvision.models as models
from maruti.imports.ml import *
from torch.nn.utils.rnn import pack_sequence
import keras
import tensorflow
import tensorflow_hub as hub
import tokenization
from sklearn.model_selection import train_test_split
import tensorflow as tf<prepare_x_and_y>
test_dir = '.. /input/deepfake-detection-challenge/test_videos/'
test_videos = sorted(os.listdir(test_dir))
X = np.array(train['text'])
 y = np.array(train['target'] )<categorify>
with tensorflow.device('/gpu:0'):
model = tensorflow.keras.models.load_model('/kaggle/input/pytorch-keras-models/Resnext101.h5' )
def bert_encode(texts,tokenizer, max_len = 512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 pad_len = max_len - len(input_sequence)
 tokens += [0]*pad_len
pad_mask = [1]*len(input_sequence)+[0]*pad_len
segment_id = [0]*max_len
all_tokens.append(tokens)
 all_masks.append(pad_mask)
 all_segments.append(segment_id)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
device = torch.device("cuda" )
def build_model(bert_layer,max_len=512):
input_word_ids = tf.keras.layers.Input(shape=(max_len,),dtype=tf.int32,name="input_word_ids")
 input_mask = tf.keras.layers.Input(shape=(max_len,),dtype=tf.int32,name="input_mask")
 input_segment_ids = tf.keras.layers.Input(shape=(max_len,),dtype=tf.int32,name="input_segment_ids")
 _,sequence_output = bert_layer([input_word_ids,input_mask,input_segment_ids])
 clf_output = sequence_output[:,0,:]
model_X = tf.keras.layers.Dense(100,activation='relu' )(clf_output)
 model_X = tf.keras.layers.BatchNormalization()(model_X)
 model_X = tf.keras.layers.Dropout(0.5 )(model_X)
 model_X = tf.keras.layers.Dense(100,activation='relu' )(model_X)
 model_X = tf.keras.layers.BatchNormalization()(model_X)
 model_X = tf.keras.layers.Dropout(0.5 )(model_X)
 model_X = tf.keras.layers.Dense(100,activation='relu' )(model_X)
 model_X = tf.keras.layers.BatchNormalization()(model_X)
 model_X = tf.keras.layers.Dropout(0.5 )(model_X)
 out = tf.keras.layers.Dense(1,activation='sigmoid' )(model_X)
 model = tf.keras.models.Model(inputs=[input_word_ids,input_mask,input_segment_ids],outputs=out)
 return model<choose_model_class>
preprocess = transforms.Compose([
transforms.ToPILImage() ,
transforms.Resize([256,256]),
transforms.CenterCrop([224,224]),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])] )
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<feature_engineering>
mtcnn = MTCNN(select_largest=False,device=device)
 group_transform = mdata.group_transform['val']
group_transform = lambda x: torch.stack(tuple(map(preprocess, x)))
 def get_brightness_score(img):
return maruti.vision.image.brightness_score(img)
 def adjust_brightness(img):
if(get_brightness_score(img)< 1.5):
return maruti.vision.image.adjust_brightness(img,1.5)
 def get_batch(path):
frame_count, _ = mvis.vid_info(path)
 frame_idx = np.linspace(0, frame_count-1, 8, dtype=int)
 frame_list = list(mvis.get_face_frames(path, frame_idx))
for i in frame_list:
i = np.array(adjust_brightness(i))
return frame_list
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<split>
speed_test = False
if speed_test:
start = time.perf_counter()
for vid in tqdm(test_videos[:10]):
print(predict(test_dir+vid))
print(( time.perf_counter() -start)/10 )
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)
 train_input = bert_encode(X_train,tokenizer,max_len=264)
 val_input = bert_encode(X_test,tokenizer,max_len=264)
 test_input = bert_encode(test.text.values,tokenizer,max_len=264)
 train_labels = y_train
val_labels = y_test<train_model>
start = time.perf_counter()
predictions = []
for i, vid in tqdm(enumerate(test_videos)) :
if i%20==19:
os.system(f'echo {str(i)} {predictions[-1]:.2f}')
 try:
pred = []
x = np.array(get_batch(test_dir+vid))
for i in x:
i = np.reshape(i,(1,224,224,3))
predictions.append(np.mean(np.array(model.predict(x))))
except Exception as e:
print(vid+' error:'+str(e))
predictions.append(0.5)
 print(( time.perf_counter() -start)/len(test_videos))
<save_to_csv><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df['label'] = np.clip(submission_df['label'],0.05,0.95)
 submission_df.to_csv("submission.csv", index=False )