Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<train_on_grid>
%matplotlib inline
ans = {}
for i in range(1, 222):
imp_col = imp_feature.iloc[:i].index
ridge = KernelRidge(alpha = 0.5263157894736842, coef0 = 3.5, degree = 2, kernel ='polynomial')
 ridge = ridge.fit(X_train[imp_col], y_train_log)
 ans[i] = np.sqrt(mean_squared_error(y_train_log,ridge.predict(X_train[imp_col])) )<define_variables>
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 )
minimum = ans[1]
ind_min = 1
for ind in range(1,len(ans.values())) :
if ans[ind] < minimum:
minimum = ans[ind]
ind_min = ind<features_selection>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
imp_col = imp_feature.iloc[:ind_min+1].index<compute_test_metric>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
def neg_rmse(y_true, y_pred):
return -1.0*np.sqrt(mean_squared_error(y_true,y_pred))
neg_rmse = make_scorer(neg_rmse )<train_model>
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 )
model = KernelRidge(alpha = 0.6842105263157894, coef0 = 3.5, degree = 2, kernel = 'polynomial')
 model.fit(X_train[imp_col], y_train_log)
 print("RMSE of the whole training set: {}".format(np.sqrt(mean_squared_error(y_train_log,model.predict(X_train[imp_col])))) )<predict_on_test>
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 )
y_pred = np.exp(model.predict(X_test[imp_col]))<save_to_csv>
input_size = 224
def save_ans(ans, pasanger_id, name_alg):
submission = pd.DataFrame({'Id':pasanger_id,'SalePrice':ans})
 print(submission.shape)
 filename = r'./{}.csv'.format(name_alg)
 submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )<compute_test_metric>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
save_ans(y_pred, test_id,'submission' )<load_from_csv>
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 )
train_df = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test_df = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<drop_column>
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 preprocessing_null(data_df):
data_df.drop(['Alley', 'PoolQC', 'Fence', 'MiscFeature', 'Id'], axis=1, inplace=True)
 Bsmtlist = ['BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
Bsmtlist2=['BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF']
Garagelist = ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']
Bathlist = ['BsmtFullBath', 'BsmtHalfBath']
Extlist = ['Exterior1st', 'Exterior2nd']
data_df.loc[:, Bsmtlist]=data_df.loc[:, Bsmtlist].fillna('TA')
 data_df['Electrical']=data_df['Electrical'].fillna('SBrkr')
 data_df['LotFrontage']=data_df['LotFrontage'].fillna(data_df['LotFrontage'].mean())
 data_df['FireplaceQu'] = data_df['FireplaceQu'].fillna('NA')
 data_df.loc[:, Garagelist] = data_df.loc[:, Garagelist].fillna('NA')
 data_df['GarageYrBlt']=data_df['GarageYrBlt'].fillna(2005)
 data_df.loc[:, 'MasVnrType'] = data_df.loc[:, 'MasVnrType'].fillna('None')
 data_df['MasVnrArea']=data_df['MasVnrArea'].fillna(0)
 data_df.loc[:, Bsmtlist2]=data_df[Bsmtlist2].fillna(0)
 data_df['TotalBsmtSF']=data_df['TotalBsmtSF'].fillna(0)
 data_df['GarageArea']=data_df['GarageArea'].fillna(data_df['GarageArea'].median())
 data_df['GarageCars']=data_df['GarageCars'].fillna(data_df['GarageCars'].median())
 data_df[Bathlist]=data_df[Bathlist].fillna(0)
 data_df[Extlist]=data_df[Extlist].fillna('VinylSd')
 data_df['MSZoning']=data_df['MSZoning'].fillna('TA')
 data_df['Utilities']=data_df['Utilities'].fillna('AllPub')
 data_df['KitchenQual']=data_df['KitchenQual'].fillna('TA')
 data_df['Functional']=data_df['Functional'].fillna('Typ')
 data_df['SaleType']=data_df['SaleType'].fillna('WD')
 return data_df<prepare_x_and_y>
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
train_target = train_df['SalePrice']
train_feature = train_df.drop('SalePrice', axis=1 )<categorify>
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 )
train_feature = preprocessing_null(train_feature)
 test_feature = preprocessing_null(test_df )<set_options>
speed_test = False
%matplotlib inline<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', 500)
 corr[corr>0.7]<drop_column>
predictions = predict_on_video_set(test_videos, num_workers=4 )
<split><EOS> 
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )