Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<split>
%matplotlib inline
train_feature_num, train_feature_obj = split_num_obj(train_feature)
 test_feature_num, test_feature_obj = split_num_obj(test_feature )<categorify>
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 )
train_dummies = pd.get_dummies(train_feature_obj)
 test_dummies = pd.get_dummies(test_feature_obj)
 not_in_train = [column for column in train_dummies.columns if column not in test_dummies.columns]
not_in_test = [column for column in test_dummies.columns if column not in train_dummies.columns]
print('
',train_dummies.shape, test_dummies.shape, '
')
 print('
', not_in_train, '
')
 print('
', not_in_test )<define_search_space>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
df_num_cat_col = df_num_col[[0, 3, 4, 15, 16, 17, 18, 19, 20, 21, 22, 28, 29, 30, 31]]<split>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
def split_num_cat(data_df_num):
data_df_num_cat = data_df_num[df_num_cat_col]
data_df_num_non_cat = data_df_num.drop(df_num_cat_col, axis=1)
 return data_df_num_cat, data_df_num_non_cat
train_feature_num_cat, train_feature_num_non_cat = split_num_cat(train_feature_num)
 test_feature_num_cat, test_feature_num_non_cat = split_num_cat(test_feature_num)
 print(train_feature_num_cat.shape,test_feature_num_cat.shape)
 print(train_feature_num_non_cat.shape, test_feature_num_non_cat.shape )<split>
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 )
all_data = pd.concat(( train_df, test_df)).reset_index(drop=True)
 all_data.drop('SalePrice', axis=1, inplace=True)
 print(all_data.shape)
 all_data = preprocessing_null(all_data)
 all_data = drop_corr_ftr(all_data)
 print(all_data.shape)
 all_data_num, all_data_obj = split_num_obj(all_data)
 print(all_data_obj.shape )<categorify>
frames_per_video = 64
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 )
warnings.filterwarnings(action='ignore')
 label = LabelEncoder()
for col in all_data_obj.columns:
label.fit(all_data_obj.loc[:, col])
 train_feature_obj.loc[:, col] = label.transform(train_feature_obj.loc[:, col])
 test_feature_obj.loc[:, col] = label.transform(test_feature_obj.loc[:, col] )<feature_engineering>
input_size = 224
train_target = np.log1p(train_target)
 train_feature_num_non_cat = np.log1p(train_feature_num_non_cat)
 test_feature_num_non_cat = np.log1p(test_feature_num_non_cat )<concatenate>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
train_feature_fin = pd.concat([train_feature_num_cat, train_feature_num_non_cat, train_feature_obj], axis=1)
 test_feature_fin = pd.concat([test_feature_num_cat, test_feature_num_non_cat, test_feature_obj], axis=1 )<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 )
cond1 = train_target>500000
cond2 = train_feature_num_non_cat['GrLivArea']>4000<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
train_feature_fin = train_feature_fin.drop(train_feature_num_non_cat[cond1|cond2].index, axis=0 )<train_on_grid>
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.481
def get_best_estimator(model, params):
grid_model = GridSearchCV(model, param_grid=params, scoring="neg_mean_squared_error", cv=5)
 grid_model.fit(train_feature_fin, train_target)
 rmse = np.sqrt(-1*grid_model.best_score_)
 print('{0}, param:{1}, rmse:{2}'.format(model.__class__.__name__, grid_model.best_params_,\
np.round(rmse, 4)))
 return grid_model.best_estimator_
ridge_params = {'alpha':[0.05, 0.1, 1, 5, 8, 10, 15]}
lasso_params = {'alpha':[0.001, 0.005, 0.008, 0.05, 0.1, 0.3, 0.5, 1, 5, 10]}
elastic_params = {'alpha':[0.05, 0.1, 0.5, 1, 3, 5, 8]}
ridge_reg = Ridge()
lasso_reg = Lasso()
elastic_reg = ElasticNet(l1_ratio=0.7)
 lasso_be = get_best_estimator(lasso_reg, lasso_params)
 ridge_be = get_best_estimator(ridge_reg, ridge_params)
 elastic_be = get_best_estimator(elastic_reg, elastic_params )<choose_model_class>
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 )
lgbm_params = {
'max_depth':[5, 10, 15, 20, 25, 30],
'learning_rate':[0.01, 0.05, 0.1, 0.5, 1],
}
lgbm_reg = LGBMRegressor(n_estimators=1000)
 lgbm_be = get_best_estimator(lgbm_reg, lgbm_params )<predict_on_test>
predictions = predict_on_video_set(test_videos, num_workers=4 )
preds = np.expm1(lgbm_be.predict(test_feature_fin))<save_to_csv>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
test=pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 my_submission = pd.DataFrame({'Id': test.Id,
'SalePrice': preds})
 my_submission.to_csv('submission.csv', index=False )<load_from_csv>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
train=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv',index_col=0)
 test=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv',index_col=0)
 submission=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')
 Id_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' ).values[:,0]
data=train
print(train.shape, test.shape, submission.shape, Id_test.shape )<set_options>
%matplotlib inline
warnings.filterwarnings("ignore" )
%matplotlib inline 
<drop_column>
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=train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )<prepare_x_and_y>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
Ytrain=train['SalePrice']
train=train[list(test)]
all_data=pd.concat(( train, test), axis=0)
 print(all_data.shape)
 Ytrain=np.log(Ytrain+1 )<count_missing_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 )
cols=list(all_data)
 for col in list(all_data):
if(all_data[col].isnull().sum())==0:
cols.remove(col)
 else:
pass
print(len(cols))<data_type_conversions>
frames_per_video = 64
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 )
for col in('PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu', 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'MasVnrType', 'MSSubClass'):
all_data[col] = all_data[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath', 'MasVnrArea','LotFrontage'):
all_data[col] = all_data[col].fillna(0)
 for col in('MSZoning', 'Electrical', 'KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType', 'Functional', 'Utilities'):
all_data[col] = all_data[col].fillna(all_data[col].mode() [0])
 print(f"Total count of missing values in all_data : {all_data.isnull().sum().sum() }" )<feature_engineering>
input_size = 150
all_data['TotalSF']=all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
all_data['No2ndFlr']=(all_data['2ndFlrSF']==0 )<feature_engineering>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
all_data['TotalBath']=all_data['BsmtFullBath'] + all_data['FullBath'] +(all_data['BsmtHalfBath']/2)+(all_data['HalfBath']/2 )<feature_engineering>
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 class Pooling(nn.Module):
def __init__(self):
super(Pooling, self ).__init__()
self.p1 = nn.AdaptiveAvgPool2d(( 1,1))
self.p2 = nn.AdaptiveMaxPool2d(( 1,1))
def forward(self, x):
x1 = self.p1(x)
 x2 = self.p2(x)
 return(x1+x2)* 0.5
model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,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-trained-model/model.pth'))
net.append(model )
all_data['YrBltAndRemod']=all_data['YearBuilt']+all_data['YearRemodAdd']<define_variables>
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.481
Basement = ['BsmtCond', 'BsmtExposure', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual', 'BsmtUnfSF', 'TotalBsmtSF']
Bsmt=all_data[Basement]<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 )
Bsmt=Bsmt.replace(to_replace='Po', value=1)
 Bsmt=Bsmt.replace(to_replace='Fa', value=2)
 Bsmt=Bsmt.replace(to_replace='TA', value=3)
 Bsmt=Bsmt.replace(to_replace='Gd', value=4)
 Bsmt=Bsmt.replace(to_replace='Ex', value=5)
 Bsmt=Bsmt.replace(to_replace='None', value=0)
 Bsmt=Bsmt.replace(to_replace='No', value=1)
 Bsmt=Bsmt.replace(to_replace='Mn', value=2)
 Bsmt=Bsmt.replace(to_replace='Av', value=3)
 Bsmt=Bsmt.replace(to_replace='Gd', value=4)
 Bsmt=Bsmt.replace(to_replace='Unf', value=1)
 Bsmt=Bsmt.replace(to_replace='LwQ', value=2)
 Bsmt=Bsmt.replace(to_replace='Rec', value=3)
 Bsmt=Bsmt.replace(to_replace='BLQ', value=4)
 Bsmt=Bsmt.replace(to_replace='ALQ', value=5)
 Bsmt=Bsmt.replace(to_replace='GLQ', value=6 )<feature_engineering>
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )
Bsmt['BsmtScore']= Bsmt['BsmtQual'] * Bsmt['BsmtCond'] * Bsmt['TotalBsmtSF']
all_data['BsmtScore']=Bsmt['BsmtScore']
Bsmt['BsmtFin'] =(Bsmt['BsmtFinSF1'] * Bsmt['BsmtFinType1'])+(Bsmt['BsmtFinSF2'] * Bsmt['BsmtFinType2'])
 all_data['BsmtFinScore']=Bsmt['BsmtFin']
all_data['BsmtDNF']=(all_data['BsmtFinScore']==0 )<define_variables>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
garage=['GarageArea','GarageCars','GarageCond','GarageFinish','GarageQual','GarageType','GarageYrBlt']
Garage=all_data[garage]
all_data['NoGarage']=(all_data['GarageArea']==0 )<categorify>
submission_df = pd.DataFrame({"filename": test_videos} )
Garage=Garage.replace(to_replace='Po', value=1)
 Garage=Garage.replace(to_replace='Fa', value=2)
 Garage=Garage.replace(to_replace='TA', value=3)
 Garage=Garage.replace(to_replace='Gd', value=4)
 Garage=Garage.replace(to_replace='Ex', value=5)
 Garage=Garage.replace(to_replace='None', value=0)
 Garage=Garage.replace(to_replace='Unf', value=1)
 Garage=Garage.replace(to_replace='RFn', value=2)
 Garage=Garage.replace(to_replace='Fin', value=3)
 Garage=Garage.replace(to_replace='CarPort', value=1)
 Garage=Garage.replace(to_replace='Basment', value=4)
 Garage=Garage.replace(to_replace='Detchd', value=2)
 Garage=Garage.replace(to_replace='2Types', value=3)
 Garage=Garage.replace(to_replace='Basement', value=5)
 Garage=Garage.replace(to_replace='Attchd', value=6)
 Garage=Garage.replace(to_replace='BuiltIn', value=7)
 Garage['GarageScore']=(Garage['GarageArea'])*(Garage['GarageCars'])*(Garage['GarageFinish'])*(Garage['GarageQual'])*(Garage['GarageType'])
 all_data['GarageScore']=Garage['GarageScore']<drop_column>
submission_df["label"] = 0.65*submission_df_resnext["label"] + 0.35*submission_df_xception["label"]
<data_type_conversions><EOS> 
submission_df.to_csv("submission.csv", index=False )