Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<define_variables>
import os, sys, time
import cv2
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
train_folds, valid_folds, test_folds, files_train, files_valid, files_test = prepare_k_datasets()
print('TRAIN BATCH')
 plot_batch(train_folds[0] )<choose_model_class>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"] )
EFNS = [efn.EfficientNetB0, efn.EfficientNetB1, efn.EfficientNetB2, efn.EfficientNetB3,
efn.EfficientNetB4, efn.EfficientNetB5, efn.EfficientNetB6, efn.EfficientNetB7]
def get_lr_callback_CA(batch_size=32, epochs=12, cycles=2, lr_max=1e-4, fold_number=0):
def lrfn(epoch):
epochs_per_cycle = floor(epochs/cycles)
 cos_inner =(pi *(epoch % epochs_per_cycle)) /(epochs_per_cycle)
 lr = lr_max/2 *(cos(cos_inner)+ 1)
 return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 rng = [i for i in range(epochs if EPOCHS[fold_number]<epochs else EPOCHS[fold_number])]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 plt.title("Learning rate schedule: {:.3g} to {:.3g}".format(y[0], y[-1]))
plt.xlabel('Epoch')
 plt.ylabel('Learning rate')
 plt.tight_layout()
plt.show()
return lr_callback
def get_lr_callback(batch_size=8, epochs=12, fold_number=0):
lr_start = LR_MIN
lr_max = 0.00000125 * REPLICAS * batch_size
lr_min = LR_MIN
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 rng = [i for i in range(epochs if EPOCHS[fold_number]<epochs else EPOCHS[fold_number])]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 plt.title("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
plt.xlabel('Epoch')
 plt.ylabel('Learning rate')
 plt.tight_layout()
plt.show()
return lr_callback
def build_model(img_dims, ef):
inp = tf.keras.layers.Input(shape=(img_dims, img_dims, 3))
base = EFNS[ef](input_shape=(img_dims, img_dims, 3), weights='imagenet', include_top=False)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = Dropout(0.3 )(x)
 x = tf.keras.layers.Dense(1, activation='sigmoid' )(x)
 model = tf.keras.Model(inputs=inp, outputs=x)
 model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05),
metrics=['AUC', 'binary_accuracy'])
 return model
def train_model(epochs_number, train_gen, valid_gen, steps_per_epoch, fold_number):
early_stopping = tf.keras.callbacks.EarlyStopping(patience=6, restore_best_weights=True)
 checkpoint = tf.keras.callbacks.ModelCheckpoint('./FOLD' + str(fold_number+1)+ '_model.h5', monitor='val_loss', verbose=False,
save_best_only=True, save_weights_only=True, mode='min', save_freq='epoch')
 history = model.fit(train_gen,
epochs=epochs_number,
steps_per_epoch=steps_per_epoch,
validation_data=valid_gen,
callbacks=[get_lr_callback(BATCH_SIZES[fold_number], epochs=EPOCHS[fold_number], fold_number=fold_number), checkpoint, early_stopping],
verbose=VERBOSE)
 return history<define_variables>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
oof_pred = []; oof_tar = []; oof_val = []; oof_names = []; oof_folds = []
preds = np.zeros(( count_data_items(files_test[0]), 1))
for k in range(0, FOLDS):
if DEVICE=='TPU':
if tpu: tf.tpu.experimental.initialize_tpu_system(tpu)
 print('FOLD', k+1)
 print('Image Size %i with EfficientNet B%i and batch_size %i, INC2019=%i, INC(2018,2017)=%i'
%(IMG_SIZES[k],EFF_NETS[k],BATCH_SIZES[k]*REPLICAS, INC2019[k], INC2018_2017[k]))
print('Train/valid/test images count: ', count_data_items(files_train[k]), '/', count_data_items(files_valid[k]), '/', count_data_items(files_test[k]))
print('Building...')
 K.clear_session()
with strategy.scope() :
model = build_model(IMG_SIZES[k], EFF_NETS[k])
 print('Training...')
 history = train_model(epochs_number=EPOCHS[k],
train_gen=train_folds[k],
valid_gen=valid_folds[k],
steps_per_epoch=count_data_items(files_train[k])/BATCH_SIZES[k]//REPLICAS,
fold_number=k)
 print('Best weights loading...')
 model.load_weights('./FOLD' + str(k+1)+ '_model.h5')
 print('Predicting OOF with TTA...')
 ds_valid = get_dataset(files_valid[k], labeled=False, return_image_names=False, augment=True, repeat=True, shuffle=False, dim=IMG_SIZES[k], batch_size=BATCH_SIZES[k] * 4)
 ct_valid = count_data_items(files_valid[k])
 STEPS = TTA * ct_valid / BATCH_SIZES[k] / 4 / REPLICAS
pred = model.predict(ds_valid, steps=STEPS, verbose=VERBOSE)[:TTA * ct_valid,]
oof_pred.append(np.mean(pred.reshape(( ct_valid, TTA), order='F'), axis=1))
ds_valid = get_dataset(files_valid[k], augment=False, repeat=False, dim=IMG_SIZES[k], labeled=True, return_image_names=True)
 oof_tar.append(np.array([target.numpy() for img, target in iter(ds_valid.unbatch())]))
oof_folds.append(np.ones_like(oof_tar[-1], dtype='int8')* k)
 ds = get_dataset(files_valid[k], augment=False, repeat=False, dim=IMG_SIZES[k], labeled=False, return_image_names=True)
 oof_names.append(np.array([img_name.numpy().decode("utf8")for img, img_name in iter(ds.unbatch())]))
