Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<load_from_csv>
!pip install.. /input/kaggle-efficientnet-repo/efficientnet-1.0.0-py3-none-any.whl
df = pd.read_csv(CFG['data_path'] + 'sample_submission.csv')
 CFG['targets'] = ['ETT - Abnormal',
'ETT - Borderline',
'ETT - Normal',
'NGT - Abnormal',
'NGT - Borderline',
'NGT - Incompletely Imaged',
'NGT - Normal',
'CVC - Abnormal',
'CVC - Borderline',
'CVC - Normal',
'Swan Ganz Catheter Present']
CFG['num_classes'] = len(CFG['targets'])
 if CFG['debug'] and len(df)== 3582:
print('Subsetting data in the debug mode...')
 df = df.head(CFG['batch_size'])
 print(df.shape )<load_from_csv>
import pandas as pd
import tensorflow as tf
import cv2
import glob
from tqdm.notebook import tqdm
import numpy as np
import os
from keras.layers import *
from keras import Model
import matplotlib.pyplot as plt
import time
from keras.applications.xception import Xception
import efficientnet.keras as efn
for m in CFG['models']:
tmp_train_preds = pd.read_csv(m + '/oof.csv')
 tmp_train_preds.columns = ['StudyInstanceUID'] + CFG['targets'] + ['PatientID', 'fold'] + [m + ' ' + c for c in CFG['targets']]
if m == CFG['models'][0]:
train_preds = tmp_train_preds
else:
train_preds = train_preds.merge(tmp_train_preds[['StudyInstanceUID'] + [m + ' ' + c for c in CFG['targets']]], how = 'left', on = 'StudyInstanceUID')
 weights = []
for model_idx, m in enumerate(CFG['models']):
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[[m + ' ' + c for c in CFG['targets']]].values)[0] / 5
weights.append(score)
 sorted_ids = list(np.argsort(np.array(weights)))
 sorted_weights = [weights[i] for i in sorted_ids]
CFG['models'] = [CFG['models'][i] for i in sorted_ids]
CFGs = [CFGs[i] for i in sorted_ids]
print('-' * 45)
 print('{:<5}{:<33}{:>5}'.format('ID', 'Model', 'AUC'))
print('-' * 45)
 for model_idx, m in enumerate(CFG['models']):
print('{:<5}{:<33}{:.4f}'.format(model_idx + 1, m, sorted_weights[model_idx]))
print('-' * 45 )<categorify>
import torch
import torch.nn as nn
import torch.nn.functional as F
for c in CFG['targets']:
class_preds = train_preds.filter(like = 'kaggle' ).filter(like = c ).columns
for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
train_preds[blend + ' ' + c] = compute_blend(train_preds, class_preds, blend, CFG)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
train_preds['w' + blend + ' ' + c] = compute_blend(train_preds, class_preds, blend, CFG, weights = np.array(sorted_weights))
print('-' * 18)
 print('{:<10}{:>5}'.format('Blend', 'AUC'))
print('-' * 18)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[[blend + ' ' + c for c in CFG['targets']]].values)[0] / 5
print('{:<10}{:>5.4f}'.format(blend, score))
print('-' * 18)
 for blend in ['amean', 'median', 'gmean', 'pmean', 'rmean']:
score = 0
for fold_idx in range(5):
tmp_train_preds = train_preds.loc[train_preds['fold'] == fold_idx]
score += get_score(tmp_train_preds[CFG['targets']].values, tmp_train_preds[['w' + blend + ' ' + c for c in CFG['targets']]].values)[0] / 5
print('{:<10}{:>5.4f}'.format('w' + blend, score))
print('-' * 18 )<create_dataframe>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
def get_dataset(CFG):
class ImageData(Dataset):
def __init__(self,
df,
path,
transform = None,
labeled = False,
indexed = False):
self.df = df
self.path = path
self.transform = transform
self.labeled = labeled
self.indexed = indexed
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
path = os.path.join(self.path, self.df.iloc[idx]['StudyInstanceUID'] + '.jpg')
 image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
 if image is None:
raise FileNotFoundError(path)

mask = image > 0
image = image[np.ix_(mask.any(1), mask.any(0)) ]
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform is not None:
image = self.transform(image = image)['image']
if CFG['normalize'] == 'public':
image = image.astype(np.float32)
 image = image.transpose(2, 0, 1)
 image = torch.tensor(image ).float()
