Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<categorify>
%matplotlib inline
non_numeric=all_data.select_dtypes(include='object')
 def onehot(col_list):
global all_data
while len(col_list)!=0:
col=col_list.pop(0)
 data_encoded=pd.get_dummies(all_data[col], prefix=col)
 all_data=pd.merge(all_data, data_encoded, on='Id')
 all_data=all_data.drop(columns=col)
 print(all_data.shape)
 onehot(list(non_numeric))<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"])
 len(test_videos )
numeric=all_data.select_dtypes(exclude='object')
 def log_transform(col_list):
transformed_col=[]
while len(col_list)!=0:
col=col_list.pop(0)
 if all_data[col].skew() > 0.5:
all_data[col]=np.log(all_data[col]+1)
 transformed_col.append(col)
 else:
pass
print(f"{len(transformed_col)} features had been tranformed")
 print(all_data.shape)
 log_transform(list(numeric))<import_modules>
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )
from sklearn.model_selection import KFold, cross_val_score, GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor<choose_model_class>
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu
kfold = KFold(n_splits=4 )<choose_model_class>
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 )
GBC = GradientBoostingRegressor()
gb_param_grid = {'n_estimators' : [100, 200, 500],
'learning_rate': [0.01, 0.05, 0.1, 0.2],
'max_depth': [3, 5, 10, 15],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold,
scoring="neg_mean_squared_error", n_jobs= 4, verbose = 1)
 gsGBC.fit(Xtrain,Ytrain)
 GBC_best = gsGBC.best_estimator_
gsGBC.best_score_<train_on_grid>
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 )
XGB=XGBRegressor()
xgb_param_grid = {'learning_rate':[0.01, 0.05, 0.1, 0.2],
'n_estimators':[100, 1000, 2000],
'max_depth':[3, 5, 10, 15]}
gsXGB = GridSearchCV(XGB,param_grid = xgb_param_grid,
cv=kfold, scoring="neg_mean_squared_error",
n_jobs=4, verbose =1)
 gsXGB.fit(Xtrain,Ytrain)
 XGB_best = gsXGB.best_estimator_
gsXGB.best_score_<choose_model_class>
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )
LGB = LGBMRegressor()
lgb_param_grid = {
'num_leaves' : [1,5,10],
'learning_rate': [1,0.1,0.01,0.001],
'n_estimators': [50, 100, 200, 500, 1000,5000],
'max_depth': [15,20,25],
'num_leaves': [50, 100, 200],
'min_split_gain': [0.3, 0.4]
}
gsLGB = GridSearchCV(LGB,param_grid = lgb_param_grid,
cv=kfold, scoring="neg_mean_squared_error",
n_jobs= 4, verbose = 1)
 gsLGB.fit(Xtrain,Ytrain)
 LGB_best = gsLGB.best_estimator_
gsLGB.best_score_<import_modules>
input_size = 224
from sklearn.ensemble import VotingRegressor<train_model>
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 )
votingC = VotingRegressor(estimators=[('LGB', LGB_best), 
('GBC',GBC_best), 
('XGB', XGB_best)], n_jobs=4)
 votingC = votingC.fit(Xtrain, Ytrain)
<predict_on_test>
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
test_SalePrice = pd.Series(votingC.predict(Xtest), name="SalePrice" )<prepare_output>
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 = pd.DataFrame({
"Id" :Id_test,
"SalePrice": test_SalePrice
})
 submission.head()<save_to_csv>
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 )
submission.to_csv('submission_voting.csv', index=False )<load_from_csv>
%%time
predictions = predict_on_video_set(test_videos, num_workers=4 )
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 print("train shape:" + str(train.shape))
print("test shape:" + str(test.shape))<import_modules>
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )
from scipy.stats import skew, norm
import scipy.stats as stats
from scipy.special import boxcox1p
from scipy.stats import boxcox_normmax<filter>
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet
train[(train['GrLivArea'] > 4500)&(train['SalePrice'] < 250000)] [cols]<filter>
input_size = 150
cols_outliers = cols[:]
cols_outliers.extend(['YrSold', 'Neighborhood'])
 train[(train['OverallQual'] >= 9)&(train['GrLivArea'] > 3000)] [cols_outliers]<filter>
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 )
cols_outliers_test = cols_outliers[:]
cols_outliers_test.remove('SalePrice')
 test[(test['OverallQual'] >= 9)&(test['GrLivArea'] > 3000)] [cols_outliers_test]<drop_column>
checkpoint = torch.load('.. /input/deepfake-xception-trained-model/model.pth', map_location=gpu)
 model = FCN(model, 2048 ).to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint
train.drop([523, 1298], axis = 0, inplace = True )<filter>
%%time
predictions = predict_on_video_set(test_videos, num_workers=4 )
train[(train['OverallQual'] == 4)&(train['SalePrice'] > 250000)] [cols]<create_dataframe>
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )
pd.DataFrame(train[(train['OverallQual'] == 4)][cols].mean() ).T<filter>
submission_df = pd.DataFrame({"filename": test_videos} )
train[(train['OverallQual'] == 8)&(train['SalePrice'] > 500000)] [cols]<create_dataframe>
r1 = 0.46441
r2 = 0.52189
total = r1 + r2
r11 = r1/total
r22 = r2/total
pd.DataFrame(train[(train['OverallQual'] == 8)][cols].mean() ).T<prepare_output>
submission_df["label"] = r22*submission_df_resnext["label"] + r11*submission_df_xception["label"]
data_sets = [train, test]
data_sets_null = []
for data_set in data_sets:
n_houses = data_set.shape[0]
data_set = data_set.isnull().sum().sort_values(ascending = False)
 data_set = pd.DataFrame(data_set[(data_set != 0)], columns = ['NullSum'])
 data_set['NullPercent'] =(data_set['NullSum']/n_houses)*100
data_sets_null.append(data_set)
 train_null, test_null = data_sets_null<concatenate>
submission_df.to_csv("submission.csv", index=False )