Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_model>
%matplotlib inline
print('Train images: %d' %len(os.listdir(os.path.join(WORK_DIR, "train"))))<load_from_csv>
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 )
train = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
train_images = WORK_DIR + "/train/" + train['StudyInstanceUID'] + '.jpg'
ss = pd.read_csv(os.path.join(WORK_DIR, 'sample_submission.csv'))
test_images = WORK_DIR + "/test/" + ss['StudyInstanceUID'] + '.jpg'
label_cols = ss.columns[1:]
labels = train[label_cols].values
train_annot = pd.read_csv(os.path.join(WORK_DIR, "train_annotations.csv"))
print('Labels:
', '*'*20, '
', label_cols.values)
 print('*'*50)
 train.head()<define_variables>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
BATCH_SIZE = 8 * 1
STEPS_PER_EPOCH = len(train)* 0.85 / BATCH_SIZE
VALIDATION_STEPS = len(train)* 0.15 / BATCH_SIZE
EPOCHS = 30
TARGET_SIZE = 750<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 )
def build_decoder(with_labels = True,
target_size =(TARGET_SIZE, TARGET_SIZE),
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)
 img = tf.image.adjust_brightness(img, 0.1)
 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<create_dataframe>
frames_per_video = 150
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 )
test_df = build_dataset(
 test_images, bsize = BATCH_SIZE, repeat = False,
shuffle = False, augment = False, cache = False)
 test_df<choose_model_class>
input_size =224
<choose_model_class>
mean = [0.43216, 0.394666, 0.37645]
std = [0.22803, 0.22145, 0.216989]
normalize_transform = Normalize(mean,std )
print('Our Xception CNN has %d layers' %len(model.layers))<create_dataframe>
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )
img_tensor = build_dataset(
 pd.Series(test_images[0]), bsize = 1,repeat = False,
shuffle = False, augment = False, cache = False )<save_to_csv>
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
ss[label_cols] = model.predict(test_df, verbose = 1)
 ss.to_csv('submission.csv', index = False )<define_variables>
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
class CFG:
device = 'GPU'
cpu_workers = 2
debug = True
seed = 13353
batch_size = 50
num_tta = 2
num_folds = 3
fold_idx = False
fold_blend = 'pmean'
model_blend = 'pmean'
power = 1/11
w_public = 0.25
lgb_folds = 5
label_features = False
sort_targets = True
pred_as_feature = True
lgb_stop_rounds = 200
lgb_params = {'objective': 'binary',
'metrics': 'auc',
'n_estimators': 10000,
'learning_rate': 0.01,
'num_leaves': 8,
'max_depth': 5,
'min_child_samples': 20,
'subsample': 0.3,
'colsample_bytree': 0.5,
'reg_alpha': 0.1,
'reg_lambda': 0.1,
'silent': True,
'verbosity': -1,
'n_jobs' : -1,
'random_state': 13353}
data_path = '/kaggle/input/ranzcr-clip-catheter-line-classification/'
models = ['/kaggle/input/ranzcr-v12/',
'/kaggle/input/ranzcr-v15-pub/',
'/kaggle/input/ranzcr-v17-pub/',
'/kaggle/input/ranzcr-v13-pub/',
'/kaggle/input/ranzcr-v14-pub/']<drop_column>
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )
CFG = dict(vars(CFG))
for key in ['__dict__', '__doc__', '__module__', '__weakref__']:
del CFG[key]<load_pretrained>
speed_test = False
CFGs = []
for model in CFG['models']:
model_cfg = pickle.load(open(model + 'configuration.pkl', 'rb'))
CFGs.append(model_cfg)
 print('Numer of models:', len(CFGs))<set_options>
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )
pd.set_option('display.max_columns', 100)
 ImageFile.LOAD_TRUNCATED_IMAGES = True
%matplotlib inline
warnings.filterwarnings('ignore')
 sys.path.append('.. /input/timm-pytorch-image-models/pytorch-image-models-master')
<train_model>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<compute_test_metric><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )