Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<categorify>
%matplotlib inline
if False:
word_vectorizer = pickle.load(open("word_vectorizer.pk", "rb"))
char_vectorizer = pickle.load(open("char_vectorizer.pk", "rb"))
train_word_features = word_vectorizer.transform(train_text)
 test_word_features = word_vectorizer.transform(test_text)
 train_char_features = char_vectorizer.transform(train_text)
 test_char_features = char_vectorizer.transform(test_text)
 train_features = hstack([train_char_features, train_word_features])
 test_features = hstack([test_char_features, test_word_features] )<drop_column>
frames_per_vid = [17, 25, 30, 32, 35, 36, 38, 39, 40, 49, 56, 64, 72, 80, 81, 82, 100]
public_LB = [0.46788, 0.46776, 0.46611, 0.46542, 0.46643, 0.46484, 0.46444, 0.46603, 0.46635, 0.46620, 0.46481, 0.46441, 0.46559, 0.46518, 0.46453, 0.46482, 0.46495]
df_viz = pd.DataFrame({'frames_per_vid': frames_per_vid, 'public_LB':public_LB} )
del train_word_features, test_word_features,train_char_features, test_char_features,
del word_vectorizer, char_vectorizer<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"])
 frame_h = 5
frame_l = 5
len(test_videos )
%%time
scores = []
submission = pd.DataFrame.from_dict({'id': test['id']})
 for class_name in class_names:
train_target = train[class_name]
classifier = LogisticRegression(solver='sag',n_jobs=-1)
 cv_score = np.mean(cross_val_score(classifier, train_features, train_target, cv=3, scoring='roc_auc'))
scores.append(cv_score)
 print('CV score for class {} is {}'.format(class_name, cv_score))
classifier.fit(train_features, train_target)
 submission[class_name] = classifier.predict_proba(test_features)[:, 1]
with open(f'logreg_{class_name}.pk', 'wb')as f:
pickle.dump(classifier, f)
 print('Total CV score is {}'.format(np.mean(scores)))
 submission.to_csv('submission.csv', index=False )<import_modules>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
import tensorflow as tf
from tensorflow.keras import layers
import os
import re
import math
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<define_variables>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
GCS_DS_PATH = ".. /input/ranzcr-clip-catheter-line-classification"<load_from_csv>
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 )
train_df = pd.read_csv(GCS_DS_PATH+"/train.csv")
 train_df.index = train_df["StudyInstanceUID"]
del train_df["StudyInstanceUID"]
train_annot_df = pd.read_csv(GCS_DS_PATH+"/train_annotations.csv")
 train_annot_df.index = train_annot_df["StudyInstanceUID"]
del train_annot_df["StudyInstanceUID"]<define_variables>
frames_per_video = 65
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 )
classes = list(train_df.columns[:-1])
 classes_normal= [name for name in classes[:-1] if name.split(" - ")[1] == "Normal"]
classes_abnormal= [name for name in classes[:-1] if name.split(" - ")[1] == "Abnormal"]
classes_borderline = [name for name in classes[:-1] if name.split(" - ")[1] == "Borderline"]
classes_count = train_df[classes].sum(axis = 0)
 num_classes = len(classes_count)
 print("Number of Classes: {}".format(num_classes))
classes_count<define_variables>
input_size = 224
class_weights = {}
ls = list(classes_count.values)
 tot_samples = sum(ls)
 for i in range(num_classes):
class_weights[i] = tot_samples/(num_classes*ls[i])
 class_weights<count_values>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
patient_ids = train_df["PatientID"].unique()
patientwise_count = train_df['PatientID'].value_counts()
num_patients = len(patientwise_count)
 print("Number of patients: ",num_patients)
 patientwise_count<define_variables>
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 )
IMAGE_SIZE = [600,600]
AUTO = tf.data.experimental.AUTOTUNE
TEST_FILENAMES = tf.io.gfile.glob(GCS_DS_PATH + '/test_tfrecords/*.tfrec' )<categorify>
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
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.image.resize(image, [*IMAGE_SIZE])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"StudyInstanceUID" : tf.io.FixedLenFeature([], tf.string),
"image" : tf.io.FixedLenFeature([], tf.string),
"ETT - Abnormal" : tf.io.FixedLenFeature([], tf.int64),
"ETT - Borderline" : tf.io.FixedLenFeature([], tf.int64),
"ETT - Normal" : tf.io.FixedLenFeature([], tf.int64),
"NGT - Abnormal" : tf.io.FixedLenFeature([], tf.int64),
"NGT - Borderline" : tf.io.FixedLenFeature([], tf.int64),
"NGT - Incompletely Imaged" : tf.io.FixedLenFeature([], tf.int64),
"NGT - Normal" : tf.io.FixedLenFeature([], tf.int64),
"CVC - Abnormal" : tf.io.FixedLenFeature([], tf.int64),
"CVC - Borderline" : tf.io.FixedLenFeature([], tf.int64),
"CVC - Normal" : tf.io.FixedLenFeature([], tf.int64),
"Swan Ganz Catheter Present" : tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = [example['ETT - Abnormal'],
example['ETT - Borderline'],
example['ETT - Normal'],
example['NGT - Abnormal'],
example['NGT - Borderline'],
example['NGT - Incompletely Imaged'],
example['NGT - Normal'],
example['CVC - Abnormal'],
example['CVC - Borderline'],
example['CVC - Normal'],
example['Swan Ganz Catheter Present']]
label = [tf.cast(i,tf.float32)for i in label]
return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"StudyInstanceUID" : tf.io.FixedLenFeature([], tf.string),
"image" : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['StudyInstanceUID']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset<split>
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
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image,label
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} unlabeled test images'.format(NUM_TEST_IMAGES))<train_model>
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 )
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
test_ds = get_test_dataset()
print("Test:", test_ds )<install_modules>
speed_test = False
!pip install /kaggle/input/kerasapplications -q
!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps 
<load_pretrained>
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)) )
model = tf.keras.models.load_model(".. /input/ranzcr-clip-tpu/model.h5" )<save_to_csv>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<set_options><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )