Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<define_variables>
import os
import sys
import numpy as np
import pandas as pd
import cv2
from keras import layers, Model
for i, word in enumerate(mydict.items()):
print(word)
 if i > 9:
break<find_best_params>
!pip install.. /input/efnet6weights/efficientnet-1.1.0-py3-none-any.whl
lsi_model.num_topics<split>
class VideoReader:

 def __init__(self, verbose=True):

 self.verbose = verbose
def read_frames_at_indices(self, path, frame_idxs):

 assert len(frame_idxs)> 0
capture = cv2.VideoCapture(path)
 result = self._read_frames_at_indices(path, capture, frame_idxs)
 capture.release()
return result
def _read_frames_at_indices(self, path, capture, frame_idxs):
try:
frames = []
idxs_read = []
for frame_idx in range(frame_idxs[0], frame_idxs[-1] + 1):
ret = capture.grab()
if not ret:
if self.verbose:
print("Error grabbing frame %d from movie %s" %(frame_idx, path))
break
current = len(idxs_read)
 if frame_idx == frame_idxs[current]:
ret, frame = capture.retrieve()
if not ret or frame is None:
if self.verbose:
print("Error retrieving frame %d from movie %s" %(frame_idx, path))
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 frames.append(frame)
 idxs_read.append(frame_idx)
 if len(frames)> 0:
return np.stack(frames), idxs_read
if self.verbose:
print("No frames read from movie %s" % path)
 return None
except:
if self.verbose:
print("Exception while reading movie %s" % path)
 return None
video_reader = VideoReader()
def transform(df):
corpus = [mydict.doc2bow(text)for text in df]
corpus = tf_model[corpus]
corpus = lsi_model[corpus]
return corpus
print(len(df["tokens"]))
X = transform(df["tokens"])
 print(len(X))
X_test = transform(df_test["tokens"])
 X_train, X_val, y_train, y_val = train_test_split(X, df["target"], test_size=0.1, random_state=42 )<feature_engineering>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)] )
def make_vec(X, num_top):
matrix = np.zeros(( len(X), num_top))
for i, row in enumerate(X):
matrix[i, list(map(lambda tup: tup[0], row)) ] = list(map(lambda tup: tup[1], row))
return matrix
make_vec(X_train, lsi_model.num_topics ).shape<split>
weights = ["efnet_6_2020-03-26_14-08-45_epoch_06",
"efnet_6_2020-03-27_21-28-54_epoch_06",
"efnet_6_2020-03-29_21-28-21_epoch_06",
"efnet_6_2020-03-29_21-29-50_epoch_06",
"efnet_6_2020-03-30_00-26-00_epoch_06"
]
def transform(df, tf_model, model):
corpus = [mydict.doc2bow(text)for text in df]
corpus = tf_model[corpus]
corpus = model[corpus]
corpus = make_vec(corpus, model.num_topics)
 return corpus
print(len(df["tokens"]))
X = transform(df["tokens"], tf_model, lsi_model)
 print(len(X))
X_test = transform(df_test["tokens"], tf_model, lsi_model)
 X_train, X_val, y_train, y_val = train_test_split(X, df["target"], test_size=0.1, random_state=42 )<save_to_csv>
def efnet(backbone='6', input_shape=(224,224,3), num_classes=1):
DENSE_KERNEL_INITIALIZER = {
'class_name': 'VarianceScaling',
'config': {
'scale': 1./ 3.,
'mode': 'fan_out',
'distribution': 'uniform'
}
}
if backbone == '0':
EFNet = efn.EfficientNetB0
elif backbone == '1':
EFNet = efn.EfficientNetB1
elif backbone == '2':
EFNet = efn.EfficientNetB2
elif backbone == '3':
EFNet = efn.EfficientNetB3
elif backbone == '4':
EFNet = efn.EfficientNetB4
elif backbone == '5':
EFNet = efn.EfficientNetB5
elif backbone == '6':
EFNet = efn.EfficientNetB6
elif backbone == '7':
EFNet = efn.EfficientNetB7
base_model = EFNet(input_shape=input_shape, weights=None, include_top=False, classes=num_classes)
 x = layers.GlobalAveragePooling2D()(base_model.output)
 x = layers.Dropout(0.2 )(x)
 output = layers.Dense(num_classes, activation='sigmoid', kernel_initializer=DENSE_KERNEL_INITIALIZER )(x)
 return Model(inputs=[base_model.input], outputs=[output] )
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_lsi.csv",index=False )<save_to_csv>
models = []
for i in range(len(weights)) :
models.append(efnet() )
model = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).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 = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).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_lsi_rf.csv",index=False )<choose_model_class>
for model, weight in zip(models, weights):
model.load_weights('/kaggle/input/efnet6weights/' + weight + '.hdf5' )
lda_model = LdaModel(corpus_tf, id2word=mydict, num_topics=100, dtype=np.float64 )<count_unique_values>
faces_batch_size = 55
frames_per_video = 55
face_size = 224
clip = 1e-15
ws = [1/(len(models)) ]*len(models )
<split><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": out_probs})
 submission_df.to_csv("submission.csv", index=False )