Kaggle competition: Deepfake Detection Challenge
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<choose_model_class>
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<split>
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
import time
if __name__ == '__main__':
seed_everything(CFG['seed'])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_inference_transforms() , output_label=False)
 test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/pytorch-efficientnet-baseline-train-amp-aug/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
val_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, val_loader, device)]
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
val_preds = np.mean(val_preds, axis=0)
 tst_preds = np.mean(tst_preds, axis=0)
 print('fold {} validation loss = {:.5f}'.format(fold, log_loss(valid_.label.values, val_preds)))
 print('fold {} validation accuracy = {:.5f}'.format(fold,(valid_.label.values==np.argmax(val_preds, axis=1)).mean()))
del model
torch.cuda.empty_cache()<feature_engineering>
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='' )
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<save_to_csv>
cm = detection_graph.as_default()
cm.__enter__()
test.to_csv('submission.csv', index=False )<set_options>
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' )
pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 100)
<load_pretrained>
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, start_frame):
frame_count=10
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(start_frame,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)
CACHE_PATH = '.. /input/mlp012003weights'
def save_pickle(dic, save_path):
with open(save_path, 'wb')as f:
pickle.dump(dic, f)
 def load_pickle(load_path):
with open(load_path, 'rb')as f:
message_dict = pickle.load(f)
 return message_dict
f_mean = np.load(f'{CACHE_PATH}/f_mean_online.npy' )<define_variables>
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,0)
 if a is None:
continue
cv2.imwrite('./videos/'+filename,a)
 except Exception as err:
print(err )
feat_cols = [f'feature_{i}' for i in range(130)]
all_feat_cols = [col for col in feat_cols]
all_feat_cols.extend(['cross_41_42_43', 'cross_1_2'])
 target_cols = ['action', 'action_1', 'action_2', 'action_3', 'action_4']<import_modules>
os.mkdir('./videos_2/')
 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,95)
 if a is None:
continue
cv2.imwrite('./videos_2/'+filename,a)
 except Exception as err:
print(err )
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torch.nn import CrossEntropyLoss, MSELoss
from torch.nn.modules.loss import _WeightedLoss
import torch.nn.functional as F<choose_model_class>
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 )
class Model(nn.Module):
def __init__(self):
super(Model, self ).__init__()
self.batch_norm0 = nn.BatchNorm1d(len(all_feat_cols))
self.dropout0 = nn.Dropout(0.9)
 dropout_rate = 0.9
hidden_size = 256
self.dense1 = nn.Linear(len(all_feat_cols), hidden_size)
 self.batch_norm1 = nn.BatchNorm1d(hidden_size)
 self.dropout1 = nn.Dropout(dropout_rate)
 self.dense2 = nn.Linear(hidden_size+len(all_feat_cols), hidden_size)
 self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(dropout_rate)
 self.dense3 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(dropout_rate)
 self.dense4 = nn.Linear(hidden_size+hidden_size, hidden_size)
 self.batch_norm4 = nn.BatchNorm1d(hidden_size)
 self.dropout4 = nn.Dropout(dropout_rate)
 self.dense5 = nn.Linear(hidden_size+hidden_size, len(target_cols))
self.Relu = nn.ReLU(inplace=True)
 self.PReLU = nn.PReLU()
self.LeakyReLU = nn.LeakyReLU(negative_slope=0.01, inplace=True)
 self.RReLU = nn.RReLU()
def forward(self, x):
x = self.batch_norm0(x)
 x = self.dropout0(x)
 x1 = self.dense1(x)
 x1 = self.batch_norm1(x1)
 x1 = self.LeakyReLU(x1)
 x1 = self.dropout1(x1)
 x = torch.cat([x, x1], 1)
 x2 = self.dense2(x)
 x2 = self.batch_norm2(x2)
 x2 = self.LeakyReLU(x2)
 x2 = self.dropout2(x2)
 x = torch.cat([x1, x2], 1)
 x3 = self.dense3(x)
 x3 = self.batch_norm3(x3)
 x3 = self.LeakyReLU(x3)
 x3 = self.dropout3(x3)
 x = torch.cat([x2, x3], 1)
 x4 = self.dense4(x)
 x4 = self.batch_norm4(x4)
 x4 = self.LeakyReLU(x4)
 x4 = self.dropout4(x4)
 x = torch.cat([x3, x4], 1)
 x = self.dense5(x)
 return x<set_options>
model=Model(inp,x)
 weights = ['.. /input/deepfake-20/saved-model-01-0.06.hdf5', '.. /input/deepfake-20/saved-model-02-0.05.hdf5', '.. /input/model-epoch-3/saved-model-03-0.06.hdf5','.. /input/model-02/saved-model-01-0.06.hdf5']*2