print('Predicting Test with TTA...')
 ds_test = get_dataset(files_test[k], labeled=False, return_image_names=False, augment=True, repeat=True, shuffle=False, dim=IMG_SIZES[k], batch_size=BATCH_SIZES[k] * 4)
 ct_test = count_data_items(files_test[k])
 STEPS = TTA * ct_test / BATCH_SIZES[k] / 4 / REPLICAS
pred = model.predict(ds_test, steps=STEPS, verbose=VERBOSE)[:TTA * ct_test,]
preds[:,0] += np.mean(pred.reshape(( ct_test, TTA), order='F'), axis=1)* WGTS[k]
auc = roc_auc_score(oof_tar[-1], oof_pred[-1])
 oof_val.append(np.max(history.history['val_auc']))
print('OOF AUC without TTA = %.4f, with TTA = %.4f' %(oof_val[-1], auc))
print('History ploting...')
 plt.figure(figsize=(15,5))
plt.plot(history.history['auc'],'-o',label='Train AUC',color='
plt.plot(history.history['val_auc'],'-o',label='Val AUC',color='
x = np.argmax(history.history['val_auc']); y = np.max(history.history['val_auc'])
 xdist = plt.xlim() [1] - plt.xlim() [0]; ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
%.4f'%y,size=14)
 plt.ylabel('AUC',size=14); plt.xlabel('Epoch',size=14)
 plt.legend(loc=2)
 plt2 = plt.gca().twinx()
plt2.plot(history.history['loss'],'-o',label='Train Loss',color='
plt2.plot(history.history['val_loss'],'-o',label='Val Loss',color='
x = np.argmin(history.history['val_loss']); y = np.min(history.history['val_loss'])
 ydist = plt.ylim() [1] - plt.ylim() [0]
plt.scatter(x,y,s=200,color='
%.4f'%y,size=14)
 plt.ylabel('Loss',size=14)
 plt.title('FOLD %i - Image Size %i, EfficientNet B%i'%(k+1,IMG_SIZES[k],EFF_NETS[k]),size=18)
 plt.legend(loc=3)
 plt.show()<save_to_csv>
sys.path.insert(0, "/kaggle/input/xxxxxx/pytorchretinaface")
 sys.path.insert(0, "/kaggle/input/zzzzzz/mymodel" )
oof = np.concatenate(oof_pred)
 true = np.concatenate(oof_tar)
 names = np.concatenate(oof_names)
 folds = np.concatenate(oof_folds)
 auc = roc_auc_score(true, oof)
 print('Overall OOF AUC with TTA = %.4f' % auc)
 df_oof = pd.DataFrame(dict(image_name=names, target=true, pred=oof, fold=folds))
df_oof.to_csv('oof.csv', index=False)
 df_oof.head()<save_to_csv>
arcface_src = np.array([
[122.5, 141.25],
[197.5, 141.25],
[160., 178.75],
[137.5, 225.25],
[182.5, 225.25] ], dtype=np.float32)
 arcface_src = np.expand_dims(arcface_src, axis=0)
 def estimate_norm(lmk, image_size = 112, mode='arcface'):
assert lmk.shape==(5,2)
 tform = trans.SimilarityTransform()
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
 min_M = []
min_index = []
min_error = float('inf')
 if mode=='arcface':
src = arcface_src
else:
src = src_map[image_size]
for i in np.arange(src.shape[0]):
tform.estimate(lmk, src[i])
 M = tform.params[0:2,:]
results = np.dot(M, lmk_tran.T)
 results = results.T
error = np.sum(np.sqrt(np.sum(( results - src[i])** 2,axis=1)))
 if error< min_error:
min_error = error
min_M = M
min_index = i
return min_M, min_index
def norm_crop(img, landmark, image_size=112, mode='arcface'):
M, pose_index = estimate_norm(landmark, image_size, mode)
 warped = cv2.warpAffine(img,M,(image_size, image_size), borderValue = 0.0)
 return warped
def check_keys(model, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
 model_keys = set(model.state_dict().keys())
 used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
print('Missing keys:{}'.format(len(missing_keys)))
 print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
 print('Used keys:{}'.format(len(used_pretrained_keys)))
 assert len(used_pretrained_keys)> 0, 'load NONE from pretrained checkpoint'
return True
def remove_prefix(state_dict, prefix):

 f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix)else x
return {f(key): value for key, value in state_dict.items() }
def load_model(model, pretrained_path, load_to_cpu):
print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
 else:
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys() :
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
 else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
 check_keys(model, pretrained_dict)
 model.load_state_dict(pretrained_dict, strict=False)
 return model
torch.set_grad_enabled(False)
 cfg = cfg_re50
cfg['pretrain']=False
!cp /kaggle/input/xxxxxx/pytorchretinaface/weights./ -rf
net = RetinaFace(cfg=cfg, phase = 'test')
 net = load_model(net, './weights/Resnet50_Final.pth', False)
 net.eval()
print('Finished loading model!')
 cudnn.benchmark = True
device = torch.device("cuda")
 net = net.to(device )
<load_from_csv><EOS> 
modelx=WSDAN(num_classes=2, M=8, net='xception', pretrained=False ).cuda()
modelx.load_state_dict(torch.load('/kaggle/input/zzzzzz/mymodel/ckpt_x.pth')['state_dict'])
 modely=WSDAN(num_classes=2, M=8, net='efficientnet', pretrained=False ).cuda()
modely.load_state_dict(torch.load('/kaggle/input/zzzzzz/mymodel/ckpt_e.pth')['state_dict'])
 modelx.eval()
modely.eval()
predictions = predict_on_video_set(test_videos,modelx,modely)
 submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )