Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<prepare_output>
%matplotlib inline
Submission['Id'] = test_data['Id']
Submission.set_index('Id',inplace = True )<save_to_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 )
Submission.to_csv('Submission.csv' )<load_from_csv>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
train_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data<load_from_csv>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
test_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv')
 test_data.head()<count_values>
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_data['Cover_Type'].value_counts() 
<prepare_x_and_y>
frames_per_video = 17
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_data.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_data['Cover_Type']
X_train,X_value,y_train,y_value=train_test_split(X,y,random_state=40)
 print(X_train.shape,y_train.shape)
 print(X_value.shape,y_value.shape)
<train_model>
input_size = 224
RFC=RandomForestClassifier(n_estimators=70)
 RFC.fit(X_train,y_train)
<compute_test_metric>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
RFC.score(X_value,y_value)
<predict_on_test>
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 )
predict=RFC.predict(test_data.drop(labels=['Id'],axis=1))
Submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 Submission.head()<prepare_output>
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
Submission['Id']=test_data['Id']
Submission.set_index('Id',inplace=True)
 Submission.head() 
<save_to_csv>
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
Submission.to_csv('Submission.csv')
<import_modules>
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 )
import tensorflow as tf
import numpy as np
import pandas as pd 
<load_from_csv>
speed_test = False
training = pd.read_csv('.. /input/digit-recognizer/train.csv' )<prepare_x_and_y>
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)) )
x_train, y_train = training.iloc[:, 1:], training.iloc[:, 0:1]<feature_engineering>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<choose_model_class><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )