Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_model>
%matplotlib inline
scaler = StandardScaler()
scaler.fit(X_num)
 X_num = scaler.transform(X_num)
 print(f'Categorical Shape: {X_cat.shape}')
 print(f'Numerical Shape: {X_num.shape}')
<concatenate>
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 )
X = np.hstack(( X_num, X_cat))
print(X.shape )<define_variables>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
features = [
'Elevation','Horizontal_Distance_To_Roadways', 'Horizontal_Distance_To_Hydrology',
'Vertical_Distance_To_Hydrology','Aspect','Slope','Euclidian_Distance_To_Hydrology',
'Mean_Elevation_Vertical_Distance_Hydrology','Mean_Distance_Hydrology_Firepoints',
'Mean_Distance_Hydrology_Roadways','Mean_Distance_Firepoints_Roadways'
]<save_to_csv>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
y = train["Cover_Type"]
X = pd.get_dummies(train[features])
 X_test = pd.get_dummies(test[features])
 model = ExtraTreesClassifier(n_estimators=1000, random_state=42, max_features='log2')
 model.fit(X,y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'Id': test.Id, 'Cover_Type': predictions})
 output.to_csv('forest_cover_submission.csv', index=False)
 print("Your submission was successfully saved!" )<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_data =pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data.head() 
<load_from_csv>
frames_per_video = frame_h * frame_l
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_data = pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv')
 test_data.head()<count_values>
input_size = 224
train_data['Cover_Type'].value_counts()<prepare_x_and_y>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
X = train_data.drop(labels =['Id','Cover_Type'],axis=1)
 Y = train_data['Cover_Type']
Y<import_modules>
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 )
from sklearn.model_selection import train_test_split<import_modules>
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
from sklearn.model_selection import train_test_split<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
x_train,x_valid,y_train,y_valid = train_test_split(X,Y,random_state = 21 )<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 )
from sklearn.ensemble import RandomForestClassifier<train_model>
speed_test = False
rfc = RandomForestClassifier(n_estimators = 80)
 rfc.fit(x_train,y_train )<compute_test_metric>
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)) )
rfc.score(x_valid,y_valid )<predict_on_test>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<prepare_output><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )