Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<load_pretrained>
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl
w2v_model = gensim.downloader.load("word2vec-google-news-300")
 type(w2v_model )<create_dataframe>
import pandas as pd
import tensorflow as tf
import cv2
import glob
from tqdm.notebook import tqdm
import numpy as np
import os
import efficientnet.keras as efn
from keras.layers import *
from keras import Model
import matplotlib.pyplot as plt
X_all = pd.concat([df["tokens"], df_test["tokens"]] ).reset_index(drop=True)
 documents = [TaggedDocument(doc, [i])for i, doc in enumerate(X_all)]
del X_all
model_d2v = Doc2Vec(documents, vector_size=500, window=2, min_count=1, workers=-1 )<features_selection>
detection_graph = tf.Graph()
with detection_graph.as_default() :
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile('.. /input/mobilenet-face/frozen_inference_graph_face.pb', 'rb')as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
 tf.import_graph_def(od_graph_def, name='' )
print(len(df["tokens"]))
X = [model_d2v.infer_vector(texts)for texts in df["tokens"]]
X = np.array(X)
 print(len(X))
X_test = [model_d2v.infer_vector(texts)for texts in df_test["tokens"]]
X_test = np.array(X_test )<save_to_csv>
cm = detection_graph.as_default()
cm.__enter__()
X_train, X_val, y_train, y_val = train_test_split(X, df["target"], test_size=0.1, random_state=42)
 print(X_train.shape)
 model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print(accuracy_score(y_val, y_val_pred), f1_score(y_val, y_val_pred))
print(confusion_matrix(y_val, y_val_pred))
model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X, df.target)
 y_test_pred = model.predict(X_test)
 sub_df = pd.read_csv(os.path.join('.. /input/nlp-getting-started/', 'sample_submission.csv'))
sub_df["target"] = y_test_pred
sub_df.to_csv("submission_lda_lsi.csv",index=False )<install_modules>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess=tf.compat.v1.Session(graph=detection_graph, config=config)
 image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
 boxes_tensor = detection_graph.get_tensor_by_name('detection_boxes:0')
 scores_tensor = detection_graph.get_tensor_by_name('detection_scores:0')
 num_detections = detection_graph.get_tensor_by_name('num_detections:0' )
!pip install git+git://github.com/AndLen/simpletransformers.git --quiet<import_modules>
def get_img(images):
global boxes,scores,num_detections
im_heights,im_widths=[],[]
imgs=[]
for image in images: 
(im_height,im_width)=image.shape[:-1]
imgs.append(image)
 im_heights.append(im_height)
 im_widths.append(im_widths)
 imgs=np.array(imgs)
(boxes, scores_)= sess.run(
 [boxes_tensor, scores_tensor],
feed_dict={image_tensor: imgs})
 finals=[]
for x in range(boxes.shape[0]):
scores=scores_[x]
max_=np.where(scores==scores.max())[0][0]
box=boxes[x][max_]
ymin, xmin, ymax, xmax = box 
(left, right, top, bottom)=(xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
 left, right, top, bottom = int(left), int(right), int(top), int(bottom)
 image=imgs[x]
finals.append(cv2.cvtColor(cv2.resize(image[max([0,top-40]):bottom+80,max([0,left-40]):right+80],(240,240)) ,cv2.COLOR_BGR2RGB))
return finals
def detect_video(video):
frame_count=10
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(0,v_len,frame_count, endpoint=False, dtype=np.int)
 imgs=[]
i=0
for frame_idx in range(int(v_len)) :
ret = capture.grab()
if not ret:
print("Error grabbing frame %d from movie %s" %(frame_idx, video))
if frame_idx >= frame_idxs[i]:
if frame_idx-frame_idxs[i]>20:
return None
ret, frame = capture.retrieve()
if not ret or frame is None:
print("Error retrieving frame %d from movie %s" %(frame_idx, video))
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 imgs.append(frame)
 i += 1
if i >= len(frame_idxs):
break
imgs=get_img(imgs)
 if len(imgs)<10:
return None
return np.hstack(imgs)
import csv
import os
import torch
from transformers import pipeline
import gc
import seaborn as sns
from matplotlib import pyplot as plt
from scipy.special import softmax
from simpletransformers.classification import(ClassificationModel, ClassificationArgs)
 import sklearn
from sklearn.model_selection import train_test_split<load_from_csv>
os.mkdir('./videos/')
 for x in tqdm(glob.glob('.. /input/deepfake-detection-challenge/test_videos/*.mp4')) :
try:
filename=x.replace('.. /input/deepfake-detection-challenge/test_videos/','' ).replace('.mp4','.jpg')
 a=detect_video(x)
 if a is None:
continue
cv2.imwrite('./videos/'+filename,a)
 except Exception as err:
print(err )
test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 training = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv" )<count_missing_values>
bottleneck = efn.EfficientNetB1(weights=None,include_top=False,pooling='avg')
 inp=Input(( 10,240,240,3))
x=TimeDistributed(bottleneck )(inp)
 x = LSTM(128 )(x)
 x = Dense(64, activation='elu' )(x)
 x = Dense(1,activation='sigmoid' )(x )
training["text"].isna().sum()<rename_columns>
model=Model(inp,x)
 model.load_weights('.. /input/dfdc-model3/model.h5')
 def get_birghtness(img):
return img/img.max()
def process_img(img,flip=False):
imgs=[]
for x in range(10):
if flip:
imgs.append(get_birghtness(cv2.flip(img[:,x*240:(x+1)*240,:],1)))
 else:
imgs.append(get_birghtness(img[:,x*240:(x+1)*240,:]))
return np.array(imgs )
training_df = training[["text", "target"]]
training_df.columns = ["text", "labels"]<split>
sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 test_files=glob.glob('./videos/*.jpg')
 submission=pd.DataFrame()
submission['filename']=os.listdir(( '.. /input/deepfake-detection-challenge/test_videos/'))
submission['label']=0.5
filenames=[]
batch=[]
batch1=[]
preds=[]
<set_options>
new_preds=[]
for x in preds:
new_preds.append(x[0])
 print(sum(new_preds)/len(new_preds))
gc.collect()
torch.cuda.empty_cache()<choose_model_class>
for x,y in zip(new_preds,filenames):
submission.loc[submission['filename']==y,'label']=min([max([0.1,x]),0.9] )
model_args = ClassificationArgs(num_train_epochs=2,
overwrite_output_dir=True)
 model_args.manual_seed = 42
model_args.best_model_dir = "/kaggle/working/best_model"
model_args.output_dir = "/kaggle/temp/output"
model_args.normalization = True
model_args.reprocess_input_data = True
model_args.train_batch_size = 80
model_args.eval_batch_size = 80
model_args.early_stopping_metric = "mcc"
model_args.early_stopping_metric_minimize = False
model_args.use_early_stopping = True
model_args.early_stopping_consider_epochs = True
model_args.early_stopping_patience = 1
model = ClassificationModel(model_type='bertweet',
model_name='vinai/bertweet-base',
args = model_args,
num_labels = 2 )<train_model>
submission.to_csv('submission.csv', index=False)
 !rm -r videos