if self.labeled:
label = torch.tensor(self.df.iloc[idx][CFG['targets']] ).float()
if self.indexed:
idx = torch.tensor(idx)
 return idx, image, label
else:
return image, label
else:
return image
return ImageData<choose_model_class>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
def get_model(CFG, device, num_classes):
if CFG['weights'] != 'public':
model = timm.create_model(model_name = CFG['backbone'],
pretrained = False,
in_chans = CFG['channels'])
 if 'efficient' in CFG['backbone']:
model.classifier = nn.Linear(model.classifier.in_features, num_classes)
 else:
model.fc = nn.Linear(model.fc.in_features, num_classes)
 else:
class CustomModel(nn.Module):
def __init__(self, model_name = 'resnet200d', out_dim = 11, pretrained = False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, out_dim)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
model = CustomModel(CFG['backbone'], num_classes, True)
 return model<create_dataframe>
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )
cv_start = time.time()
gc.collect()
all_counter = 0
fold_counter = 0 if not CFG['fold_idx'] else CFG['fold_idx']
all_cnn_preds = None
for model_idx in range(len(CFG['models'])) :
ImageData = get_dataset(CFGs[model_idx])
 test_dataset = ImageData(df = df,
path = CFG['data_path'] + 'test/',
transform = get_augs(CFGs[model_idx], image_size = CFGs[model_idx]['image_size']),
labeled = False,
indexed = False)
 test_loader = DataLoader(test_dataset,
batch_size = CFG['batch_size'],
shuffle = False,
num_workers = CFG['cpu_workers'],
pin_memory = True)
 for fold_idx in tqdm(range(CFG['num_folds'])) :
model = get_model(CFGs[model_idx], device = device, num_classes = CFG['num_classes'])
 model = model.to(device)
 model.load_state_dict(torch.load(CFG['models'][model_idx] + 'weights_fold{}.pth'.format(fold_counter),map_location = device))
model.eval()
PROBS = []
with torch.no_grad() :
for batch_idx, inputs in enumerate(test_loader):
inputs = inputs.to(device)
 probs = torch.zeros(( inputs.shape[0], CFG['num_classes']), device = device)
 for tta_idx in range(CFG['num_tta']):
preds = model(get_tta_flips(inputs, tta_idx))
probs += preds.sigmoid()
PROBS.append(probs.detach().cpu() / CFG['num_tta'])
 cnn_preds = pd.DataFrame(torch.cat(PROBS ).numpy() , columns = [CFG['models'][model_idx] + str(fold_idx)+ '/' + c for c in CFG['targets']])
 all_cnn_preds = pd.concat([all_cnn_preds, cnn_preds], axis = 1)
 all_counter += 1
if not CFG['fold_idx']:
fold_counter += 1
if fold_counter == CFG['num_folds']:
fold_counter = 0
del model, inputs, preds, probs, PROBS
gc.collect()
del test_loader, test_dataset
gc.collect()
print('Finished {} preds x {} TTA in {:.2f} hours'.format(all_counter, CFG['num_tta'],(time.time() - cv_start)/ 3600))<categorify>
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )
print('Blending fold predictions with: ' + CFG['fold_blend'])
 for m in CFG['models']:
for c in CFG['targets']:
class_preds = all_cnn_preds.filter(like = m ).filter(like = c ).columns
all_cnn_preds[m + c] = compute_blend(all_cnn_preds, class_preds, CFG['fold_blend'], CFG)
 all_cnn_preds.drop(class_preds, axis = 1, inplace = True)
 all_cnn_preds.head()<load_from_csv>
input_size = 224
for m in CFG['models']:
tmp_train_preds = pd.read_csv(m + '/oof.csv')
 tmp_train_preds.columns = ['StudyInstanceUID'] + CFG['targets'] + ['PatientID', 'fold'] + [m + '' + c for c in CFG['targets']]
if m == CFG['models'][0]:
train_preds = tmp_train_preds
else:
train_preds = train_preds.merge(tmp_train_preds[['StudyInstanceUID'] + [m + '' + c for c in CFG['targets']]], how = 'left', on = 'StudyInstanceUID')
 train_preds.head()<concatenate>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
test_preds = all_cnn_preds.copy()
test_preds = pd.concat([df['StudyInstanceUID'], test_preds], axis = 1)
 test_preds.head()<create_dataframe>
frames_per_video = 10
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet)
X = train_preds.copy()
X_test = test_preds.copy()
drop_features = ['StudyInstanceUID', 'PatientID', 'fold'] + CFG['targets']
features = [f for f in X.columns if f not in drop_features]
print(len(features), 'features')
 display(features )<load_from_csv>
class HisResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(HisResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )
folds = pd.read_csv('/kaggle/input/how-to-properly-split-folds/train_folds.csv')
 del X['fold']
X = X.merge(folds[['StudyInstanceUID', 'fold']], how = 'left', on = 'StudyInstanceUID' )<sort_values>
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='' )
if CFG['sort_targets']:
sorted_targets = ['Swan Ganz Catheter Present',
'ETT - Normal',
'ETT - Abnormal',
'ETT - Borderline',
'NGT - Abnormal',
'NGT - Normal',
'NGT - Incompletely Imaged',
'NGT - Borderline',
'CVC - Abnormal',
'CVC - Normal',
'CVC - Borderline']<prepare_x_and_y>
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = HisResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
cnn_oof = np.zeros(( len(X), CFG['num_classes']))
lgb_oof = np.zeros(( len(X), CFG['num_classes']))
lgb_tst = np.zeros(( len(X_test), CFG['lgb_folds'], CFG['num_classes']))
all_lgb_preds = None
cv_start = time.time()
print('-' * 45)
 print('{:<28}{:<7}{:>5}'.format('Label', 'Model', 'AUC'))
print('-' * 45)
 for label in sorted_targets:
y = X[label]
label_features = [f for f in features if label in f] if CFG['label_features'] else features
lgb_auc = 0
cnn_auc = 0
for fold in range(CFG['lgb_folds']):
trn_idx = X.loc[X['fold'] != fold].index
val_idx = X.loc[X['fold'] == fold].index
X_train, y_train = X.iloc[trn_idx][label_features], y.iloc[trn_idx]
X_valid, y_valid = X.iloc[val_idx][label_features], y.iloc[val_idx]
X_test_label = X_test[label_features]
clf = lgb.LGBMClassifier(**CFG['lgb_params'])
 clf = clf.fit(X_train, y_train,
eval_set = [(X_valid, y_valid)],
early_stopping_rounds = CFG['lgb_stop_rounds'],
verbose = False)
 cnn_oof[val_idx, CFG['targets'].index(label)] = compute_blend(X_valid, list(X_valid.filter(like = label ).columns), CFG['fold_blend'], CFG)
 lgb_oof[val_idx, CFG['targets'].index(label)] = clf.predict_proba(X_valid)[:, 1]
lgb_tst[:, fold, CFG['targets'].index(label)] = clf.predict_proba(X_test_label)[:, 1]
cnn_auc += roc_auc_score(y_valid ,cnn_oof[val_idx, CFG['targets'].index(label)])/ CFG['lgb_folds']
lgb_auc += roc_auc_score(y_valid, lgb_oof[val_idx, CFG['targets'].index(label)])/ CFG['lgb_folds']
print('{:<29}{:<7}{:>5.4f}'.format(label, 'CNN', cnn_auc))
print('{:<29}{:<7}{:>5.4f}'.format(label, 'LGB', lgb_auc))
print('-' * 45)
 if cnn_auc >= lgb_auc:
for fold in range(CFG['lgb_folds']):
val_idx = X.loc[X['fold'] == fold].index
X_valid, y_valid = X.iloc[val_idx][label_features], y.iloc[val_idx]
lgb_oof[val_idx, CFG['targets'].index(label)] = compute_blend(X_valid, list(X_valid.filter(like = label ).columns), CFG['fold_blend'], CFG)
 lgb_tst[:, fold, CFG['targets'].index(label)] = compute_blend(X_test_label, list(X_test_label.filter(like = label ).columns), CFG['fold_blend'], CFG)
 for fold in range(CFG['lgb_folds']):
lgb_preds = pd.DataFrame(lgb_tst[:, fold, CFG['targets'].index(label)], columns = [str(fold)+ '/' + label])
 all_lgb_preds = pd.concat([all_lgb_preds, lgb_preds], axis = 1)
 if CFG['pred_as_feature']:
X['LGB ' + label] = lgb_oof[:, CFG['targets'].index(label)]
X_test['LGB ' + label] = np.mean(lgb_tst[:, :, CFG['targets'].index(label)], axis = 1)
 features.append('LGB ' + label)
 score_cnn = 0
score_lgb = 0
for fold in range(CFG['lgb_folds']):
val_idx = X.loc[X['fold'] == fold].index
score_cnn += get_score(X.iloc[val_idx][CFG['targets']].values, cnn_oof[val_idx, :])[0] / CFG['lgb_folds']
score_lgb += get_score(X.iloc[val_idx][CFG['targets']].values, lgb_oof[val_idx, :])[0] / CFG['lgb_folds']
print('{:<29}{:<7}{:>5.4f}'.format('OVERALL', 'CNN', score_cnn))
print('{:<29}{:<7}{:>5.4f}'.format('OVERALL', 'LGB', score_lgb))
print('-' * 45)
 print('Finished in {:.2f} minutes'.format(( time.time() - cv_start)/ 60))
del cnn_oof, lgb_tst, lgb_oof, clf, X_train, X_valid, X_test, X_test_label, y_train, y_valid
del features, label_features, trn_idx, val_idx, folds, train_preds, test_preds
gc.collect()<categorify>
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5
print('Blending fold predictions with: ' + CFG['fold_blend'])
 for c in CFG['targets']:
class_preds = all_lgb_preds.filter(like = c ).columns
all_lgb_preds[c] = compute_blend(all_lgb_preds, class_preds, CFG['fold_blend'], CFG)
 all_lgb_preds.drop(class_preds, axis = 1, inplace = True)
 all_lgb_preds.head()<define_variables>
cm = detection_graph.as_default()
cm.__enter__()
if CFG['w_public'] > 0:
gc.collect()
BATCH_SIZE = 96
IMAGE_SIZE = 640
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH_resnet200d = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'
MODEL_PATH_seresnet152d = '.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth'
class TestDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.file_names = df['StudyInstanceUID'].values
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
file_name = self.file_names[idx]
file_path = f'{TEST_PATH}/{file_name}.jpg'
image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
 mask = image > 0
image = image[np.ix_(mask.any(1), mask.any(0)) ]
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 if self.transform:
augmented = self.transform(image=image)
 image = augmented['image']
return image
def get_transforms(CFG):
return A.Compose([A.Resize(IMAGE_SIZE, IMAGE_SIZE),
A.Normalize() ,
ToTensorV2() ])
 class ResNet200D(nn.Module):
def __init__(self, model_name = 'resnet200d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class SeResNet152D(nn.Module):
def __init__(self, model_name = 'seresnet152d'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total = len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis = 0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH_resnet200d)['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 del model
models152D = []
model = SeResNet152D()
model.load_state_dict(torch.load(MODEL_PATH_seresnet152d)['model'])
 model.eval()
model.to(device)
 models152D.append(model)
 del model
test_dataset = TestDataset(df, transform = get_transforms(CFG))
test_loader = DataLoader(test_dataset, batch_size = BATCH_SIZE, shuffle = False, num_workers = CFG['cpu_workers'], pin_memory = True)
 predictions200d = inference(models200D, test_loader, device)
 predictions152d = inference(models152D, test_loader, device)
 target_cols = df.iloc[:, 1:12].columns.tolist()
predictions200d = pd.DataFrame(predictions200d, columns = ['200d/' + c for c in target_cols])
 predictions152d = pd.DataFrame(predictions152d, columns = ['152d/' + c for c in target_cols])
 predictions = pd.concat([predictions200d, predictions152d], axis = 1)
 df_pub = predictions.copy()
display(df_pub.head() )<categorify>
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' )
if CFG['w_public'] == 0:
df_pub = all_lgb_preds.copy()
else:
for c in CFG['targets']:
class_preds = df_pub.filter(like = c ).columns
df_pub[c] = compute_blend(df_pub, class_preds, CFG['model_blend'], CFG, weights = np.array([2/3, 1/3]))
df_pub.drop(class_preds, axis = 1, inplace = True)
 df_pub.head()<categorify>
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):
frame_count=10
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(0,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 )
all_preds = all_lgb_preds.copy()
all_preds.columns = ['my/' + c for c in all_preds.columns]
df_pub.columns = ['public/' + c for c in df_pub.columns]
preds = pd.concat([all_preds, df_pub], axis = 1)
 for c in CFG['targets']:
class_preds = preds.filter(like = c ).columns
preds[c] = compute_blend(preds, class_preds, CFG['model_blend'], CFG, weights = np.array([1 - CFG['w_public'], CFG['w_public']]))
preds.drop(class_preds, axis = 1, inplace = True)
 preds.head()<save_to_csv>
res_predictions =[]
if all_counter == len(CFG['models'] * CFG['num_folds']):
for c in CFG['targets']:
df[c] = preds[c].rank(pct = True)
 df.to_csv('submission.csv', index = False)
 display(df.head() )<install_modules>
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)
 y_pred = predict_on_video(x, batch_size=frames_per_video)
 res_predictions.append(y_pred)
 if a is None:
