Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<choose_model_class>
!pip install.. /input/pytorchcv/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
cv_scores = []
cv_std = []
kf = KFold(n_splits = 10, shuffle = True, random_state = 1)
 baseline_models = ['Linear_Reg.','Bayesian_Ridge_Reg.','LGBM_Reg.','SVR',
'Dec_Tree_Reg.','Random_Forest_Reg.', 'XGB_Reg.',
'Grad_Boost_Reg.','Cat_Boost_Reg.','Stacked_Reg.']
lreg = LinearRegression()
score_lreg = cv_rmse(lreg)
 cv_scores.append(score_lreg.mean())
 cv_std.append(score_lreg.std())
 brr = BayesianRidge(compute_score=True)
 score_brr = cv_rmse(brr)
 cv_scores.append(score_brr.mean())
 cv_std.append(score_brr.std())
 l_gbm = LGBMRegressor(objective='regression')
 score_l_gbm = cv_rmse(l_gbm)
 cv_scores.append(score_l_gbm.mean())
 cv_std.append(score_l_gbm.std())
 svr = SVR()
score_svr = cv_rmse(svr)
 cv_scores.append(score_svr.mean())
 cv_std.append(score_svr.std())
 dtr = DecisionTreeRegressor()
score_dtr = cv_rmse(dtr)
 cv_scores.append(score_dtr.mean())
 cv_std.append(score_dtr.std())
 rfr = RandomForestRegressor()
score_rfr = cv_rmse(rfr)
 cv_scores.append(score_rfr.mean())
 cv_std.append(score_rfr.std())
 xgb = xgb.XGBRegressor()
score_xgb = cv_rmse(xgb)
 cv_scores.append(score_xgb.mean())
 cv_std.append(score_xgb.std())
 gbr = GradientBoostingRegressor()
score_gbr = cv_rmse(gbr)
 cv_scores.append(score_gbr.mean())
 cv_std.append(score_gbr.std())
 catb = CatBoostRegressor()
score_catb = cv_rmse(catb)
 cv_scores.append(score_catb.mean())
 cv_std.append(score_catb.std())
 stack_gen = StackingRegressor(regressors=(CatBoostRegressor() ,
LinearRegression() ,
BayesianRidge() ,
GradientBoostingRegressor()),
meta_regressor = CatBoostRegressor() ,
use_features_in_secondary = True)
 score_stack_gen = cv_rmse(stack_gen)
 cv_scores.append(score_stack_gen.mean())
 cv_std.append(score_stack_gen.std())
 final_cv_score = pd.DataFrame(baseline_models, columns = ['Regressors'])
 final_cv_score['RMSE_mean'] = cv_scores
final_cv_score['RMSE_std'] = cv_std<feature_engineering>
%matplotlib inline
warnings.filterwarnings("ignore" )
feat_imp = gbr.get_feature_importance(prettified=True)
 feat_imp<define_search_space>
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_on_grid>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
<predict_on_test>
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 )
stack_gen.fit(train_X, train_Y)
 test_pred = stack_gen.predict(test_X)
 submission = pd.DataFrame(test_id, columns = ['Id'])
 test_pred = np.expm1(test_pred)
 submission['SalePrice'] = test_pred
submission.head()<save_to_csv>
frames_per_video = 20
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 )
submission.loc[1089, 'SalePrice'] = 125000
submission.to_csv("result.csv", index = False, header = True )<filter>
input_size = 150
submission[submission['Id'] == 2550]<set_options>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))
class Head(torch.nn.Module):
def __init__(self, in_f, out_f):
super(Head, self ).__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, 512)
 self.d = nn.Dropout(0.5)
 self.o = nn.Linear(512, out_f)
 self.b1 = nn.BatchNorm1d(in_f)
 self.b2 = nn.BatchNorm1d(512)
 self.r = nn.ReLU()
def forward(self, x):
x = self.f(x)
 x = self.b1(x)
 x = self.d(x)
 x = self.l(x)
 x = self.r(x)
 x = self.b2(x)
 x = self.d(x)
 out = self.o(x)
 return out
class FCN(torch.nn.Module):
def __init__(self, base, in_f):
super(FCN, self ).__init__()
self.base = base
self.h1 = Head(in_f, 1)
 def forward(self, x):
x = self.base(x)
 return self.h1(x)
 net = []
model = FCN(model, 2048)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-xception/modelv2.pth'))
net.append(model )
train = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 train.head()<load_from_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
test = pd.read_csv('.. /input/forest-cover-type-prediction/test.csv')
 test.head()<feature_engineering>
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 )
test['Euclidian_Distance_To_Hydrology'] =(test['Horizontal_Distance_To_Hydrology']**2 + test['Vertical_Distance_To_Hydrology']**2)**0.5
test['Mean_Elevation_Vertical_Distance_Hydrology'] =(test['Elevation'] + test['Vertical_Distance_To_Hydrology'])/2
test['Mean_Distance_Hydrology_Firepoints'] =(test['Horizontal_Distance_To_Hydrology'] + test['Horizontal_Distance_To_Fire_Points'])/2
test['Mean_Distance_Hydrology_Roadways'] =(test['Horizontal_Distance_To_Hydrology'] + test['Horizontal_Distance_To_Roadways'])/2
test['Mean_Distance_Firepoints_Roadways'] =(test['Horizontal_Distance_To_Fire_Points'] + test['Horizontal_Distance_To_Roadways'])/2
test<feature_engineering>
speed_test = False
train['Euclidian_Distance_To_Hydrology'] =(train['Horizontal_Distance_To_Hydrology']**2 + train['Vertical_Distance_To_Hydrology']**2)**0.5
train['Mean_Elevation_Vertical_Distance_Hydrology'] =(train['Elevation'] + train['Vertical_Distance_To_Hydrology'])/2
train['Mean_Distance_Hydrology_Firepoints'] =(train['Horizontal_Distance_To_Hydrology'] + train['Horizontal_Distance_To_Fire_Points'])/2
train['Mean_Distance_Hydrology_Roadways'] =(train['Horizontal_Distance_To_Hydrology'] + train['Horizontal_Distance_To_Roadways'])/2
train['Mean_Distance_Firepoints_Roadways'] =(train['Horizontal_Distance_To_Fire_Points'] + train['Horizontal_Distance_To_Roadways'])/2
train<data_type_conversions>
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)) )
<data_type_conversions>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
<prepare_x_and_y><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )