Kaggle competition: Deepfake Detection Challenge
X_train, X_valid, y_train, y_valid = train_test_split(train, target, test_size=0.15, random_state=0 )<train_model>
%matplotlib inline
lgbm = LGBMRegressor(tree_method='gpu_hist',learning_rate=0.07, max_depth=15, random_state=0, n_estimators=2000, n_jobs=-1)
 lgbm.fit(X_train, y_train, eval_set=[(X_valid, y_valid)])
 lgbm.score(X_valid, y_valid )<train_model>
package_path = '.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master'
sys.path.append(package_path)
xgb = XGBRegressor(learning_rate=0.005, max_depth=8, n_estimators=6000, n_jobs=-1, tree_method='gpu_hist', random_state=3)
 xgb.fit(X_train, y_train, eval_set=[(X_valid, y_valid)])
 xgb.score(X_valid, y_valid )<train_model>
def seed_everything(seed=1234):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
ensemble = VotingRegressor(estimators=[("xgb", xgb),("lgbm", lgbm)], weights=[1.1,1])
 ensemble.fit(X_train, y_train)
 ensemble.score(X_valid, y_valid )<train_model>
seed_everything(0 )
model = ensemble.fit(train, target)
 model.score(train, target )<predict_on_test>
weight_path = 'efficientnet_b0_epoch_15_loss_0.158.pth'
trained_weights_path = os.path.join('.. /input/deepfake-detection-model-weight', weight_path)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 torch.backends.cudnn.benchmark=True
pred = model.predict(test)
 pred<load_from_csv>
test_dir = '.. /input/deepfake-detection-challenge/test_videos'
os.listdir(test_dir)[:5]
id = pd.read_csv('/kaggle/input/tabular-playground-series-jan-2021/test.csv')['id']<save_to_csv>
class ImageTransform:
def __init__(self, size, mean, std):
self.data_transform = transforms.Compose([
transforms.Resize(( size, size), interpolation=Image.BILINEAR),
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ])
 def __call__(self, img):
return self.data_transform(img )
output = pd.DataFrame({'id': id, 'target': pred})
 output.to_csv("submission.csv", index=False)
 print("saved" )<import_modules>
class DeepfakeDataset(Dataset):
def __init__(self, file_list, device, detector, transform, img_num=20, frame_window=10):
self.file_list = file_list
self.device = device
self.detector = detector
self.transform = transform
self.img_num = img_num
self.frame_window = frame_window
def __len__(self):
return len(self.file_list)
 def __getitem__(self, idx):
mov_path = self.file_list[idx]
img_list = []
try:
all_image = get_img_from_mov(mov_path, self.img_num, self.frame_window)
 except:
return [], mov_path.split('/')[-1]
for image in all_image:
try:
_image = image[np.newaxis, :, :, :]
boxes, probs = self.detector.detect(_image, landmarks=False)
 x = int(boxes[0][0][0])
 y = int(boxes[0][0][1])
 z = int(boxes[0][0][2])
 w = int(boxes[0][0][3])
 image = image[y-15:w+15, x-15:z+15]
image = Image.fromarray(image)
 image = self.transform(image)
 img_list.append(image)
 except:
img_list.append(None)
 img_list = [c for c in img_list if c is not None]
return img_list, mov_path.split('/')[-1]
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
input_path = Path('/kaggle/input/tabular-playground-series-jan-2021/' )<load_from_csv>
model = EfficientNet.from_name('efficientnet-b0')
 model._fc = nn.Linear(in_features=model._fc.in_features, out_features=1)
 model.load_state_dict(torch.load(trained_weights_path, map_location=torch.device(device)) )
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 print(train.shape)
 train.head()<load_from_csv>
test_file = [os.path.join(test_dir, path)for path in os.listdir(test_dir)]
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 print(test.shape)
 test.head()<count_missing_values>
def predict_dfdc(dataset, model):
torch.cuda.empty_cache()
pred_list = []
path_list = []
model = model.to(device)
 model.eval()
with torch.no_grad() :
for i in tqdm(range(len(dataset))):
pred = 0
imgs, mov_path = dataset.__getitem__(i)
 if len(imgs)== 0:
pred_list.append(0.5)
 path_list.append(mov_path)
 continue
for i in range(len(imgs)) :
img = imgs[i]
output = model(img.unsqueeze(0 ).to(device))
pred += torch.sigmoid(output ).item() / len(imgs)
 pred_list.append(pred)
 path_list.append(mov_path)
 torch.cuda.empty_cache()
return path_list, pred_list
train.isnull().sum()<feature_engineering>
res = pd.DataFrame({
'filename': path_list,
'label': pred_list,
})
 res.sort_values(by='filename', ascending=True, inplace=True )
<feature_engineering><EOS> 
res.to_csv('submission.csv', index=False )