sub_file = ['submission_'+str(i)+'.csv' for i in range(1,9)]
video = ['./videos/']*4+['./videos_2/']*4
for xxxxx in range(8):
start = time.time()
model.load_weights(weights[xxxxx])
 def get_birghtness(img):
return img/img.max()
def process_img(img,flip=[False]*10):
imgs=[]
for x in range(10):
if flip[x]:
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)
 sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 test_files=glob.glob(video[xxxxx]+'*.jpg')
 submission=pd.DataFrame()
submission['filename']=os.listdir(( '.. /input/deepfake-detection-challenge/test_videos/'))
submission['label']=0.5
filenames=[]
batch=[]
batch1=[]
batch2=[]
batch3=[]
preds=[]
for x in test_files:
img=process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB))
if img is None:
continue
batch.append(img)
 batch1.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[True]*10))
batch2.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[True,False]*5))
batch3.append(process_img(cv2.cvtColor(cv2.imread(x),cv2.COLOR_BGR2RGB),[False,True]*5))
filenames.append(x.replace(video[xxxxx],'' ).replace('.jpg','.mp4'))
if len(batch)==16:
preds+=(((0.25*model.predict(np.array(batch)))) +(( 0.25*model.predict(np.array(batch1)))) +(( 0.25*model.predict(np.array(batch2)))) +(( 0.25*model.predict(np.array(batch3)))) ).tolist()
batch=[]
batch1=[]
batch2=[]
batch3=[]
if len(batch)!=0:
preds+=(((0.25*model.predict(np.array(batch)))) +(( 0.25*model.predict(np.array(batch1)))) +(( 0.25*model.predict(np.array(batch2)))) +(( 0.25*model.predict(np.array(batch3)))) ).tolist()
print(time.time() -start)
 new_preds=[]
for x in preds:
new_preds.append(x[0])
 print(sum(new_preds)/len(new_preds))
for x,y in zip(new_preds,filenames):
submission.loc[submission['filename']==y,'label']=min([max([0.05,x]),0.95])
 submission.to_csv(sub_file[xxxxx], index=False )
if torch.cuda.is_available() :
print('using device: cuda')
 device = torch.device("cuda:0")
 else:
print('using device: cpu')
 device = torch.device('cpu' )<load_pretrained>
!rm -r videos
!rm -r videos_2
NFOLDS = 5
model_list = []
tmp = np.zeros(len(feat_cols))
for _fold in range(NFOLDS):
torch.cuda.empty_cache()
model = Model()
model.to(device)
 model_weights = f"{CACHE_PATH}/online_model{_fold}.pth"
model.load_state_dict(torch.load(model_weights, map_location=device))
model.eval()
model_list.append(model )<import_modules>
df1 = pd.read_csv('submission_1.csv' ).set_index('filename' ).transpose().to_dict()
df2 = pd.read_csv('submission_2.csv' ).set_index('filename' ).transpose().to_dict()
df3 = pd.read_csv('submission_3.csv' ).set_index('filename' ).transpose().to_dict()
df4 = pd.read_csv('submission_4.csv' ).set_index('filename' ).transpose().to_dict()
df5 = pd.read_csv('submission_5.csv' ).set_index('filename' ).transpose().to_dict()
df6 = pd.read_csv('submission_6.csv' ).set_index('filename' ).transpose().to_dict()
df7 = pd.read_csv('submission_7.csv' ).set_index('filename' ).transpose().to_dict()
df8 = pd.read_csv('submission_8.csv' ).set_index('filename' ).transpose().to_dict()
filename = []
label = []
for i in df1.keys() :
filename.append(i)
 a = []
if df1[i]['label']!=0.5:
a.append(df1[i]['label'])
 if df2[i]['label']!=0.5:
a.append(df2[i]['label'])
 if df3[i]['label']!=0.5:
a.append(df3[i]['label'])
 if df4[i]['label']!=0.5:
a.append(df4[i]['label'])
 if df5[i]['label']!=0.5:
a.append(df5[i]['label'])
 if df6[i]['label']!=0.5:
a.append(df6[i]['label'])
 if df7[i]['label']!=0.5:
a.append(df7[i]['label'])
 if df8[i]['label']!=0.5:
a.append(df8[i]['label'])
 if len(a)==0:
label.append(0.5)
 else:
label.append(min([max([0.05,sum(a)/len(a)]),0.95]))
df = pd.DataFrame()
df['filename'] = filename
df['label'] = label
print(np.array(df['label'] ).mean())
 df.to_csv('submission.csv', index=False )
<choose_model_class><EOS> 
!rm submission_1.csv
!rm submission_2.csv
!rm submission_3.csv
!rm submission_4.csv
!rm submission_5.csv
!rm submission_6.csv
!rm submission_7.csv
!rm submission_8.csv