continue
cv2.imwrite('./videos/'+filename,a)
 except Exception as err:
print(err )
!pip install /kaggle/input/kerasapplications -q
!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps<import_modules>
bottleneck_EfficientNetB1 = efn.EfficientNetB1(weights=None,include_top=False,pooling='avg')
 inp=Input(( 10,240,240,3))
x=TimeDistributed(bottleneck_EfficientNetB1 )(inp)
 x = LSTM(128 )(x)
 x = Dense(64, activation='elu' )(x)
 x = Dense(1,activation='sigmoid' )(x)
 model_EfficientNetB1=Model(inp,x)
 bottleneck_Xception = Xception(weights=None,
include_top=False,pooling='avg')
 y=TimeDistributed(bottleneck_Xception )(inp)
 y = LSTM(128 )(y)
 y = Dense(64, activation='elu' )(y)
 y = Dense(1,activation='sigmoid' )(y)
 model_Xception=Model(inp,y )
import os, gc
import efficientnet.tfkeras as efn
import numpy as np
import pandas as pd
import tensorflow as tf<categorify>
model_EfficientNetB1.load_weights('.. /input/efficientnetb1dfdc/EfficientNetB1-e_2_b_4_f_30-10.h5')
 model_Xception.load_weights('.. /input/xceptiondfdc/Xception-e_2_b_4_f_30-10.h5' )
def auto_select_accelerator() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("Running on TPU:", tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print(f"Running on {strategy.num_replicas_in_sync} replicas")
 return strategy
def build_decoder(with_labels=True, target_size=(300, 300), ext='jpg'):
def decode(path):
file_bytes = tf.io.read_file(path)
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels=3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels=3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset(paths, labels=None, bsize=32, cache=True,
decode_fn=None, augment_fn=None,
augment=True, repeat=True, shuffle=1024,
cache_dir=""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None)
 if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls=AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls=AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset<define_variables>
def get_birghtness(img):
return img/img.max()
def process_img(img,flip=False):
imgs=[]
for x in range(10):
if flip:
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 )
COMPETITION_NAME = "ranzcr-clip-catheter-line-classification"
strategy = auto_select_accelerator()
BATCH_SIZE = strategy.num_replicas_in_sync * 16<load_from_csv>
sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 test_files=glob.glob('./videos/*.jpg')
 submission=pd.DataFrame()
submission['filename']=os.listdir(( '.. /input/deepfake-detection-challenge/test_videos/'))
submission['label']=0.5
filenames=[]
batch=[]
batch1=[]
preds=[]
model_paths = [
'.. /input/ranzcr-last-models/0.952_model_640_47.h5',
'.. /input/ranzcr-last-models/0.953_model_616_51.h5',
'.. /input/ranzcr-last-models/0.953_model_640_43.h5',
'.. /input/ranzcr-last-models/0.954_model_640_42.h5',
'.. /input/ranzcr-last-models/0.954_model_632_48.h5',
]
subs = []
for model_path in model_paths:
load_dir = f"/kaggle/input/{COMPETITION_NAME}/"
sub_df = pd.read_csv(load_dir + 'sample_submission.csv')
 test_paths = load_dir + "test/" + sub_df['StudyInstanceUID'] + '.jpg'
label_cols = sub_df.columns[1:]
image_size = int(model_path.split('_')[2])
 test_decoder = build_decoder(with_labels=False, target_size=(image_size, image_size))
dtest = build_dataset(
 test_paths, bsize=BATCH_SIZE, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder 
)
 with strategy.scope() :
model = tf.keras.models.load_model(model_path)
 print('predict:', model_path)
 sub_df[label_cols] = model.predict(dtest, verbose=1)
 subs.append(sub_df)
 del model
gc.collect()<save_to_csv>
new_preds=[]
for x,y in zip(preds,res_predictions):
new_preds.append(x[0]+(0.2*y))
print(sum(new_preds)/len(new_preds))
submission = pd.concat(subs)
 submission = submission.groupby('StudyInstanceUID' ).mean()
submission.to_csv('submission.csv')
 submission<install_modules>
for x,y in zip(new_preds,filenames):
submission.loc[submission['filename']==y,'label']=x
<install_modules><EOS> 
submission.to_csv('submission.csv', index=False)
 !rm -r videos