Kaggle competition: Deepfake Detection Challenge
submission_df.to_csv("submission.csv", index=False )<set_options>
BASE_PATH = '/kaggle/input/deepfake-detection-challenge/'
TEST_VIDEO_PATH = BASE_PATH + 'test_videos/'
SAMP_PATH = BASE_PATH + 'sample_submission.csv'
test_img_list = glob.glob(f'{TEST_VIDEO_PATH}*.mp4' )
warnings.filterwarnings("ignore")
 %matplotlib inline 
<set_options>
class EffnetTest(nn.Module):
def __init__(self, version):
super(EffnetTest, self ).__init__()
self.model = EfficientNet.from_name(f"efficientnet-{version}", override_params={"num_classes":1})
 self.model.fc = nn.Linear(512, 1)
 self.model._norm_layer = nn.GroupNorm(num_groups=32, num_channels=3)
 def forward(self, x):
x = self.model(x)
 return torch.sigmoid(x )
pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 500)
 folder = '/kaggle/input/mercedes-benz-greener-manufacturing/'<load_from_csv>
class SimpleCNNInference:
def __init__(self, model_path, version='b6', img_size=200):
self.img_size = img_size
self.transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.Resize(size=(self.img_size, self.img_size)) ,
transforms.ToTensor() ,
transforms.Normalize(( 0.485, 0.456, 0.406),(0.229, 0.224, 0.225)) ])
 self.device = torch.device("cuda")
 self.model = EffnetTest(version=version)
 self.model.load_state_dict(torch.load(model_path, map_location="cuda:0"))
self.model.to(self.device)
 self.model.eval()
def predict_single_from_array(self, img_frame):
img_frame = cv2.cvtColor(img_frame, cv2.COLOR_BGR2RGB)
 img_frame = self.transform(img_frame ).to(self.device ).unsqueeze(0)
 return float(self.model(img_frame))
train_df = pd.read_csv(folder + 'train.csv.zip')
 test_df = pd.read_csv(folder + 'test.csv.zip')
 sub_df = pd.read_csv(folder + 'sample_submission.csv.zip')
 print('train_df: ', train_df.shape)
 print('test_df: ', test_df.shape)
 print('sub_df: ', sub_df.shape )<define_variables>
test_filenames = [string.split('/')[-1] for string in test_img_list]
detector = RetinaFaceDetector(weights="/kaggle/input/resnetretinaface/Resnet50_Final.pth")
 inference = SimpleCNNInference(model_path="/kaggle/input/effnetb6-200-0741/EffnetB6_pytorch_group_imgface6_200_0.0741.pth",
version='b6', img_size=200)
 inference2 = SimpleCNNInference(model_path="/kaggle/input/testefficientnet/testEfficientNet.pth",
version='b5', img_size=224)
 inference3 = SimpleCNNInference(model_path="/kaggle/input/effnetb5-imgface6-0074/EffnetB5_imgface6_0.074.pth",
version='b5', img_size=224)
 inference4 = SimpleCNNInference(model_path="/kaggle/input/effnetb6-0071-finetuned/EffnetB6_pytorch_group_imgface6_0.071_finetuned.pth",
version='b6', img_size=224)
 inference5 = SimpleCNNInference(model_path="/kaggle/input/effnetb4-0691/EffnetB4_pytorch_group_imgface6_0.0691.pth",
version='b4', img_size=224)
 frame_skip = 16
res = 2
final_predictions = dict()
for i, video in enumerate(tqdm(test_img_list)) :
single_predictions = []
cap = cv2.VideoCapture(video)
 for j in range(300):
_ = cap.grab()
if j % frame_skip == 0:
pass
else:
continue
try:
_, frame = cap.retrieve()
result = detector.detect_faces(frame[::res, ::res])
 if result == []:
continue
bounding_box = result[0]
x1, y1 = bounding_box[0]*res, bounding_box[1]*res
x2, y2 = bounding_box[2]*res, bounding_box[3]*res
frame_face = frame[y1:y2, x1:x2]
pred = inference.predict_single_from_array(frame_face)
 single_predictions.append(1-pred)
 pred2 = inference2.predict_single_from_array(frame_face)
 single_predictions.append(1-pred2)
 pred3 = inference3.predict_single_from_array(frame_face)
 single_predictions.append(1-pred3)
 pred4 = inference4.predict_single_from_array(frame_face)
 single_predictions.append(1-pred4)
 pred5 = inference5.predict_single_from_array(frame_face)
 single_predictions.append(1-pred5)
 except:
print("E")
 final_predictions[test_filenames[i]] = np.mean(single_predictions)
 cv2.destroyAllWindows()
cat_cols = dfs_train.categoricals.tolist()<feature_engineering>
sub = pd.read_csv(SAMP_PATH)
 sub['label'] = sub['filename'].map(final_predictions ).clip(0.01, 0.99 ).fillna(0.5 )
<filter><EOS> 
sub.to_csv('submission.csv', index=False )