Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<categorify>
%%capture
!pip install /kaggle/input/facenet-pytorch-vggface2/facenet_pytorch-2.2.9-py3-none-any.whl
!pip install /kaggle/input/xt-training/pynvml-8.0.4-py3-none-any.whl
!pip install /kaggle/input/xt-training/xt_training-1.4.0-py3-none-any.whl
!pip install /kaggle/input/imageio-ffmpeg/imageio_ffmpeg-0.3.0-py3-none-manylinux2010_x86_64.whl
!pip install /kaggle/input/imutils/imutils-0.5.3/
!cp -R /kaggle/input/xtract-ai-dfdc/dfdc./
class ImageData(Dataset):
def __init__(self, df, data_dir, transform):
super().__init__()
self.df = df
self.data_dir = data_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, index):
img_name = self.df.id[index]
label = self.df.has_cactus[index]
img_path = os.path.join(self.data_dir, img_name)
 image = mpimg.imread(img_path)
 image = self.transform(image)
 return image, label<set_options>
import os
import glob
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader, Subset
from torch import optim
from tqdm.notebook import tqdm
from matplotlib import pyplot as plt
import albumentations as A
from tqdm.notebook import tqdm
from xt_training import metrics, Runner
from xt_training.runner import Logger
from dfdc.datasets.video_dataset import VideoDataset
from dfdc.models.video_models import FaceSequenceClassifier, FaceClassifier
epochs = 15
batch_size = 20
device = torch.device('cuda:0' )<categorify>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 print('Running on device: {}'.format(device))
data_transf = transforms.Compose([transforms.ToPILImage() , transforms.ToTensor() ])
 train_data = ImageData(df = df, data_dir = train_dir, transform = data_transf)
 train_loader = DataLoader(dataset = train_data, batch_size = batch_size )<set_options>
class Ensemble(torch.nn.Module):
def __init__(self, unpack=None, permute=False, mapping=None, **kwargs):
super().__init__()
self.permute = permute
self.mapping = mapping
if unpack is None:
self.unpack = lambda x, i: x
else:
self.unpack = unpack
for name, model in kwargs.items() :
self.add_module(name, model)
 def forward(self, x):
out = []
for i, layer in enumerate(self._modules.values()):
if self.mapping is None:
in_i = i
else:
in_i = self.mapping[i]
x_i = self.unpack(x, in_i)
 out.append(layer(x_i))
out = torch.stack(out)
 if self.permute:
out = out.permute(( 1, 0, 2))
return out
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<choose_model_class>
face_model1 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model1.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_alltrain_moreaug_20200317.pt'))
face_model1.classifier.fc = torch.nn.Sequential()
face_model2 = FaceClassifier(pretrained=False, base_model='resnet')
 face_model2.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_lessaug_resnet101_20200321.pt'))
face_model2.classifier.fc = torch.nn.Sequential()
face_model3 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model3.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_20200314.pt'))
face_model3.classifier.fc = torch.nn.Sequential()
face_model4 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model4.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_20200223.pt'))
face_model4.classifier.fc = torch.nn.Sequential()
face_model = Ensemble(
 unpack=None,
m1=face_model1,
m2=face_model2,
m3=face_model3,
m4=face_model4, 
)
 face_model.to(device)
 face_model.eval()
@torch.no_grad()
def face_model_transform(x):
return face_model(x.to(device))
model = models.resnet50(pretrained=True)
 model.cuda()
optimizer = optim.Adam(model.parameters() , lr=0.001)
 loss_func = nn.CrossEntropyLoss()<train_model>
test_trans = A.ReplayCompose([
A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ,
A.Resize(160, 160, always_apply=True)
 ])
 video_root = '/kaggle/input/deepfake-detection-challenge/'
test_dataset = VideoDataset(
 video_root,
transform=test_trans,
out_transform=face_model_transform,
is_test=True,
sample_frames=-1,
shuffle=False,
stride=10,
n_frames=-1,
device=device,
reader='imutils',
path_include='test_videos/', 
)
 batch_size = 1
num_workers = 0
test_loader = DataLoader(
 test_dataset,
batch_size=batch_size,
num_workers=num_workers
)
%%time
for epoch in tqdm(range(epochs)) :
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 labels = labels.to(device)
 outputs = model(images)
 loss = loss_func(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i+1)% 500 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, epochs, loss.item()))<load_from_csv>
video_model1 = FaceSequenceClassifier(mode='linear')
 video_model1.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_alltrain_lessaug_20200319.pt'))
video_model2 = FaceSequenceClassifier(mode='linear')
 video_model2.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_lessaug_resnet101_20200321.pt'))
video_model3 = FaceSequenceClassifier(mode='linear')
 video_model3.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_ep8_20200314.pt'))
video_model4 = FaceSequenceClassifier(mode='linear')
 video_model4.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_20200223.pt'))
video_model5 = FaceSequenceClassifier(mode='conv')
 video_model5.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/video_model_best_alltrain_lessaug_conv_20200319.pt'))
scales = [1, 1, 1, 1, 0.5]
def unpack(x, i):
return [x[0][:, i], x[1]]
video_model = Ensemble(
 unpack=unpack,
permute=True,
m1=video_model1,
m2=video_model2,
m3=video_model3,
m4=video_model4,
m5=video_model5,
mapping=[0, 1, 2, 3, 0] 
)
 video_model.to(device)
 video_model.eval()
submit = pd.read_csv('.. /input/sample_submission.csv')
 test_data = ImageData(df = submit, data_dir = test_dir, transform = data_transf)
 test_loader = DataLoader(dataset = test_data, shuffle=False )<categorify>
sample_dataset = VideoDataset(
 video_root,
transform=test_trans,
out_transform=face_model_transform,
is_test=True,
sample_frames=-1,
shuffle=False,
stride=10,
n_frames=-1,
device=device,
reader='imutils',
path_include='train_sample_videos/', 
)
 sample_dataset.samples[0] =('/kaggle/input/deepfake-detection-challenge/train_sample_videos/aagfhgtpmv.mp4', 1)
 sample_loader = DataLoader(
 sample_dataset,
batch_size=batch_size,
num_workers=num_workers 
)
 for x, y in sample_loader:
break
print(x[0].abs().mean())
 x = [x_i.to(device)for x_i in x]
y.to(device)
 with torch.no_grad() :
y_pred = video_model(x)
 for i, scale in enumerate(scales):
y_pred[:, i] = y_pred[:, i] * scale
y_pred
predict = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.to(device), target.to(device)
 output = model(data)
 _, pred = torch.max(output.data, 1)
 predict.append(pred )<save_to_csv>
runner = Runner(model=video_model, device=device)
 y_pred, _ = runner(test_loader, 'test', return_preds=True )
<import_modules><EOS> 
for i, scale in enumerate(scales):
y_pred[:, i] = y_pred[:, i] * scale
labels = torch.nn.functional.softmax(y_pred.mean(dim=1), dim=1)[:, 1].numpy()
filenames = [os.path.basename(f)for f in test_dataset.video_files]
submission = pd.DataFrame({'filename': filenames, 'label': labels})
 submission.to_csv('submission.csv', index=False)
 plt.hist(submission.label, 30)
 submission